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

[PATCH] setup: do not create $X/gitdir unnecessarily when accessing git file $X

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Nov 2, 2015, 19:08 UTC
Message-ID
<1446491306-13493-1-git-send-email-pclouds@gmail.com>
In-Reply-To
<xmqqwpu7klmu.fsf@gitster.mtv.corp.google.com>

$X/gitdir is created, or refreshed, in order to keep a linked worktree from being pruned. But while git file is used as the foundation for linked worktrees, it's used for other purposes as well and we should not create $X/gitdir in those cases.

Tighten the check. Only update an existing file, which is an indication this is a linked worktree.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 setup.c            | 2 +-
 t/t0002-gitfile.sh | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
diff --git a/setup.c b/setup.c
index d343725..b30d923 100644
--- a/setup.c
+++ b/setup.c
@@ -440,7 +440,7 @@ static void update_linked_gitdir(const char *gitfile, const char *gitdir)
 	struct stat st;
 
 	strbuf_addf(&path, "%s/gitdir", gitdir);
-	if (stat(path.buf, &st) || st.st_mtime + 24 * 3600 < time(NULL))
+	if (!stat(path.buf, &st) && st.st_mtime + 24 * 3600 < time(NULL))
 		write_file(path.buf, "%s", gitfile);
 	strbuf_release(&path);
 }
diff --git a/t/t0002-gitfile.sh b/t/t0002-gitfile.sh
index 9670e8c..b1b59f2 100755
--- a/t/t0002-gitfile.sh
+++ b/t/t0002-gitfile.sh
@@ -99,6 +99,13 @@ test_expect_success 'check rev-list' '
 	test "$SHA" = "$(git rev-list HEAD)"
 '
 
+test_expect_success '$REAL/gitdir is not created on ordinary git file' '
+	echo "gitdir: $REAL" >expected &&
+	test_cmp expected .git &&
+	git status &&
+	! test -f "$REAL"/gitdir
+'
+
 test_expect_success 'setup_git_dir twice in subdir' '
 	git init sgd &&
 	(
-- 
2.2.0.513.g477eb31
Previous: Junio C HamanoNext: Eric Sunshine
Message 8 of 18 in “What's the ".git/gitdir" file?”
  1. Kyle MeyerOct 27, 2015
  2. Stefan BellerOct 27, 2015
  3. Randall S. BeckerOct 27, 2015
  4. Stefan BellerOct 27, 2015
  5. Junio C HamanoOct 27, 2015
  6. Mike RappazzoOct 27, 2015
  7. Junio C HamanoOct 28, 2015
  8. setup: do not create $X/gitdir unnecessarily when accessing git file $XNguyễn Thái Ngọc Duy, Nov 2, 2015
  9. Eric SunshineNov 2, 2015
  10. Jeff KingNov 2, 2015
  11. Junio C HamanoNov 2, 2015
  12. Jeff KingNov 2, 2015
  13. Duy NguyenNov 3, 2015
  14. Junio C HamanoNov 3, 2015
  15. worktree: stop supporting moving worktrees manuallyNguyễn Thái Ngọc Duy, Dec 27, 2015
  16. Eric SunshineDec 28, 2015
  17. Duy NguyenDec 29, 2015
  18. Eric SunshineDec 31, 2015

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.