completion: ensure that the repository path given on the command line exists
authorSZEDER Gábor <szeder.dev@gmail.com>
Fri, 3 Feb 2017 02:48:15 +0000 (03:48 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sat, 4 Feb 2017 06:18:40 +0000 (22:18 -0800)
The __gitdir() helper function prints the path to the git repository
to its stdout or stays silent and returns with error when it can't
find a repository or when the repository given via $GIT_DIR doesn't
exist.

This is not the case, however, when the path in $__git_dir, i.e. the
path to the repository specified on the command line via 'git
--git-dir=<path>', doesn't exist: __gitdir() still outputs it as if it
were a real existing repository, making some completion functions
believe that they operate on an existing repository.

Check that the path in $__git_dir exists and return with error without
printing anything to stdout if it doesn't.

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/completion/git-completion.bash
t/t9902-completion.sh
index a43322b04a8e923edc7c2567a118966b35f6caa1..92801329268232a2ae048b0dea615f67069607e9 100644 (file)
@@ -40,6 +40,7 @@ __gitdir ()
 {
        if [ -z "${1-}" ]; then
                if [ -n "${__git_dir-}" ]; then
+                       test -d "$__git_dir" || return 1
                        echo "$__git_dir"
                elif [ -n "${GIT_DIR-}" ]; then
                        test -d "${GIT_DIR-}" || return 1
index 7956cb9b16d5fcda6fe5eeecca7f4af929f16900..7667baabff0437cbe74db3411313949a34612ae5 100755 (executable)
@@ -211,6 +211,14 @@ test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
        test_cmp expected "$actual"
 '
 
+test_expect_success '__gitdir - non-existing path in $__git_dir' '
+       (
+               __git_dir="non-existing" &&
+               test_must_fail __gitdir >"$actual"
+       ) &&
+       test_must_be_empty "$actual"
+'
+
 test_expect_success '__gitdir - non-existing $GIT_DIR' '
        (
                GIT_DIR="$ROOT/non-existing" &&