Skip to content

Commit

Permalink
feat: add support for fetching more history
Browse files Browse the repository at this point in the history
  • Loading branch information
jackton1 committed Oct 25, 2022
1 parent f10717d commit fe56d42
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/test.yml
Expand Up @@ -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: ./
Expand All @@ -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: ./
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions action.yml
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
31 changes: 26 additions & 5 deletions diff-sha.sh
Expand Up @@ -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=$?
Expand All @@ -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
Expand Down Expand Up @@ -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=$?

Expand All @@ -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"
Expand All @@ -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=$?

Expand Down

0 comments on commit fe56d42

Please sign in to comment.