From f0af2257d9ab310a9cfbd6832d9ae48a61b6c10d Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Thu, 19 Nov 2026 13:50:31 -0800 Subject: [PATCH 1/4] set-git-range: complete a shallow clone that lacks a merge commit's merge base In a GitHub Actions push build of a branch other than the default branch, the clone has no origin/, so set-git-range starts the range at the merge base of HEAD's two parents. When HEAD merges in more commits of the default branch than the clone's depth, the shallow clone lacks that merge base, and set-git-range failed with "cannot determine the commit range". Now it completes the clone and tries again, as it already did when computing a pull request's fork point. --- set-git-range | 30 ++--- tests/set-git-range-test/Makefile | 3 +- .../github-push-shallow-merge-test.sh | 104 ++++++++++++++++++ 3 files changed, 123 insertions(+), 14 deletions(-) create mode 100755 tests/set-git-range-test/github-push-shallow-merge-test.sh diff --git a/set-git-range b/set-git-range index 30eb3a7..8129181 100755 --- a/set-git-range +++ b/set-git-range @@ -201,6 +201,16 @@ _sgr_unshallow() { fi } +# Prints the merge base of commits $1 and $2, or nothing if they have none. +# A shallow clone can lack the merge base, as when a merge commit's second +# parent brings in more commits than the clone's depth; in that case, this +# completes the clone. +_sgr_merge_base() { + git merge-base "$1" "$2" 2> /dev/null && return + _sgr_unshallow > /dev/null + git merge-base "$1" "$2" 2> /dev/null +} + # Fetches $base_branch, the branch that a pull request is against, and sets # $base_tip to its commit. Does nothing on a second call, because fetching a # branch fetches its complete history, so once is enough. @@ -399,15 +409,9 @@ _sgr_set_git_range_body() { echo "${SCRIPT_NAME}: computing commit range start locally, as the fork point from ${base_branch:-the default branch}." >&2 _sgr_fetch_base_branch if [ -n "$base_tip" ]; then - CI_COMMIT_RANGE_START=$(git merge-base "$base_tip" "$CI_COMMIT_RANGE_END" 2> /dev/null) - if [ -z "$CI_COMMIT_RANGE_START" ]; then - # In a shallow clone, `git merge-base` either gives the right answer - # or no answer, so completing the clone is needed only now. The base - # branch does not need to be fetched again; only this clone's own - # history was missing. - _sgr_unshallow - CI_COMMIT_RANGE_START=$(git merge-base "$base_tip" "$CI_COMMIT_RANGE_END" 2> /dev/null) - fi + # The base branch does not need to be fetched again if the clone is + # completed; only this clone's own history can be missing. + CI_COMMIT_RANGE_START=$(_sgr_merge_base "$base_tip" "$CI_COMMIT_RANGE_END") fi if [ -z "$CI_COMMIT_RANGE_START" ]; then # Last resort. Under CircleCI, HEAD is the pull request's head branch, @@ -542,7 +546,7 @@ _sgr_set_git_range_body() { # a range covering only the last commit, with nothing said about the guess. if [ -n "$CI_BRANCH" ] && [ "$CI_BRANCH" = "$CI_DEFAULT_BRANCH_NAME" ]; then if git show --summary HEAD | grep -q ^Merge:; then - CI_COMMIT_RANGE_START=$(git merge-base HEAD^1 HEAD^2) + CI_COMMIT_RANGE_START=$(_sgr_merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(_sgr_head1) fi @@ -590,7 +594,7 @@ _sgr_set_git_range_body() { fi else if git show --summary HEAD | grep -q ^Merge:; then - CI_COMMIT_RANGE_START=$(git merge-base HEAD^1 HEAD^2) + CI_COMMIT_RANGE_START=$(_sgr_merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(_sgr_head1) fi @@ -651,7 +655,7 @@ _sgr_set_git_range_body() { echo "Resetting CI_COMMIT_RANGE_START because CI_COMMIT_RANGE=$CI_COMMIT_RANGE" >&2 fi if git show --summary "$CI_COMMIT_RANGE_END" | grep -q ^Merge:; then - CI_COMMIT_RANGE_START=$(git merge-base "$CI_COMMIT_RANGE_END^1" "$CI_COMMIT_RANGE_END^2") + CI_COMMIT_RANGE_START=$(_sgr_merge_base "$CI_COMMIT_RANGE_END^1" "$CI_COMMIT_RANGE_END^2") else if git rev-parse --verify --quiet "$CI_COMMIT_RANGE_END^1" > /dev/null 2>&1; then CI_COMMIT_RANGE_START=$(git rev-parse "$CI_COMMIT_RANGE_END^1") @@ -723,7 +727,7 @@ _sgr_status=$? # Clean up before restoring `set -e`, which makes the shell exit here if the # client uses `set -e` and this script failed. unset -f _sgr_set_git_range_body _sgr_redact_tokens _sgr_head1 _sgr_unshallow \ - _sgr_fetch_base_branch _sgr_commit_range_is_usable + _sgr_merge_base _sgr_fetch_base_branch _sgr_commit_range_is_usable if [ -n "$_sgr_had_errexit" ]; then set -e fi diff --git a/tests/set-git-range-test/Makefile b/tests/set-git-range-test/Makefile index f0c1c6d..8f4c7ac 100644 --- a/tests/set-git-range-test/Makefile +++ b/tests/set-git-range-test/Makefile @@ -2,8 +2,9 @@ all: test -test: ../../set-ci-org-and-branch ../../set-git-range set-git-range-test.sh azure-shallow-test.sh +test: ../../set-ci-org-and-branch ../../set-git-range set-git-range-test.sh azure-shallow-test.sh github-push-shallow-merge-test.sh ./set-git-range-test.sh ./azure-shallow-test.sh + ./github-push-shallow-merge-test.sh clean: diff --git a/tests/set-git-range-test/github-push-shallow-merge-test.sh b/tests/set-git-range-test/github-push-shallow-merge-test.sh new file mode 100755 index 0000000..f4a4fce --- /dev/null +++ b/tests/set-git-range-test/github-push-shallow-merge-test.sh @@ -0,0 +1,104 @@ +#!/bin/sh + +# Tests the commit range that `set-git-range` yields in a GitHub Actions job +# for a push to a branch other than the default branch, when the pushed commit +# merges the default branch into the branch. +# +# The `actions/checkout` step fetches only the pushed commit, to the depth that +# its `fetch-depth` gives, so the clone has no `origin/main`. `set-git-range` +# then starts the range at the merge base of the merge commit's two parents. +# This test checks that it finds that merge base even when the clone lacks it, +# as when the merge brings in more commits of the default branch than the +# clone's depth. + +set -eu + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)" +PLUME_SCRIPTS="$(CDPATH='' cd -- "${SCRIPT_DIR}/../.." && pwd -P)" + +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT HUP INT TERM + +### The origin repository: main, and a branch that forked from it and then +### merged many later commits of main. + +git init -q -b main "$work/upstream" +cd "$work/upstream" +git config user.email test@example.com +git config user.name "Test User" +echo 0 > main.txt +git add main.txt +git commit -q -m "Fork point" +FORK_POINT="$(git rev-parse HEAD)" +git checkout -q -b feature +echo 1 > feature.txt +git add feature.txt +git commit -q -m "Feature commit" +git checkout -q main +i=1 +while [ "$i" -le 10 ]; do + echo "$i" > main.txt + git commit -q -a -m "Main commit $i" + i=$((i + 1)) +done +git checkout -q feature +git merge -q --no-edit main > /dev/null +MERGE="$(git rev-parse HEAD)" +git checkout -q main +git clone -q --bare . "$work/origin.git" + +### The test + +status=0 + +# Make a clone the way that `actions/checkout` makes it for a push event. +clone="$work/clone" +git init -q -b main "$clone" +# A file:// URL, because `git fetch --depth` ignores the depth for a local path. +git -C "$clone" remote add origin "file://$work/origin.git" +git -C "$clone" fetch -q --depth=3 origin "+$MERGE:refs/remotes/origin/feature" +git -C "$clone" checkout -q --detach "$MERGE" +if git -C "$clone" merge-base "$MERGE^1" "$MERGE^2" > /dev/null 2>&1; then + echo "FAIL: test setup: the clone contains the merge base" + exit 1 +fi + +# Run with an empty environment except for the GitHub Actions variables, so +# that this test behaves the same whether or not it is itself running under CI. +run_status=0 +# The inner script is single-quoted on purpose: its arguments are passed +# positionally, so that this shell does not expand them into it. +# shellcheck disable=SC2016 +actual="$(env -i PATH="$PATH" HOME="$HOME" \ + GITHUB_ACTIONS=true \ + GITHUB_EVENT_NAME=push \ + GITHUB_REF=refs/heads/feature \ + GITHUB_REF_NAME=feature \ + GITHUB_REPOSITORY=testorg/testrepo \ + GITHUB_SHA="$MERGE" \ + sh -c ' + cd "$1" || exit 2 + PLUME_SCRIPTS="$2" + export PLUME_SCRIPTS + . "$2/set-git-range" || exit 2 + printf "%s %s" "$CI_COMMIT_RANGE_START" "$CI_COMMIT_RANGE_END" + ' sh "$clone" "$PLUME_SCRIPTS" 2> "$work/stderr")" || run_status=$? + +description="a GitHub Actions push of a merge commit whose merge base is absent from the shallow clone" +expected="$FORK_POINT $MERGE" +if [ "$run_status" -ne 0 ]; then + echo "FAIL: $description" + echo " exit status: $run_status" + echo " standard error:" + sed 's/^/ /' "$work/stderr" + status=1 +elif [ "$actual" = "$expected" ]; then + echo "PASS: $description" +else + echo "FAIL: $description" + echo " expected start and end: $expected" + echo " actual start and end: $actual" + status=1 +fi + +exit "$status" From 5ab07f48667e449074a40fee1f76c04975b486f5 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Thu, 19 Nov 2026 14:08:13 -0800 Subject: [PATCH 2/4] ci-info: complete a shallow clone that lacks a merge commit's merge base Keep `ci-info` in step with `set-git-range` by giving it the same `merge_base` helper. In both copies, show the error of `git merge-base` when it fails even after the clone is completed, and clarify the comment at the pull-request fork-point computation. --- ci-info | 39 ++++++++++++++++++++------------------- set-git-range | 9 ++++++--- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/ci-info b/ci-info index 2203f0d..99a8597 100755 --- a/ci-info +++ b/ci-info @@ -254,6 +254,18 @@ unshallow() { fi } +# Prints the merge base of commits $1 and $2, or nothing if they have none. +# A shallow clone can lack the merge base, as when a merge commit's second +# parent brings in more commits than the clone's depth; in that case, this +# completes the clone. +merge_base() { + # Hide the first try's errors, which a shallow clone can cause. Show the + # second try's, which explain a failure that completing the clone did not fix. + git merge-base "$1" "$2" 2> /dev/null && return + unshallow > /dev/null + git merge-base "$1" "$2" +} + # Fetches $base_branch, the branch that a pull request is against, and sets # $base_tip to its commit. Does nothing on a second call, because fetching a # branch fetches its complete history, so once is enough. @@ -710,15 +722,10 @@ elif [ -n "$CIRCLECI" ]; then emit_echo "${SCRIPT_NAME}: computing commit range start locally, as the fork point from ${base_branch:-the default branch}." fetch_base_branch if [ -n "$base_tip" ]; then - CI_COMMIT_RANGE_START=$(git merge-base "$base_tip" "$CI_COMMIT_RANGE_END" 2> /dev/null) - if [ -z "$CI_COMMIT_RANGE_START" ]; then - # In a shallow clone, `git merge-base` either gives the right answer - # or no answer, so completing the clone is needed only now. The base - # branch does not need to be fetched again; only this clone's own - # history was missing. - unshallow - CI_COMMIT_RANGE_START=$(git merge-base "$base_tip" "$CI_COMMIT_RANGE_END" 2> /dev/null) - fi + # `merge_base` completes the clone if needed. The base branch need not + # be fetched again, because only this clone's own history can be + # missing. + CI_COMMIT_RANGE_START=$(merge_base "$base_tip" "$CI_COMMIT_RANGE_END") fi if [ -z "$CI_COMMIT_RANGE_START" ]; then # Last resort. Under CircleCI, HEAD is the pull request's head branch, @@ -885,7 +892,7 @@ if [ -z "$CI_COMMIT_RANGE" ]; then fi if [ "$CI_BRANCH" = "$DEFAULT_BRANCH_NAME" ]; then if git show --summary HEAD | grep -q ^Merge:; then - CI_COMMIT_RANGE_START=$(git merge-base HEAD^1 HEAD^2) + CI_COMMIT_RANGE_START=$(merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(head1) fi @@ -905,13 +912,7 @@ if [ -z "$CI_COMMIT_RANGE" ]; then base_branch=${base_branch:-$DEFAULT_BRANCH_NAME} fetch_base_branch if [ -n "$base_tip" ]; then - CI_COMMIT_RANGE_START=$(git merge-base "$base_tip" "$CI_COMMIT_RANGE_END" 2> /dev/null) - if [ -z "$CI_COMMIT_RANGE_START" ]; then - # In a shallow clone, `git merge-base` either gives the right answer - # or no answer, so completing the clone is needed only now. - unshallow - CI_COMMIT_RANGE_START=$(git merge-base "$base_tip" "$CI_COMMIT_RANGE_END" 2> /dev/null) - fi + CI_COMMIT_RANGE_START=$(merge_base "$base_tip" "$CI_COMMIT_RANGE_END") if [ -n "$CI_COMMIT_RANGE_START" ] && [ "$DEBUG" = "--debug" ]; then emit_echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START as the fork point from ${base_branch}" fi @@ -924,7 +925,7 @@ if [ -z "$CI_COMMIT_RANGE" ]; then fi else if git show --summary HEAD | grep -q ^Merge:; then - CI_COMMIT_RANGE_START=$(git merge-base HEAD^1 HEAD^2) + CI_COMMIT_RANGE_START=$(merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(head1) fi @@ -959,7 +960,7 @@ if [ "$CI_COMMIT_RANGE_START" = "$CI_COMMIT_RANGE_END" ]; then emit_echo "Resetting CI_COMMIT_RANGE_START because CI_COMMIT_RANGE=$CI_COMMIT_RANGE" fi if git show --summary "$CI_COMMIT_RANGE_END" | grep -q ^Merge:; then - CI_COMMIT_RANGE_START=$(git merge-base "$CI_COMMIT_RANGE_END^1" "$CI_COMMIT_RANGE_END^2") + CI_COMMIT_RANGE_START=$(merge_base "$CI_COMMIT_RANGE_END^1" "$CI_COMMIT_RANGE_END^2") else if git rev-parse --verify --quiet "$CI_COMMIT_RANGE_END^1" > /dev/null 2>&1; then CI_COMMIT_RANGE_START=$(git rev-parse "$CI_COMMIT_RANGE_END^1") diff --git a/set-git-range b/set-git-range index 8129181..c52355c 100755 --- a/set-git-range +++ b/set-git-range @@ -206,9 +206,11 @@ _sgr_unshallow() { # parent brings in more commits than the clone's depth; in that case, this # completes the clone. _sgr_merge_base() { + # Hide the first try's errors, which a shallow clone can cause. Show the + # second try's, which explain a failure that completing the clone did not fix. git merge-base "$1" "$2" 2> /dev/null && return _sgr_unshallow > /dev/null - git merge-base "$1" "$2" 2> /dev/null + git merge-base "$1" "$2" } # Fetches $base_branch, the branch that a pull request is against, and sets @@ -409,8 +411,9 @@ _sgr_set_git_range_body() { echo "${SCRIPT_NAME}: computing commit range start locally, as the fork point from ${base_branch:-the default branch}." >&2 _sgr_fetch_base_branch if [ -n "$base_tip" ]; then - # The base branch does not need to be fetched again if the clone is - # completed; only this clone's own history can be missing. + # `_sgr_merge_base` completes the clone if needed. The base branch + # need not be fetched again, because only this clone's own history can + # be missing. CI_COMMIT_RANGE_START=$(_sgr_merge_base "$base_tip" "$CI_COMMIT_RANGE_END") fi if [ -z "$CI_COMMIT_RANGE_START" ]; then From df31a829f5fd278632bb2d571c7fb13361f9995b Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Thu, 19 Nov 2026 14:13:09 -0800 Subject: [PATCH 3/4] set-git-range: fetch the default branch to start a branch's range For a branch other than the default branch, start the range at the fork point from the freshly fetched default branch, as `ci-info` does, rather than at "origin/", which can be stale or absent. In a GitHub Actions push build it is absent, so the range started at the merge base of a merge commit's parents and included every commit that the merge brought in. --- set-git-range | 30 ++- .../github-push-shallow-merge-test.sh | 188 +++++++++++------- 2 files changed, 142 insertions(+), 76 deletions(-) diff --git a/set-git-range b/set-git-range index c52355c..d42036d 100755 --- a/set-git-range +++ b/set-git-range @@ -589,13 +589,31 @@ _sgr_set_git_range_body() { case $CI_BRANCH in renovate/*) CI_COMMIT_RANGE_START=$(_sgr_head1) ;; *) - if [ -z "$head_in_default_branch" ] \ - && git rev-parse --verify --quiet "origin/$CI_DEFAULT_BRANCH_NAME" > /dev/null 2>&1; then - CI_COMMIT_RANGE_START=$(git rev-parse "origin/$CI_DEFAULT_BRANCH_NAME") - if [ -n "$debug" ]; then - echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START from git rev-parse origin/$CI_DEFAULT_BRANCH_NAME" >&2 + CI_COMMIT_RANGE_START="" + if [ -z "$head_in_default_branch" ]; then + # Fetch the default branch rather than trusting + # "origin/$CI_DEFAULT_BRANCH_NAME", which can be arbitrarily out of + # date or absent. For example, the GitHub Actions `actions/checkout` + # step fetches only the commit being built, and when CircleCI + # restores a clone from its cache, its checkout step fetches only the + # branch being built. + base_branch=${base_branch:-$CI_DEFAULT_BRANCH_NAME} + _sgr_fetch_base_branch + if [ -n "$base_tip" ]; then + CI_COMMIT_RANGE_START=$(_sgr_merge_base "$base_tip" HEAD) + if [ -n "$CI_COMMIT_RANGE_START" ] && [ -n "$debug" ]; then + echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START as the fork point from ${base_branch}" >&2 + fi + fi + if [ -z "$CI_COMMIT_RANGE_START" ] \ + && git rev-parse --verify --quiet "origin/$CI_DEFAULT_BRANCH_NAME" > /dev/null 2>&1; then + CI_COMMIT_RANGE_START=$(git rev-parse "origin/$CI_DEFAULT_BRANCH_NAME") + if [ -n "$debug" ]; then + echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START from git rev-parse origin/$CI_DEFAULT_BRANCH_NAME" >&2 + fi fi - else + fi + if [ -z "$CI_COMMIT_RANGE_START" ]; then if git show --summary HEAD | grep -q ^Merge:; then CI_COMMIT_RANGE_START=$(_sgr_merge_base HEAD^1 HEAD^2) else diff --git a/tests/set-git-range-test/github-push-shallow-merge-test.sh b/tests/set-git-range-test/github-push-shallow-merge-test.sh index f4a4fce..1d359e3 100755 --- a/tests/set-git-range-test/github-push-shallow-merge-test.sh +++ b/tests/set-git-range-test/github-push-shallow-merge-test.sh @@ -1,15 +1,16 @@ #!/bin/sh # Tests the commit range that `set-git-range` yields in a GitHub Actions job -# for a push to a branch other than the default branch, when the pushed commit -# merges the default branch into the branch. +# for a push of a merge commit, in a shallow clone that lacks the merge base of +# the merge commit's parents. # # The `actions/checkout` step fetches only the pushed commit, to the depth that -# its `fetch-depth` gives, so the clone has no `origin/main`. `set-git-range` -# then starts the range at the merge base of the merge commit's two parents. -# This test checks that it finds that merge base even when the clone lacks it, -# as when the merge brings in more commits of the default branch than the -# clone's depth. +# its `fetch-depth` gives. This test checks that: +# * For a push to a branch other than the default branch, the range is the +# branch's changes relative to the default branch, even when the branch has +# just merged in more commits of the default branch than the clone's depth. +# * For a push to the default branch, the range starts at the merge base of +# the merge commit's parents, even when the clone lacks that merge base. set -eu @@ -19,86 +20,133 @@ PLUME_SCRIPTS="$(CDPATH='' cd -- "${SCRIPT_DIR}/../.." && pwd -P)" work="$(mktemp -d)" trap 'rm -rf "$work"' EXIT HUP INT TERM -### The origin repository: main, and a branch that forked from it and then -### merged many later commits of main. +### The origin repository + +# commit_files PREFIX COUNT: makes COUNT commits on the current branch, each of +# which changes file PREFIX.txt. +commit_files() { + j=1 + while [ "$j" -le "$2" ]; do + echo "$j" > "$1.txt" + git add "$1.txt" + git commit -q -m "$1 commit $j" + j=$((j + 1)) + done +} git init -q -b main "$work/upstream" cd "$work/upstream" git config user.email test@example.com git config user.name "Test User" -echo 0 > main.txt -git add main.txt +echo 0 > base.txt +git add base.txt git commit -q -m "Fork point" FORK_POINT="$(git rev-parse HEAD)" + +# The branch "feature" makes one commit, then merges in 10 commits of main. git checkout -q -b feature -echo 1 > feature.txt -git add feature.txt -git commit -q -m "Feature commit" +commit_files feature 1 git checkout -q main -i=1 -while [ "$i" -le 10 ]; do - echo "$i" > main.txt - git commit -q -a -m "Main commit $i" - i=$((i + 1)) -done +commit_files main 10 +MAIN_TIP="$(git rev-parse HEAD)" git checkout -q feature git merge -q --no-edit main > /dev/null -MERGE="$(git rev-parse HEAD)" +FEATURE_MERGE="$(git rev-parse HEAD)" + +# The branch "long" forks from the fork point and makes 10 commits, then main +# merges it, after 5 more commits of main. +git checkout -q -b long "$FORK_POINT" +commit_files long 10 git checkout -q main +commit_files main2 5 +git merge -q --no-edit long > /dev/null +MAIN_MERGE="$(git rev-parse HEAD)" + +git branch -q -D long + +# Two origin repositories: one in which main has merged "long", and one in +# which main is as it was when "feature" merged it. +git clone -q --bare . "$work/origin-merged.git" +git reset -q --hard "$MAIN_TIP" git clone -q --bare . "$work/origin.git" ### The test status=0 -# Make a clone the way that `actions/checkout` makes it for a push event. -clone="$work/clone" -git init -q -b main "$clone" -# A file:// URL, because `git fetch --depth` ignores the depth for a local path. -git -C "$clone" remote add origin "file://$work/origin.git" -git -C "$clone" fetch -q --depth=3 origin "+$MERGE:refs/remotes/origin/feature" -git -C "$clone" checkout -q --detach "$MERGE" -if git -C "$clone" merge-base "$MERGE^1" "$MERGE^2" > /dev/null 2>&1; then - echo "FAIL: test setup: the clone contains the merge base" - exit 1 -fi - -# Run with an empty environment except for the GitHub Actions variables, so -# that this test behaves the same whether or not it is itself running under CI. -run_status=0 -# The inner script is single-quoted on purpose: its arguments are passed -# positionally, so that this shell does not expand them into it. -# shellcheck disable=SC2016 -actual="$(env -i PATH="$PATH" HOME="$HOME" \ - GITHUB_ACTIONS=true \ - GITHUB_EVENT_NAME=push \ - GITHUB_REF=refs/heads/feature \ - GITHUB_REF_NAME=feature \ - GITHUB_REPOSITORY=testorg/testrepo \ - GITHUB_SHA="$MERGE" \ - sh -c ' - cd "$1" || exit 2 - PLUME_SCRIPTS="$2" - export PLUME_SCRIPTS - . "$2/set-git-range" || exit 2 - printf "%s %s" "$CI_COMMIT_RANGE_START" "$CI_COMMIT_RANGE_END" - ' sh "$clone" "$PLUME_SCRIPTS" 2> "$work/stderr")" || run_status=$? - -description="a GitHub Actions push of a merge commit whose merge base is absent from the shallow clone" -expected="$FORK_POINT $MERGE" -if [ "$run_status" -ne 0 ]; then - echo "FAIL: $description" - echo " exit status: $run_status" - echo " standard error:" - sed 's/^/ /' "$work/stderr" - status=1 -elif [ "$actual" = "$expected" ]; then - echo "PASS: $description" -else - echo "FAIL: $description" - echo " expected start and end: $expected" - echo " actual start and end: $actual" - status=1 -fi +# check ORIGIN BRANCH COMMIT EXPECTED_START EXPECTED_FILES DESCRIPTION: makes a +# clone of ORIGIN the way that `actions/checkout` makes it for a push of COMMIT +# to BRANCH, sources `set-git-range` in it, and checks the range's start and +# end and the files that its diff changes. DESCRIPTION is used for +# diagnostics. +check() { + origin="$1" + branch="$2" + commit="$3" + expected_start="$4" + expected_files="$5" + description="$6" + clone="$work/clone-$branch" + + git init -q -b main "$clone" + # A file:// URL, because `git fetch --depth` ignores the depth for a local path. + git -C "$clone" remote add origin "file://$origin" + git -C "$clone" fetch -q --depth=3 origin "+$commit:refs/remotes/origin/$branch" + git -C "$clone" checkout -q --detach "$commit" + if git -C "$clone" merge-base "$commit^1" "$commit^2" > /dev/null 2>&1; then + echo "FAIL: test setup for $description: the clone contains the merge base" + status=1 + return + fi + + # Run with an empty environment except for the GitHub Actions variables, so + # that this test behaves the same whether or not it is itself running under + # CI. + run_status=0 + # The inner script is single-quoted on purpose: its arguments are passed + # positionally, so that this shell does not expand them into it. + # shellcheck disable=SC2016 + actual="$(env -i PATH="$PATH" HOME="$HOME" \ + GITHUB_ACTIONS=true \ + GITHUB_EVENT_NAME=push \ + GITHUB_REF="refs/heads/$branch" \ + GITHUB_REF_NAME="$branch" \ + GITHUB_REPOSITORY=testorg/testrepo \ + GITHUB_SHA="$commit" \ + sh -c ' + cd "$1" || exit 2 + PLUME_SCRIPTS="$2" + export PLUME_SCRIPTS + . "$2/set-git-range" || exit 2 + echo "$CI_COMMIT_RANGE_START $CI_COMMIT_RANGE_END" + git diff --name-only "$CI_COMMIT_RANGE" || exit 3 + ' sh "$clone" "$PLUME_SCRIPTS" 2> "$work/stderr")" || run_status=$? + + expected="$expected_start $commit +$expected_files" + if [ "$run_status" -ne 0 ]; then + echo "FAIL: $description" + echo " exit status: $run_status" + echo " standard error:" + sed 's/^/ /' "$work/stderr" + status=1 + elif [ "$actual" = "$expected" ]; then + echo "PASS: $description" + else + echo "FAIL: $description" + echo " expected start and end, then changed files:" + printf '%s\n' "$expected" | sed 's/^/ /' + echo " actual start and end, then changed files:" + printf '%s\n' "$actual" | sed 's/^/ /' + status=1 + fi +} + +check "$work/origin.git" feature "$FEATURE_MERGE" "$MAIN_TIP" "feature.txt" \ + "a GitHub Actions push to a branch, of a merge of the default branch whose merge base is absent from the shallow clone" +check "$work/origin-merged.git" main "$MAIN_MERGE" "$FORK_POINT" "long.txt +main.txt +main2.txt" \ + "a GitHub Actions push to the default branch, of a merge whose merge base is absent from the shallow clone" exit "$status" From 1006df14b67a4cd1baf140c59fbee4629a125653 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Thu, 19 Nov 2026 14:35:06 -0800 Subject: [PATCH 4/4] set-git-range, ci-info: detect a merge in a shallow clone; handle an orphan branch * Decide whether a commit is a merge by reading the commit itself. In a shallow clone, `git show --summary` shows no parents for a commit at the depth limit, so a push of a merge in a `--depth=1` clone got an empty range. * For a branch push, fall back to "origin/" only when the fetch of the default branch fails. After a successful fetch, a branch with no merge base with the default branch, such as an orphan "gh-pages", has none with "origin/" either, so `git diff` rejected the range. * In the tests, exit on HUP, INT, or TERM rather than continue after the work directory is deleted. --- ci-info | 37 +++++++++++++------ set-git-range | 29 +++++++++++---- .../set-git-range-test/azure-shallow-test.sh | 6 ++- .../github-push-shallow-merge-test.sh | 10 +++-- .../set-git-range-test/set-git-range-test.sh | 6 ++- 5 files changed, 63 insertions(+), 25 deletions(-) diff --git a/ci-info b/ci-info index 99a8597..6676079 100755 --- a/ci-info +++ b/ci-info @@ -247,6 +247,16 @@ head1() { fi } +# Returns true if commit $1 is a merge commit, that is, has two or more +# parents. This reads the commit itself, because in a shallow clone, `git show` +# and `git rev-list` show no parents for a commit at the clone's depth limit, +# such as HEAD in a clone created with `git clone --depth=1`. +is_merge() { + # Only the header precedes the first empty line; the message could contain a + # line that starts with "parent ". + [ "$(git cat-file commit "$1" | sed '/^$/q' | grep -c '^parent ')" -ge 2 ] +} + # Converts a shallow clone into a complete one. unshallow() { if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then @@ -891,7 +901,7 @@ if [ -z "$CI_COMMIT_RANGE" ]; then emit_echo "Setting CI_COMMIT_RANGE from CI_BRANCH=$CI_BRANCH" fi if [ "$CI_BRANCH" = "$DEFAULT_BRANCH_NAME" ]; then - if git show --summary HEAD | grep -q ^Merge:; then + if is_merge HEAD; then CI_COMMIT_RANGE_START=$(merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(head1) @@ -912,23 +922,26 @@ if [ -z "$CI_COMMIT_RANGE" ]; then base_branch=${base_branch:-$DEFAULT_BRANCH_NAME} fetch_base_branch if [ -n "$base_tip" ]; then + # If the end has no merge base with the fetched tip, as for an orphan + # branch such as "gh-pages", leave the start empty for the code below. + # Do not fall back to "origin/$DEFAULT_BRANCH_NAME": the fetch has + # just updated it, so it has no merge base with the end either, and + # `git diff` would reject the range. CI_COMMIT_RANGE_START=$(merge_base "$base_tip" "$CI_COMMIT_RANGE_END") if [ -n "$CI_COMMIT_RANGE_START" ] && [ "$DEBUG" = "--debug" ]; then emit_echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START as the fork point from ${base_branch}" fi + elif git rev-parse --verify --quiet "origin/$DEFAULT_BRANCH_NAME" > /dev/null 2>&1; then + CI_COMMIT_RANGE_START=$(git rev-parse "origin/$DEFAULT_BRANCH_NAME") + if [ "$DEBUG" = "--debug" ]; then + emit_echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START from git rev-parse origin/$DEFAULT_BRANCH_NAME" + fi fi if [ -z "$CI_COMMIT_RANGE_START" ]; then - if git rev-parse --verify --quiet "origin/$DEFAULT_BRANCH_NAME" > /dev/null 2>&1; then - CI_COMMIT_RANGE_START=$(git rev-parse "origin/$DEFAULT_BRANCH_NAME") - if [ "$DEBUG" = "--debug" ]; then - emit_echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START from git rev-parse origin/$DEFAULT_BRANCH_NAME" - fi + if is_merge HEAD; then + CI_COMMIT_RANGE_START=$(merge_base HEAD^1 HEAD^2) else - if git show --summary HEAD | grep -q ^Merge:; then - CI_COMMIT_RANGE_START=$(merge_base HEAD^1 HEAD^2) - else - CI_COMMIT_RANGE_START=$(head1) - fi + CI_COMMIT_RANGE_START=$(head1) fi fi ;; @@ -959,7 +972,7 @@ if [ "$CI_COMMIT_RANGE_START" = "$CI_COMMIT_RANGE_END" ]; then if [ "$VERBOSE" = "--verbose" ]; then emit_echo "Resetting CI_COMMIT_RANGE_START because CI_COMMIT_RANGE=$CI_COMMIT_RANGE" fi - if git show --summary "$CI_COMMIT_RANGE_END" | grep -q ^Merge:; then + if is_merge "$CI_COMMIT_RANGE_END"; then CI_COMMIT_RANGE_START=$(merge_base "$CI_COMMIT_RANGE_END^1" "$CI_COMMIT_RANGE_END^2") else if git rev-parse --verify --quiet "$CI_COMMIT_RANGE_END^1" > /dev/null 2>&1; then diff --git a/set-git-range b/set-git-range index d42036d..714da03 100755 --- a/set-git-range +++ b/set-git-range @@ -194,6 +194,16 @@ _sgr_head1() { fi } +# Returns true if commit $1 is a merge commit, that is, has two or more +# parents. This reads the commit itself, because in a shallow clone, `git show` +# and `git rev-list` show no parents for a commit at the clone's depth limit, +# such as HEAD in a clone created with `git clone --depth=1`. +_sgr_is_merge() { + # Only the header precedes the first empty line; the message could contain a + # line that starts with "parent ". + [ "$(git cat-file commit "$1" | sed '/^$/q' | grep -c '^parent ')" -ge 2 ] +} + # Converts a shallow clone into a complete one. _sgr_unshallow() { if [ "$(git rev-parse --is-shallow-repository)" = "true" ]; then @@ -548,7 +558,7 @@ _sgr_set_git_range_body() { # a job where neither could be determined would then take this arm and get # a range covering only the last commit, with nothing said about the guess. if [ -n "$CI_BRANCH" ] && [ "$CI_BRANCH" = "$CI_DEFAULT_BRANCH_NAME" ]; then - if git show --summary HEAD | grep -q ^Merge:; then + if _sgr_is_merge HEAD; then CI_COMMIT_RANGE_START=$(_sgr_merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(_sgr_head1) @@ -600,13 +610,16 @@ _sgr_set_git_range_body() { base_branch=${base_branch:-$CI_DEFAULT_BRANCH_NAME} _sgr_fetch_base_branch if [ -n "$base_tip" ]; then + # If HEAD has no merge base with the fetched tip, as for an orphan + # branch such as "gh-pages", leave the start empty for the code + # below. Do not fall back to "origin/$CI_DEFAULT_BRANCH_NAME": + # the fetch has just updated it, so it has no merge base with HEAD + # either, and `git diff` would reject the range. CI_COMMIT_RANGE_START=$(_sgr_merge_base "$base_tip" HEAD) if [ -n "$CI_COMMIT_RANGE_START" ] && [ -n "$debug" ]; then echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START as the fork point from ${base_branch}" >&2 fi - fi - if [ -z "$CI_COMMIT_RANGE_START" ] \ - && git rev-parse --verify --quiet "origin/$CI_DEFAULT_BRANCH_NAME" > /dev/null 2>&1; then + elif git rev-parse --verify --quiet "origin/$CI_DEFAULT_BRANCH_NAME" > /dev/null 2>&1; then CI_COMMIT_RANGE_START=$(git rev-parse "origin/$CI_DEFAULT_BRANCH_NAME") if [ -n "$debug" ]; then echo "Set CI_COMMIT_RANGE_START=$CI_COMMIT_RANGE_START from git rev-parse origin/$CI_DEFAULT_BRANCH_NAME" >&2 @@ -614,7 +627,7 @@ _sgr_set_git_range_body() { fi fi if [ -z "$CI_COMMIT_RANGE_START" ]; then - if git show --summary HEAD | grep -q ^Merge:; then + if _sgr_is_merge HEAD; then CI_COMMIT_RANGE_START=$(_sgr_merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(_sgr_head1) @@ -675,7 +688,7 @@ _sgr_set_git_range_body() { if [ -n "$verbose" ]; then echo "Resetting CI_COMMIT_RANGE_START because CI_COMMIT_RANGE=$CI_COMMIT_RANGE" >&2 fi - if git show --summary "$CI_COMMIT_RANGE_END" | grep -q ^Merge:; then + if _sgr_is_merge "$CI_COMMIT_RANGE_END"; then CI_COMMIT_RANGE_START=$(_sgr_merge_base "$CI_COMMIT_RANGE_END^1" "$CI_COMMIT_RANGE_END^2") else if git rev-parse --verify --quiet "$CI_COMMIT_RANGE_END^1" > /dev/null 2>&1; then @@ -747,8 +760,8 @@ _sgr_status=$? # Clean up before restoring `set -e`, which makes the shell exit here if the # client uses `set -e` and this script failed. -unset -f _sgr_set_git_range_body _sgr_redact_tokens _sgr_head1 _sgr_unshallow \ - _sgr_merge_base _sgr_fetch_base_branch _sgr_commit_range_is_usable +unset -f _sgr_set_git_range_body _sgr_redact_tokens _sgr_head1 _sgr_is_merge \ + _sgr_unshallow _sgr_merge_base _sgr_fetch_base_branch _sgr_commit_range_is_usable if [ -n "$_sgr_had_errexit" ]; then set -e fi diff --git a/tests/set-git-range-test/azure-shallow-test.sh b/tests/set-git-range-test/azure-shallow-test.sh index 70cc90c..54a4dba 100755 --- a/tests/set-git-range-test/azure-shallow-test.sh +++ b/tests/set-git-range-test/azure-shallow-test.sh @@ -21,7 +21,11 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)" PLUME_SCRIPTS="$(CDPATH='' cd -- "${SCRIPT_DIR}/../.." && pwd -P)" work="$(mktemp -d)" -trap 'rm -rf "$work"' EXIT HUP INT TERM +trap 'rm -rf "$work"' EXIT +# On a signal, exit, which runs the EXIT trap, rather than continue without $work. +trap 'exit 129' HUP +trap 'exit 130' INT +trap 'exit 143' TERM ### The origin repository: main, a pull request branch that forked from it ### long ago, and the merge commit that Azure would build. diff --git a/tests/set-git-range-test/github-push-shallow-merge-test.sh b/tests/set-git-range-test/github-push-shallow-merge-test.sh index 1d359e3..b3e040f 100755 --- a/tests/set-git-range-test/github-push-shallow-merge-test.sh +++ b/tests/set-git-range-test/github-push-shallow-merge-test.sh @@ -18,7 +18,11 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)" PLUME_SCRIPTS="$(CDPATH='' cd -- "${SCRIPT_DIR}/../.." && pwd -P)" work="$(mktemp -d)" -trap 'rm -rf "$work"' EXIT HUP INT TERM +trap 'rm -rf "$work"' EXIT +# On a signal, exit, which runs the EXIT trap, rather than continue without $work. +trap 'exit 129' HUP +trap 'exit 130' INT +trap 'exit 143' TERM ### The origin repository @@ -77,8 +81,8 @@ status=0 # check ORIGIN BRANCH COMMIT EXPECTED_START EXPECTED_FILES DESCRIPTION: makes a # clone of ORIGIN the way that `actions/checkout` makes it for a push of COMMIT # to BRANCH, sources `set-git-range` in it, and checks the range's start and -# end and the files that its diff changes. DESCRIPTION is used for -# diagnostics. +# end and the files that its diff changes. DESCRIPTION names the case in the +# PASS or FAIL line that this prints. check() { origin="$1" branch="$2" diff --git a/tests/set-git-range-test/set-git-range-test.sh b/tests/set-git-range-test/set-git-range-test.sh index 32082da..1c20466 100755 --- a/tests/set-git-range-test/set-git-range-test.sh +++ b/tests/set-git-range-test/set-git-range-test.sh @@ -17,7 +17,11 @@ SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)" PLUME_SCRIPTS="$(CDPATH='' cd -- "${SCRIPT_DIR}/../.." && pwd -P)" work="$(mktemp -d)" -trap 'rm -rf "$work"' EXIT HUP INT TERM +trap 'rm -rf "$work"' EXIT +# On a signal, exit, which runs the EXIT trap, rather than continue without $work. +trap 'exit 129' HUP +trap 'exit 130' INT +trap 'exit 143' TERM ### A repository with dotted tag names