Skip to content

Commit

Permalink
fix: error fetch remote ref when using fetch depth of 1 (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
jackton1 committed Feb 20, 2023
1 parent 063e674 commit 23e3c43
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -439,16 +439,9 @@ jobs:
with:
fetch-depth: 0

- name: Get Base SHA
id: get-base-sha
run: |
echo "base_sha=$(git rev-parse "$(git tag --sort=-v:refname | head -n 2 | tail -n 1)")" >> $GITHUB_OUTPUT
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v35
with:
base_sha: ${{ steps.get-base-sha.outputs.base_sha }}

- name: Get changed files in the .github folder
id: changed-files-specific
Expand Down
1 change: 1 addition & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ runs:
GITHUB_REF: ${{ github.ref }}
GITHUB_SHA: ${{ github.sha }}
GITHUB_WORKSPACE: ${{ github.workspace }}
GITHUB_EVENT_BASE_REF: ${{ github.event.base_ref }}
GITHUB_EVENT_HEAD_REPO_FORK: ${{ github.event.pull_request.head.repo.fork }}
GITHUB_EVENT_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_EVENT_PULL_REQUEST_BASE_REF: ${{ github.event.pull_request.base.ref }}
Expand Down
31 changes: 26 additions & 5 deletions diff-sha.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ PREVIOUS_SHA=""
CURRENT_SHA=""
DIFF="..."
IS_TAG="false"
SOURCE_BRANCH=""

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

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

IS_SHALLOW=$(git rev-parse --is-shallow-repository) && exit_status=$? || exit_status=$?

if [[ $exit_status -ne 0 ]]; then
echo "::error::Unable to determine if the repository is shallow"
exit 1
fi

if [[ -z $GITHUB_EVENT_PULL_REQUEST_BASE_REF ]]; then
echo "Running on a push event..."
TARGET_BRANCH=$GITHUB_REFNAME
CURRENT_BRANCH=$TARGET_BRANCH

if $(git rev-parse --is-shallow-repository); then
if [[ "$IS_SHALLOW" == "true" ]]; then
echo "Fetching remote refs..."
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$CURRENT_BRANCH":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null
if [[ "$IS_TAG" == "false" ]]; then
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$CURRENT_BRANCH":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null
elif [[ "$SOURCE_BRANCH" != "" ]]; then
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin +refs/heads/"$SOURCE_BRANCH":refs/remotes/origin/"$SOURCE_BRANCH" 1>/dev/null
fi
# shellcheck disable=SC2086
git submodule foreach git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" || true
fi
Expand Down Expand Up @@ -102,6 +116,13 @@ if [[ -z $GITHUB_EVENT_PULL_REQUEST_BASE_REF ]]; then
echo "::error::Unable to locate a previous commit for the specified date: $INPUT_SINCE"
exit 1
fi
elif [[ "$IS_TAG" == "true" ]]; then
PREVIOUS_SHA=$(git rev-parse "$(git tag --sort=-v:refname | head -n 2 | tail -n 1)") && exit_status=$? || exit_status=$?

if [[ -z "$PREVIOUS_SHA" ]]; then
echo "::error::Unable to locate a previous commit for the specified tag: $GITHUB_REF"
exit 1
fi
else
if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then
PREVIOUS_SHA=""
Expand Down Expand Up @@ -162,7 +183,7 @@ else
TARGET_BRANCH=$CURRENT_BRANCH
fi

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

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

0 comments on commit 23e3c43

Please sign in to comment.