From ab7a203db7c816f5c4356c05e12f3f23e0623390 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 5 Nov 2022 10:02:50 +0100 Subject: [PATCH 1/6] Check if git binary exists --- action.yml | 3 +++ entrypoint.sh | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/action.yml b/action.yml index 64797216..044e0e01 100644 --- a/action.yml +++ b/action.yml @@ -70,6 +70,9 @@ inputs: create_branch: description: Create new branch with the name of `branch`-input in local and remote repository, if it doesn't exist yet. default: false + internal_git_binary: + description: Internal use only. Path git binary that should be used. + default: git outputs: changes_detected: diff --git a/entrypoint.sh b/entrypoint.sh index fc8ba436..9d9f9bf0 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -7,6 +7,8 @@ if "$INPUT_DISABLE_GLOBBING"; then fi _main() { + _check_if_git_is_available + _switch_to_repository if _git_is_dirty || "$INPUT_SKIP_DIRTY_CHECK"; then @@ -42,6 +44,11 @@ _main() { fi } +_check_if_git_is_available() { + PATH_TO_GIT=$(which $INPUT_INTERNAL_GIT_BINARY); + + echo "::debug::Path to git binary ${PATH_TO_GIT}"; +} _switch_to_repository() { echo "INPUT_REPOSITORY value: $INPUT_REPOSITORY"; From c25d668b438d0dc14f72e08633606deda05f8c47 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 5 Nov 2022 10:02:59 +0100 Subject: [PATCH 2/6] Add Test --- tests/git-auto-commit.bats | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index f0b23245..f85f653a 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -37,6 +37,7 @@ setup() { export INPUT_SKIP_CHECKOUT=false export INPUT_DISABLE_GLOBBING=false export INPUT_CREATE_BRANCH=false + export INPUT_INTERNAL_GIT_BINARY=git # Set GitHub environment variables used by the GitHub Action temp_github_output_file=$(mktemp -t github_output_test.XXXXX) @@ -1041,3 +1042,13 @@ cat_github_output() { assert_line "::set-output name=changes_detected::false" refute_line -e "::set-output name=commit_hash::[0-9a-f]{40}$" } + +@test "It fails hard if git is not available" { + INPUT_INTERNAL_GIT_BINARY=binary-does-not-exist + + touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt + + run git_auto_commit + + assert_failure; +} From 38a37d1bd4e4df18543444d166bc6ee866ab0f95 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 5 Nov 2022 11:28:50 +0100 Subject: [PATCH 3/6] Use hash to check git binary --- entrypoint.sh | 10 +++++++--- tests/git-auto-commit.bats | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 9d9f9bf0..5661a916 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -45,9 +45,13 @@ _main() { } _check_if_git_is_available() { - PATH_TO_GIT=$(which $INPUT_INTERNAL_GIT_BINARY); - - echo "::debug::Path to git binary ${PATH_TO_GIT}"; + if hash -- "$INPUT_INTERNAL_GIT_BINARY" 2> /dev/null; then + echo "git found"; + echo "::debug::git binary found"; + else + echo "git not found. git-auto-commit requires git to be available." >&2 + exit 1 + fi } _switch_to_repository() { diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index f85f653a..bbec8493 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -1051,4 +1051,5 @@ cat_github_output() { run git_auto_commit assert_failure; + assert_line "git not found. git-auto-commit requires git to be available." } From c2499a189e79015f38cf17ff034a81bee75fbe4d Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 5 Nov 2022 11:34:02 +0100 Subject: [PATCH 4/6] Set proper error message --- entrypoint.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/entrypoint.sh b/entrypoint.sh index 5661a916..d0e31f55 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,6 +49,7 @@ _check_if_git_is_available() { echo "git found"; echo "::debug::git binary found"; else + echo "::error ::git not found. git-auto-commit requires git to be available." echo "git not found. git-auto-commit requires git to be available." >&2 exit 1 fi From 94ac00a90c0446f1c60b54210b908b8b3a753857 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 5 Nov 2022 11:39:38 +0100 Subject: [PATCH 5/6] Cleanup Error Message --- entrypoint.sh | 3 +-- tests/git-auto-commit.bats | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index d0e31f55..3085fb39 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -50,8 +50,7 @@ _check_if_git_is_available() { echo "::debug::git binary found"; else echo "::error ::git not found. git-auto-commit requires git to be available." - echo "git not found. git-auto-commit requires git to be available." >&2 - exit 1 + exit 1; fi } diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index bbec8493..79b9ce8f 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -1051,5 +1051,5 @@ cat_github_output() { run git_auto_commit assert_failure; - assert_line "git not found. git-auto-commit requires git to be available." + assert_line "::error ::git not found. git-auto-commit requires git to be available." } From 7280be6ce37ca900b933362abd6fb3cc0f6df002 Mon Sep 17 00:00:00 2001 From: Stefan Zweifel Date: Sat, 5 Nov 2022 11:49:43 +0100 Subject: [PATCH 6/6] Cleanup --- action.yml | 2 +- entrypoint.sh | 5 ++--- tests/git-auto-commit.bats | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index 044e0e01..4e745269 100644 --- a/action.yml +++ b/action.yml @@ -71,7 +71,7 @@ inputs: description: Create new branch with the name of `branch`-input in local and remote repository, if it doesn't exist yet. default: false internal_git_binary: - description: Internal use only. Path git binary that should be used. + description: Internal use only! Path to git binary used to check if git is available. (Don't change this!) default: git outputs: diff --git a/entrypoint.sh b/entrypoint.sh index 3085fb39..52449a64 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -46,10 +46,9 @@ _main() { _check_if_git_is_available() { if hash -- "$INPUT_INTERNAL_GIT_BINARY" 2> /dev/null; then - echo "git found"; - echo "::debug::git binary found"; + echo "::debug::git binary found."; else - echo "::error ::git not found. git-auto-commit requires git to be available." + echo "::error ::git-auto-commit could not find git binary. Please make sure git is available." exit 1; fi } diff --git a/tests/git-auto-commit.bats b/tests/git-auto-commit.bats index 79b9ce8f..92a3434e 100644 --- a/tests/git-auto-commit.bats +++ b/tests/git-auto-commit.bats @@ -1051,5 +1051,5 @@ cat_github_output() { run git_auto_commit assert_failure; - assert_line "::error ::git not found. git-auto-commit requires git to be available." + assert_line "::error ::git-auto-commit could not find git binary. Please make sure git is available." }