From 3b8d744951d11a2fea4a7bac19f043bc07b0c832 Mon Sep 17 00:00:00 2001 From: Randy Fay Date: Mon, 11 May 2026 15:42:45 -0600 Subject: [PATCH] fix: clean working tree after issue branch checkout, fixes #151 When cloning with git clone --reference, origin/main may be ahead of the issue branch base. git updates the index correctly during checkout but can leave working tree files from those newer main commits behind as modified or untracked, producing a dirty git status. Add git reset --hard HEAD and git clean -fd immediately after the issue branch checkout in both drupal-core and drupal-contrib templates so the working tree exactly matches the checked-out branch. Add a dirty-tree check to both test-issue-branches.sh scripts so this class of regression is caught by the test suite. Update update-drupal-cache to fast-forward the seed local main branch after fetching so git status on the server stays clean. Requires reinstalling: sudo install -m 755 drupal-core/scripts/update-drupal-cache /usr/local/bin/update-drupal-cache Co-Authored-By: Claude Sonnet 4.6 --- drupal-contrib/scripts/test-issue-branches.sh | 10 ++++++++++ drupal-contrib/template.tf | 6 ++++++ drupal-core/scripts/test-issue-branches.sh | 10 ++++++++++ drupal-core/scripts/update-drupal-cache | 4 ++++ drupal-core/template.tf | 7 +++++++ 5 files changed, 37 insertions(+) diff --git a/drupal-contrib/scripts/test-issue-branches.sh b/drupal-contrib/scripts/test-issue-branches.sh index f8bde7f..56c56c1 100755 --- a/drupal-contrib/scripts/test-issue-branches.sh +++ b/drupal-contrib/scripts/test-issue-branches.sh @@ -95,6 +95,16 @@ for SPEC in "${TESTS[@]}"; do continue fi log "Checked out issue branch: $BRANCH" + DIRTY=$(git status --short 2>/dev/null) + if [ -n "$DIRTY" ]; then + log "ERROR: dirty git tree after issue branch checkout:" + echo "$DIRTY" | head -20 + RESULTS["$DIR_KEY"]="FAIL (dirty git tree after checkout)" + DURATIONS["$DIR_KEY"]=$((SECONDS - START)) + cd "$HOME" + continue + fi + log "✓ git tree clean after checkout" fi # --- Configure DDEV if needed --- diff --git a/drupal-contrib/template.tf b/drupal-contrib/template.tf index e86ed69..e093916 100644 --- a/drupal-contrib/template.tf +++ b/drupal-contrib/template.tf @@ -545,6 +545,12 @@ GIT_EXCLUDE_EOF git checkout -b "$ISSUE_BRANCH" "$REMOTE_NAME/$ISSUE_BRANCH" >> "$SETUP_LOG" 2>&1; then log_setup "✓ Checked out branch $ISSUE_BRANCH" update_status "✓ Branch checkout: Success" + # Ensure working tree exactly matches the checked-out branch. + # origin/main may be ahead of the issue branch base; git can leave + # files from those newer commits as modified/untracked after checkout. + git reset --hard HEAD >> "$SETUP_LOG" 2>&1 || true + git clean -fd >> "$SETUP_LOG" 2>&1 || true + log_setup "✓ Working tree reset to match branch" else log_setup "⚠ Warning: Could not checkout $ISSUE_BRANCH — remaining on current branch" update_status "⚠ Branch checkout: Warning" diff --git a/drupal-core/scripts/test-issue-branches.sh b/drupal-core/scripts/test-issue-branches.sh index 7f1924a..c98f613 100755 --- a/drupal-core/scripts/test-issue-branches.sh +++ b/drupal-core/scripts/test-issue-branches.sh @@ -105,6 +105,16 @@ for PAIR in "${TESTS[@]}"; do cd "$HOME" continue fi + DIRTY=$(git status --short 2>/dev/null) + if [ -n "$DIRTY" ]; then + log "ERROR: dirty git tree after issue branch checkout:" + echo "$DIRTY" | head -20 + RESULTS["$DIR_KEY"]="FAIL (dirty git tree after checkout)" + DURATIONS["$DIR_KEY"]=$((SECONDS - START)) + cd "$HOME" + continue + fi + log "✓ git tree clean after checkout" elif [ "$TARGET_BRANCH" != "main" ]; then log "Checking out $TARGET_BRANCH..." if ! (git checkout -b "$TARGET_BRANCH" "origin/$TARGET_BRANCH" 2>&1 || \ diff --git a/drupal-core/scripts/update-drupal-cache b/drupal-core/scripts/update-drupal-cache index 6cfa224..70c32d8 100755 --- a/drupal-core/scripts/update-drupal-cache +++ b/drupal-core/scripts/update-drupal-cache @@ -56,6 +56,10 @@ echo "" echo "Fetching Drupal core git objects..." git -C "$SEED_DIR" fetch --all --prune +echo "Fast-forwarding local main branch..." +git -C "$SEED_DIR" merge --ff-only origin/main || \ + echo "Warning: could not fast-forward main (local changes?); objects still updated." + echo "" echo "=== Seed cache updated successfully ===" echo "Completed: $(date)" diff --git a/drupal-core/template.tf b/drupal-core/template.tf index 5e52230..0aaa06e 100644 --- a/drupal-core/template.tf +++ b/drupal-core/template.tf @@ -635,6 +635,13 @@ STATUS_HEADER if git -C "$DRUPAL_DIR" checkout -b "$ISSUE_BRANCH" "issue/$ISSUE_BRANCH" >> "$SETUP_LOG" 2>&1 || \ git -C "$DRUPAL_DIR" checkout "$ISSUE_BRANCH" >> "$SETUP_LOG" 2>&1; then log_setup " ✓ Checked out branch: $ISSUE_BRANCH" + # Ensure the working tree exactly matches the checked-out branch. + # When cloning with --reference, origin/main may be ahead of the + # issue branch base; git updates the index but can leave working tree + # files from those newer main commits behind as modified/untracked. + git -C "$DRUPAL_DIR" reset --hard HEAD >> "$SETUP_LOG" 2>&1 || true + git -C "$DRUPAL_DIR" clean -fd >> "$SETUP_LOG" 2>&1 || true + log_setup " ✓ Working tree reset to match branch" else log_setup "✗ Failed to check out branch $ISSUE_BRANCH" SETUP_FAILED=true