diff --git a/helper/is-git-repo b/helper/is-git-repo index cf03a16..869c2e3 100755 --- a/helper/is-git-repo +++ b/helper/is-git-repo @@ -3,7 +3,9 @@ # is_git_repo() { - git rev-parse --show-toplevel > /dev/null 2>&1 + # --git-dir succeeds for both normal and bare repositories, whereas + # --show-toplevel fails inside a bare repository (it has no working tree). + git rev-parse --git-dir > /dev/null 2>&1 result=$? if test $result != 0; then >&2 echo 'Not a git repo!' diff --git a/tests/is-git-repo.bats b/tests/is-git-repo.bats new file mode 100644 index 0000000..118d487 --- /dev/null +++ b/tests/is-git-repo.bats @@ -0,0 +1,32 @@ +# shellcheck shell=bash + +source "$BATS_TEST_DIRNAME/test_util.sh" + +setup_file() { + test_util.setup_file +} + +setup() { + test_util.cd_test +} + +@test "is_git_repo succeeds inside a normal (non-bare) repository" { + test_util.git_init + + run bash "$BATS_TEST_DIRNAME/../helper/is-git-repo" + assert_success +} + +@test "is_git_repo succeeds inside a bare repository (see #1238)" { + git init --bare --initial-branch main repo.git + cd repo.git + + run bash "$BATS_TEST_DIRNAME/../helper/is-git-repo" + assert_success +} + +@test "is_git_repo fails outside of a repository" { + run bash "$BATS_TEST_DIRNAME/../helper/is-git-repo" + assert_failure + assert_output 'Not a git repo!' +}