2022-02-14 18:28:12 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2022-07-21 20:33:15 -04:00
|
|
|
INITIAL_COMMIT="false"
|
|
|
|
|
2022-02-14 18:28:12 -05:00
|
|
|
echo "::group::changed-files-diff-sha"
|
|
|
|
|
|
|
|
if [[ -n $INPUT_PATH ]]; then
|
|
|
|
REPO_DIR="$GITHUB_WORKSPACE/$INPUT_PATH"
|
2022-05-29 20:32:23 -04:00
|
|
|
|
2022-06-11 12:51:58 -04:00
|
|
|
echo "::debug::Resolving repository path: $REPO_DIR"
|
2022-02-14 18:28:12 -05:00
|
|
|
if [[ ! -d "$REPO_DIR" ]]; then
|
2022-06-24 00:17:41 -04:00
|
|
|
echo "::error::Invalid repository path: $REPO_DIR"
|
2022-02-14 18:28:12 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
cd "$REPO_DIR"
|
|
|
|
fi
|
|
|
|
|
2022-08-13 15:50:50 -06:00
|
|
|
echo "Verifying git version..."
|
|
|
|
|
2022-08-21 10:44:28 -06:00
|
|
|
function __version() {
|
|
|
|
echo "$@" | awk -F. '{ printf("%d%03d%03d%03d\n", $1,$2,$3,$4); }';
|
2022-08-13 15:50:50 -06:00
|
|
|
}
|
|
|
|
|
2022-09-02 18:23:52 -06:00
|
|
|
GIT_VERSION=$(git --version | awk '{print $3}') && exit_status=$? || exit_status=$?
|
2022-06-24 00:17:41 -04:00
|
|
|
|
|
|
|
if [[ $exit_status -ne 0 ]]; then
|
|
|
|
echo "::error::git not installed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-08-13 15:50:50 -06:00
|
|
|
if [[ $(__version "$GIT_VERSION") -lt $(__version "2.18.0") ]]; then
|
|
|
|
echo "::error::Invalid git version. Please upgrade git ($GIT_VERSION) to >= (2.18.0)"
|
|
|
|
exit 1
|
|
|
|
else
|
|
|
|
echo "Valid git version found: ($GIT_VERSION)"
|
|
|
|
fi
|
|
|
|
|
2022-06-11 12:51:58 -04:00
|
|
|
echo "::debug::Getting HEAD SHA..."
|
2022-02-14 18:28:12 -05:00
|
|
|
|
|
|
|
if [[ -z $INPUT_SHA ]]; then
|
2022-09-02 18:23:52 -06:00
|
|
|
CURRENT_SHA=$(git rev-list -n 1 "HEAD" 2>&1) && exit_status=$? || exit_status=$?
|
2022-02-14 18:28:12 -05:00
|
|
|
else
|
2022-08-14 20:38:10 -06:00
|
|
|
CURRENT_SHA=$INPUT_SHA; exit_status=$?
|
2022-02-14 18:28:12 -05:00
|
|
|
fi
|
|
|
|
|
2022-09-02 18:40:51 -06:00
|
|
|
echo "::debug::Verifying the current commit SHA: $CURRENT_SHA"
|
2022-09-02 16:41:25 -06:00
|
|
|
git rev-parse --quiet --verify "$CURRENT_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?
|
2022-02-14 18:28:12 -05:00
|
|
|
|
|
|
|
if [[ $exit_status -ne 0 ]]; then
|
2022-06-24 00:17:41 -04:00
|
|
|
echo "::error::Unable to locate the current sha: $CURRENT_SHA"
|
|
|
|
echo "::error::You seem to be missing 'fetch-depth: 0' or 'fetch-depth: 2'. See https://github.com/tj-actions/changed-files#usage"
|
2022-02-14 18:28:12 -05:00
|
|
|
exit 1
|
2022-07-18 16:34:41 -04:00
|
|
|
else
|
|
|
|
echo "::debug::Current SHA: $CURRENT_SHA"
|
2022-02-14 18:28:12 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z $GITHUB_BASE_REF ]]; then
|
2022-09-24 19:59:18 -06:00
|
|
|
echo "Running on a push event..."
|
2022-09-24 19:08:58 -06:00
|
|
|
TARGET_BRANCH=${GITHUB_REF/refs\/heads\//} && exit_status=$? || exit_status=$?
|
|
|
|
CURRENT_BRANCH=$TARGET_BRANCH && exit_status=$? || exit_status=$?
|
2022-07-18 16:34:41 -04:00
|
|
|
|
2022-02-14 18:28:12 -05:00
|
|
|
if [[ -z $INPUT_BASE_SHA ]]; then
|
2022-09-24 19:08:58 -06:00
|
|
|
PREVIOUS_SHA=$GITHUB_EVENT_BEFORE
|
2022-09-24 19:33:05 -06:00
|
|
|
|
|
|
|
if [[ -z "$PREVIOUS_SHA" || "$PREVIOUS_SHA" == "0000000000000000000000000000000000000000" ]]; then
|
2022-09-24 19:59:18 -06:00
|
|
|
echo "::debug::First commit detected"
|
2022-09-24 20:21:08 -06:00
|
|
|
PREVIOUS_SHA=$(git rev-parse "$(git branch -r --sort=-committerdate | head -1 | xargs)")
|
2022-09-24 19:33:05 -06:00
|
|
|
fi
|
|
|
|
|
2022-09-24 19:08:58 -06:00
|
|
|
if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then
|
2022-09-24 20:52:05 -06:00
|
|
|
PREVIOUS_SHA=$(git rev-parse "$CURRENT_SHA^1")
|
|
|
|
|
|
|
|
if [[ "$PREVIOUS_SHA" == "$CURRENT_SHA" ]]; then
|
|
|
|
INITIAL_COMMIT="true"
|
|
|
|
echo "::debug::Initial commit detected"
|
|
|
|
fi
|
2022-02-26 05:09:14 -05:00
|
|
|
fi
|
2022-02-14 18:28:12 -05:00
|
|
|
else
|
2022-09-24 19:08:58 -06:00
|
|
|
PREVIOUS_SHA=$INPUT_BASE_SHA
|
2022-09-02 18:23:52 -06:00
|
|
|
TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1) && exit_status=$? || exit_status=$?
|
2022-09-24 19:08:58 -06:00
|
|
|
CURRENT_BRANCH=$TARGET_BRANCH
|
2022-02-14 18:28:12 -05:00
|
|
|
fi
|
|
|
|
|
2022-09-24 19:08:58 -06: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=$?
|
2022-02-14 18:28:12 -05:00
|
|
|
|
|
|
|
if [[ $exit_status -ne 0 ]]; then
|
2022-06-24 00:17:41 -04:00
|
|
|
echo "::error::Unable to locate the previous sha: $PREVIOUS_SHA"
|
|
|
|
echo "::error::You seem to be missing 'fetch-depth: 0' or 'fetch-depth: 2'. See https://github.com/tj-actions/changed-files#usage"
|
2022-02-14 18:28:12 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2022-09-24 19:59:18 -06:00
|
|
|
echo "Running on a pull request event..."
|
2022-02-14 18:28:12 -05:00
|
|
|
TARGET_BRANCH=$GITHUB_BASE_REF
|
|
|
|
CURRENT_BRANCH=$GITHUB_HEAD_REF
|
|
|
|
|
2022-07-18 16:34:41 -04:00
|
|
|
echo "::debug::GITHUB_BASE_REF: $TARGET_BRANCH..."
|
|
|
|
|
2022-02-14 18:28:12 -05:00
|
|
|
if [[ -z $INPUT_BASE_SHA ]]; then
|
2022-09-23 22:56:09 -06:00
|
|
|
PREVIOUS_SHA=$GITHUB_PULL_REQUEST_BASE_SHA && exit_status=$? || exit_status=$?
|
|
|
|
echo "::debug::Previous SHA: $PREVIOUS_SHA"
|
2022-02-14 18:28:12 -05:00
|
|
|
else
|
|
|
|
PREVIOUS_SHA=$INPUT_BASE_SHA
|
2022-09-02 18:23:52 -06:00
|
|
|
TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1) && exit_status=$? || exit_status=$?
|
2022-07-18 16:34:41 -04:00
|
|
|
echo "::debug::Previous SHA: $PREVIOUS_SHA"
|
|
|
|
echo "::debug::Target branch: $TARGET_BRANCH"
|
2022-02-14 18:28:12 -05:00
|
|
|
fi
|
|
|
|
|
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=$?
|
2022-02-14 18:28:12 -05:00
|
|
|
|
|
|
|
if [[ $exit_status -ne 0 ]]; then
|
2022-06-24 00:17:41 -04:00
|
|
|
echo "::error::Unable to locate the previous sha: $PREVIOUS_SHA"
|
|
|
|
echo "::error::You seem to be missing 'fetch-depth: 0' or 'fetch-depth: 2'. See https://github.com/tj-actions/changed-files#usage"
|
2022-02-14 18:28:12 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-07-21 20:33:15 -04:00
|
|
|
if [[ -n "$PREVIOUS_SHA" && -n "$CURRENT_SHA" && "$PREVIOUS_SHA" == "$CURRENT_SHA" && "$INITIAL_COMMIT" == "false" ]]; then
|
2022-07-18 16:34:41 -04:00
|
|
|
echo "::error::Similar commit hashes detected: previous sha: $PREVIOUS_SHA is equivalent to the current sha: $CURRENT_SHA"
|
2022-09-02 15:08:19 -06:00
|
|
|
echo "::error::You seem to be missing 'fetch-depth: 0' or 'fetch-depth: 2'. See https://github.com/tj-actions/changed-files#usage"
|
2022-07-18 16:34:41 -04:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-02-14 18:28:12 -05:00
|
|
|
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"
|
|
|
|
|
|
|
|
echo "::endgroup::"
|