2022-02-14 23:28:12 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
set -eu
|
|
|
|
|
2022-07-22 00:33:15 +00:00
|
|
|
INITIAL_COMMIT="false"
|
|
|
|
|
2022-02-14 23:28:12 +00:00
|
|
|
echo "::group::changed-files-diff-sha"
|
|
|
|
|
|
|
|
if [[ -n $INPUT_PATH ]]; then
|
|
|
|
REPO_DIR="$GITHUB_WORKSPACE/$INPUT_PATH"
|
2022-05-30 00:32:23 +00:00
|
|
|
|
2022-06-11 16:51:58 +00:00
|
|
|
echo "::debug::Resolving repository path: $REPO_DIR"
|
2022-02-14 23:28:12 +00:00
|
|
|
if [[ ! -d "$REPO_DIR" ]]; then
|
2022-06-24 04:17:41 +00:00
|
|
|
echo "::error::Invalid repository path: $REPO_DIR"
|
2022-02-14 23:28:12 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
cd "$REPO_DIR"
|
|
|
|
fi
|
|
|
|
|
2022-06-24 04:17:41 +00:00
|
|
|
git --version 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?
|
|
|
|
|
|
|
|
if [[ $exit_status -ne 0 ]]; then
|
|
|
|
echo "::error::git not installed"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-06-11 16:51:58 +00:00
|
|
|
echo "::debug::Getting HEAD SHA..."
|
2022-02-14 23:28:12 +00:00
|
|
|
|
|
|
|
if [[ -z $INPUT_SHA ]]; then
|
2022-07-18 20:34:41 +00:00
|
|
|
CURRENT_SHA=$(git rev-list -n 1 "HEAD" 2>&1) && exit_status=$? || exit_status=$?
|
2022-02-14 23:28:12 +00:00
|
|
|
else
|
|
|
|
CURRENT_SHA=$INPUT_SHA && exit_status=$? || exit_status=$?
|
|
|
|
fi
|
|
|
|
|
|
|
|
git rev-parse --quiet --verify "$CURRENT_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?
|
|
|
|
|
|
|
|
if [[ $exit_status -ne 0 ]]; then
|
2022-06-24 04:17:41 +00:00
|
|
|
echo "::error::Unable to locate the current sha: $CURRENT_SHA"
|
|
|
|
git --version
|
|
|
|
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 23:28:12 +00:00
|
|
|
exit 1
|
2022-07-18 20:34:41 +00:00
|
|
|
else
|
|
|
|
echo "::debug::Current SHA: $CURRENT_SHA"
|
2022-02-14 23:28:12 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z $GITHUB_BASE_REF ]]; then
|
|
|
|
TARGET_BRANCH=${GITHUB_REF/refs\/heads\//}
|
|
|
|
CURRENT_BRANCH=$TARGET_BRANCH
|
|
|
|
|
2022-07-18 20:34:41 +00:00
|
|
|
echo "::debug::GITHUB_BASE_REF unset using $TARGET_BRANCH..."
|
|
|
|
|
2022-02-14 23:28:12 +00:00
|
|
|
if [[ -z $INPUT_BASE_SHA ]]; then
|
2022-07-18 20:34:41 +00:00
|
|
|
if [[ $(git rev-list --count "HEAD") -gt 1 ]]; then
|
2022-07-22 00:33:15 +00:00
|
|
|
PREVIOUS_SHA=$(git rev-parse "@~" 2>&1) && exit_status=$? || exit_status=$?
|
2022-07-18 20:34:41 +00:00
|
|
|
echo "::debug::Previous SHA: $PREVIOUS_SHA"
|
2022-02-26 10:09:14 +00:00
|
|
|
else
|
2022-05-14 06:35:56 +00:00
|
|
|
PREVIOUS_SHA=$CURRENT_SHA && exit_status=$? || exit_status=$?
|
2022-07-22 00:33:15 +00:00
|
|
|
INITIAL_COMMIT="true"
|
2022-06-11 16:51:58 +00:00
|
|
|
echo "::debug::Initial commit detected"
|
2022-07-18 20:34:41 +00:00
|
|
|
echo "::debug::Previous SHA: $PREVIOUS_SHA"
|
2022-02-26 10:09:14 +00:00
|
|
|
fi
|
2022-02-14 23:28:12 +00:00
|
|
|
else
|
|
|
|
PREVIOUS_SHA=$INPUT_BASE_SHA && exit_status=$? || exit_status=$?
|
|
|
|
TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1) && exit_status=$? || exit_status=$?
|
2022-07-18 20:34:41 +00:00
|
|
|
echo "::debug::Previous SHA: $PREVIOUS_SHA"
|
|
|
|
echo "::debug::Target branch: $TARGET_BRANCH"
|
2022-02-14 23:28:12 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?
|
|
|
|
|
|
|
|
if [[ $exit_status -ne 0 ]]; then
|
2022-06-24 04:17:41 +00: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 23:28:12 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
TARGET_BRANCH=$GITHUB_BASE_REF
|
|
|
|
CURRENT_BRANCH=$GITHUB_HEAD_REF
|
|
|
|
|
2022-07-18 20:34:41 +00:00
|
|
|
echo "::debug::GITHUB_BASE_REF: $TARGET_BRANCH..."
|
|
|
|
|
2022-02-14 23:28:12 +00:00
|
|
|
if [[ -z $INPUT_BASE_SHA ]]; then
|
2022-02-17 07:35:49 +00:00
|
|
|
if [[ "$INPUT_USE_FORK_POINT" == "true" ]]; then
|
2022-06-11 16:51:58 +00:00
|
|
|
echo "::debug::Getting fork point..."
|
2022-05-15 01:19:45 +00:00
|
|
|
git fetch --no-tags -u --progress origin "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$?
|
2022-05-15 01:29:27 +00:00
|
|
|
PREVIOUS_SHA=$(git merge-base --fork-point "${TARGET_BRANCH}" "$(git name-rev --name-only "$CURRENT_SHA")") && exit_status=$? || exit_status=$?
|
2022-07-18 20:34:41 +00:00
|
|
|
echo "::debug::Previous SHA: $PREVIOUS_SHA"
|
2022-02-17 07:35:49 +00:00
|
|
|
else
|
2022-05-15 01:19:45 +00:00
|
|
|
git fetch --no-tags -u --progress origin --depth=1 "${TARGET_BRANCH}":"${TARGET_BRANCH}" && exit_status=$? || exit_status=$?
|
2022-07-18 20:34:41 +00:00
|
|
|
PREVIOUS_SHA=$(git rev-list -n 1 "${TARGET_BRANCH}" 2>&1) && exit_status=$? || exit_status=$?
|
|
|
|
echo "::debug::Previous SHA: $PREVIOUS_SHA"
|
2022-02-17 07:35:49 +00:00
|
|
|
fi
|
2022-02-14 23:28:12 +00:00
|
|
|
else
|
2022-05-15 01:19:45 +00:00
|
|
|
git fetch --no-tags -u --progress origin --depth=1 "$INPUT_BASE_SHA" && exit_status=$? || exit_status=$?
|
2022-02-14 23:28:12 +00:00
|
|
|
PREVIOUS_SHA=$INPUT_BASE_SHA
|
|
|
|
TARGET_BRANCH=$(git name-rev --name-only "$PREVIOUS_SHA" 2>&1) && exit_status=$? || exit_status=$?
|
2022-07-18 20:34:41 +00:00
|
|
|
echo "::debug::Previous SHA: $PREVIOUS_SHA"
|
|
|
|
echo "::debug::Target branch: $TARGET_BRANCH"
|
2022-02-14 23:28:12 +00:00
|
|
|
fi
|
|
|
|
|
2022-06-11 16:51:58 +00:00
|
|
|
echo "::debug::Verifying commit SHA..."
|
2022-02-14 23:28:12 +00:00
|
|
|
git rev-parse --quiet --verify "$PREVIOUS_SHA^{commit}" 1>/dev/null 2>&1 && exit_status=$? || exit_status=$?
|
|
|
|
|
|
|
|
if [[ $exit_status -ne 0 ]]; then
|
2022-06-24 04:17:41 +00: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 23:28:12 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2022-07-22 00:33:15 +00:00
|
|
|
if [[ -n "$PREVIOUS_SHA" && -n "$CURRENT_SHA" && "$PREVIOUS_SHA" == "$CURRENT_SHA" && "$INITIAL_COMMIT" == "false" ]]; then
|
2022-07-18 20:34:41 +00:00
|
|
|
echo "::error::Similar commit hashes detected: previous sha: $PREVIOUS_SHA is equivalent to 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"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2022-02-14 23:28:12 +00: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::"
|