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

Re: questions about git-mktree - [PATCH] proposed '--batch' option

From
JMJosh Micich <josh.micich@gmail.com>
Date
May 10, 2009, 18:54 UTC
Message-ID
<loom.20090510T184606-644@post.gmane.org>
In-Reply-To
<7vvdo8zxwp.fsf@alter.siamese.dyndns.org>
Junio C Hamano <gitster <at> pobox.com> writes:
> 
> I'm fixing it.  You _might_ get your recursive mktree as a side effect at
> the end of the series, but no promises 

Is it possible add a '--batch' option to git-mktree? Here is a change I made locally which seems to work. Hopefully it's not too hard to incorporate with your other changes. -josh

 Documentation/git-mktree.txt |   14 +++++-
 mktree.c                     |   98 +++++++++++++++++++++++++----------------
diff --git a/Documentation/git-mktree.txt b/Documentation/git-mktree.txt
index af19f06..1aa24b5 100644
--- a/Documentation/git-mktree.txt
+++ b/Documentation/git-mktree.txt
@@ -8,18 +8,25 @@ git-mktree - Build a tree-object from ls-tree formatted text
 
 SYNOPSIS
 --------
-'git mktree' [-z]
+'git mktree' [-z] [--batch]
 
 DESCRIPTION
 -----------
 Reads standard input in non-recursive `ls-tree` output format,
-and creates a tree object.  The object name of the tree object
-built is written to the standard output.
+and creates a tree object.  The order of the tree entries is 
+normalised by mktree so pre-sorting the input is not required.
+The object name of the tree object built is written to the
+standard output.
 
 OPTIONS
 -------
 -z::
 	Read the NUL-terminated `ls-tree -z` output instead.
+--batch::
+	Allow building of more than one tree object before exiting.
+	Each tree is separated by as single blank line. The final
+	new-line is optional.  Note - if the '-z' option is used,
+	lines are terminated with NUL.
 
 Author
 ------
diff --git a/mktree.c b/mktree.c
index 137a095..2743aaa 100644
--- a/mktree.c
+++ b/mktree.c
@@ -62,7 +62,7 @@ static void write_tree(unsigned char *sha1)
 	write_sha1_file(buf.buf, buf.len, tree_type, sha1);
 }
 
-static const char mktree_usage[] = "git mktree [-z]";
+static const char mktree_usage[] = "git mktree [-z] [--batch]";
 
 int main(int ac, char **av)
 {
@@ -70,6 +70,7 @@ int main(int ac, char **av)
 	struct strbuf p_uq = STRBUF_INIT;
 	unsigned char sha1[20];
 	int line_termination = '\n';
+	int is_batch_mode = 0;
 
 	git_extract_argv0_path(av[0]);
 
@@ -79,53 +80,74 @@ int main(int ac, char **av)
 		char *arg = av[1];
 		if (!strcmp("-z", arg))
 			line_termination = 0;
+		else if (!strcmp("--batch", arg))
+			is_batch_mode = 1;
 		else
 			usage(mktree_usage);
 		ac--;
 		av++;
 	}
 
-	while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
-		char *ptr, *ntr;
-		unsigned mode;
-		enum object_type type;
-		char *path;
-
-		ptr = sb.buf;
-		/* Input is non-recursive ls-tree output format
-		 * mode SP type SP sha1 TAB name
-		 */
-		mode = strtoul(ptr, &ntr, 8);
-		if (ptr == ntr || !ntr || *ntr != ' ')
-			die("input format error: %s", sb.buf);
-		ptr = ntr + 1; /* type */
-		ntr = strchr(ptr, ' ');
-		if (!ntr || sb.buf + sb.len <= ntr + 40 ||
-		    ntr[41] != '\t' ||
-		    get_sha1_hex(ntr + 1, sha1))
-			die("input format error: %s", sb.buf);
-		type = sha1_object_info(sha1, NULL);
-		if (type < 0)
-			die("object %s unavailable", sha1_to_hex(sha1));
-		*ntr++ = 0; /* now at the beginning of SHA1 */
-		if (type != type_from_string(ptr))
-			die("object type %s mismatch (%s)", ptr, 
typename(type));
-
-		path = ntr + 41;  /* at the beginning of name */
-		if (line_termination && path[0] == '"') {
-			strbuf_reset(&p_uq);
-			if (unquote_c_style(&p_uq, path, NULL)) {
-				die("invalid quoting");
+	int got_eof = 0;
+	while (!got_eof) {
+		while (1) {
+			if (strbuf_getline(&sb, stdin, line_termination) == 
EOF) {
+				got_eof = 1;
+				break;
+			}
+			if (sb.buf[0] == '\0') {
+				// empty lines denote tree boundaries in batch 
mode
+				if (is_batch_mode) {
+					break;
+				}
+				die("input format error: (blank line only valid 
in batch mode)");
+			}
+			char *ptr, *ntr;
+			unsigned mode;
+			enum object_type type;
+			char *path;
+	
+			ptr = sb.buf;
+			/* Input is non-recursive ls-tree output format
+			 * mode SP type SP sha1 TAB name
+			 */
+			mode = strtoul(ptr, &ntr, 8);
+			if (ptr == ntr || !ntr || *ntr != ' ')
+				die("input format error: %s", sb.buf);
+			ptr = ntr + 1; /* type */
+			ntr = strchr(ptr, ' ');
+			if (!ntr || sb.buf + sb.len <= ntr + 40 ||
+				ntr[41] != '\t' ||
+				get_sha1_hex(ntr + 1, sha1))
+				die("input format error: %s", sb.buf);
+			type = sha1_object_info(sha1, NULL);
+			if (type < 0)
+				die("object %s unavailable", 
sha1_to_hex(sha1));
+			*ntr++ = 0; /* now at the beginning of SHA1 */
+			if (type != type_from_string(ptr))
+				die("object type %s mismatch (%s)", ptr, 
typename(type));
+
+			path = ntr + 41;  /* at the beginning of name */
+			if (line_termination && path[0] == '"') {
+				strbuf_reset(&p_uq);
+				if (unquote_c_style(&p_uq, path, NULL)) {
+					die("invalid quoting");
+				}
+				path = p_uq.buf;
 			}
-			path = p_uq.buf;
-		}
 
-		append_to_tree(mode, sha1, path);
+			append_to_tree(mode, sha1, path);
+		}
+		if (is_batch_mode && got_eof && used < 1) {
+			// allow input to finish with a new-line (or not)
+		} else {
+			write_tree(sha1);
+			puts(sha1_to_hex(sha1));
+			fflush(stdout);
+		}
+		used=0; // reset tree entry buffer for re-use in batch mode
 	}
 	strbuf_release(&p_uq);
 	strbuf_release(&sb);
-
-	write_tree(sha1);
-	puts(sha1_to_hex(sha1));
 	exit(0);
 }
Previous: Junio C Hamano
Message 16 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.