fix: diff generation for PR checkouts

actions/checkout for PRs only fetches refs/pull/N/head into FETCH_HEAD.
Remote tracking branches (origin/main) don't exist.

Fix: explicit refspec fetch (refs/heads/main:refs/remotes/origin/main)
with fallback to GITEA_BASE_REF / GITHUB_BASE_REF for the target branch.
Also removed --filter=blob:none which could cause empty diffs.
Added diagnostic logging for base ref and file stats.
This commit is contained in:
Markus Hofstetter
2026-05-19 00:47:01 +02:00
parent bb34a3f2ec
commit 81edd958f9

View File

@@ -73,14 +73,34 @@ echo "::endgroup::"
# ─── Phase 2: Generate diff ─────────────────────────────────────────────────── # ─── Phase 2: Generate diff ───────────────────────────────────────────────────
echo "::group::Generate diff" echo "::group::Generate diff"
# Ensure we have full history (runner may have done a shallow checkout) # actions/checkout for PRs only fetches the PR ref (refs/pull/N/head).
git fetch --unshallow --filter=blob:none origin 2>/dev/null || true # It does NOT create remote tracking branches like origin/main.
git fetch origin main 2>/dev/null || git fetch origin master 2>/dev/null || true # We must explicitly fetch the base branch.
# Unshallow if needed (fetch-depth: 0 already does this, but be safe)
git fetch --unshallow origin 2>/dev/null || true
# Fetch base branch with explicit refspec to ensure origin/main exists
if git fetch origin refs/heads/main:refs/remotes/origin/main 2>/dev/null; then
BASE="origin/main" BASE="origin/main"
if ! git rev-parse --verify "$BASE" >/dev/null 2>&1; then elif git fetch origin refs/heads/master:refs/remotes/origin/master 2>/dev/null; then
BASE="origin/master" BASE="origin/master"
else
# Fallback: try Gitea/GitHub event context for the target branch
TARGET_BRANCH="${GITEA_BASE_REF:-${GITHUB_BASE_REF:-}}"
if [ -n "${TARGET_BRANCH}" ] && git fetch origin "refs/heads/${TARGET_BRANCH}:refs/remotes/origin/${TARGET_BRANCH}" 2>/dev/null; then
BASE="origin/${TARGET_BRANCH}"
else
echo "::warning::Could not fetch base branch. Trying origin/HEAD."
git fetch origin 2>/dev/null || true
BASE="origin/HEAD"
fi fi
fi
echo "Base ref: ${BASE} -> $(git rev-parse --short "${BASE}" 2>/dev/null || echo 'NOT FOUND')"
echo "HEAD: $(git rev-parse --short HEAD)"
echo "Files changed:"
git diff --stat "${BASE}...HEAD" 2>/dev/null | tail -3 || echo "(could not stat diff)"
# Build exclude pathspecs # Build exclude pathspecs
EXCLUDE_ARGS="" EXCLUDE_ARGS=""