Skip to content

Commit 27d1872

Browse files
gnodetclaude
andcommitted
ci: progressively deepen fetch for merge-base reachability
Instead of a fixed --depth=200 fetch that can miss the merge base for long-lived or stale branches, try 200 → 1000 → unshallow until git merge-base succeeds. Most PRs resolve at depth 200 (no extra cost); only old branches need the deeper fetches. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8624aaa commit 27d1872

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

.github/workflows/pr-build-main.yml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,25 @@ jobs:
8484
if: ${{ !inputs.skip_full_build }}
8585
run: |
8686
# Scalpel needs the merge base between HEAD and the base branch.
87-
# The checkout is depth=1, so deepen both sides for merge-base reachability.
88-
git fetch --deepen=200 2>/dev/null || true
89-
git fetch --no-tags --depth=200 origin "${GITHUB_BASE_REF:-main}:refs/remotes/origin/${GITHUB_BASE_REF:-main}"
87+
# The checkout is depth=1, so we progressively deepen until the
88+
# merge base is reachable: 200 → 1000 → full history.
89+
# Scalpel is observational — fetch failures must not break the build.
90+
BASE_REF="${GITHUB_BASE_REF:-main}"
91+
for depth in 200 1000; do
92+
git fetch --deepen=$depth 2>/dev/null || true
93+
git fetch --no-tags --depth=$depth origin "${BASE_REF}:refs/remotes/origin/${BASE_REF}" || true
94+
if git merge-base HEAD "origin/${BASE_REF}" >/dev/null 2>&1; then
95+
echo "Merge base reachable at depth $depth"
96+
break
97+
fi
98+
echo "Merge base not reachable at depth $depth, deepening..."
99+
done
100+
# If still not reachable, fetch full history as last resort
101+
if ! git merge-base HEAD "origin/${BASE_REF}" >/dev/null 2>&1; then
102+
echo "Merge base still not reachable, fetching full history"
103+
git fetch --unshallow 2>/dev/null || true
104+
git fetch --no-tags origin "${BASE_REF}:refs/remotes/origin/${BASE_REF}" || true
105+
fi
90106
- id: install-packages
91107
uses: ./.github/actions/install-packages
92108
- id: install-mvnd

.github/workflows/sonar-build.yml

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,23 @@ jobs:
4747
persist-credentials: false
4848
- name: Fetch base branch for Scalpel change detection
4949
run: |
50-
git fetch --deepen=200 2>/dev/null || true
51-
git fetch --no-tags --depth=200 origin "${GITHUB_BASE_REF:-main}:refs/remotes/origin/${GITHUB_BASE_REF:-main}"
50+
# Scalpel is observational — fetch failures must not break the build.
51+
BASE_REF="${GITHUB_BASE_REF:-main}"
52+
for depth in 200 1000; do
53+
git fetch --deepen=$depth 2>/dev/null || true
54+
git fetch --no-tags --depth=$depth origin "${BASE_REF}:refs/remotes/origin/${BASE_REF}" || true
55+
if git merge-base HEAD "origin/${BASE_REF}" >/dev/null 2>&1; then
56+
echo "Merge base reachable at depth $depth"
57+
break
58+
fi
59+
echo "Merge base not reachable at depth $depth, deepening..."
60+
done
61+
# If still not reachable, fetch full history as last resort
62+
if ! git merge-base HEAD "origin/${BASE_REF}" >/dev/null 2>&1; then
63+
echo "Merge base still not reachable, fetching full history"
64+
git fetch --unshallow 2>/dev/null || true
65+
git fetch --no-tags origin "${BASE_REF}:refs/remotes/origin/${BASE_REF}" || true
66+
fi
5267
- id: install-packages
5368
uses: ./.github/actions/install-packages
5469

0 commit comments

Comments
 (0)