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

[PATCH 2/3] index-pack: complain when --stdin is used outside of a repo

From
Jeff King <peff@peff.net>
Date
Dec 16, 2016, 02:30 UTC
Message-ID
<20161216023058.a2ksbkhb2lwkiglf@sigill.intra.peff.net>
In-Reply-To
<20161216022904.cjang6napnl2vkc6@sigill.intra.peff.net>

The index-pack builtin is marked as RUN_SETUP_GENTLY, because it's perfectly fine to index a pack in the filesystem outside of any repository. However, --stdin mode will write the result to the object database, which does not make sense outside of a repository. Doing so creates a bogus ".git" directory with nothing in it except the newly-created pack and its index.

Instead, let's flag this as an error and abort.
Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/index-pack.c   |  2 ++
 t/t5300-pack-object.sh | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
diff --git a/builtin/index-pack.c b/builtin/index-pack.c
index 0a27bab11b..d450a6ada2 100644
--- a/builtin/index-pack.c
+++ b/builtin/index-pack.c
@@ -1730,6 +1730,8 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
 		usage(index_pack_usage);
 	if (fix_thin_pack && !from_stdin)
 		die(_("--fix-thin cannot be used without --stdin"));
+	if (from_stdin && !startup_info->have_repository)
+		die(_("--stdin requires a git repository"));
 	if (!index_name && pack_name)
 		index_name = derive_filename(pack_name, ".idx", &index_name_buf);
 	if (keep_msg && !keep_name && pack_name)
diff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh
index 899e52d50f..43a672c345 100755
--- a/t/t5300-pack-object.sh
+++ b/t/t5300-pack-object.sh
@@ -406,6 +406,21 @@ test_expect_success 'verify resulting packs' '
 	git verify-pack test-11-*.pack
 '
 
+test_expect_success 'set up pack for non-repo tests' '
+	# make sure we have a pack with no matching index file
+	cp test-1-*.pack foo.pack
+'
+
+test_expect_success 'index-pack --stdin complains of non-repo' '
+	nongit test_must_fail git index-pack --stdin <foo.pack &&
+	test_path_is_missing non-repo/.git
+'
+
+test_expect_success 'index-pack <pack> works in non-repo' '
+	nongit git index-pack ../foo.pack &&
+	test_path_is_file foo.idx
+'
+
 #
 # WARNING!
 #
-- 
2.11.0.348.g960a0b554
Previous: Jeff KingNext: Jeff King
Message 6 of 14 in “index-pack outside of repository?”
  1. Jeff KingDec 15, 2016
  2. Junio C HamanoDec 16, 2016
  3. Jeff KingDec 16, 2016
  4. Jeff KingDec 16, 2016
  5. 1/3 t5000: extract nongit function to test-lib-functions.shJeff King, Dec 16, 2016
  6. 2/3 index-pack: complain when --stdin is used outside of a repoJeff King, Dec 16, 2016
  7. 3/3 t: use nongit() function where applicableJeff King, Dec 16, 2016
  8. Junio C HamanoDec 16, 2016
  9. Junio C HamanoDec 16, 2016
  10. Junio C HamanoDec 16, 2016
  11. Jeff KingDec 16, 2016
  12. Junio C HamanoDec 16, 2016
  13. Junio C HamanoDec 16, 2016
  14. Jeff KingDec 16, 2016

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.