3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-02-22 11:17:38 +00:00

Merge pull request #709 from tj-actions/feat/add-support-for-fetching-more-history

This commit is contained in:
Tonye Jack 2022-10-25 15:39:04 -06:00 committed by GitHub
commit af3b754ee2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 17 deletions

View file

@ -203,8 +203,8 @@ jobs:
shell: shell:
bash bash
test-single-commit-history: test-limited-commit-history:
name: Test changed-files single commit history name: Test changed-files with limited commit history
runs-on: ${{ matrix.platform }} runs-on: ${{ matrix.platform }}
strategy: strategy:
fail-fast: false fail-fast: false
@ -216,11 +216,10 @@ jobs:
- name: Checkout to branch - name: Checkout to branch
uses: actions/checkout@v3 uses: actions/checkout@v3
with: with:
fetch-depth: 1 fetch-depth: 2
- name: Run changed-files with a single commit history - name: Run changed-files with a single commit history
id: changed-files id: changed-files
continue-on-error: true
uses: ./ uses: ./
- name: Show output - name: Show output

View file

@ -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). | | 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. | | 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. | | 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 ## Examples

View file

@ -77,10 +77,10 @@ inputs:
description: "Output changed files in JSON format which can be used for matrix jobs" description: "Output changed files in JSON format which can be used for matrix jobs"
required: false required: false
default: "false" default: "false"
target_branch_fetch_depth: max_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)." description: "Max limit for fetching branch history. **NOTE**: This can be adjusted to resolve errors with insufficient history."
required: false required: false
default: "20" default: "120"
outputs: outputs:
added_files: added_files:
@ -171,7 +171,7 @@ runs:
INPUT_SINCE: ${{ inputs.since }} INPUT_SINCE: ${{ inputs.since }}
INPUT_UNTIL: ${{ inputs.until }} INPUT_UNTIL: ${{ inputs.until }}
INPUT_PATH: ${{ inputs.path }} 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 - name: Glob match
uses: tj-actions/glob@v15 uses: tj-actions/glob@v15
id: glob id: glob

View file

@ -67,6 +67,26 @@ else
echo "::debug::Current SHA: $CURRENT_SHA" echo "::debug::Current SHA: $CURRENT_SHA"
fi 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}" &>/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))
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 if [[ -z $GITHUB_BASE_REF ]]; then
echo "Running on a push event..." echo "Running on a push event..."
TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} && exit_status=$? || exit_status=$? TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} && exit_status=$? || exit_status=$?
@ -82,8 +102,6 @@ if [[ -z $GITHUB_BASE_REF ]]; then
exit 1 exit 1
fi fi
else else
git fetch --no-tags -u --progress origin --depth="$INPUT_TARGET_BRANCH_FETCH_DEPTH" "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$?
PREVIOUS_SHA="" PREVIOUS_SHA=""
if [[ "$GITHUB_EVENT_FORCED" == "false" ]]; then if [[ "$GITHUB_EVENT_FORCED" == "false" ]]; then
@ -117,6 +135,9 @@ if [[ -z $GITHUB_BASE_REF ]]; then
echo "::debug::Target branch $TARGET_BRANCH..." echo "::debug::Target branch $TARGET_BRANCH..."
echo "::debug::Current branch $CURRENT_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" 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=$? 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 TARGET_BRANCH=$GITHUB_BASE_REF
CURRENT_BRANCH=$GITHUB_HEAD_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 if [[ -z $INPUT_BASE_SHA ]]; then
PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$? PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$?
echo "::debug::Previous SHA: $PREVIOUS_SHA" echo "::debug::Previous SHA: $PREVIOUS_SHA"
@ -143,6 +161,9 @@ else
echo "::debug::Target branch: $TARGET_BRANCH" echo "::debug::Target branch: $TARGET_BRANCH"
echo "::debug::Current branch: $CURRENT_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" 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=$? git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?