From 69b90fbdbe2b02b550daf99a7987222f58291f79 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 4 Dec 2022 23:13:42 -0700 Subject: [PATCH 1/7] fix: determining the merge-base with limited history. --- diff-sha.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index d0d645ac..863537c0 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -224,7 +224,7 @@ else depth=$INPUT_FETCH_DEPTH max_depth=$INPUT_MAX_FETCH_DEPTH - while ! git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA"..."$CURRENT_SHA" 1>/dev/null; do + while ! git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null; do echo "Fetching $depth commits..." # shellcheck disable=SC2086 From a20b263d03e128745ede721a7b7344f324251841 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 4 Dec 2022 23:16:45 -0700 Subject: [PATCH 2/7] Updated to use merge-base --- diff-sha.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index 863537c0..9435ea1a 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -203,7 +203,7 @@ else 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=$? + PREVIOUS_SHA=$(git merge-base origin/"$TARGET_BRANCH" HEAD 2>&1) && exit_status=$? || exit_status=$? fi fi From f282e19e523425ed86464aef10c890e6e583172c Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 4 Dec 2022 23:27:32 -0700 Subject: [PATCH 3/7] Updated to use merge-base --- diff-sha.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index 9435ea1a..154df435 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -203,7 +203,7 @@ else 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 merge-base origin/"$TARGET_BRANCH" HEAD 2>&1) && exit_status=$? || exit_status=$? + PREVIOUS_SHA=$(git rev-parse origin/"$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$? fi fi @@ -230,6 +230,8 @@ else # shellcheck disable=SC2086 git fetch $EXTRA_ARGS -u --progress --deepen="$depth" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" + PREVIOUS_SHA=$(git merge-base origin/"$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) + if [[ $depth -gt $max_depth ]]; then echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA" exit 1 From ef8591de30d4dbf1ae8d2ec0c1c8d2f505c4aac3 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 4 Dec 2022 23:33:22 -0700 Subject: [PATCH 4/7] Updated to use merge-base --- diff-sha.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 154df435..43346aa5 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -200,8 +200,12 @@ else PREVIOUS_SHA=$(git rev-parse origin/"$CURRENT_BRANCH" 2>&1) && exit_status=$? || exit_status=$? fi else - PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? - + PREVIOUS_SHA=$(git merge-base "$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) && exit_status=$? || exit_status=$? + + if [[ -z "$PREVIOUS_SHA" ]]; then + PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? + fi + 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 From 0d9ec12402fabe56ca0056840e6a904519754af1 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 4 Dec 2022 23:54:27 -0700 Subject: [PATCH 5/7] Fixed test --- diff-sha.sh | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 43346aa5..7dba02fd 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -215,6 +215,31 @@ 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 + + while ! git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null; do + echo "Fetching $depth commits..." + + # shellcheck disable=SC2086 + git fetch $EXTRA_ARGS -u --progress --deepen="$depth" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" + + PREVIOUS_SHA=$(git merge-base origin/"$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) + + if [[ $depth -gt $max_depth ]]; then + echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA" + exit 1 + fi + + depth=$((depth + 300)) + done + 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=$? @@ -223,31 +248,6 @@ else echo "::debug::Target branch: $TARGET_BRANCH" echo "::debug::Current branch: $CURRENT_BRANCH" - if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "false" ]]; then - if [[ -f .git/shallow ]]; then - depth=$INPUT_FETCH_DEPTH - max_depth=$INPUT_MAX_FETCH_DEPTH - - while ! git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null; do - echo "Fetching $depth commits..." - - # shellcheck disable=SC2086 - git fetch $EXTRA_ARGS -u --progress --deepen="$depth" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" - - PREVIOUS_SHA=$(git merge-base origin/"$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) - - if [[ $depth -gt $max_depth ]]; then - echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA" - exit 1 - fi - - depth=$((depth + 300)) - done - else - echo "::debug::Not a shallow clone, skipping merge-base check." - fi - fi - echo "::debug::Verifying the previous commit SHA: $PREVIOUS_SHA" git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$? From a699a3be4a68a8727496e823c08b6d8760c23ab4 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 00:02:56 -0700 Subject: [PATCH 6/7] Fixed test --- diff-sha.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index 7dba02fd..ea00ba8d 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -226,7 +226,7 @@ else # shellcheck disable=SC2086 git fetch $EXTRA_ARGS -u --progress --deepen="$depth" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" - PREVIOUS_SHA=$(git merge-base origin/"$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) + PREVIOUS_SHA=$(git merge-base origin/"$TARGET_BRANCH" "$CURRENT_SHA" 2>&1) && exit_status=$? || exit_status=$? if [[ $depth -gt $max_depth ]]; then echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_SHA" From 477b3805799bb3f730bede86285a0362c52b87cf Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 5 Dec 2022 00:05:01 -0700 Subject: [PATCH 7/7] Updated to use git diff --- diff-sha.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index ea00ba8d..78acf662 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -220,7 +220,7 @@ else depth=$INPUT_FETCH_DEPTH max_depth=$INPUT_MAX_FETCH_DEPTH - while ! git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null; do + while ! git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA"..."$CURRENT_SHA"; do echo "Fetching $depth commits..." # shellcheck disable=SC2086