@matheusbsbs / matheus-bagliano / commits / 2d92ab32fd
Fork 0

rev-parse: make --show-toplevel without a worktree an error

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>

Jeff King committed Nov 19, 2019 at 03:05 UTC 2d92ab32fd624349d308334befbf07adf9f179b9
3 files changed +14 -1
Documentation/git-rev-parse.txt
+2 -1
@@ -262,7 +262,8 @@ print a message to stderr and exit with nonzero status.
262 directory.
263
264 --show-toplevel::
265 - Show the absolute path of the top-level directory.
265 + Show the absolute path of the top-level directory of the working
266 + tree. If there is no working tree, report an error.
267
268 --show-superproject-working-tree::
269 Show the absolute path of the root of the superproject's
builtin/rev-parse.c
+2
@@ -803,6 +803,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
803 const char *work_tree = get_git_work_tree();
804 if (work_tree)
805 puts(work_tree);
806 + else
807 + die("this operation must be run in a work tree");
808 continue;
809 }
810 if (!strcmp(arg, "--show-superproject-working-tree")) {
t/t1500-rev-parse.sh
+10
@@ -146,6 +146,16 @@ test_expect_success 'rev-parse --show-object-format in repo' '
146 grep "unknown mode for --show-object-format: squeamish-ossifrage" err
147 '
148
149 +test_expect_success '--show-toplevel from subdir of working tree' '
150 + pwd >expect &&
151 + git -C sub/dir rev-parse --show-toplevel >actual &&
152 + test_cmp expect actual
153 +'
154 +
155 +test_expect_success '--show-toplevel from inside .git' '
156 + test_must_fail git -C .git rev-parse --show-toplevel
157 +'
158 +
159 test_expect_success 'showing the superproject correctly' '
160 git rev-parse --show-superproject-working-tree >out &&
161 test_must_be_empty out &&