From 6497e8ef995cd3c31c3db72f2528ef231963dafa Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 13 Dec 2022 13:24:35 -0700 Subject: [PATCH] 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