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

[RFC] require-work-tree wants more than what its name says

From
Junio C Hamano <gitster@pobox.com>
Date
May 3, 2011, 23:33 UTC
Message-ID
<7vhb9bgy0a.fsf@alter.siamese.dyndns.org>

Somebody tried "git pull" from a random place completely outside the work tree, while exporting GIT_DIR and GIT_WORK_TREE that are set to correct places, e.g.

	GIT_WORK_TREE=$HOME/git.git
        GIT_DIR=$GIT_WORK_TREE/.git
        export GIT_WORK_TREE GIT_DIR
        cd /tmp
        git pull

At the beginning of git-pull, we check "require-work-tree" and then "cd-to-toplevel". I _think_ the original intention when I wrote the command was "we MUST have a work tree, our $(cwd) might not be at the top-level directory of it", and no stronger than that. That check is a very sensible thing to do before doing cd-to-toplevel. We check that the place we would want to go exists, and then go there.

But the implementation of require_work_tree we have today is quite different. I don't have energy to dig the history, but currently it says:

 	test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ||
 	die "fatal: $0 cannot be used without a working tree."

Which is completely bogus. Even though we may happen to be just outside of it right now, we may have a working tree that we can cd_to_toplevel back to.

I recall there was a discussion sometime last year about this topic, and vaguely recall somebody proposed to swap the order of cd-to-toplevel and require-work-tree. While I agree that would sweep the issue under the rug, I think the right solution would be to apply the attached patch; and then audit all the callers that call "require_work_tree" to see if any of them meant "No, it is not Ok just to have working tree somewhere! I want you to be IN that working tree when you call me", and convert them to call the new require_to_be_in_work_tree instead.

Thoughts?
diff --git a/git-sh-setup.sh b/git-sh-setup.sh
index aa16b83..0b25f12 100644
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -141,6 +141,13 @@ cd_to_toplevel () {
 }
 
 require_work_tree () {
+	if test "z$(git rev-parse --is-bare-repository)" != zfalse
+	then
+		die "fatal: $0 cannot be used without a working tree."
+	fi
+}
+
+require_to_be_in_work_tree () {
 	test "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = true ||
 	die "fatal: $0 cannot be used without a working tree."
 }
Next: Jeff King
Message 1 of 12 in “[RFC] require-work-tree wants more than what its name says”
  1. Junio C HamanoMay 3, 2011
  2. Jeff KingMay 4, 2011
  3. Junio C HamanoMay 4, 2011
  4. Jeff KingMay 4, 2011
  5. Junio C HamanoMay 5, 2011
  6. Jeff KingMay 5, 2011
  7. Jeff KingMay 5, 2011
  8. Sverre RabbelierMay 5, 2011
  9. Junio C HamanoMay 5, 2011
  10. Michael J GruberMay 4, 2011
  11. Jeff KingMay 4, 2011
  12. Junio C HamanoMay 4, 2011

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.