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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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);
 }





--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]