retitle 656223 git: running a command through an alias should not suppress "no
worktree" safety
severity 656223 minor
quit
Hi Lorenz,
Lorenz H-S wrote:
> In particular, "git status" returns with 128 and prints "fatal: This operation
> must be run in a work tree", while "git s" reports changes and the contents of
> the .git folder as untracked files.
Yep, I suppose we should treat this as a bug.
When expanding an alias, git does the usual "cd ..; cd .." dance
looking for the $GIT_DIR/config file, in case the alias is defined
there. In the usual case, once the config file has been found, we are
already in a convenient place for many aliases to run (since this way
they do not have to worry about handling the case of running in a
subdirectory sub/dir of the toplevel of the worktree) so git does not
chdir back.
However, it does still remember where it was first invoked, in the
$GIT_PREFIX environment variable.
To avoid the fuss of repeating the search, it also remembers $GIT_DIR.
Unfortunately, setting GIT_DIR without setting GIT_WORK_TREE is an
old convention for setting the worktree to the cwd.
The upshot: you can do
[alias]
s = !unset GIT_DIR && cd \"$GIT_PREFIX\" && git status -s
to get the behavior you are expecting, and git should probably find a
way to get the same effect automatically, either by repeating the
repository search for the aliased command or by also exporting a
GIT_NO_WORK_TREE variable to reflect the lack of discovered worktree.
Hope that helps,
Jonathan
--
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]