From d3b961906e511b4b324b1f0f3b3350e092ed4b53 Mon Sep 17 00:00:00 2001 From: Constantin Comendant Date: Thu, 11 Apr 2024 16:01:52 +0200 Subject: [PATCH 1/3] Fail (and log message) if attempting to execute git commands in a directory that is not a git-repo. --- entrypoint.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 6fe017a0..7cf5098f 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -82,8 +82,15 @@ _git_is_dirty() { echo "INPUT_FILE_PATTERN: ${INPUT_FILE_PATTERN}"; read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN"; + # capture stderr + gitStatusMessage="$((git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}} >/dev/null ) 2>&1)" # shellcheck disable=SC2086 - [ -n "$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})" ] + gitStatus="$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})" + if [ $? -ne 0 ]; then + _log "error" "git-status failed with:<$gitStatusMessage>"; + exit 1 + fi + [ -n "$gitStatus" ] } _switch_to_branch() { From 769fc3bcd9bd9178a794ec9d8a6c612b5cdb90fb Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Thu, 11 Apr 2024 20:55:20 +0200 Subject: [PATCH 2/3] Add Test --- tests/git-auto-commit.bats | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index daf5bc0b..d08597cc 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -8,6 +8,7 @@ setup() { export FAKE_LOCAL_REPOSITORY="${BATS_TEST_DIRNAME}/tests_local_repository" export FAKE_REMOTE="${BATS_TEST_DIRNAME}/tests_remote_repository" export FAKE_TEMP_LOCAL_REPOSITORY="${BATS_TEST_DIRNAME}/tests_clone_of_remote_repository" + export FAKE_FOLDER_WITHOUT_GIT_REPO="/tmp/tests_folder_without_git_repo" # While it is likely the GitHub hosted runners will use master as the default branch, # locally anyone may change that. So for tests lets grab whatever is currently set @@ -58,6 +59,7 @@ teardown() { rm -rf "${FAKE_LOCAL_REPOSITORY}" rm -rf "${FAKE_REMOTE}" rm -rf "${FAKE_TEMP_LOCAL_REPOSITORY}" + rm -rf "${INPUT_REPOSITORY}" if [ -z ${GITHUB_OUTPUT+x} ]; then echo "GITHUB_OUTPUT is not set" @@ -1112,3 +1114,14 @@ END run git log -n 1 assert_output --partial $COMMIT_MESSAGE } + +@test "It exits with error message if entrypoint.sh is being run not in a git repository" { + INPUT_REPOSITORY="${FAKE_FOLDER_WITHOUT_GIT_REPO}" + + mkdir "${INPUT_REPOSITORY}" + + run git_auto_commit + + assert_failure; + assert_line "::error::git-status failed with:" +} From 9eaaf8e1547b62ad3b1fbdd049809a9ba022b851 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Thu, 11 Apr 2024 20:57:05 +0200 Subject: [PATCH 3/3] Code Formatting --- entrypoint.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 7cf5098f..bff98e0e 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -83,12 +83,12 @@ _git_is_dirty() { read -r -a INPUT_FILE_PATTERN_EXPANDED <<< "$INPUT_FILE_PATTERN"; # capture stderr - gitStatusMessage="$((git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}} >/dev/null ) 2>&1)" + gitStatusMessage="$((git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}} >/dev/null ) 2>&1)"; # shellcheck disable=SC2086 - gitStatus="$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})" + gitStatus="$(git status -s $INPUT_STATUS_OPTIONS -- ${INPUT_FILE_PATTERN_EXPANDED:+${INPUT_FILE_PATTERN_EXPANDED[@]}})"; if [ $? -ne 0 ]; then _log "error" "git-status failed with:<$gitStatusMessage>"; - exit 1 + exit 1; fi [ -n "$gitStatus" ] }