diff --git a/ci-info b/ci-info index 2203f0d..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 @@ -254,6 +264,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 +732,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, @@ -884,8 +901,8 @@ 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 - CI_COMMIT_RANGE_START=$(git merge-base HEAD^1 HEAD^2) + if is_merge HEAD; then + CI_COMMIT_RANGE_START=$(merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(head1) fi @@ -905,29 +922,26 @@ 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 + # 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=$(git merge-base HEAD^1 HEAD^2) - else - CI_COMMIT_RANGE_START=$(head1) - fi + CI_COMMIT_RANGE_START=$(head1) fi fi ;; @@ -958,8 +972,8 @@ 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 - CI_COMMIT_RANGE_START=$(git merge-base "$CI_COMMIT_RANGE_END^1" "$CI_COMMIT_RANGE_END^2") + 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 CI_COMMIT_RANGE_START=$(git rev-parse "$CI_COMMIT_RANGE_END^1") diff --git a/set-git-range b/set-git-range index 30eb3a7..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 @@ -201,6 +211,18 @@ _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() { + # 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" +} + # 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 +421,10 @@ _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 + # `_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 # Last resort. Under CircleCI, HEAD is the pull request's head branch, @@ -541,8 +558,8 @@ _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 - CI_COMMIT_RANGE_START=$(git merge-base HEAD^1 HEAD^2) + if _sgr_is_merge HEAD; then + CI_COMMIT_RANGE_START=$(_sgr_merge_base HEAD^1 HEAD^2) else CI_COMMIT_RANGE_START=$(_sgr_head1) fi @@ -582,15 +599,36 @@ _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 + # 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 + 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 + fi fi - else - if git show --summary HEAD | grep -q ^Merge:; then - CI_COMMIT_RANGE_START=$(git merge-base HEAD^1 HEAD^2) + fi + if [ -z "$CI_COMMIT_RANGE_START" ]; 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) fi @@ -650,8 +688,8 @@ _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 - CI_COMMIT_RANGE_START=$(git merge-base "$CI_COMMIT_RANGE_END^1" "$CI_COMMIT_RANGE_END^2") + 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 CI_COMMIT_RANGE_START=$(git rev-parse "$CI_COMMIT_RANGE_END^1") @@ -722,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_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/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/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 new file mode 100755 index 0000000..b3e040f --- /dev/null +++ b/tests/set-git-range-test/github-push-shallow-merge-test.sh @@ -0,0 +1,156 @@ +#!/bin/sh + +# Tests the commit range that `set-git-range` yields in a GitHub Actions job +# 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. 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 + +SCRIPT_DIR="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)" +PLUME_SCRIPTS="$(CDPATH='' cd -- "${SCRIPT_DIR}/../.." && pwd -P)" + +work="$(mktemp -d)" +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 + +# 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 > 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 +commit_files feature 1 +git checkout -q main +commit_files main 10 +MAIN_TIP="$(git rev-parse HEAD)" +git checkout -q feature +git merge -q --no-edit main > /dev/null +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 + +# 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 names the case in the +# PASS or FAIL line that this prints. +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" 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