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

Re: Unexpected behavior with the rev-parse operation

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Apr 28, 2024, 18:08 UTC
Message-ID
<CAPig+cQ39Z+WjThqkxCKgOUfkZyB6PG-6RhHBYhinp-ZY4dxKA@mail.gmail.com>
In-Reply-To
<CANHPH1-6mbWdt_FLsrDEB_XVb4r3yTYMLLPUyDrL6tmS7jZgqQ@mail.gmail.com>
On Mon, Apr 15, 2024 at 9:58 AM Toru Okugawa <castor.4bit@gmail.com> wrote:
Show 13 quoted lines
> I have encountered some unexpected behavior with the rev-parse operation.
> ---
> $ ls -a
> .  ..
> $ git rev-parse --is-inside-work-tree
> fatal: not a git repository (or any of the parent directories): .git
>
> What did you expect to happen? (Expected behavior)
>
> If the current working directory is outside the work tree, the
> documentation says that `git rev-parse --is-inside-work-tree` will
> output false.
> https://git-scm.com/docs/git-rev-parse#Documentation/git-rev-parse.txt---is-inside-work-tree

I think this is working as intended, but the documentation is lacking or misleading. With very few exceptions[1], `git rev-parse` expects to be run either within a Git working tree or within a Git repository (i.e. the .git/ directory or a bare repository). Options such as --is-inside-work-tree, --is-bare-repository, etc. are meant to report upon aspects of the worktree or repository in which the command is invoked.

Looking at the source code for `git rev-parse`, it does appear that it has explicit support for the case of checking whether or not the current directory is a Git worktree or repository. The way to do that is to run `git rev-parse` without any arguments. However, even in that case, it will print the "fatal: not a git repository" error. This means it is your responsibility, as a script writer, to suppress or capture the error message (whichever is appropriate for your case). For instance:

    if test git rev-parse >/dev/null 2>&1
    then
        echo "in a Git directory or repository"
    else
        echo "not in a Git directory or repository"
    fi
[1]: --sh-quote, --parse-opt, --local-env-vars, --resolve-git-dir
Previous: Toru OkugawaNext: Junio C Hamano
Message 2 of 6 in “Unexpected behavior with the rev-parse operation”
  1. Toru OkugawaApr 15, 2024
  2. Eric SunshineApr 28, 2024
  3. Re* Unexpected behavior with the rev-parse operationJunio C Hamano, Apr 28, 2024
  4. Toru OkugawaApr 30, 2024
  5. Junio C HamanoApr 30, 2024
  6. Toru OkugawaMay 1, 2024

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.