From 2266e02e1fa7da268e2e9097dac84a1486c5eae4 Mon Sep 17 00:00:00 2001 From: melkiades Date: Tue, 15 Jul 2025 23:33:17 +0200 Subject: [PATCH 1/4] init fix --- data-raw/git_diff_main.sh | 75 ++++++++++++++++++++++++++++++++++ data-raw/rebuild_cached_data.R | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 data-raw/git_diff_main.sh diff --git a/data-raw/git_diff_main.sh b/data-raw/git_diff_main.sh new file mode 100644 index 00000000..c638c69a --- /dev/null +++ b/data-raw/git_diff_main.sh @@ -0,0 +1,75 @@ +#!/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 + +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 + # First, remove the optional .git suffix + local repo_path=${url%.git} + + # Then, remove everything before the last colon or slash, + # which typically separates the host from the repo path. + 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 " >&2 + exit 1 +fi + +# --- 2. Determine Target and Fetch --- +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: Running in main repo. Comparing against 'origin/main'." >&2 + TARGET_REMOTE="origin" + git fetch origin main --quiet # Quietly fetch the main branch +else + # Scenario: Running in a fork or in a misconfigured repo. + echo "Info: Running in a fork. Comparing against 'upstream/main'." >&2 + echo "$NORM_ORIGIN_URL $NORM_UPSTREAM_URL" >&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 + git fetch "$TARGET_REMOTE" main --quiet # Quietly fetch the main branch +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 --- +# This is the script's primary output. It prints the list of changed +# files to standard output, which the calling script can capture. +git diff "$TARGET_BRANCH" --name-only diff --git a/data-raw/rebuild_cached_data.R b/data-raw/rebuild_cached_data.R index 2d0d6fb5..d2da4105 100644 --- a/data-raw/rebuild_cached_data.R +++ b/data-raw/rebuild_cached_data.R @@ -76,7 +76,7 @@ data_deps <- sapply( } ) -git_call <- "git diff origin/main --name-only" +git_call <- "bash data-raw/git_diff_main.sh https://github.com/insightsengineering/random.cdisc.data/" updated_files <- tryCatch( system(git_call, intern = TRUE), error = function(e) e From 6cc0a1239f9d98d23e91d1c3d8296951dbe25f4c Mon Sep 17 00:00:00 2001 From: melkiades Date: Wed, 16 Jul 2025 16:03:32 +0200 Subject: [PATCH 2/4] final update --- data-raw/git_diff_main.sh | 29 ++++++++++++++++------------- data-raw/rebuild_cached_data.R | 16 +++++++++++----- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/data-raw/git_diff_main.sh b/data-raw/git_diff_main.sh index c638c69a..3134c523 100644 --- a/data-raw/git_diff_main.sh +++ b/data-raw/git_diff_main.sh @@ -12,11 +12,11 @@ set -e # Exit immediately if a command exits with a non-zero status. # This function reliably extracts the 'owner/repo' part from various URL formats. normalize_git_url() { local url=$1 - # First, remove the optional .git suffix - local repo_path=${url%.git} - - # Then, remove everything before the last colon or slash, - # which typically separates the host from the repo path. + # 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" @@ -31,6 +31,7 @@ if [ -z "$UPSTREAM_URL" ]; then 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 @@ -41,13 +42,13 @@ 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: Running in main repo. Comparing against 'origin/main'." >&2 + echo "Info: Detected main repository." >&2 TARGET_REMOTE="origin" - git fetch origin main --quiet # Quietly fetch the main branch + 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 in a misconfigured repo. - echo "Info: Running in a fork. Comparing against 'upstream/main'." >&2 - echo "$NORM_ORIGIN_URL $NORM_UPSTREAM_URL" >&2 + # 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 @@ -55,7 +56,8 @@ else else git remote set-url "$TARGET_REMOTE" "$UPSTREAM_URL" fi - git fetch "$TARGET_REMOTE" main --quiet # Quietly fetch the main branch + 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" @@ -70,6 +72,7 @@ if [[ "$CURRENT_TRACKING_BRANCH" == "$TARGET_BRANCH" ]]; then fi # --- 4. Final Output --- -# This is the script's primary output. It prints the list of changed -# files to standard output, which the calling script can capture. +echo "Info: Comparing files against '$TARGET_BRANCH'..." >&2 git diff "$TARGET_BRANCH" --name-only + +echo "Info: Done." >&2 diff --git a/data-raw/rebuild_cached_data.R b/data-raw/rebuild_cached_data.R index d2da4105..9c71725b 100644 --- a/data-raw/rebuild_cached_data.R +++ b/data-raw/rebuild_cached_data.R @@ -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") @@ -76,7 +78,11 @@ data_deps <- sapply( } ) -git_call <- "bash data-raw/git_diff_main.sh https://github.com/insightsengineering/random.cdisc.data/" +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 From 7fa43fd9cc75dd7cc8d1068be198a9295f950e6f Mon Sep 17 00:00:00 2001 From: melkiades Date: Wed, 16 Jul 2025 16:11:03 +0200 Subject: [PATCH 3/4] add info --- vignettes/rebuild_cached_data.Rmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vignettes/rebuild_cached_data.Rmd b/vignettes/rebuild_cached_data.Rmd index d47e0081..1d270e14 100644 --- a/vignettes/rebuild_cached_data.Rmd +++ b/vignettes/rebuild_cached_data.Rmd @@ -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. From ef45a5df99fa439f0adb424585d8dc7ad0fdf05f Mon Sep 17 00:00:00 2001 From: melkiades Date: Wed, 16 Jul 2025 16:19:08 +0200 Subject: [PATCH 4/4] fix --- data-raw/git_diff_main.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data-raw/git_diff_main.sh b/data-raw/git_diff_main.sh index 3134c523..a088b4e0 100644 --- a/data-raw/git_diff_main.sh +++ b/data-raw/git_diff_main.sh @@ -63,7 +63,7 @@ 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 "") +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