diff --git a/action.yml b/action.yml index 1c047883..483c70fc 100644 --- a/action.yml +++ b/action.yml @@ -88,6 +88,7 @@ inputs: description: "Maximum depth of the branch history fetched. **NOTE**: This can be adjusted to resolve errors with insufficient history." required: false default: "5000" + deprecationMessage: "This input is deprecated and will be removed in a future release." since_last_remote_commit: description: "Use the last commit on the remote branch as the `base_sha`. Defaults to the last non merge commit on the target branch for pull request events and the previous remote commit of the current branch for push events." required: true @@ -176,6 +177,7 @@ runs: GITHUB_EVENT_HEAD_REPO_FORK: ${{ github.event.pull_request.head.repo.fork }} GITHUB_EVENT_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }} GITHUB_EVENT_PULL_REQUEST_HEAD_SHA: ${{ github.event.pull_request.head.sha }} + GITHUB_EVENT_PULL_REQUEST_COMMITS: ${{ github.event.pull_request.commits }} GITHUB_EVENT_BEFORE: ${{ github.event.before }} GITHUB_EVENT_FORCED: ${{ github.event.forced }} GITHUB_REFNAME: ${{ github.ref_name }} diff --git a/diff-sha.sh b/diff-sha.sh index cd31e541..47fc3a84 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -4,13 +4,13 @@ set -euo pipefail INITIAL_COMMIT="false" GITHUB_OUTPUT=${GITHUB_OUTPUT:-""} -EXTRA_ARGS="--no-tags" +EXTRA_ARGS="--no-tags --prune --no-recurse-submodules" PREVIOUS_SHA="" CURRENT_SHA="" DIFF="..." if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then - EXTRA_ARGS="" + EXTRA_ARGS="--prune --no-recurse-submodules" fi if [[ "$GITHUB_EVENT_HEAD_REPO_FORK" == "true" ]]; then @@ -69,7 +69,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$? else # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH" + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH" 1>/dev/null 2>&1 CURRENT_SHA=$INPUT_SHA; exit_status=$? fi fi @@ -132,7 +132,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then fi else # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH" + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH" 1>/dev/null 2>&1 PREVIOUS_SHA=$INPUT_BASE_SHA fi @@ -160,11 +160,33 @@ else if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "false" ]]; then # shellcheck disable=SC2086 - git fetch -u --progress $EXTRA_ARGS --depth="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" - git branch --track "$TARGET_BRANCH" origin/"$TARGET_BRANCH" 2>/dev/null || true + git fetch -u --progress $EXTRA_ARGS --depth="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" 1>/dev/null 2>&1 + git branch --track "$TARGET_BRANCH" origin/"$TARGET_BRANCH" 1>/dev/null 2>&1 || true + # shellcheck disable=SC2086 + git fetch $EXTRA_ARGS -u --progress --depth=$(( GITHUB_EVENT_PULL_REQUEST_COMMITS + 1 )) origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null 2>&1 + + COMMON_ANCESTOR=$(git rev-list --first-parent --max-parents=0 --max-count=1 origin/"$CURRENT_BRANCH" 2>&1) && exit_status=$? || exit_status=$? + + if [[ -z "$COMMON_ANCESTOR" ]]; then + echo "::error::Unable to locate a common ancestor for the current branch: $CURRENT_BRANCH" + exit 1 + else + echo "::debug::Common ancestor: $COMMON_ANCESTOR" + fi + + DATE=$(git log --date=iso8601 --format=%cd "${COMMON_ANCESTOR}") + + if [[ -z "$DATE" ]]; then + echo "::error::Unable to locate a date for the common ancestor: $COMMON_ANCESTOR" + exit 1 + else + # shellcheck disable=SC2086 + git fetch $EXTRA_ARGS --shallow-since="${DATE}" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" 1>/dev/null 2>&1 + echo "::debug::Date: $DATE" + fi else # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" + git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null 2>&1 fi echo "::debug::Getting HEAD SHA..." @@ -178,9 +200,13 @@ else fi else if [[ -z $INPUT_SHA ]]; then - CURRENT_SHA=$(git rev-list --no-merges -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$? + CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$? else CURRENT_SHA=$INPUT_SHA; exit_status=$? + + if [[ "$CURRENT_SHA" == "$GITHUB_EVENT_PULL_REQUEST_HEAD_SHA" ]]; then + CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$? + fi fi fi @@ -215,31 +241,6 @@ else PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? fi - if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "false" ]]; then - if [[ -f .git/shallow ]]; then - depth=$INPUT_FETCH_DEPTH - max_depth=$INPUT_MAX_FETCH_DEPTH - - for ((i=0; i/dev/null 2>&1; then - break - fi - done - - if ((i >= max_depth)); then - echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_BRANCH with: $PREVIOUS_SHA$DIFF$CURRENT_SHA" - exit 1 - fi - else - echo "::debug::Not a shallow clone, skipping merge-base check." - fi - fi - echo "::debug::Previous SHA: $PREVIOUS_SHA" else PREVIOUS_SHA=$INPUT_BASE_SHA && exit_status=$? || exit_status=$?