Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bug-changed-files-diff-sha-prints-thousands-of-lines-and-takes-two-minutes-to-run #857

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ inputs:
description: "Maximum depth of the branch history fetched. **NOTE**: This can be adjusted to resolve errors with insufficient history."
required: false
default: "5000"
deprecationMessage: "This input is deprecated and will be removed in a future release."
since_last_remote_commit:
description: "Use the last commit on the remote branch as the `base_sha`. Defaults to the last non merge commit on the target branch for pull request events and the previous remote commit of the current branch for push events."
required: true
Expand Down Expand Up @@ -176,6 +177,7 @@ runs:
GITHUB_EVENT_HEAD_REPO_FORK: ${{ github.event.pull_request.head.repo.fork }}
GITHUB_EVENT_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }}
GITHUB_EVENT_PULL_REQUEST_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
GITHUB_EVENT_PULL_REQUEST_COMMITS: ${{ github.event.pull_request.commits }}
GITHUB_EVENT_BEFORE: ${{ github.event.before }}
GITHUB_EVENT_FORCED: ${{ github.event.forced }}
GITHUB_REFNAME: ${{ github.ref_name }}
Expand Down
67 changes: 34 additions & 33 deletions diff-sha.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ set -euo pipefail

INITIAL_COMMIT="false"
GITHUB_OUTPUT=${GITHUB_OUTPUT:-""}
EXTRA_ARGS="--no-tags"
EXTRA_ARGS="--no-tags --prune --no-recurse-submodules"
PREVIOUS_SHA=""
CURRENT_SHA=""
DIFF="..."

if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then
EXTRA_ARGS=""
EXTRA_ARGS="--prune --no-recurse-submodules"
fi

if [[ "$GITHUB_EVENT_HEAD_REPO_FORK" == "true" ]]; then
Expand Down Expand Up @@ -69,7 +69,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then
CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$?
else
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH"
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH" 1>/dev/null 2>&1
CURRENT_SHA=$INPUT_SHA; exit_status=$?
fi
fi
Expand Down Expand Up @@ -132,7 +132,7 @@ if [[ -z $GITHUB_BASE_REF ]]; then
fi
else
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH"
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH" 1>/dev/null 2>&1
PREVIOUS_SHA=$INPUT_BASE_SHA
fi

Expand Down Expand Up @@ -160,11 +160,33 @@ else

if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "false" ]]; then
# shellcheck disable=SC2086
git fetch -u --progress $EXTRA_ARGS --depth="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH"
git branch --track "$TARGET_BRANCH" origin/"$TARGET_BRANCH" 2>/dev/null || true
git fetch -u --progress $EXTRA_ARGS --depth="$INPUT_FETCH_DEPTH" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" 1>/dev/null 2>&1
git branch --track "$TARGET_BRANCH" origin/"$TARGET_BRANCH" 1>/dev/null 2>&1 || true
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --depth=$(( GITHUB_EVENT_PULL_REQUEST_COMMITS + 1 )) origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null 2>&1

COMMON_ANCESTOR=$(git rev-list --first-parent --max-parents=0 --max-count=1 origin/"$CURRENT_BRANCH" 2>&1) && exit_status=$? || exit_status=$?

if [[ -z "$COMMON_ANCESTOR" ]]; then
echo "::error::Unable to locate a common ancestor for the current branch: $CURRENT_BRANCH"
exit 1
else
echo "::debug::Common ancestor: $COMMON_ANCESTOR"
fi

DATE=$(git log --date=iso8601 --format=%cd "${COMMON_ANCESTOR}")

if [[ -z "$DATE" ]]; then
echo "::error::Unable to locate a date for the common ancestor: $COMMON_ANCESTOR"
exit 1
else
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS --shallow-since="${DATE}" origin +refs/heads/"$TARGET_BRANCH":refs/remotes/origin/"$TARGET_BRANCH" 1>/dev/null 2>&1
echo "::debug::Date: $DATE"
fi
else
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH"
git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH" 1>/dev/null 2>&1
fi

echo "::debug::Getting HEAD SHA..."
Expand All @@ -178,9 +200,13 @@ else
fi
else
if [[ -z $INPUT_SHA ]]; then
CURRENT_SHA=$(git rev-list --no-merges -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$?
CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$?
else
CURRENT_SHA=$INPUT_SHA; exit_status=$?

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

Expand Down Expand Up @@ -215,31 +241,6 @@ else
PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$?
fi

if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "false" ]]; then
if [[ -f .git/shallow ]]; then
depth=$INPUT_FETCH_DEPTH
max_depth=$INPUT_MAX_FETCH_DEPTH

for ((i=0; i<max_depth; i+=depth)); do
echo "Fetching $i commits..."

# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --deepen="$i" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH"

if git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA$DIFF$CURRENT_SHA" 1>/dev/null 2>&1; then
break
fi
done

if ((i >= max_depth)); then
echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_BRANCH with: $PREVIOUS_SHA$DIFF$CURRENT_SHA"
exit 1
fi
else
echo "::debug::Not a shallow clone, skipping merge-base check."
fi
fi

echo "::debug::Previous SHA: $PREVIOUS_SHA"
else
PREVIOUS_SHA=$INPUT_BASE_SHA && exit_status=$? || exit_status=$?
Expand Down