From bd4ca4c44527cd2970b834f3fe67c2917a2c9aa8 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 13 Dec 2022 11:26:00 -0700 Subject: [PATCH 1/8] Update diff-sha.sh --- diff-sha.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index cd31e541..60db2ee9 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -160,7 +160,7 @@ 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 fetch -u --progress $EXTRA_ARGS --depth="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" 1>/dev/null git branch --track "$TARGET_BRANCH" origin/"$TARGET_BRANCH" 2>/dev/null || true else # shellcheck disable=SC2086 @@ -224,7 +224,7 @@ else echo "Fetching $i commits..." # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$i" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" + git fetch $EXTRA_ARGS -u --progress --depth="$i" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" if git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA$DIFF$CURRENT_SHA" 1>/dev/null 2>&1; then break From 8b8c347ee911e31bcbd91198c7e6a0eca13781ea Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 13 Dec 2022 12:11:37 -0700 Subject: [PATCH 2/8] Update diff-sha.sh --- diff-sha.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index 60db2ee9..d1de7492 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -220,7 +220,7 @@ else depth=$INPUT_FETCH_DEPTH max_depth=$INPUT_MAX_FETCH_DEPTH - for ((i=0; i Date: Tue, 13 Dec 2022 12:20:37 -0700 Subject: [PATCH 3/8] Update diff-sha.sh --- diff-sha.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index d1de7492..ce320c60 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -160,11 +160,11 @@ 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" 1>/dev/null - 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 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..." @@ -224,7 +224,7 @@ else echo "Fetching $i commits..." # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --depth="$i" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" + git fetch $EXTRA_ARGS -u --progress --depth="$i" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null 2>&1 if git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA$DIFF$CURRENT_SHA" 1>/dev/null 2>&1; then break From 6497e8ef995cd3c31c3db72f2528ef231963dafa Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 13 Dec 2022 13:24:35 -0700 Subject: [PATCH 4/8] Updated to fetch the common ancestor --- action.yml | 1 + diff-sha.sh | 34 ++++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/action.yml b/action.yml index 1c047883..310ccec8 100644 --- a/action.yml +++ b/action.yml @@ -176,6 +176,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 ce320c60..f51e3ac0 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 @@ -162,6 +162,28 @@ else # shellcheck disable=SC2086 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" 1>/dev/null 2>&1 @@ -221,14 +243,14 @@ else max_depth=$INPUT_MAX_FETCH_DEPTH for ((i=depth; i/dev/null 2>&1; then + break + fi + echo "Fetching $i commits..." # shellcheck disable=SC2086 git fetch $EXTRA_ARGS -u --progress --depth="$i" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null 2>&1 - - if git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA$DIFF$CURRENT_SHA" 1>/dev/null 2>&1; then - break - fi done if ((i >= max_depth)); then From 9cac2f8ea161459c397c5696e413511a539fc64e Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 13 Dec 2022 13:51:00 -0700 Subject: [PATCH 5/8] Update diff-sha.sh --- diff-sha.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index f51e3ac0..8235fc0f 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -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 From 49ac42e0253d381f0d5e26d1da24e5452d2a4aef Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 13 Dec 2022 14:02:31 -0700 Subject: [PATCH 6/8] Deprecate max_fetch_depth --- action.yml | 1 + diff-sha.sh | 25 ------------------------- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/action.yml b/action.yml index 310ccec8..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 diff --git a/diff-sha.sh b/diff-sha.sh index 8235fc0f..d2e1bbb1 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -237,31 +237,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=depth; i/dev/null 2>&1; then - break - fi - - echo "Fetching $i commits..." - - # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --depth="$i" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null 2>&1 - 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=$? From 2405ae6c59e4bcea355fc8bca67e86e80db2ea3d Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 13 Dec 2022 14:13:22 -0700 Subject: [PATCH 7/8] Updated the current sha --- diff-sha.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/diff-sha.sh b/diff-sha.sh index d2e1bbb1..e11d3e3f 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -203,6 +203,10 @@ else CURRENT_SHA=$(git rev-list --no-merges -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 --no-merges -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$? + fi fi fi From 4f939f8e80bfe2ebe34716e079a69f5e13c6858a Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 13 Dec 2022 14:25:47 -0700 Subject: [PATCH 8/8] fixed test --- diff-sha.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index e11d3e3f..47fc3a84 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -200,12 +200,12 @@ 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 --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=$? fi fi fi