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

GIT_DIR in aliases [Re: Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]

From
DRDamien Robert <damien.olivier.robert@gmail.com>
Date
Feb 27, 2020, 15:50 UTC
Message-ID
<20200227155057.7idpa447ixo6sf6j@feanor>
In-Reply-To
<20200227104330.hp7zf2suquxsf6zw@feanor>
From Damien Robert, Thu 27 Feb 2020 at 11:43:30 (+0100) :
Show 12 quoted lines
> And looking at setup.c:760 it appears that `set_git_dir` (which set up among
> other things the GIT_DIR env) is called because we are in a worktree.
> 
> More precisely: we have discovered a git dir (a git file), so we call
> setup_discovered_git_dir, which itself calls setup_explicit_git_dir because
> we are in a work tree environment. And set_up_explicit_git_dir calls
> set_git_dir.
> 
> So I don't know what's the proper way to fix this. Keep track when we add
> ourselves the GIT_DIR rather than when it is already provided and remove it
> before executing a shell alias? Don't run the full configuration setup when
> we are executing a shell alias anyway?

Sorry about the spam. Thinking about it some more, this has nothing to do with submodules or worktrees, but about how we expect shell aliases to work.

In Documentation/config/alias, it is documented that the alias will run a the top level and GIT_PREFIX will be set.

In git.c#handle_alias, it is explicitly stated that shell alias call setup_git_directory_gently because: /* Aliases expect GIT_PREFIX, GIT_DIR etc to be set */

So one might argue that the behaviour I observed is not a bug, but it is still surprising for me (as a user), and maybe this could be stated more clearly in the docs?

Furthermore there is a question of consistency. GIT_DIR will not always be set before running a shell alias. Looking at `setup_discovered_git_dir`, it will be set if we are in a bare dir, or core.worktree / WORK_TREE is set, or if we have a gitfile.

The annoying side effect is that I cannot use as an alias a command that iterate over submodules and run git commands inside them, because in this alias GIT_DIR will be set sometimes, and sometimes not (a quick fix would be to unset GIT_DIR in my alias).

So what would be the best way to handle this? Thanks, Damien

Previous: Damien RobertNext: Jeff King
Message 8 of 13 in “Nested submodule checkout”
  1. Damien RobertFeb 14, 2020
  2. Philippe BlainFeb 17, 2020
  3. Damien RobertFeb 18, 2020
  4. Philippe BlainFeb 19, 2020
  5. Nested submodule status bugDamien Robert, Feb 26, 2020
  6. Damien RobertFeb 27, 2020
  7. Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]Damien Robert, Feb 27, 2020
  8. GIT_DIR in aliases [Re: Spurious GIT_DIR set when in a worktree [was Re: Nested submodule status bug]Damien Robert, Feb 27, 2020
  9. Jeff KingFeb 28, 2020
  10. Junio C HamanoFeb 28, 2020
  11. Damien RobertMar 3, 2020
  12. Philippe BlainFeb 29, 2020
  13. Damien RobertMar 3, 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.