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

Inconsistent results from git rev-parse --show-toplevel

From
CACrabtree, Andrew <andrew.crabtree@hpe.com>
Date
Jan 24, 2020, 18:07 UTC
Message-ID
<TU4PR8401MB111758B9513DD7D8B96CBFAAF90E0@TU4PR8401MB1117.NAMPRD84.PROD.OUTLOOK.COM>

I ran into an issue where 'git rev-parse --show-toplevel' is reporting the current directory instead of the root of the repository. In my case I'm running it from within the pre-commit hook, and it seems like it might be related to GIT_DIR being set. It may also be related to using worktrees. Haven't had a chance to test thoroughly.

Regards, -Andrew

Git Version -  2.25.0
pre-commit hook

#!/bin/bash env pwd git rev-parse --show-toplevel cd subdir pwd git-rev-parse --show-toplevel

Create a repo with a subdirectory.   Commit as normal.  Output from the hook looks as expected.  Create a worktree, and commit there and it appears as though it is returning incorrect results after changing directory.

Git repo is in /tmp/revparse_test Worktree is in /tmp/revparse_test_worktree

Committing in /tmp/revparse_test yields the below, as expected /tmp/revparse_test /tmp/revparse_test /tmp/revparse_test/subdir /tmp/revparse_test

Committing in the worktree yields below, with the final line seeming incorrect. /tmp/revparse_test_worktree /tmp/revparse_test_worktree /tmp/revparse_test_worktree/subdir /tmp/revparse_test_worktree/subdir

Next: Junio C Hamano
Message 1 of 8 in “Inconsistent results from git rev-parse --show-toplevel”
  1. Crabtree, AndrewJan 24, 2020
  2. Junio C HamanoJan 24, 2020
  3. Crabtree, AndrewJan 25, 2020
  4. Jeff KingJan 25, 2020
  5. Crabtree, AndrewJan 25, 2020
  6. Jeff KingJan 30, 2020
  7. Crabtree, AndrewJan 30, 2020
  8. Jeff KingFeb 14, 2020

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.