3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-02-06 19:41:20 +00:00
changed-files/diff-sha.sh

282 lines
9.6 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
2022-11-04 15:21:49 -06:00
set -euo pipefail
INITIAL_COMMIT="false"
2022-10-14 13:01:08 -06:00
GITHUB_OUTPUT=${GITHUB_OUTPUT:-""}
2022-11-06 23:28:15 -07:00
EXTRA_ARGS="--no-tags"
2022-11-17 08:24:46 -07:00
PREVIOUS_SHA=""
CURRENT_SHA=""
2022-12-09 23:52:38 -07:00
DIFF="..."
2022-11-06 23:28:15 -07:00
if [[ "$GITHUB_REF" == "refs/tags/"* ]]; then
EXTRA_ARGS=""
fi
2022-12-09 23:52:38 -07:00
if [[ "$GITHUB_EVENT_HEAD_REPO_FORK" == "true" ]]; then
DIFF=".."
fi
echo "::group::changed-files-diff-sha"
if [[ -n $INPUT_PATH ]]; then
REPO_DIR="$GITHUB_WORKSPACE/$INPUT_PATH"
echo "::debug::Resolving repository path: $REPO_DIR"
if [[ ! -d "$REPO_DIR" ]]; then
echo "::error::Invalid repository path: $REPO_DIR"
exit 1
fi
cd "$REPO_DIR"
fi
function __version() {
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
}
2022-10-30 21:03:21 -06:00
echo "Verifying git version..."
2022-09-02 18:23:52 -06:00
GIT_VERSION=$(git --version | awk '{print $3}') && exit_status=$? || exit_status=$?
if [[ $exit_status -ne 0 ]]; then
echo "::error::git not installed"
exit 1
fi
if [[ $(__version "$GIT_VERSION") -lt $(__version "2.18.0") ]]; then
2022-10-30 21:03:21 -06:00
echo "::error::Invalid git version. Please upgrade ($GIT_VERSION) to >= (2.18.0)"
exit 1
else
echo "Valid git version found: ($GIT_VERSION)"
fi
2022-11-03 12:29:02 -06:00
if [[ -z $GITHUB_BASE_REF ]]; then
echo "Running on a push event..."
2022-12-05 14:29:20 -07:00
TARGET_BRANCH=$GITHUB_REFNAME
2022-12-05 14:34:38 -07:00
CURRENT_BRANCH=$TARGET_BRANCH
2022-11-03 12:29:02 -06:00
echo "::debug::Getting HEAD SHA..."
if [[ -n "$INPUT_UNTIL" ]]; then
echo "::debug::Getting HEAD SHA for '$INPUT_UNTIL'..."
CURRENT_SHA=$(git log -1 --format="%H" --date=local --until="$INPUT_UNTIL") && exit_status=$? || exit_status=$?
2022-10-15 18:21:02 -06:00
2022-11-03 12:29:02 -06:00
if [[ $exit_status -ne 0 ]]; then
echo "::error::Invalid until date: $INPUT_UNTIL"
exit 1
fi
2022-10-15 18:21:02 -06:00
else
2022-11-03 12:29:02 -06:00
if [[ -z $INPUT_SHA ]]; then
CURRENT_SHA=$(git rev-list -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$?
2022-11-03 12:29:02 -06:00
else
2022-12-05 00:12:48 -07:00
# shellcheck disable=SC2086
2022-12-10 00:21:14 -07:00
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH"
CURRENT_SHA=$INPUT_SHA; exit_status=$?
2022-11-03 12:29:02 -06:00
fi
2022-10-15 18:21:02 -06:00
fi
2022-11-03 12:29:02 -06:00
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=$?
2022-11-03 12:29:02 -06:00
if [[ $exit_status -ne 0 ]]; then
echo "::error::Unable to locate the current sha: $CURRENT_SHA"
echo "::error::Please verify that current sha is valid, and increase the fetch_depth to a number higher than $INPUT_FETCH_DEPTH."
exit 1
else
echo "::debug::Current SHA: $CURRENT_SHA"
fi
if [[ -z $INPUT_BASE_SHA ]]; then
if [[ -n "$INPUT_SINCE" ]]; then
2022-10-15 16:01:59 -06:00
echo "::debug::Getting base SHA for '$INPUT_SINCE'..."
2022-10-15 17:25:37 -06:00
PREVIOUS_SHA=$(git log --format="%H" --date=local --since="$INPUT_SINCE" | tail -1) && exit_status=$? || exit_status=$?
2022-10-15 16:26:42 -06:00
if [[ -z "$PREVIOUS_SHA" ]]; then
echo "::error::Unable to locate a previous commit for the specified date: $INPUT_SINCE"
exit 1
fi
else
if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then
PREVIOUS_SHA=""
if [[ "$GITHUB_EVENT_FORCED" == "false" || -z "$GITHUB_EVENT_FORCED" ]]; then
PREVIOUS_SHA=$GITHUB_EVENT_BEFORE
fi
else
PREVIOUS_SHA=$(git rev-list -n 1 "$TARGET_BRANCH" 2>&1) && exit_status=$? || exit_status=$?
if [[ -z "$PREVIOUS_SHA" ]]; then
if [[ "$GITHUB_EVENT_FORCED" == "false" || -z "$GITHUB_EVENT_FORCED" ]]; then
PREVIOUS_SHA=$GITHUB_EVENT_BEFORE
fi
fi
fi
if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then
PREVIOUS_SHA=$(git rev-parse "$(git branch -r --sort=-committerdate | head -1 | xargs)")
fi
2022-09-24 20:52:05 -06:00
if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then
2022-10-30 20:38:35 -06:00
if ! git rev-parse "$PREVIOUS_SHA^1" &>/dev/null; then
INITIAL_COMMIT="true"
PREVIOUS_SHA=$(git rev-parse "$CURRENT_SHA")
echo "::warning::Initial commit detected no previous commit found."
else
PREVIOUS_SHA=$(git rev-parse "$PREVIOUS_SHA^1")
fi
else
if [[ -z "$PREVIOUS_SHA" ]]; then
2022-11-23 21:46:23 -07:00
echo "::error::Unable to locate a previous commit."
exit 1
fi
2022-09-24 20:52:05 -06:00
fi
fi
else
2022-12-05 00:12:48 -07:00
# shellcheck disable=SC2086
2022-12-10 00:21:14 -07:00
git fetch $EXTRA_ARGS -u --progress --deepen="$INPUT_FETCH_DEPTH" origin "$CURRENT_BRANCH"
PREVIOUS_SHA=$INPUT_BASE_SHA
fi
echo "::debug::Target branch $TARGET_BRANCH..."
echo "::debug::Current branch $CURRENT_BRANCH..."
2022-09-02 18:40:51 -06:00
echo "::debug::Verifying the previous commit SHA: $PREVIOUS_SHA"
2022-09-02 18:23:52 -06:00
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"
2022-11-03 11:33:33 -06:00
echo "::error::Please verify that the previous sha commit is valid, and increase the fetch_depth to a number higher than $INPUT_FETCH_DEPTH."
exit 1
fi
else
2022-09-24 19:59:18 -06:00
echo "Running on a pull request event..."
TARGET_BRANCH=$GITHUB_BASE_REF
CURRENT_BRANCH=$GITHUB_HEAD_REF
2022-11-17 12:25:27 -07:00
if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then
TARGET_BRANCH=$CURRENT_BRANCH
fi
2022-11-03 12:29:02 -06:00
echo "Fetching remote refs..."
if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "false" ]]; then
2022-11-06 23:28:15 -07:00
# shellcheck disable=SC2086
2022-11-11 07:47:09 -07:00
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
else
2022-11-18 06:28:49 -07:00
# shellcheck disable=SC2086
git fetch $EXTRA_ARGS -u --progress --depth="$INPUT_FETCH_DEPTH" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH"
fi
2022-11-03 12:29:02 -06:00
echo "::debug::Getting HEAD SHA..."
if [[ -n "$INPUT_UNTIL" ]]; then
echo "::debug::Getting HEAD SHA for '$INPUT_UNTIL'..."
CURRENT_SHA=$(git log -1 --format="%H" --date=local --until="$INPUT_UNTIL") && exit_status=$? || exit_status=$?
if [[ $exit_status -ne 0 ]]; then
echo "::error::Invalid until date: $INPUT_UNTIL"
exit 1
fi
else
if [[ -z $INPUT_SHA ]]; then
CURRENT_SHA=$(git rev-list --no-merges -n 1 HEAD 2>&1) && exit_status=$? || exit_status=$?
else
CURRENT_SHA=$INPUT_SHA; exit_status=$?
fi
fi
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"
echo "::error::Please verify that current sha is valid, and increase the fetch_depth to a number higher than $INPUT_FETCH_DEPTH."
exit 1
else
echo "::debug::Current SHA: $CURRENT_SHA"
2022-12-10 00:05:52 -07:00
fi
2022-11-03 12:29:02 -06:00
2022-11-03 11:47:33 -06:00
if [[ -z $INPUT_BASE_SHA ]]; then
if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "true" ]]; then
PREVIOUS_SHA=$GITHUB_EVENT_BEFORE
if ! git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1; then
2022-11-15 10:38:56 -07:00
# shellcheck disable=SC2046
2022-11-17 12:59:15 -07:00
PREVIOUS_SHA=$(git rev-parse origin/"$CURRENT_BRANCH" 2>&1) && exit_status=$? || exit_status=$?
fi
else
2022-12-09 23:58:40 -07:00
PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA
2022-12-12 11:14:49 -07:00
if ! git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA$DIFF$CURRENT_SHA" 1>/dev/null 2>&1; then
PREVIOUS_SHA=$(git merge-base "$PREVIOUS_SHA" "$CURRENT_SHA" 2>&1) && exit_status=$? || exit_status=$?
fi
fi
2022-11-07 08:04:54 -07:00
if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then
PREVIOUS_SHA=$GITHUB_EVENT_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$?
fi
2022-11-07 08:04:54 -07:00
2022-12-04 23:54:27 -07:00
if [[ "$INPUT_SINCE_LAST_REMOTE_COMMIT" == "false" ]]; then
if [[ -f .git/shallow ]]; then
depth=$INPUT_FETCH_DEPTH
max_depth=$INPUT_MAX_FETCH_DEPTH
2022-12-10 00:30:40 -07:00
for ((i=0; i<max_depth; i+=depth)); do
2022-12-12 11:14:49 -07:00
echo "Fetching $i commits..."
2022-10-12 13:23:59 -06:00
2022-12-04 23:54:27 -07:00
# shellcheck disable=SC2086
2022-12-12 11:16:39 -07:00
git fetch $EXTRA_ARGS -u --progress --deepen="$i" origin +"$GITHUB_REF":refs/remotes/origin/"$CURRENT_BRANCH"
2022-11-15 09:31:41 -07:00
2022-12-10 00:30:40 -07:00
if git diff --name-only --ignore-submodules=all "$PREVIOUS_SHA$DIFF$CURRENT_SHA" 1>/dev/null 2>&1; then
break
2022-12-04 23:54:27 -07:00
fi
done
2022-12-10 00:30:40 -07:00
if ((i >= max_depth)); then
2022-12-12 00:18:33 -07:00
echo "::error::Unable to locate a common ancestor between $TARGET_BRANCH and $CURRENT_BRANCH with: $PREVIOUS_SHA$DIFF$CURRENT_SHA"
2022-12-10 00:30:40 -07:00
exit 1
fi
2022-12-04 23:54:27 -07:00
else
echo "::debug::Not a shallow clone, skipping merge-base check."
fi
2022-11-15 09:31:41 -07:00
fi
2022-12-04 23:54:27 -07:00
echo "::debug::Previous SHA: $PREVIOUS_SHA"
else
PREVIOUS_SHA=$INPUT_BASE_SHA && exit_status=$? || exit_status=$?
2022-11-15 09:31:41 -07:00
fi
2022-12-04 23:54:27 -07:00
echo "::debug::Target branch: $TARGET_BRANCH"
echo "::debug::Current branch: $CURRENT_BRANCH"
2022-09-02 18:40:51 -06:00
echo "::debug::Verifying the previous commit SHA: $PREVIOUS_SHA"
2022-09-02 18:23:52 -06:00
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"
2022-11-03 11:33:33 -06:00
echo "::error::Please verify that the previous sha is valid, and increase the fetch_depth to a number higher than $INPUT_FETCH_DEPTH."
exit 1
fi
fi
2022-11-03 06:23:50 -06:00
if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" && "$INITIAL_COMMIT" == "false" ]]; then
2022-11-03 06:50:01 -06:00
echo "::error::Similar commit hashes detected: previous sha: $PREVIOUS_SHA is equivalent to the current sha: $CURRENT_SHA."
2022-11-03 11:33:33 -06:00
echo "::error::Please verify that both commits are valid, and increase the fetch_depth to a number higher than $INPUT_FETCH_DEPTH."
exit 1
fi
if [[ -z "$GITHUB_OUTPUT" ]]; then
echo "::set-output name=target_branch::$TARGET_BRANCH"
echo "::set-output name=current_branch::$CURRENT_BRANCH"
echo "::set-output name=previous_sha::$PREVIOUS_SHA"
echo "::set-output name=current_sha::$CURRENT_SHA"
else
cat <<EOF >> "$GITHUB_OUTPUT"
2022-10-12 13:35:11 -05:00
target_branch=$TARGET_BRANCH
current_branch=$CURRENT_BRANCH
previous_sha=$PREVIOUS_SHA
current_sha=$CURRENT_SHA
EOF
fi
echo "::endgroup::"