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

[PATCH 6/6] mktree --missing: allow missing objects

From
Junio C Hamano <gitster@pobox.com>
Date
May 10, 2009, 18:49 UTC
Message-ID
<1241981391-19639-7-git-send-email-gitster@pobox.com>
In-Reply-To
<1241981391-19639-6-git-send-email-gitster@pobox.com>

We need to allow input lines that point at objects that we do not have when dealing with submodule entries anyway. This adds an explicit option to allow missing objects of other types, to be consistent with the use of --info-only option to the update-index command and --missing-ok option to the write-tree command.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-mktree.c  |   11 ++++++++---
 t/t1010-mktree.sh |   10 ++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/builtin-mktree.c b/builtin-mktree.c
index 17cdb3d..c2fd0f2 100644
--- a/builtin-mktree.c
+++ b/builtin-mktree.c
@@ -67,7 +67,7 @@ static const char *mktree_usage[] = {
 	NULL
 };
 
-static void mktree_line(char *buf, size_t len, int line_termination)
+static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
 {
 	char *ptr, *ntr;
 	unsigned mode;
@@ -92,9 +92,12 @@ static void mktree_line(char *buf, size_t len, int line_termination)
 
 	/* It is perfectly normal if we do not have a commit from a submodule */
 	if (!S_ISGITLINK(mode))
+		allow_missing = 1;
+
+	if (!allow_missing)
 		type = sha1_object_info(sha1, NULL);
 	else
-		type = OBJ_COMMIT;
+		type = object_type(mode);
 
 	if (type < 0)
 		die("object %s unavailable", sha1_to_hex(sha1));
@@ -118,15 +121,17 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
 	struct strbuf sb = STRBUF_INIT;
 	unsigned char sha1[20];
 	int line_termination = '\n';
+	int allow_missing = 0;
 	const struct option option[] = {
 		OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
+		OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing objects", 1),
 		OPT_END()
 	};
 
 	ac = parse_options(ac, av, option, mktree_usage, 0);
 
 	while (strbuf_getline(&sb, stdin, line_termination) != EOF)
-		mktree_line(sb.buf, sb.len, line_termination);
+		mktree_line(sb.buf, sb.len, line_termination, allow_missing);
 
 	strbuf_release(&sb);
 
diff --git a/t/t1010-mktree.sh b/t/t1010-mktree.sh
index 4d9b138..9956e3a 100755
--- a/t/t1010-mktree.sh
+++ b/t/t1010-mktree.sh
@@ -10,6 +10,11 @@ test_expect_success setup '
 		mkdir "$d" && echo "$d/one" >"$d/one" &&
 		git add "$d"
 	done &&
+	echo zero >one &&
+	git update-index --add --info-only one &&
+	git write-tree --missing-ok >tree.missing &&
+	git ls-tree $(cat tree.missing) >top.missing &&
+	git ls-tree -r $(cat tree.missing) >all.missing &&
 	echo one >one &&
 	git add one &&
 	git write-tree >tree &&
@@ -48,6 +53,11 @@ test_expect_success 'ls-tree output in wrong order given to mktree (2)' '
 	test_cmp tree.withsub actual
 '
 
+test_expect_success 'allow missing object with --missing' '
+	git mktree --missing <top.missing >actual &&
+	test_cmp tree.missing actual
+'
+
 test_expect_failure 'mktree reads ls-tree -r output (1)' '
 	git mktree <all >actual &&
 	test_cmp tree actual
-- 
1.6.3.9.g6345d
Previous: Junio C HamanoNext: René Scharfe
Message 13 of 16 in “questions about git-mktree”
  1. Jon SeymourMay 10, 2009
  2. Uwe Kleine-KönigMay 10, 2009
  3. A Large Angry SCMMay 10, 2009
  4. Jakub NarebskiMay 10, 2009
  5. Junio C HamanoMay 10, 2009
  6. Junio C HamanoMay 10, 2009
  7. 0/6 Modernize mktree somewhatJunio C Hamano, May 10, 2009
  8. 1/6 build-in git-mktreeJunio C Hamano, May 10, 2009
  9. 2/6 mktree: use parse-optionsJunio C Hamano, May 10, 2009
  10. 3/6 builtin-mktree.c: use a helper function to handle one line of inputJunio C Hamano, May 10, 2009
  11. 4/6 mktree: do not barf on a submodule commitJunio C Hamano, May 10, 2009
  12. 5/6 t1010: add mktree testJunio C Hamano, May 10, 2009
  13. 6/6 mktree --missing: allow missing objectsJunio C Hamano, May 10, 2009
  14. René ScharfeMay 10, 2009
  15. Junio C HamanoMay 11, 2009
  16. Josh MicichMay 10, 2009

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.