Skip to content

Commit 23e3c43

Browse files
authored
fix: error fetch remote ref when using fetch depth of 1 (#996)
1 parent 063e674 commit 23e3c43

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

README.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -439,16 +439,9 @@ jobs:
439439
with:
440440
fetch-depth: 0
441441
442-
- name: Get Base SHA
443-
id: get-base-sha
444-
run: |
445-
echo "base_sha=$(git rev-parse "$(git tag --sort=-v:refname | head -n 2 | tail -n 1)")" >> $GITHUB_OUTPUT
446-
447442
- name: Get changed files
448443
id: changed-files
449444
uses: tj-actions/changed-files@v35
450-
with:
451-
base_sha: ${{ steps.get-base-sha.outputs.base_sha }}
452445
453446
- name: Get changed files in the .github folder
454447
id: changed-files-specific

action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ runs:
187187
GITHUB_REF: ${{ github.ref }}
188188
GITHUB_SHA: ${{ github.sha }}
189189
GITHUB_WORKSPACE: ${{ github.workspace }}
190+
GITHUB_EVENT_BASE_REF: ${{ github.event.base_ref }}
190191
GITHUB_EVENT_HEAD_REPO_FORK: ${{ github.event.pull_request.head.repo.fork }}
191192
GITHUB_EVENT_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
192193
GITHUB_EVENT_PULL_REQUEST_BASE_REF: ${{ github.event.pull_request.base.ref }}

diff-sha.sh

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ PREVIOUS_SHA=""
99
CURRENT_SHA=""
1010
DIFF="..."
1111
IS_TAG="false"
12+
SOURCE_BRANCH=""
1213

1314
if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then
1415
IS_TAG="true"
1516
EXTRA_ARGS="--prune --no-recurse-submodules"
17+
SOURCE_BRANCH=${GITHUB_EVENT_BASE_REF#refs/heads/}
1618
fi
1719

1820
if [[ -z $GITHUB_EVENT_PULL_REQUEST_BASE_REF || "$GITHUB_EVENT_HEAD_REPO_FORK" == "true" ]]; then
@@ -52,15 +54,27 @@ else
5254
echo "Valid git version found: ($GIT_VERSION)"
5355
fi
5456

57+
IS_SHALLOW=$(git rev-parse --is-shallow-repository) && exit_status=$? || exit_status=$?
58+
59+
if [[ $exit_status -ne 0 ]]; then
60+
echo "::error::Unable to determine if the repository is shallow"
61+
exit 1
62+
fi
63+
5564
if [[ -z $GITHUB_EVENT_PULL_REQUEST_BASE_REF ]]; then
5665
echo "Running on a push event..."
5766
TARGET_BRANCH=$GITHUB_REFNAME
5867
CURRENT_BRANCH=$TARGET_BRANCH
5968

60-
if $(git rev-parse --is-shallow-repository); then
69+
if [[ "$IS_SHALLOW" == "true" ]]; then
6170
echo "Fetching remote refs..."
62-
# shellcheck disable=SC2086
63-
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$CURRENT_BRANCH":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null
71+
if [[ "$IS_TAG" == "false" ]]; then
72+
# shellcheck disable=SC2086
73+
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$CURRENT_BRANCH":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null
74+
elif [[ "$SOURCE_BRANCH" != "" ]]; then
75+
# shellcheck disable=SC2086
76+
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$SOURCE_BRANCH":refs/remotes/origin/"$SOURCE_BRANCH" 1>/dev/null
77+
fi
6478
# shellcheck disable=SC2086
6579
git submodule foreach git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" || true
6680
fi
@@ -102,6 +116,13 @@ if [[ -z $GITHUB_EVENT_PULL_REQUEST_BASE_REF ]]; then
102116
echo "::error::Unable to locate a previous commit for the specified date: $INPUT_SINCE"
103117
exit 1
104118
fi
119+
elif [[ "$IS_TAG" == "true" ]]; then
120+
PREVIOUS_SHA=$(git rev-parse "$(git tag --sort=-v:refname | head -n 2 | tail -n 1)") && exit_status=$? || exit_status=$?
121+
122+
if [[ -z "$PREVIOUS_SHA" ]]; then
123+
echo "::error::Unable to locate a previous commit for the specified tag: $GITHUB_REF"
124+
exit 1
125+
fi
105126
else
106127
if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then
107128
PREVIOUS_SHA=""
@@ -162,7 +183,7 @@ else
162183
TARGET_BRANCH=$CURRENT_BRANCH
163184
fi
164185

165-
if $(git rev-parse --is-shallow-repository); then
186+
if [[ "$IS_SHALLOW" == "true" ]]; then
166187
echo "Fetching remote refs..."
167188
# shellcheck disable=SC2086
168189
git fetch $EXTRA_ARGS -u --progress origin pull/"$GITHUB_EVENT_PULL_REQUEST_NUMBER"/head:"$CURRENT_BRANCH" 1>/dev/null
@@ -213,7 +234,7 @@ else
213234
else
214235
PREVIOUS_SHA=$(git rev-parse origin/"$TARGET_BRANCH") && exit_status=$? || exit_status=$?
215236

216-
if $(git rev-parse --is-shallow-repository); then
237+
if [[ "$IS_SHALLOW" == "true" ]]; then
217238
# check if the merge base is in the local history
218239
if ! git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 1>/dev/null 2>&1; then
219240
echo "::debug::Merge base is not in the local history, fetching remote target branch..."

0 commit comments

Comments
 (0)