]> git.ipfire.org Git - thirdparty/git.git/commitdiff
prefix_path: show gitdir if worktree unavailable
authorEmily Shaffer <emilyshaffer@google.com>
Tue, 3 Mar 2020 04:05:06 +0000 (20:05 -0800)
committerJunio C Hamano <gitster@pobox.com>
Sun, 15 Mar 2020 16:35:46 +0000 (09:35 -0700)
If there is no worktree at present, we can still hint the user about
Git's current directory by showing them the absolute path to the Git
directory. Even though the Git directory doesn't make it as easy to
locate the worktree in question, it can still help a user figure out
what's going on while developing a script.

This fixes a segmentation fault introduced in e0020b2f
("prefix_path: show gitdir when arg is outside repo", 2020-02-14).

Signed-off-by: Emily Shaffer <emilyshaffer@google.com>
[jc: added minimum tests, with help from Szeder Gábor]
Signed-off-by: Junio C Hamano <gitster@pobox.com>
pathspec.c
setup.c
t/t6136-pathspec-in-bare.sh [new file with mode: 0755]

index 166d255642db4c9b854dbd19018cbc854d1c4ca1..8243e06eab48373d12729959fde8fe666eb312b2 100644 (file)
@@ -438,9 +438,13 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
        } else {
                match = prefix_path_gently(prefix, prefixlen,
                                           &prefixlen, copyfrom);
-               if (!match)
+               if (!match) {
+                       const char *hint_path = get_git_work_tree();
+                       if (!hint_path)
+                               hint_path = get_git_dir();
                        die(_("%s: '%s' is outside repository at '%s'"), elt,
-                           copyfrom, absolute_path(get_git_work_tree()));
+                           copyfrom, absolute_path(hint_path));
+               }
        }
 
        item->match = match;
diff --git a/setup.c b/setup.c
index 17814a080b8ed9660b1018f210a5d62770fcde89..f4897287f768b0ab9d247b757a04ec63e463b4e0 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -120,9 +120,13 @@ char *prefix_path_gently(const char *prefix, int len,
 char *prefix_path(const char *prefix, int len, const char *path)
 {
        char *r = prefix_path_gently(prefix, len, NULL, path);
-       if (!r)
+       if (!r) {
+               const char *hint_path = get_git_work_tree();
+               if (!hint_path)
+                       hint_path = get_git_dir();
                die(_("'%s' is outside repository at '%s'"), path,
-                   absolute_path(get_git_work_tree()));
+                   absolute_path(hint_path));
+       }
        return r;
 }
 
diff --git a/t/t6136-pathspec-in-bare.sh b/t/t6136-pathspec-in-bare.sh
new file mode 100755 (executable)
index 0000000..b117251
--- /dev/null
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+test_description='diagnosing out-of-scope pathspec'
+
+. ./test-lib.sh
+
+test_expect_success 'setup a bare and non-bare repository' '
+       test_commit file1 &&
+       git clone --bare . bare
+'
+
+test_expect_success 'log and ls-files in a bare repository' '
+       (
+               cd bare &&
+               test_must_fail git log -- .. >out 2>err &&
+               test_must_be_empty out &&
+               test_i18ngrep "outside repository" err &&
+
+               test_must_fail git ls-files -- .. >out 2>err &&
+               test_must_be_empty out &&
+               test_i18ngrep "outside repository" err
+       )
+'
+
+test_expect_success 'log and ls-files in .git directory' '
+       (
+               cd .git &&
+               test_must_fail git log -- .. >out 2>err &&
+               test_must_be_empty out &&
+               test_i18ngrep "outside repository" err &&
+
+               test_must_fail git ls-files -- .. >out 2>err &&
+               test_must_be_empty out &&
+               test_i18ngrep "outside repository" err
+       )
+'
+
+test_done