diff --git a/data-raw/git_diff_main.sh b/data-raw/git_diff_main.sh new file mode 100644 index 00000000..a088b4e0 --- /dev/null +++ b/data-raw/git_diff_main.sh @@ -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 + +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 " >&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 diff --git a/data-raw/rebuild_cached_data.R b/data-raw/rebuild_cached_data.R index 2d0d6fb5..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 <- "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 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.