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

Re: In some rebases, `exec git -C ...` has wrong working directory

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Apr 27, 2018, 07:45 UTC
Message-ID
<nycvar.QRO.7.76.6.1804270939400.72@tvgsbejvaqbjf.bet>
In-Reply-To
<CAFW+GMAJcCG4mKe4TxFXXrfdRhZKXv8ffC-rNShFLW2J1_FANA@mail.gmail.com>
Hi William,
On Thu, 26 Apr 2018, William Chargin wrote:
Show 13 quoted lines
> Here is a repro script:
> 
>     #!/bin/sh
>     set -eux
>     git --version
>     tmpdir="$(mktemp -d)"
>     cd "${tmpdir}"
>     mkdir target repo
>     cd repo
>     git init
>     touch file; git add file
>     git commit -m "Initial commit"
>     git rebase HEAD --exec "git -C ${tmpdir}/target init"

We do take pains to pass the GIT_DIR to the exec'ed command, and it is the GIT_DIR of the worktree in which the rebase runs.

Show 5 quoted lines
> The end of this script prints something like
> 
>     Executing: git -C /tmp/tmp.gd2q51jO93/target init
>     Reinitialized existing Git repository in /tmp/tmp.gd2q51jO93/repo/.git/
>     Successfully rebased and updated refs/heads/master.
So that is actually what I expected.

I might even have one or two scripts relying on that feature, where something like

	exec git -C /somewhere/else show HEAD:some-file >some-other-file
is executed, and works.
Show 20 quoted lines
> But this is wrong: the repository should be initialized in `target`, not
> reinitialized in `repo`.
> 
> Notes:
> 
>   - This propagates to subprocesses: if you run `exec make test` and
>     your test suite ends up calling `git -C`, then the same problem
>     occurs.
> 
>   - Substituting `rebase --root` for `rebase HEAD` causes the problem to
>     go away.
> 
>   - The `rebase HEAD` exec context adds the `GIT_DIR` environment
>     variable, and this is sufficient to reproduce the problem:
>     running `GIT_DIR="$PWD" git -C /tmp/target init` puts the repo in
>     the current working directory. The `rebase --root` context adds no
>     such environment variable. (You can use `--exec 'env >tempfile'` to
>     verify these.)
> 
> My `git --version` is 2.16.2.
Even `pu` has the same behavior.

I do not think that we can sensibly *remove* GIT_DIR from the environment variables passed to the exec'ed command, as we have been doing that for ages, and some scripts (as demonstrated above) started relying on that behavior. So if we changed it, we would break backwards-compatibility, which is something we try to avoid very much in Git.

Maybe you could a contribute a patch to the documentation? Something that tells people who use the `--exec` option that they want to prefix their command with `unset GIT_DIR; ` when their command wants to work with a different Git repository than the current one?

Ciao, Johannes

Previous: William CharginNext: William Chargin
Message 2 of 3 in “In some rebases, `exec git -C ...` has wrong working directory”
  1. William CharginApr 27, 2018
  2. Johannes SchindelinApr 27, 2018
  3. William CharginApr 27, 2018

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.