Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions data-raw/git_diff_main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash

# A script to diff the current branch against the project's true main branch.
# It handles both forked and non-forked repository scenarios robustly.
# Its primary output (stdout) is the list of changed files.
#
# Usage: ./git_diff_main.sh <main_repo_url>

set -e # Exit immediately if a command exits with a non-zero status.

# --- Helper Function to Normalize Git URLs ---
# This function reliably extracts the 'owner/repo' part from various URL formats.
normalize_git_url() {
local url=$1
# Step 1: Remove an optional trailing slash
local repo_path=${url%/}
# Step 2: Remove the optional .git suffix
repo_path=${repo_path%.git}
# Step 3: Remove everything up to the last colon or slash
repo_path=${repo_path##*[:/]}

echo "$repo_path"
}

# --- 1. Validate Input ---
UPSTREAM_URL="$1"
if [ -z "$UPSTREAM_URL" ]; then
# Print usage instructions to standard error
echo "Usage: $0 <main_repo_url>" >&2
exit 1
fi

# --- 2. Determine Target and Fetch ---
echo "Info: Analyzing repository remotes..." >&2
TARGET_REMOTE=""
ORIGIN_URL=$(git remote get-url origin 2>/dev/null || echo "") # Get origin URL or empty string

# Normalize both URLs to get just the 'owner/repo' string for comparison.
NORM_UPSTREAM_URL=$(normalize_git_url "$UPSTREAM_URL")
NORM_ORIGIN_URL=$(normalize_git_url "$ORIGIN_URL")

# Check if the normalized 'owner/repo' strings match
if [[ -n "$NORM_ORIGIN_URL" && "$NORM_ORIGIN_URL" == "$NORM_UPSTREAM_URL" ]]; then
# Scenario: Running in the main (non-forked) repository.
echo "Info: Detected main repository." >&2
TARGET_REMOTE="origin"
echo "Info: Fetching latest data from 'origin' remote... (this may take a moment)" >&2
git fetch origin main --quiet
else
# Scenario: Running in a fork or a repo with a mismatched origin URL.
echo "Info: Detected fork. Configuring 'upstream' remote." >&2
TARGET_REMOTE="upstream"
# Add or update the 'upstream' remote to point to the main repo
if ! git remote get-url "$TARGET_REMOTE" &>/dev/null; then
git remote add "$TARGET_REMOTE" "$UPSTREAM_URL"
else
git remote set-url "$TARGET_REMOTE" "$UPSTREAM_URL"
fi
echo "Info: Fetching latest data from 'upstream' remote... (this may take a moment)" >&2
git fetch "$TARGET_REMOTE" main --quiet
fi

TARGET_BRANCH="$TARGET_REMOTE/main"

# --- 3. Safety Check: Avoid Diffing a Branch Against Itself ---
CURRENT_TRACKING_BRANCH=$(git rev-parse --abbrev-ref '@{u}' 2>/dev/null || echo "")

if [[ "$CURRENT_TRACKING_BRANCH" == "$TARGET_BRANCH" ]]; then
echo "Warning: Current branch is already tracking '$TARGET_BRANCH'. No changes to diff." >&2
# Exit successfully with no output
exit 0
fi

# --- 4. Final Output ---
echo "Info: Comparing files against '$TARGET_BRANCH'..." >&2
git diff "$TARGET_BRANCH" --name-only

echo "Info: Done." >&2
16 changes: 11 additions & 5 deletions data-raw/rebuild_cached_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ flatten_list_of_deps <- function(updated_data, data_deps) {
fin_up
}


library(random.cdisc.data)
library(diffdf)
library(dplyr)
# Loading libs - not useful messaging is suppressed
suppressMessages({
library(random.cdisc.data)
library(diffdf)
library(dplyr)
})

# Call function to match random number generation from previous R versions
RNGkind(sample.kind = "Rounding")
Expand All @@ -76,7 +78,11 @@ data_deps <- sapply(
}
)

git_call <- "git diff origin/main --name-only"
if (!grepl(x = getwd(), pattern = "data-raw")) {
stop("This script should be run in the data-raw directory.")
}

git_call <- "bash ./git_diff_main.sh https://github.com/insightsengineering/random.cdisc.data/"
updated_files <- tryCatch(
system(git_call, intern = TRUE),
error = function(e) e
Expand Down
2 changes: 1 addition & 1 deletion vignettes/rebuild_cached_data.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ datasets should be updated, edit the `data_to_update` vector below, entering the
## Update Cached Data

**Note:** Prior to running the following code chunk, please ensure that you have reinstalled the `random.cdisc.data`
package after completing all dataset modifications. Or use `devtools::load_all()` to reload functions. Execute the `data-raw/rebuild_cached_data.R` script to create, compare and save cached data to the `data/` directory.
package after completing all dataset modifications. Or use `devtools::load_all()` to reload functions. Execute the `data-raw/rebuild_cached_data.R` script to create, compare and save cached data to the `data/` directory. We suggest to do `Rscript rebuild_cached_data.R` or `source("rebuild_cached_data.R")` from the `data-raw/` directory.

Loading