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

Re: [PATCH] Make git log work for git CWD outside of work tree

From
Duy Nguyen <pclouds@gmail.com>
Date
Apr 10, 2017, 12:01 UTC
Message-ID
<CACsJy8CLBY22j3EjR4PW3n+K6PWUzb-HCgxTVeCGpwtApZF-6g@mail.gmail.com>
In-Reply-To
<xmqqvaqdqfhe.fsf@gitster.mtv.corp.google.com>
On Mon, Apr 10, 2017 at 7:21 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 25 quoted lines
> If your arrangement is even more exotic, e.g. you have these two
> variables set, and then are running from OUTSIDE the working tree,
> my knee-jerk reaction is that you should get your head examined, as
> it is totally unclear what "git add foo" would mean if you did this:
>
>     $ export GIT_DIR=~/myproject/.git GIT_WORK_TREE=~/myproject
>     $ cd ~/myproject/../somewhere/else
>     $ git add foo
>
> But it should still "work" in the sense that the above command
> should notice that you are trying to add "../somewhere/else/foo" to
> the index, which is obviously nonsense, and die with a message.
>
> Similarly, if you replace "git add foo" with "git log", it still
> should work in the above, i.e.
>
>     $ export GIT_DIR=~/myproject/.git GIT_WORK_TREE=~/myproject
>     $ cd ~/myproject/../somewhere/else
>     $ git log
>
> If Git is not chdir(2)ing to ~/myproject before calling cmd_log()
> in the above (again, this is my down week so I didn't and will not
> check with the code myself), we may want to call that a bug and fix
> it, so that you do not have to do anything special to get to the
> path of ".mailmap" that is at the top-level.

The behavior is "documented" in t1510 since f3bb8b4b84 (Merge branch 'nd/setup' - 2010-12-28)

"11. When user's cwd is outside worktree, cwd remains unchanged,
    prefix is NULL."

This behavior probably started long before my topic though, mine was more of documentation, making worktree detection more consistent. It's the same case with defining GIT_DIR without GIT_WORK_TREE, I think: scripts started to depend on a behavior that we did not clearly define, by the time we knew what we wanted and we kept the old behavior forever.

I think it's just safer to go with Johannes' suggestion.

An alternative is, when you have found out you need to read .mailmap, you call setup_work_tree() then, which prepares the worktree for you (including moving back to cwd) or dies if worktree does not exist, or no-op if worktree has already been asked by somebody. Many commands do lazy worktree initialization this way.

-- 
Duy
Previous: Junio C HamanoNext: Jeff King
Message 5 of 15 in “Make git log work for git CWD outside of work tree”
  1. Make git log work for git CWD outside of work treeDanny Sauer, Apr 9, 2017
  2. Johannes SchindelinApr 9, 2017
  3. Danny SauerApr 9, 2017
  4. Junio C HamanoApr 10, 2017
  5. Duy NguyenApr 10, 2017
  6. Jeff KingApr 10, 2017
  7. Duy NguyenApr 12, 2017
  8. Junio C HamanoApr 12, 2017
  9. Duy NguyenApr 12, 2017
  10. Jeff KingApr 12, 2017
  11. Duy NguyenApr 12, 2017
  12. Jeff KingApr 13, 2017
  13. Junio C HamanoApr 17, 2017
  14. Duy NguyenApr 17, 2017
  15. Jeff KingApr 12, 2017

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.