From 7f686480673c31e61eeac5a34ddea3f75c4433b0 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 30 Oct 2022 10:16:43 -0600 Subject: [PATCH 1/7] fix: bug with finding merge-base --- diff-sha.sh | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index f2df0b98..3c4af86a 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -68,8 +68,8 @@ else fi function deepenShallowCloneToFindCommit() { - local ref="$1" - local target_branch="$2" + local target_branch="$1" + local ref="$2" local depth=20 local max_depth=$INPUT_MAX_FETCH_DEPTH @@ -87,6 +87,26 @@ function deepenShallowCloneToFindCommit() { done } +function deepenShallowCloneToFindCommitPullRequest() { + local target_branch="$1" + local current_branch="$2" + local depth=20 + local max_depth=$INPUT_MAX_FETCH_DEPTH + + while ! git merge-base "$target_branch" "$current_branch" &>/dev/null; do + echo "::debug::Unable to find merge-base in shallow clone. Increasing depth to $((depth * 2))..." + + depth=$((depth * 2)) + + if [[ $depth -gt $max_depth ]]; then + echo "::error::Unable to find merge-base in shallow clone. Please increase 'max_fetch_depth' to at least $depth." + exit 1 + fi + + git fetch --no-tags -u --progress --deepen="$depth" origin "$target_branch" "$current_branch" + done +} + if [[ -z $GITHUB_BASE_REF ]]; then echo "Running on a push event..." TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} && exit_status=$? || exit_status=$? @@ -136,7 +156,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then echo "::debug::Current branch $CURRENT_BRANCH..." echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" - deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "$TARGET_BRANCH" + deepenShallowCloneToFindCommit "$TARGET_BRANCH" "$PREVIOUS_SHA" 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=$? @@ -162,7 +182,7 @@ else echo "::debug::Current branch: $CURRENT_BRANCH" echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" - deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "$TARGET_BRANCH" + deepenShallowCloneToFindCommitPullRequest "$TARGET_BRANCH" "$CURRENT_BRANCH" 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 252a35e796a19f955902c1bae82aecb0fc28df9a Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 30 Oct 2022 10:20:35 -0600 Subject: [PATCH 2/7] Create 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 3c4af86a..ee8ca329 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -93,7 +93,7 @@ function deepenShallowCloneToFindCommitPullRequest() { local depth=20 local max_depth=$INPUT_MAX_FETCH_DEPTH - while ! git merge-base "$target_branch" "$current_branch" &>/dev/null; do + while [ -z $(git merge-base "$target_branch" "$current_branch") ]; do echo "::debug::Unable to find merge-base in shallow clone. Increasing depth to $((depth * 2))..." depth=$((depth * 2)) From c276f6ff7e04c078cc1baf3d7bc44fb7521e6eca Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 30 Oct 2022 10:27:25 -0600 Subject: [PATCH 3/7] 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 ee8ca329..0660f86d 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -93,7 +93,7 @@ function deepenShallowCloneToFindCommitPullRequest() { local depth=20 local max_depth=$INPUT_MAX_FETCH_DEPTH - while [ -z $(git merge-base "$target_branch" "$current_branch") ]; do + while [ -z "$(git merge-base "$target_branch" "$current_branch")" ]; do echo "::debug::Unable to find merge-base in shallow clone. Increasing depth to $((depth * 2))..." depth=$((depth * 2)) @@ -103,7 +103,7 @@ function deepenShallowCloneToFindCommitPullRequest() { exit 1 fi - git fetch --no-tags -u --progress --deepen="$depth" origin "$target_branch" "$current_branch" + git fetch --no-tags -u --progress --deepen="$depth" origin "$target_branch":"$target_branch" done } From 79704e5d8fc0b49fc207ff817d5191caae593d05 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 30 Oct 2022 10:34:50 -0600 Subject: [PATCH 4/7] 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 0660f86d..2f1ff268 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -93,7 +93,7 @@ function deepenShallowCloneToFindCommitPullRequest() { local depth=20 local max_depth=$INPUT_MAX_FETCH_DEPTH - while [ -z "$(git merge-base "$target_branch" "$current_branch")" ]; do + while ! git diff "$target_branch"..."$current_branch"; do echo "::debug::Unable to find merge-base in shallow clone. Increasing depth to $((depth * 2))..." depth=$((depth * 2)) From c1cc30c8d4fdb1702db018a6878fd41ed6ac94ef Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 30 Oct 2022 16:51:35 -0600 Subject: [PATCH 5/7] 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 2f1ff268..203f19c9 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -88,12 +88,12 @@ function deepenShallowCloneToFindCommit() { } function deepenShallowCloneToFindCommitPullRequest() { - local target_branch="$1" - local current_branch="$2" + local base_ref="$1" + local ref="$2" local depth=20 local max_depth=$INPUT_MAX_FETCH_DEPTH - while ! git diff "$target_branch"..."$current_branch"; do + while ! git diff "$base_ref"..."$ref"; do echo "::debug::Unable to find merge-base in shallow clone. Increasing depth to $((depth * 2))..." depth=$((depth * 2)) @@ -182,7 +182,7 @@ else echo "::debug::Current branch: $CURRENT_BRANCH" echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" - deepenShallowCloneToFindCommitPullRequest "$TARGET_BRANCH" "$CURRENT_BRANCH" + deepenShallowCloneToFindCommitPullRequest "$PREVIOUS_SHA" "$CURRENT_SHA" 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 fcedc1dc3fe78507618591b4c07f8d1188513f08 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 30 Oct 2022 16:58:38 -0600 Subject: [PATCH 6/7] Fixed bug with command. --- diff-sha.sh | 30 ++++++------------------------ 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index 203f19c9..cc5e0d4c 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -68,38 +68,20 @@ else fi function deepenShallowCloneToFindCommit() { - local target_branch="$1" - local ref="$2" - local depth=20 - local max_depth=$INPUT_MAX_FETCH_DEPTH - - while ! git rev-parse --quiet --verify "$ref^{commit}" &>/dev/null; do - echo "::debug::Unable to find commit '$ref' in shallow clone. Increasing depth to $((depth * 2))..." - - depth=$((depth * 2)) - - if [[ $depth -gt $max_depth ]]; then - echo "::error::Unable to find commit '$ref' in shallow clone. Maximum depth of $max_depth reached." - exit 1 - fi - - git fetch --no-tags -u --progress --deepen="$depth" origin "$target_branch":"$target_branch" - done -} - -function deepenShallowCloneToFindCommitPullRequest() { local base_ref="$1" local ref="$2" + local diff="$3" + local target_branch="$4" local depth=20 local max_depth=$INPUT_MAX_FETCH_DEPTH - while ! git diff "$base_ref"..."$ref"; do + while ! git diff "$base_ref$diff$ref" &>/dev/null; do echo "::debug::Unable to find merge-base in shallow clone. Increasing depth to $((depth * 2))..." depth=$((depth * 2)) if [[ $depth -gt $max_depth ]]; then - echo "::error::Unable to find merge-base in shallow clone. Please increase 'max_fetch_depth' to at least $depth." + echo "::error::Unable to find merge-base in shallow clone. Please increase 'max_fetch_depth' to at least $((depth + 20))." exit 1 fi @@ -156,7 +138,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then echo "::debug::Current branch $CURRENT_BRANCH..." echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" - deepenShallowCloneToFindCommit "$TARGET_BRANCH" "$PREVIOUS_SHA" + deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "$CURRENT_SHA" ".." "$TARGET_BRANCH" 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=$? @@ -182,7 +164,7 @@ else echo "::debug::Current branch: $CURRENT_BRANCH" echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" - deepenShallowCloneToFindCommitPullRequest "$PREVIOUS_SHA" "$CURRENT_SHA" + deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "$CURRENT_SHA" "..." "$TARGET_BRANCH" 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 a8c8155e8e2a754ba77cfe04253ec8896cd27a67 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Sun, 30 Oct 2022 17:06:59 -0600 Subject: [PATCH 7/7] update function --- diff-sha.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/diff-sha.sh b/diff-sha.sh index cc5e0d4c..426f12bc 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -69,8 +69,8 @@ fi function deepenShallowCloneToFindCommit() { local base_ref="$1" - local ref="$2" - local diff="$3" + local diff="$2" + local ref="$3" local target_branch="$4" local depth=20 local max_depth=$INPUT_MAX_FETCH_DEPTH @@ -138,7 +138,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then echo "::debug::Current branch $CURRENT_BRANCH..." echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" - deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "$CURRENT_SHA" ".." "$TARGET_BRANCH" + deepenShallowCloneToFindCommit "$PREVIOUS_SHA" ".." "$CURRENT_SHA" "$TARGET_BRANCH" 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=$? @@ -164,7 +164,7 @@ else echo "::debug::Current branch: $CURRENT_BRANCH" echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" - deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "$CURRENT_SHA" "..." "$TARGET_BRANCH" + deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "..." "$CURRENT_SHA" "$TARGET_BRANCH" 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=$?