Skip to content

Commit

Permalink
Merge pull request #606 from tj-actions/fix/bug-using-invalid-fetch-d…
Browse files Browse the repository at this point in the history
…epth

fix: bug using invalid fetch-depth
  • Loading branch information
jackton1 committed Sep 3, 2022
2 parents 3f5b196 + 5dd27b1 commit 60f4aab
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/manual-test.yml
Expand Up @@ -11,7 +11,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]
platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]

steps:
- name: Checkout
Expand Down
49 changes: 43 additions & 6 deletions .github/workflows/test.yml
Expand Up @@ -30,7 +30,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]
platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]

steps:
- name: Checkout
Expand Down Expand Up @@ -70,7 +70,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]
platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]
steps:
- name: Checkout into dir1
uses: actions/checkout@v3
Expand Down Expand Up @@ -126,11 +126,48 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]
platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]

steps:
- name: Checkout to branch
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Run changed-files with similar base and commit sha
id: changed-files
continue-on-error: true
uses: ./
with:
base_sha: d1c0ee4
sha: d1c0ee4

- name: Exit with 1 if no error is raised
if: steps.changed-files.outcome != 'failure'
run: |
echo "Expected: (failure) got ${{ steps.changed-files.outcome }}"
exit 1
- name: Show output
run: |
echo '${{ toJSON(steps.changed-files.outputs) }}'
shell:
bash

test-single-commit-history:
name: Test changed-files single commit history
runs-on: ${{ matrix.platform }}
strategy:
fail-fast: false
max-parallel: 4
matrix:
platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]

steps:
- name: Checkout to branch
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: Run changed-files with a single commit history
id: changed-files
Expand All @@ -149,7 +186,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]
platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]

steps:
- name: Checkout to branch
Expand Down Expand Up @@ -201,7 +238,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]
platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]

steps:
- name: Checkout to branch
Expand Down Expand Up @@ -253,7 +290,7 @@ jobs:
fail-fast: false
max-parallel: 4
matrix:
platform: [ubuntu-latest, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]
platform: [ubuntu-latest, ubuntu-22.04, windows-latest, macos-latest, macos-11, ubuntu-18.04, windows-2022]

steps:
- name: Checkout
Expand Down
33 changes: 19 additions & 14 deletions diff-sha.sh
Expand Up @@ -23,7 +23,7 @@ function __version() {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
}

GIT_VERSION=$(git --version | awk '{print $3}'); exit_status=$?
GIT_VERSION=$(git --version | awk '{print $3}') && exit_status=$? || exit_status=$?

if [[ $exit_status -ne 0 ]]; then
echo "::error::git not installed"
Expand All @@ -40,12 +40,13 @@ fi
echo "::debug::Getting HEAD SHA..."

if [[ -z $INPUT_SHA ]]; then
CURRENT_SHA=$(git rev-list -n 1 "HEAD" 2>&1); exit_status=$?
CURRENT_SHA=$(git rev-list -n 1 "HEAD" 2>&1) && exit_status=$? || exit_status=$?
else
CURRENT_SHA=$INPUT_SHA; exit_status=$?
fi

git rev-parse --quiet --verify "$CURRENT_SHA^{commit}" 1>/dev/null 2>&1; exit_status=$?
echo "::debug::Verifying the current commit SHA: $CURRENT_SHA"
git rev-parse --quiet --verify "$CURRENT_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?

if [[ $exit_status -ne 0 ]]; then
echo "::error::Unable to locate the current sha: $CURRENT_SHA"
Expand All @@ -62,8 +63,10 @@ if [[ -z $GITHUB_BASE_REF ]]; then
echo "::debug::GITHUB_BASE_REF unset using $TARGET_BRANCH..."

if [[ -z $INPUT_BASE_SHA ]]; then
git fetch --no-tags -u --progress origin --depth=2 "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$?

if [[ $(git rev-list --count "HEAD") -gt 1 ]]; then
PREVIOUS_SHA=$(git rev-parse "@~" 2>&1); exit_status=$?
PREVIOUS_SHA=$(git rev-parse "@~1" 2>&1) && exit_status=$? || exit_status=$?
echo "::debug::Previous SHA: $PREVIOUS_SHA"
else
PREVIOUS_SHA=$CURRENT_SHA; exit_status=$?
Expand All @@ -73,12 +76,13 @@ if [[ -z $GITHUB_BASE_REF ]]; then
fi
else
PREVIOUS_SHA=$INPUT_BASE_SHA; exit_status=$?
TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1); exit_status=$?
TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1) && exit_status=$? || exit_status=$?
echo "::debug::Previous SHA: $PREVIOUS_SHA"
echo "::debug::Target branch: $TARGET_BRANCH"
fi

git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1; exit_status=$?
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=$?

if [[ $exit_status -ne 0 ]]; then
echo "::error::Unable to locate the previous sha: $PREVIOUS_SHA"
Expand All @@ -94,24 +98,24 @@ else
if [[ -z $INPUT_BASE_SHA ]]; then
if [[ "$INPUT_USE_FORK_POINT" == "true" ]]; then
echo "::debug::Getting fork point..."
git fetch --no-tags -u --progress origin "${TARGET_BRANCH}":"${TARGET_BRANCH}"; exit_status=$?
PREVIOUS_SHA=$(git merge-base --fork-point "${TARGET_BRANCH}" "$(git name-rev --name-only "$CURRENT_SHA")"); exit_status=$?
git fetch --no-tags -u --progress origin "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$?
PREVIOUS_SHA=$(git merge-base --fork-point "${TARGET_BRANCH}" "$(git name-rev --name-only "$CURRENT_SHA")") && exit_status=$? || exit_status=$?
echo "::debug::Previous SHA: $PREVIOUS_SHA"
else
git fetch --no-tags -u --progress origin --depth=1 "${TARGET_BRANCH}":"${TARGET_BRANCH}"; exit_status=$?
PREVIOUS_SHA=$(git rev-list -n 1 "${TARGET_BRANCH}" 2>&1); exit_status=$?
git fetch --no-tags -u --progress origin --depth=1 "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$?
PREVIOUS_SHA=$(git rev-list -n 1 "${TARGET_BRANCH}" 2>&1) && exit_status=$? || exit_status=$?
echo "::debug::Previous SHA: $PREVIOUS_SHA"
fi
else
git fetch --no-tags -u --progress origin --depth=1 "$(git rev-parse --verify "$INPUT_BASE_SHA")"; exit_status=$?
git fetch --no-tags -u --progress origin --depth=1 "$(git rev-parse --verify "$INPUT_BASE_SHA")" && exit_status=$? || exit_status=$?
PREVIOUS_SHA=$INPUT_BASE_SHA
TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1); exit_status=$?
TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1) && exit_status=$? || exit_status=$?
echo "::debug::Previous SHA: $PREVIOUS_SHA"
echo "::debug::Target branch: $TARGET_BRANCH"
fi

echo "::debug::Verifying commit SHA..."
git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1; exit_status=$?
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=$?

if [[ $exit_status -ne 0 ]]; then
echo "::error::Unable to locate the previous sha: $PREVIOUS_SHA"
Expand All @@ -122,6 +126,7 @@ fi

if [[ -n "$PREVIOUS_SHA" && -n "$CURRENT_SHA" && "$PREVIOUS_SHA" == "$CURRENT_SHA" && "$INITIAL_COMMIT" == "false" ]]; then
echo "::error::Similar commit hashes detected: previous sha: $PREVIOUS_SHA is equivalent to the current sha: $CURRENT_SHA"
echo "::error::You seem to be missing 'fetch-depth: 0' or 'fetch-depth: 2'. See https://github.com/tj-actions/changed-files#usage"
exit 1
fi

Expand Down

0 comments on commit 60f4aab

Please sign in to comment.