]> git.ipfire.org Git - thirdparty/git.git/commitdiff
rev-parse: make --show-toplevel without a worktree an error
authorJeff King <peff@peff.net>
Tue, 19 Nov 2019 08:05:43 +0000 (03:05 -0500)
committerJunio C Hamano <gitster@pobox.com>
Wed, 20 Nov 2019 01:19:58 +0000 (10:19 +0900)
Ever since it was introduced in 7cceca5ccc (Add 'git rev-parse
--show-toplevel' option., 2010-01-12), the --show-toplevel option has
treated a missing working tree as a quiet success: it neither prints a
toplevel path, but nor does it report any kind of error.

While a caller could distinguish this case by looking for an empty
response, the behavior is rather confusing. We're better off complaining
that there is no working tree, as other internal commands would do in
similar cases (e.g., "git status" or any builtin with NEED_WORK_TREE set
would just die()). So let's do the same here.

While we're at it, let's clarify the documentation and add some tests,
both for the new behavior and for the more mundane case (which was not
covered).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/git-rev-parse.txt
builtin/rev-parse.c
t/t1500-rev-parse.sh

index 9985477efe9c3e7defb639fa30517d0ea93eb7b6..19b12b6d43ce5bfeb2dba36e30640ddaf0aca664 100644 (file)
@@ -262,7 +262,8 @@ print a message to stderr and exit with nonzero status.
        directory.
 
 --show-toplevel::
-       Show the absolute path of the top-level directory.
+       Show the absolute path of the top-level directory of the working
+       tree. If there is no working tree, report an error.
 
 --show-superproject-working-tree::
        Show the absolute path of the root of the superproject's
index 85ce2095bf21cb386dd9577823139da01f0d0967..7a00da820355b61c548449c55381577a2232a0e8 100644 (file)
@@ -803,6 +803,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                const char *work_tree = get_git_work_tree();
                                if (work_tree)
                                        puts(work_tree);
+                               else
+                                       die("this operation must be run in a work tree");
                                continue;
                        }
                        if (!strcmp(arg, "--show-superproject-working-tree")) {
index 0177fd815c03d9f3dafeb99e3e915bd89ba714c2..603019b54167eb1b2050349dcca45af74a56031b 100755 (executable)
@@ -146,6 +146,16 @@ test_expect_success 'rev-parse --show-object-format in repo' '
        grep "unknown mode for --show-object-format: squeamish-ossifrage" err
 '
 
+test_expect_success '--show-toplevel from subdir of working tree' '
+       pwd >expect &&
+       git -C sub/dir rev-parse --show-toplevel >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success '--show-toplevel from inside .git' '
+       test_must_fail git -C .git rev-parse --show-toplevel
+'
+
 test_expect_success 'showing the superproject correctly' '
        git rev-parse --show-superproject-working-tree >out &&
        test_must_be_empty out &&