git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git rev-parse --show-toplevel inside `.git` returns 0 and prints nothing

From
Anthony Sottile <asottile@umich.edu>
Date
Nov 19, 2019, 04:13 UTC
Message-ID
<CA+dzEBmekzDVdqy=4GDF+Wm8e-YTPEdbh0oVowZNQYO67vEhEg@mail.gmail.com>
In-Reply-To
<20191119033311.GA18613@sigill.intra.peff.net>
On Mon, Nov 18, 2019 at 7:33 PM Jeff King <peff@peff.net> wrote:
Show 75 quoted lines
>
> On Tue, Nov 19, 2019 at 11:52:54AM +0900, Junio C Hamano wrote:
>
> > If I were designing the feature today, with today's rest-of-git in
> > mind, I would say
> >
> >  - In a bare repository, exit with non-zero status after giving an
> >    error message "no working tree".
> >
> >  - In a repository that has a single associated working tree, show
> >    the path to the top-level of that working tree and exit with zero
> >    status.
>
> Do you mean to do this even in when the cwd is inside .git?
>
> I think that's confusing, because you don't actually have a working tree
> at all. E.g.:
>
>   $ git rev-parse --show-toplevel
>   /home/peff/tmp
>   $ git status -b --short
>   ## No commits yet on master
>
>   $ cd .git
>   $ git rev-parse --show-toplevel
>   $ git status -b --short
>   fatal: this operation must be run in a work tree
>
> So internal commands like status accept that we have no working tree in
> this situation. But "--show-toplevel" just prints nothing. I'd amend
> your second point to be "If we are in the working tree of a repository,
> show the path to the top-level of that working tree and exit with zero
> status".
>
> And then that leaves another case: we are not in the working tree of the
> repository. In which case I think it should be the same as the bare
> repository.
>
> And from that, your multi-working-tree case falls out naturally:
>
> > In a repository that has more than one working trees (which is one
> > of the things "todasy's rest-of-git" has that did not exist back
> > when --show-prefix/--show-toplevel etc. were invented), then what?
> > Would it make sense to show the primary working tree?  What if the
> > worktree(s) were made off of a bare repository, in which case nobody
> > is the primary?
>
> There may be multiple working trees, but we can only be in one of them
> at a time. So that's the one that we show.
>
> And the only real change here is that "--show-toplevel" prints an error
> and exits non-zero when we won't have a working tree. Something like:
>
> diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
> index 3857fd1b8a..81161f2dfb 100644
> --- a/builtin/rev-parse.c
> +++ b/builtin/rev-parse.c
> @@ -805,6 +805,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")) {
>
>
> I think the reason this hasn't come up until now is callers are expected
> to use require_work_tree() or "rev-parse --is-inside-work-tree" first.
>
> It would probably make sense for the rev-parse documentation to also
> clarify what "the top-level directory" is.
>
> -Peff

I realize I forgot to include the X to my Y :) -- this was a totally silly case that I got as a bug report: https://github.com/pre-commit/pre-commit/issues/1219

I *expected* an error case but didn't get one
Anthony
Previous: Jeff KingNext: Jeff King
Message 4 of 6 in “git rev-parse --show-toplevel inside `.git` returns 0 and prints nothing”
  1. Anthony SottileNov 18, 2019
  2. Junio C HamanoNov 19, 2019
  3. Jeff KingNov 19, 2019
  4. Anthony SottileNov 19, 2019
  5. Jeff KingNov 19, 2019
  6. Jeff KingNov 19, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.