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

[PATCH 2/3] sha1-file: emit error if an alternate looks like a repository

From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Date
Dec 4, 2018, 13:27 UTC
Message-ID
<20181204132716.19208-3-avarab@gmail.com>
In-Reply-To
<87tvjtvah0.fsf@evledraar.gmail.com>

Since 26125f6b9b ("detect broken alternates.", 2006-02-22) we've emitted an error if the alternates directory doesn't exist, but not for the common misstep of adding a path to another git repository as an alternate, as opposed to its "objects" directory.

Let's check for this, i.e. whether X/objects or X/.git/objects exists if the user supplies X and print an error (which as a commit leading up to this one shows doesn't change the exit code, just "warns").

This check is intentionally not implemented by e.g. requiring that any of X/?? exists or X/info or X/pack exists. It's a legitimate use-case to point to an existing alternate that hasn't been populated yet, but pointing to one where an "X/objects" or "X/.git/objects" directory exists is definitely a mistake we should warn the user about.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 sha1-file.c               | 10 +++++++++-
 t/t5613-info-alternate.sh | 14 ++++++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/sha1-file.c b/sha1-file.c
index 5bd11c85bc..f142f81658 100644
--- a/sha1-file.c
+++ b/sha1-file.c
@@ -376,12 +376,20 @@ static int alt_odb_usable(struct raw_object_store *o,
 {
 	struct alternate_object_database *alt;
 
-	/* Detect cases where alternate disappeared */
 	if (!is_directory(path->buf)) {
+		/* Detect cases where alternate disappeared */
 		error(_("object directory %s does not exist; "
 			"check .git/objects/info/alternates"),
 		      path->buf);
 		return 0;
+	} else if (is_directory(mkpath("%s/objects", path->buf)) ||
+		   is_directory(mkpath("%s/.git/objects", path->buf))) {
+		/* Detect cases where alternate is a git repository */
+		error(_("object directory %s looks like a git repository; "
+			"alternates must point to the 'objects' directory. "
+			"check .git/objects/info/alternates"),
+		      path->buf);
+		return 0;
 	}
 
 	/*
diff --git a/t/t5613-info-alternate.sh b/t/t5613-info-alternate.sh
index d2964c57b7..b959e21421 100755
--- a/t/t5613-info-alternate.sh
+++ b/t/t5613-info-alternate.sh
@@ -143,4 +143,18 @@ test_expect_success 'print "error" on non-existing alternate' '
 	test_i18ngrep "does not exist; check" stderr
 '
 
+test_expect_success 'print "error" on alternate that looks like a git repository' '
+	git init --bare J &&
+	git init --bare K &&
+
+	# H is bare, G is not
+	echo ../../H >J/objects/info/alternates &&
+	echo ../../G >K/objects/info/alternates &&
+
+	git -C J fsck 2>stderr &&
+	test_i18ngrep "looks like a git repository; alternates must" stderr &&
+	git -C K fsck 2>stderr &&
+	test_i18ngrep "looks like a git repository; alternates must" stderr
+'
+
 test_done
-- 
2.20.0.rc2.403.gdbc3b29805
Previous: Ævar Arnfjörð BjarmasonNext: Junio C Hamano
Message 21 of 29 in “How de-duplicate similar repositories with alternates”
  1. Ævar Arnfjörð BjarmasonNov 29, 2018
  2. Ævar Arnfjörð BjarmasonNov 29, 2018
  3. Stefan BellerNov 29, 2018
  4. Ævar Arnfjörð BjarmasonNov 29, 2018
  5. Duy NguyenNov 29, 2018
  6. Jeff KingDec 4, 2018
  7. Derrick StoleeDec 4, 2018
  8. Jeff KingDec 4, 2018
  9. Ævar Arnfjörð BjarmasonDec 4, 2018
  10. 1/3 sha1-file: test the error behavior of alt_odb_usable()Ævar Arnfjörð Bjarmason, Dec 4, 2018
  11. sha1-file: test the error behavior of alt_odb_usable()Ævar Arnfjörð Bjarmason, Mar 28, 2019
  12. Jeff KingMar 29, 2019
  13. Ævar Arnfjörð BjarmasonMar 29, 2019
  14. Ævar Arnfjörð BjarmasonApr 8, 2019
  15. Junio C HamanoApr 9, 2019
  16. Ævar Arnfjörð BjarmasonApr 9, 2019
  17. Junio C HamanoApr 9, 2019
  18. Jeff KingApr 9, 2019
  19. Junio C HamanoApr 9, 2019
  20. 0/3 sha1-file: warn if alternate is a git repo (not object dir)Ævar Arnfjörð Bjarmason, Dec 4, 2018
  21. 2/3 sha1-file: emit error if an alternate looks like a repositoryÆvar Arnfjörð Bjarmason, Dec 4, 2018
  22. Junio C HamanoDec 5, 2018
  23. Jeff KingDec 5, 2018
  24. 3/3 sha1-file: change alternate "error:" message to "warning:"Ævar Arnfjörð Bjarmason, Dec 4, 2018
  25. Junio C HamanoDec 5, 2018
  26. Jeff KingDec 5, 2018
  27. Junio C HamanoDec 5, 2018
  28. Ævar Arnfjörð BjarmasonDec 4, 2018
  29. Derrick StoleeDec 4, 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.