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

[PATCH 2/3] added --batch option to mktree

From
JMJosh Micich <josh.micich@gmail.com>
Date
May 14, 2009, 05:10 UTC
Message-ID
<loom.20090514T051004-665@post.gmane.org>
In-Reply-To
<loom.20090514T050424-673@post.gmane.org>
This option enables creation of many tree objects with a single process
Signed-off-by: Josh Micich <josh.micich@gmail.com>
---
 Documentation/git-mktree.txt |    7 ++++++-
 builtin-mktree.c             |   36 +++++++++++++++++++++++++++++-------
 2 files changed, 35 insertions(+), 8 deletions(-)
diff --git a/Documentation/git-mktree.txt b/Documentation/git-mktree.txt
index 0461062..3d2237a 100644
--- a/Documentation/git-mktree.txt
+++ b/Documentation/git-mktree.txt
@@ -8,7 +8,7 @@ git-mktree - Build a tree-object from ls-tree formatted text
 
 SYNOPSIS
 --------
-'git mktree' [-z] [--missing]
+'git mktree' [-z] [--missing] [--batch]
 
 DESCRIPTION
 -----------
@@ -25,6 +25,11 @@ OPTIONS
 	Allow missing objects.  The default behaviour (without this option)
 	is to verify that each tree entry's sha1 identifies an existing
 	object.
+--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/builtin-mktree.c b/builtin-mktree.c
index db647ce..a56c917 100644
--- a/builtin-mktree.c
+++ b/builtin-mktree.c
@@ -63,7 +63,7 @@ static void write_tree(unsigned char *sha1)
 }
 
 static const char *mktree_usage[] = {
-	"git mktree [-z] [--missing]",
+	"git mktree [-z] [--missing] [--batch]",
 	NULL
 };
 
@@ -125,20 +125,42 @@ int cmd_mktree(int ac, const char **av, const char 
*prefix)
 	unsigned char sha1[20];
 	int line_termination = '\n';
 	int allow_missing = 0;
+	int is_batch_mode = 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_SET_INT( 0 , "batch", &is_batch_mode, "interactively create 
more than one tree", 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, allow_missing);
-
+	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)");
+			}
+			mktree_line(sb.buf, sb.len, line_termination, 
allow_missing);
+		}
+		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(&sb);
-
-	write_tree(sha1);
-	puts(sha1_to_hex(sha1));
 	exit(0);
 }
-- 
1.6.3.165.g2cce5.dirty
Previous: Josh MicichNext: Junio C Hamano
Message 5 of 16 in “Proposed patch for mktree [0/3]”
  1. Josh MicichMay 14, 2009
  2. 1/3 '--missing' option for mktree: re-added strbuf_release(&p_uq), Updated man pageJosh Micich, May 14, 2009
  3. Junio C HamanoMay 14, 2009
  4. Josh MicichMay 14, 2009
  5. 2/3 added --batch option to mktreeJosh Micich, May 14, 2009
  6. Junio C HamanoMay 14, 2009
  7. Josh MicichMay 14, 2009
  8. Sverre RabbelierMay 14, 2009
  9. Josh MicichMay 14, 2009
  10. 3/3 improved validation of entry type in mktreeJosh Micich, May 14, 2009
  11. Junio C HamanoMay 14, 2009
  12. Josh MicichMay 14, 2009
  13. Josh MicichMay 14, 2009
  14. Jakub NarebskiMay 15, 2009
  15. Josh MicichMay 15, 2009
  16. Junio C HamanoMay 15, 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.