From fe56d428c0b0babb4d23d63251cab341cb1e1bd8 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 25 Oct 2022 14:12:36 -0600 Subject: [PATCH 1/5] feat: add support for fetching more history --- .github/workflows/test.yml | 9 ++++----- action.yml | 8 ++++---- diff-sha.sh | 31 ++++++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index aa66a89ad08..c47470a73e9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -99,7 +99,7 @@ jobs: uses: actions/checkout@v3 with: fetch-depth: 0 - + - name: Run changed-files since 2022-08-19 id: changed-files-since uses: ./ @@ -113,13 +113,13 @@ jobs: exit 1 shell: bash - + - name: Show output run: | echo '${{ toJSON(steps.changed-files-since.outputs) }}' shell: bash - + - name: Run changed-files until 2022-08-20 id: changed-files-until uses: ./ @@ -216,11 +216,10 @@ jobs: - name: Checkout to branch uses: actions/checkout@v3 with: - fetch-depth: 1 + fetch-depth: 2 - name: Run changed-files with a single commit history id: changed-files - continue-on-error: true uses: ./ - name: Show output diff --git a/action.yml b/action.yml index 8878f52ab08..48e4693ddc0 100644 --- a/action.yml +++ b/action.yml @@ -77,10 +77,10 @@ inputs: description: "Output changed files in JSON format which can be used for matrix jobs" required: false default: "false" - target_branch_fetch_depth: - description: "Limit fetching commits from the target branch to a specified number. **NOTE**: This can be adjusted to resolve errors with insufficient history. See: [#668](https://github.com/tj-actions/changed-files/issues/668)." + max_fetch_depth: + description: "Max limit for fetching branch history. **NOTE**: This can be adjusted to resolve errors with insufficient history." required: false - default: "20" + default: "120" outputs: added_files: @@ -171,7 +171,7 @@ runs: INPUT_SINCE: ${{ inputs.since }} INPUT_UNTIL: ${{ inputs.until }} INPUT_PATH: ${{ inputs.path }} - INPUT_TARGET_BRANCH_FETCH_DEPTH: ${{ inputs.target_branch_fetch_depth }} + INPUT_MAX_FETCH_DEPTH: ${{ inputs.max_fetch_depth }} - name: Glob match uses: tj-actions/glob@v15 id: glob diff --git a/diff-sha.sh b/diff-sha.sh index 09aab6e1a0f..f743b5f6195 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -67,6 +67,26 @@ else echo "::debug::Current SHA: $CURRENT_SHA" fi +function deepenShallowCloneToFindCommit() { + local ref="$1" + local target_branch="$2" + local depth=20 + local max_depth=$INPUT_MAX_FETCH_DEPTH + + while ! git rev-parse --quiet --verify "$ref^{commit}" 1>/dev/null 2>&1; do # !0 = true = not found + echo "::debug::Unable to find commit '$ref' in shallow clone. Increasing depth to $((depth * 2))..." + + depth=$((depth * 2)) + + if [[ $depth -gt $max_depth ]]; then + echo "::error::Unable to find commit '$ref' in shallow clone. Maximum depth of $max_depth reached." + exit 1 + fi + + git fetch --no-tags -u --progress --deepen="$depth" origin "$target_branch":"$target_branch" + done +} + if [[ -z $GITHUB_BASE_REF ]]; then echo "Running on a push event..." TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} && exit_status=$? || exit_status=$? @@ -82,8 +102,6 @@ if [[ -z $GITHUB_BASE_REF ]]; then exit 1 fi else - git fetch --no-tags -u --progress origin --depth="$INPUT_TARGET_BRANCH_FETCH_DEPTH" "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$? - PREVIOUS_SHA="" if [[ "$GITHUB_EVENT_FORCED" == "false" ]]; then @@ -117,6 +135,9 @@ if [[ -z $GITHUB_BASE_REF ]]; then echo "::debug::Target branch $TARGET_BRANCH..." echo "::debug::Current branch $CURRENT_BRANCH..." + echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" + deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "$TARGET_BRANCH" + 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=$? @@ -130,9 +151,6 @@ else TARGET_BRANCH=$GITHUB_BASE_REF CURRENT_BRANCH=$GITHUB_HEAD_REF - git fetch --no-tags -u --progress origin --depth="$INPUT_TARGET_BRANCH_FETCH_DEPTH" "${TARGET_BRANCH}":"${TARGET_BRANCH}" && - exit_status=$? || exit_status=$? - if [[ -z $INPUT_BASE_SHA ]]; then PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? echo "::debug::Previous SHA: $PREVIOUS_SHA" @@ -143,6 +161,9 @@ else echo "::debug::Target branch: $TARGET_BRANCH" echo "::debug::Current branch: $CURRENT_BRANCH" + echo "::debug::Fetching previous commit SHA: $PREVIOUS_SHA" + deepenShallowCloneToFindCommit "$PREVIOUS_SHA" "$TARGET_BRANCH" + 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=$? From cfc8494f3b6e7c835f10072e045285501ec0040f Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 25 Oct 2022 14:49:22 -0600 Subject: [PATCH 2/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a02d57ffde..fef02466066 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ Support this project with a :star: | json | `boolean` | `false` | `false` | Output changed files in JSON format which can be used for [matrix jobs](https://github.com/tj-actions/changed-files/blob/main/.github/workflows/manual-matrix-test.yml). | | since | `string` | `false` | | Get changed files for commits whose timestamp is older than the given time. | | until | `string` | `false` | | Get changed files for commits whose timestamp is earlier than the given time. | -| target\_branch\_fetch\_depth | `string` | `false` | `20` | Limit fetching commits from the target branch to a specified number. **NOTE**: This can be adjusted to resolve errors with insufficient history. See: [#668](https://github.com/tj-actions/changed-files/issues/668). | +| max\_fetch\_depth | `string` | `false` | `120` | Max limit for fetching branch history. **NOTE**: This can be adjusted to resolve errors with insufficient history. | ## Examples From 941c21b6815ee8eeb3153d6459954ce3ed3df256 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 25 Oct 2022 14:57:58 -0600 Subject: [PATCH 3/5] Simplify code --- diff-sha.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diff-sha.sh b/diff-sha.sh index f743b5f6195..4a1ef443285 100644 --- a/diff-sha.sh +++ b/diff-sha.sh @@ -73,7 +73,7 @@ function deepenShallowCloneToFindCommit() { local depth=20 local max_depth=$INPUT_MAX_FETCH_DEPTH - while ! git rev-parse --quiet --verify "$ref^{commit}" 1>/dev/null 2>&1; do # !0 = true = not found + while ! git rev-parse --quiet --verify "$ref^{commit}" &>/dev/null; do # !0 = true = not found echo "::debug::Unable to find commit '$ref' in shallow clone. Increasing depth to $((depth * 2))..." depth=$((depth * 2)) From 4b455d3d83441730c691fb670a6f11e35784d641 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 25 Oct 2022 15:00:08 -0600 Subject: [PATCH 4/5] Updated test.yml --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c47470a73e9..5e9c219bc9d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -203,8 +203,8 @@ jobs: shell: bash - test-single-commit-history: - name: Test changed-files single commit history + test-limited-commit-history: + name: Test changed-files with fetch-depth set to 2 runs-on: ${{ matrix.platform }} strategy: fail-fast: false From 5c9ad7fce677f62cb05127fe8d2b7102b87d1a41 Mon Sep 17 00:00:00 2001 From: Tonye Jack Date: Tue, 25 Oct 2022 15:00:32 -0600 Subject: [PATCH 5/5] Updated test.yml --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5e9c219bc9d..90a9c13c43f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -204,7 +204,7 @@ jobs: bash test-limited-commit-history: - name: Test changed-files with fetch-depth set to 2 + name: Test changed-files with limited commit history runs-on: ${{ matrix.platform }} strategy: fail-fast: false