Skip to contentSkip to conversation

questions about git-mktree

PATCH In review Jon Seymour 7 participants 16 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

Uwe,

Thanks for your explanation.

Does git-mktree normalize the sort order of the input or take it as it is? I can see a case for having it do normalization, if it doesn't already and probably for this to be the default behaviour.

Also, I have a need for something like git-mktree that takes a recursive git-lstree output as input.

Junio: assuming these features don't exist already, are you open to the idea of accepting patches that add them?

Regards,

jon.

#1 lore raw

Hello Jon,

Quoted 5 lines · Jon Seymour

Thanks for your explanation.

Does git-mktree normalize the sort order of the input or take it as it is? I can see a case for having it do normalization, if it doesn't already and probably for this to be the default behaviour.

As git-mktree doesn't exists I cannot answer that question ...

Anyhow, another possibility to construct a tree is using the (or an) index.

git-update-index --add --cacheinfo <mode> <object> <file> becomes handy for constructing trees.

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                              | Uwe Kleine-König            |
Industrial Linux Solutions                    | http://www.pengutronix.de/  |
#3 lore raw
In reply to Uwe Kleine-König
Quoted 10 lines · Uwe Kleine-König

Hello Jon,

Quoted 5 lines · Jon Seymour

Thanks for your explanation.

Does git-mktree normalize the sort order of the input or take it as it is? I can see a case for having it do normalization, if it doesn't already and probably for this to be the default behaviour.

As git-mktree doesn't exists I cannot answer that question ...

$ ls $(git --exec-path)/git-mktree
/usr/libexec/git-core/git-mktree

Examining mktree.c a bit you can see that there is qsort using ent_compare in write_tree procedure. It is not long file: 131 lines.

Signature · 3 lines
Jakub Narebski
Poland
ShadeHawk on #git
#4 lore raw
In reply to Jon Seymour
Quoted 3 lines · Jon Seymour

Does git-mktree normalize the sort order of the input or take it as it is? I can see a case for having it do normalization, if it doesn't already and probably for this to be the default behaviour.

It does have "accumulate and qsort before writing it out", since 83f5053 (git-mktree: reverse of git-ls-tree., 2006-02-20).

This program was done as a quick hack but it is so old that I forgot all about it. If you are going to use it, you may have to do some reality checks on it first. For example, notice that the date above predates the introduction of gitlinks, so I would not be too surprised if it barfed when given a commit object in its input.

#5 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Jon Seymour

Does git-mktree normalize the sort order of the input or take it as it is? I can see a case for having it do normalization, if it doesn't already and probably for this to be the default behaviour.

Quoted 8 lines

It does have "accumulate and qsort before writing it out", since 83f5053 (git-mktree: reverse of git-ls-tree., 2006-02-20).

This program was done as a quick hack but it is so old that I forgot all about it. If you are going to use it, you may have to do some reality checks on it first. For example, notice that the date above predates the introduction of gitlinks, so I would not be too surprised if it barfed when given a commit object in its input.

Heh, it does verify that the object listed exists locally in the repository, so submodules would not work in general.

I'm fixing it. You _might_ get your recursive mktree as a side effect at the end of the series, but no promises ;-)

#6 lore raw

cover Modernize mktree somewhat

This series updates mktree to make it a built-in, teaches it to use parse-options, and then adds tests. These are all necessary steps to modernize it.

The last one adds --missing option to make things consistent with other parts of the system. The combination of update-index and write-tree has provisions to record a tree of objects that we may not have, but so far it was not possible with mktree.

Junio C Hamano (6):
build-in git-mktree
mktree: use parse-options
builtin-mktree.c: use a helper function to handle one line of input
mktree: do not barf on a submodule commit
t1010: add mktree test
mktree --missing: allow missing objects

 Makefile          |    2 +-
 builtin-mktree.c  |  141 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 builtin.h         |    1 +
 git.c             |    1 +
 mktree.c          |  131 -------------------------------------------------
 t/t1010-mktree.sh |   71 +++++++++++++++++++++++++++
 6 files changed, 215 insertions(+), 132 deletions(-)
 create mode 100644 builtin-mktree.c
 delete mode 100644 mktree.c
 create mode 100755 t/t1010-mktree.sh
#7 lore raw

PATCH 1/6 build-in git-mktree

---
 Makefile                     |    2 +-
 mktree.c => builtin-mktree.c |   18 +++++++-----------
 builtin.h                    |    1 +
 git.c                        |    1 +
 4 files changed, 10 insertions(+), 12 deletions(-)
 rename mktree.c => builtin-mktree.c (91%)
Makefile +1 -1
index 6e21643..9d9f0da 100644
@@ -332,7 +332,6 @@ PROGRAMS += git-index-pack$X
 PROGRAMS += git-merge-index$X
 PROGRAMS += git-merge-tree$X
 PROGRAMS += git-mktag$X
-PROGRAMS += git-mktree$X
 PROGRAMS += git-pack-redundant$X
 PROGRAMS += git-patch-id$X
 PROGRAMS += git-shell$X
@@ -586,6 +585,7 @@ BUILTIN_OBJS += builtin-merge-base.o
 BUILTIN_OBJS += builtin-merge-file.o
 BUILTIN_OBJS += builtin-merge-ours.o
 BUILTIN_OBJS += builtin-merge-recursive.o
+BUILTIN_OBJS += builtin-mktree.o
 BUILTIN_OBJS += builtin-mv.o
 BUILTIN_OBJS += builtin-name-rev.o
 BUILTIN_OBJS += builtin-pack-objects.o
builtin-mktree.c renamed from mktree.c +7 -11
similarity index 91%
rename from mktree.c
rename to builtin-mktree.c
index 137a095..3d05427 100644
@@ -1,12 +1,11 @@
 /*
  * GIT - the stupid content tracker
  *
- * Copyright (c) Junio C Hamano, 2006
+ * Copyright (c) Junio C Hamano, 2006, 2009
  */
-#include "cache.h"
+#include "builtin.h"
 #include "quote.h"
 #include "tree.h"
-#include "exec_cmd.h"
 
 static struct treeent {
 	unsigned mode;
@@ -64,19 +63,15 @@ static void write_tree(unsigned char *sha1)
 
 static const char mktree_usage[] = "git mktree [-z]";
 
-int main(int ac, char **av)
+int cmd_mktree(int ac, const char **av, const char *prefix)
 {
 	struct strbuf sb = STRBUF_INIT;
 	struct strbuf p_uq = STRBUF_INIT;
 	unsigned char sha1[20];
 	int line_termination = '\n';
 
-	git_extract_argv0_path(av[0]);
-
-	setup_git_directory();
-
 	while ((1 < ac) && av[1][0] == '-') {
-		char *arg = av[1];
+		const char *arg = av[1];
 		if (!strcmp("-z", arg))
 			line_termination = 0;
 		else
@@ -92,8 +87,9 @@ int main(int ac, char **av)
 		char *path;
 
 		ptr = sb.buf;
-		/* Input is non-recursive ls-tree output format
-		 * mode SP type SP sha1 TAB name
+		/*
+		 * Read non-recursive ls-tree output format:
+		 *     mode SP type SP sha1 TAB name
 		 */
 		mode = strtoul(ptr, &ntr, 8);
 		if (ptr == ntr || !ntr || *ntr != ' ')
builtin.h +1 -0
index 425ff8e..20427d2 100644
@@ -72,6 +72,7 @@ extern int cmd_merge_base(int argc, const char **argv, const char *prefix);
 extern int cmd_merge_ours(int argc, const char **argv, const char *prefix);
 extern int cmd_merge_file(int argc, const char **argv, const char *prefix);
 extern int cmd_merge_recursive(int argc, const char **argv, const char *prefix);
+extern int cmd_mktree(int argc, const char **argv, const char *prefix);
 extern int cmd_mv(int argc, const char **argv, const char *prefix);
 extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
 extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);
git.c +1 -0
index 5a00726..7d7f949 100644
@@ -327,6 +327,7 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "merge-ours", cmd_merge_ours, RUN_SETUP },
 		{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
 		{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
+		{ "mktree", cmd_mktree, RUN_SETUP },
 		{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
 		{ "name-rev", cmd_name_rev, RUN_SETUP },
 		{ "pack-objects", cmd_pack_objects, RUN_SETUP },
Signature · 1 line
1.6.3.9.g6345d
#8 lore raw

PATCH 2/6 mktree: use parse-options

---
 builtin-mktree.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)
builtin-mktree.c +10 -10
index 3d05427..2b3145b 100644
@@ -6,6 +6,7 @@
 #include "builtin.h"
 #include "quote.h"
 #include "tree.h"
+#include "parse-options.h"
 
 static struct treeent {
 	unsigned mode;
@@ -61,7 +62,10 @@ 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]",
+	NULL
+};
 
 int cmd_mktree(int ac, const char **av, const char *prefix)
 {
@@ -69,16 +73,12 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
 	struct strbuf p_uq = STRBUF_INIT;
 	unsigned char sha1[20];
 	int line_termination = '\n';
+	const struct option option[] = {
+		OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
+		OPT_END()
+	};
 
-	while ((1 < ac) && av[1][0] == '-') {
-		const char *arg = av[1];
-		if (!strcmp("-z", arg))
-			line_termination = 0;
-		else
-			usage(mktree_usage);
-		ac--;
-		av++;
-	}
+	ac = parse_options(ac, av, option, mktree_usage, 0);
 
 	while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
 		char *ptr, *ntr;
Signature · 1 line
1.6.3.9.g6345d
#9 lore raw

PATCH 3/6 builtin-mktree.c: use a helper function to handle one line of input

The main() function used to do the whole thing; this moves the handling of a single input line to a separate function to make it easier to read.

---
 builtin-mktree.c |   82 +++++++++++++++++++++++++++--------------------------
 1 files changed, 42 insertions(+), 40 deletions(-)
builtin-mktree.c +42 -40
index 2b3145b..133ab4b 100644
@@ -67,10 +67,48 @@ static const char *mktree_usage[] = {
 	NULL
 };
 
+static void mktree_line(char *buf, size_t len, int line_termination)
+{
+	char *ptr, *ntr;
+	unsigned mode;
+	enum object_type type;
+	char *path;
+	unsigned char sha1[20];
+
+	ptr = buf;
+	/*
+	 * Read 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", buf);
+	ptr = ntr + 1; /* type */
+	ntr = strchr(ptr, ' ');
+	if (!ntr || buf + len <= ntr + 40 ||
+	    ntr[41] != '\t' ||
+	    get_sha1_hex(ntr + 1, sha1))
+		die("input format error: %s", 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] == '"') {
+		struct strbuf p_uq = STRBUF_INIT;
+		if (unquote_c_style(&p_uq, path, NULL))
+			die("invalid quoting");
+		path = strbuf_detach(&p_uq, NULL);
+	}
+	append_to_tree(mode, sha1, path);
+}
+
 int cmd_mktree(int ac, const char **av, const char *prefix)
 {
 	struct strbuf sb = STRBUF_INIT;
-	struct strbuf p_uq = STRBUF_INIT;
 	unsigned char sha1[20];
 	int line_termination = '\n';
 	const struct option option[] = {
@@ -80,45 +118,9 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
 
 	ac = parse_options(ac, av, option, mktree_usage, 0);
 
-	while (strbuf_getline(&sb, stdin, line_termination) != EOF) {
-		char *ptr, *ntr;
-		unsigned mode;
-		enum object_type type;
-		char *path;
-
-		ptr = sb.buf;
-		/*
-		 * Read 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;
-		}
-
-		append_to_tree(mode, sha1, path);
-	}
-	strbuf_release(&p_uq);
+	while (strbuf_getline(&sb, stdin, line_termination) != EOF)
+		mktree_line(sb.buf, sb.len, line_termination);
+
 	strbuf_release(&sb);
 
 	write_tree(sha1);
Signature · 1 line
1.6.3.9.g6345d
#10 lore raw

PATCH 4/6 mktree: do not barf on a submodule commit

It is perfectly normal if a tree entry points at a missing commit as long as the mode of the entry says it is a submodule.

---
 builtin-mktree.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)
builtin-mktree.c +8 -1
index 133ab4b..17cdb3d 100644
@@ -89,9 +89,16 @@ static void mktree_line(char *buf, size_t len, int line_termination)
 	    ntr[41] != '\t' ||
 	    get_sha1_hex(ntr + 1, sha1))
 		die("input format error: %s", buf);
-	type = sha1_object_info(sha1, NULL);
+
+	/* It is perfectly normal if we do not have a commit from a submodule */
+	if (!S_ISGITLINK(mode))
+		type = sha1_object_info(sha1, NULL);
+	else
+		type = OBJ_COMMIT;
+
 	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));
Signature · 1 line
1.6.3.9.g6345d
#11 lore raw

PATCH 5/6 t1010: add mktree test

So far mktree (which has always been a quick hack) had no test. At least give it a bit of test coverage.

---
 t/t1010-mktree.sh |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 0 deletions(-)
 create mode 100755 t/t1010-mktree.sh
t/t1010-mktree.sh new file +61 -0
new file mode 100755
index 0000000..4d9b138
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+test_description='git mktree'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	for d in a a. a0
+	do
+		mkdir "$d" && echo "$d/one" >"$d/one" &&
+		git add "$d"
+	done &&
+	echo one >one &&
+	git add one &&
+	git write-tree >tree &&
+	git ls-tree $(cat tree) >top &&
+	git ls-tree -r $(cat tree) >all &&
+	test_tick &&
+	git commit -q -m one &&
+	H=$(git rev-parse HEAD) &&
+	git update-index --add --cacheinfo 160000 $H sub &&
+	test_tick &&
+	git commit -q -m two &&
+	git rev-parse HEAD^{tree} >tree.withsub &&
+	git ls-tree HEAD >top.withsub &&
+	git ls-tree -r HEAD >all.withsub
+'
+
+test_expect_success 'ls-tree piped to mktree (1)' '
+	git mktree <top >actual &&
+	test_cmp tree actual
+'
+
+test_expect_success 'ls-tree piped to mktree (2)' '
+	git mktree <top.withsub >actual &&
+	test_cmp tree.withsub actual
+'
+
+test_expect_success 'ls-tree output in wrong order given to mktree (1)' '
+	perl -e "print reverse <>" <top |
+	git mktree >actual &&
+	test_cmp tree actual
+'
+
+test_expect_success 'ls-tree output in wrong order given to mktree (2)' '
+	perl -e "print reverse <>" <top.withsub |
+	git mktree >actual &&
+	test_cmp tree.withsub actual
+'
+
+test_expect_failure 'mktree reads ls-tree -r output (1)' '
+	git mktree <all >actual &&
+	test_cmp tree actual
+'
+
+test_expect_failure 'mktree reads ls-tree -r output (2)' '
+	git mktree <all.withsub >actual &&
+	test_cmp tree.withsub actual
+'
+
+test_done
Signature · 1 line
1.6.3.9.g6345d
#12 lore raw

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

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.

---
 builtin-mktree.c  |   11 ++++++++---
 t/t1010-mktree.sh |   10 ++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
builtin-mktree.c +8 -3
index 17cdb3d..c2fd0f2 100644
@@ -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);
 
t/t1010-mktree.sh +10 -0
index 4d9b138..9956e3a 100755
@@ -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
Signature · 1 line
1.6.3.9.g6345d
#13 lore raw

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

Quoted 9 lines · Junio C Hamano
builtin-mktree.c +1 -1
-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))

builtin-mktree.c static void mktree_line(char *buf, size_t len, int line_termination) · line 103

Quoted 10 lines
builtin-mktree.c +4 -1
+		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));

Don't you need this, above?

- if (!S_ISGITLINK(mode))
+ if (S_ISGITLINK(mode))

René

#14 lore raw

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

Quoted 23 lines · René Scharfe
Quoted 9 lines · Junio C Hamano
builtin-mktree.c +1 -1
-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))

builtin-mktree.c static void mktree_line(char *buf, size_t len, int line_termination) · line 103

Quoted 10 lines
builtin-mktree.c +4 -1
+		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));

Don't you need this, above?

Review

Quoted 2 lines
(quoted diff) +1 -1
-  	if (!S_ISGITLINK(mode))
+	if (S_ISGITLINK(mode))

Agh; you are of course right.

#15 lore raw

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

In reply to Junio C Hamano
Quoted 3 lines · 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 +++++++++++++++++++++++++----------------
Documentation/git-mktree.txt +10 -3
index af19f06..1aa24b5 100644
@@ -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
 ------
mktree.c +4 -26
index 137a095..2743aaa 100644
@@ -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);
}