From 210cc839c24f532fe4fbf510b7b3314ca9a2b90b Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Mon, 6 Feb 2023 18:25:06 -0700 Subject: [PATCH] fix: including non branch changes in diff output (#973) Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com> --- .github/workflows/test.yml | 3 +++ diff-sha.sh | 22 +++++++++++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ba49ca30..9e252e6d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -210,6 +210,7 @@ jobs: matrix: platform: [ubuntu-latest] fetch-depth: [1, 2] + input-fetch_depth: [1, 50] steps: - name: Checkout to branch @@ -220,6 +221,8 @@ jobs: - name: Run changed-files id: changed-files uses: ./ + with: + fetch_depth: ${{ matrix.input-fetch_depth }} - name: Show output run: | diff --git a/diff-sha.sh b/diff-sha.sh index f6052fa7..8463c41a 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -168,8 +168,7 @@ else if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" != "true" ]]; then echo "::debug::Fetching remote target branch..." # 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" 1>/dev/null 2>&1 || true + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$TARGET_BRANCH:$TARGET_BRANCH" 1>/dev/null fi fi @@ -209,10 +208,23 @@ else PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA fi else - PREVIOUS_SHA=$(git merge-base "$TARGET_BRANCH" "$CURRENT_SHA") && exit_status=$? || exit_status=$? + PREVIOUS_SHA=$(git rev-parse origin/"$TARGET_BRANCH") && exit_status=$? || exit_status=$? - if ! git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA$DIFF$CURRENT_SHA" 1>/dev/null 2>&1; then - PREVIOUS_SHA=$(git rev-parse origin/"$TARGET_BRANCH") && exit_status=$? || exit_status=$? + if [[ -f .git/shallow ]]; then + # check if the merge base is in the local history + if ! git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA"; then + echo "::debug::Merge base is not in the local history, fetching remote target branch..." + # Fetch more of the target branch history until the merge base is found + for i in {1..10}; do + # shellcheck disable=SC2086 + git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$TARGET_BRANCH:$TARGET_BRANCH" 1>/dev/null + if git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA"; then + break + fi + echo "::debug::Merge base is not in the local history, fetching remote target branch again..." + echo "::debug::Attempt $i/10" + done + fi fi fi