diff --git a/action.yml b/action.yml index 1f1b9475..160846a6 100644 --- a/action.yml +++ b/action.yml @@ -78,7 +78,7 @@ inputs: required: false default: "false" fetch_depth: - description: "Limit depth of the branch history fetched. **NOTE**: This can be adjusted to resolve errors with insufficient history." + description: "Initial depth of the branch history fetched. **NOTE**: This can be adjusted to resolve errors with insufficient history." required: false default: "40" max_fetch_depth: diff --git a/diff-sha.sh b/diff-sha.sh index 3cdd64ef..2c583f28 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -5,6 +5,8 @@ set -euo pipefail INITIAL_COMMIT="false" GITHUB_OUTPUT=${GITHUB_OUTPUT:-""} EXTRA_ARGS="--no-tags" +PREVIOUS_SHA="" +CURRENT_SHA="" if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then EXTRA_ARGS="" @@ -145,6 +147,10 @@ else echo "Running on a pull request event..." TARGET_BRANCH=$GITHUB_BASE_REF CURRENT_BRANCH=$GITHUB_HEAD_REF + + if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then + TARGET_BRANCH=$CURRENT_BRANCH + fi echo "Fetching remote refs..." @@ -188,10 +194,14 @@ else if ! git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1; then # shellcheck disable=SC2046 - PREVIOUS_SHA=$(git rev-parse $(git branch -r --sort=-committerdate | head -1) 2>&1) && exit_status=$? || exit_status=$? + PREVIOUS_SHA=$(git rev-parse origin/"$CURRENT_BRANCH" 2>&1) && exit_status=$? || exit_status=$? fi else - PREVIOUS_SHA=$(git rev-list -n 1 "$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$? + PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? + + if ! git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA"..."$CURRENT_SHA" 1>/dev/null 2>&1; then + PREVIOUS_SHA=$(git rev-parse origin/"$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$? + fi fi if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then @@ -215,7 +225,7 @@ else echo "Fetching $depth commits..." # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS --deepen="$depth" origin "$TARGET_BRANCH" "$CURRENT_SHA"; + git fetch $EXTRA_ARGS -u --progress --deepen="$depth" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" if [[ $depth -gt $max_depth ]]; then echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA"