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

[DONOTAPPLY PATCH 2/3] setup: warn about implicit worktree with $GIT_DIR

From
Jeff King <peff@peff.net>
Date
Mar 26, 2013, 20:12 UTC
Message-ID
<20130326201208.GB22522@sigill.intra.peff.net>
In-Reply-To
<20130326200851.GA22080@sigill.intra.peff.net>

It can be surprising to some users that pointing GIT_DIR to a ".git" directory does not use the working tree that surrounds the .git directory, but rather uses the current working directory as the working tree.

Git has always worked this way, and for the most part it has not been a big problem. However, given that one way of the user finding this out is by having a destructive git command impact an unexpected area of the filesystem, it would be nice to default to something less surprising and likely to cause problems (namely, having no working directory).

This breaks existing users of the feature, of course; they can adapt by setting GIT_WORK_TREE explicitly to ".", but they need to be told to do so. Therefore we'll start with a deprecation period and a warning to give them time to fix their scripts and workflows.

Signed-off-by: Jeff King <peff@peff.net>
---
 setup.c               | 21 ++++++++++++++++++++-
 t/t1510-repo-setup.sh |  8 ++++++--
 2 files changed, 26 insertions(+), 3 deletions(-)
diff --git a/setup.c b/setup.c
index 01c5476..afc245f 100644
--- a/setup.c
+++ b/setup.c
@@ -437,6 +437,23 @@ const char *read_gitfile(const char *path)
 	return path;
 }
 
+static const char warn_implicit_work_tree_msg[] =
+N_("You have set GIT_DIR (or used --git-dir) without specifying\n"
+   "a working tree. In Git 2.0, the behavior will change from using current\n"
+   "working directory as the working tree to having no working tree at all.\n"
+   "If you wish to continue the current behavior, please set GIT_WORK_TREE\n"
+   "or core.worktree explicitly. See `git help git` for more details.");
+
+static void warn_implicit_work_tree(void)
+{
+	static int warn_once;
+
+	if (warn_once++)
+		return;
+
+	warning("%s", _(warn_implicit_work_tree_msg));
+}
+
 static const char *setup_explicit_git_dir(const char *gitdirenv,
 					  char *cwd, int len,
 					  int *nongit_ok)
@@ -503,8 +520,10 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
 		free(gitfile);
 		return NULL;
 	}
-	else /* #2, #10 */
+	else { /* #2, #10 */
+		warn_implicit_work_tree();
 		set_git_work_tree(".");
+	}
 
 	/* set_git_work_tree() must have been called by now */
 	worktree = get_git_work_tree();
diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh
index cf2ee78..0910de1 100755
--- a/t/t1510-repo-setup.sh
+++ b/t/t1510-repo-setup.sh
@@ -243,7 +243,9 @@ test_expect_success '#2: worktree defaults to cwd with explicit GIT_DIR' '
 test_expect_success '#2: worktree defaults to cwd with explicit GIT_DIR' '
 	try_repo 2 unset "$here/2/.git" unset "" unset \
 		"$here/2/.git" "$here/2" "$here/2" "(null)" \
-		"$here/2/.git" "$here/2/sub" "$here/2/sub" "(null)"
+		"$here/2/.git" "$here/2/sub" "$here/2/sub" "(null)" \
+		2>message &&
+	test_i18ngrep "warning:.*GIT_DIR" message
 '
 
 test_expect_success '#2b: relative GIT_DIR' '
@@ -378,7 +380,9 @@ test_expect_success '#10: GIT_DIR can point to gitfile' '
 test_expect_success '#10: GIT_DIR can point to gitfile' '
 	try_repo 10 unset "$here/10/.git" unset gitfile unset \
 		"$here/10.git" "$here/10" "$here/10" "(null)" \
-		"$here/10.git" "$here/10/sub" "$here/10/sub" "(null)"
+		"$here/10.git" "$here/10/sub" "$here/10/sub" "(null)" \
+		2>message &&
+	test_i18ngrep "warning:.*GIT_DIR" message
 '
 
 test_expect_success '#10b: relative GIT_DIR can point to gitfile' '
-- 
1.8.2.13.g0f18d3c
Previous: Jonathan NiederNext: Jonathan Nieder
Message 30 of 35 in “git ate my home directory :-(”
  1. Richard WeinbergerMar 25, 2013
  2. Jonathan NiederMar 25, 2013
  3. Junio C HamanoMar 25, 2013
  4. Jonathan NiederMar 25, 2013
  5. Junio C HamanoMar 25, 2013
  6. Junio C HamanoMar 25, 2013
  7. Richard WeinbergerMar 25, 2013
  8. Jonathan NiederMar 25, 2013
  9. Junio C HamanoMar 25, 2013
  10. Richard WeinbergerMar 25, 2013
  11. Jonathan NiederMar 25, 2013
  12. Brandon CaseyMar 25, 2013
  13. Philip OakleyMar 26, 2013
  14. Duy NguyenMar 26, 2013
  15. Jeff KingMar 26, 2013
  16. Junio C HamanoMar 26, 2013
  17. Duy NguyenMar 27, 2013
  18. Philip OakleyMar 26, 2013
  19. Richard WeinbergerMar 26, 2013
  20. Jeff KingMar 26, 2013
  21. Richard WeinbergerMar 26, 2013
  22. demerphqMar 26, 2013
  23. Jeff KingMar 26, 2013
  24. demerphqMar 26, 2013
  25. Jeff KingMar 26, 2013
  26. Junio C HamanoMar 26, 2013
  27. Jeff KingMar 26, 2013
  28. 1/3 environment: set GIT_WORK_TREE when we figure out work treeJeff King, Mar 26, 2013
  29. Jonathan NiederMar 26, 2013
  30. 2/3 setup: warn about implicit worktree with $GIT_DIRJeff King, Mar 26, 2013
  31. Jonathan NiederMar 26, 2013
  32. Jeff KingMar 26, 2013
  33. Jonathan NiederMar 26, 2013
  34. Matthieu MoyMar 27, 2013
  35. 3/3 setup: treat GIT_DIR without GIT_WORK_TREE as a bare repoJeff King, Mar 26, 2013

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.