git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* questions about git-mktree
@ 2009-05-10 13:41 Jon Seymour
  2009-05-10 15:12 ` Uwe Kleine-König
  2009-05-10 16:39 ` Junio C Hamano
  0 siblings, 2 replies; 16+ messages in thread
From: Jon Seymour @ 2009-05-10 13:41 UTC (permalink / raw)
  To: Uwe Kleine-König; +Cc: gitster, Johannes Sixt, Git Mailing List

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.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: questions about git-mktree
  2009-05-10 13:41 questions about git-mktree Jon Seymour
@ 2009-05-10 15:12 ` Uwe Kleine-König
  2009-05-10 15:27   ` A Large Angry SCM
  2009-05-10 15:31   ` Jakub Narebski
  2009-05-10 16:39 ` Junio C Hamano
  1 sibling, 2 replies; 16+ messages in thread
From: Uwe Kleine-König @ 2009-05-10 15:12 UTC (permalink / raw)
  To: Jon Seymour; +Cc: gitster, Johannes Sixt, Git Mailing List

Hello Jon,

On Sun, May 10, 2009 at 11:41:17PM +1000, Jon Seymour wrote:
> 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

-- 
Pengutronix e.K.                              | Uwe Kleine-König            |
Industrial Linux Solutions                    | http://www.pengutronix.de/  |

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: questions about git-mktree
  2009-05-10 15:12 ` Uwe Kleine-König
@ 2009-05-10 15:27   ` A Large Angry SCM
  2009-05-10 15:31   ` Jakub Narebski
  1 sibling, 0 replies; 16+ messages in thread
From: A Large Angry SCM @ 2009-05-10 15:27 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Jon Seymour, gitster, Johannes Sixt, Git Mailing List

Uwe Kleine-König wrote:

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

http://kernel.org/pub/software/scm/git/docs/git-mktree.html

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: questions about git-mktree
  2009-05-10 15:12 ` Uwe Kleine-König
  2009-05-10 15:27   ` A Large Angry SCM
@ 2009-05-10 15:31   ` Jakub Narebski
  1 sibling, 0 replies; 16+ messages in thread
From: Jakub Narebski @ 2009-05-10 15:31 UTC (permalink / raw)
  To: Uwe Kleine-König
  Cc: Jon Seymour, gitster, Johannes Sixt, Git Mailing List

Uwe Kleine-König    <u.kleine-koenig@pengutronix.de> writes:

> Hello Jon,
> 
> On Sun, May 10, 2009 at 11:41:17PM +1000, Jon Seymour wrote:
> > 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.

-- 
Jakub Narebski
Poland
ShadeHawk on #git

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: questions about git-mktree
  2009-05-10 13:41 questions about git-mktree Jon Seymour
  2009-05-10 15:12 ` Uwe Kleine-König
@ 2009-05-10 16:39 ` Junio C Hamano
  2009-05-10 17:10   ` Junio C Hamano
  1 sibling, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 16:39 UTC (permalink / raw)
  To: Jon Seymour
  Cc: Uwe Kleine-König, gitster, Johannes Sixt, Git Mailing List

Jon Seymour <jon.seymour@gmail.com> writes:

> 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.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: questions about git-mktree
  2009-05-10 16:39 ` Junio C Hamano
@ 2009-05-10 17:10   ` Junio C Hamano
  2009-05-10 18:49     ` [PATCH 0/6] Modernize mktree somewhat Junio C Hamano
  2009-05-10 18:54     ` questions about git-mktree - [PATCH] proposed '--batch' option Josh Micich
  0 siblings, 2 replies; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 17:10 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Jon Seymour, Uwe Kleine-König, Johannes Sixt, Git Mailing List

Junio C Hamano <gitster@pobox.com> writes:

> Jon Seymour <jon.seymour@gmail.com> writes:
>
>> 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.

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

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 0/6] Modernize mktree somewhat
  2009-05-10 17:10   ` Junio C Hamano
@ 2009-05-10 18:49     ` Junio C Hamano
  2009-05-10 18:49       ` [PATCH 1/6] build-in git-mktree Junio C Hamano
  2009-05-10 18:54     ` questions about git-mktree - [PATCH] proposed '--batch' option Josh Micich
  1 sibling, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 18:49 UTC (permalink / raw)
  To: git; +Cc: Jon Seymour

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

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 1/6] build-in git-mktree
  2009-05-10 18:49     ` [PATCH 0/6] Modernize mktree somewhat Junio C Hamano
@ 2009-05-10 18:49       ` Junio C Hamano
  2009-05-10 18:49         ` [PATCH 2/6] mktree: use parse-options Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 18:49 UTC (permalink / raw)
  To: git

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 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%)

diff --git a/Makefile b/Makefile
index 6e21643..9d9f0da 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/mktree.c b/builtin-mktree.c
similarity index 91%
rename from mktree.c
rename to builtin-mktree.c
index 137a095..3d05427 100644
--- a/mktree.c
+++ b/builtin-mktree.c
@@ -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 != ' ')
diff --git a/builtin.h b/builtin.h
index 425ff8e..20427d2 100644
--- a/builtin.h
+++ b/builtin.h
@@ -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);
diff --git a/git.c b/git.c
index 5a00726..7d7f949 100644
--- a/git.c
+++ b/git.c
@@ -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 },
-- 
1.6.3.9.g6345d

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 2/6] mktree: use parse-options
  2009-05-10 18:49       ` [PATCH 1/6] build-in git-mktree Junio C Hamano
@ 2009-05-10 18:49         ` Junio C Hamano
  2009-05-10 18:49           ` [PATCH 3/6] builtin-mktree.c: use a helper function to handle one line of input Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 18:49 UTC (permalink / raw)
  To: git

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-mktree.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/builtin-mktree.c b/builtin-mktree.c
index 3d05427..2b3145b 100644
--- a/builtin-mktree.c
+++ b/builtin-mktree.c
@@ -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;
-- 
1.6.3.9.g6345d

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 3/6] builtin-mktree.c: use a helper function to handle one line of input
  2009-05-10 18:49         ` [PATCH 2/6] mktree: use parse-options Junio C Hamano
@ 2009-05-10 18:49           ` Junio C Hamano
  2009-05-10 18:49             ` [PATCH 4/6] mktree: do not barf on a submodule commit Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 18:49 UTC (permalink / raw)
  To: git

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.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-mktree.c |   82 +++++++++++++++++++++++++++--------------------------
 1 files changed, 42 insertions(+), 40 deletions(-)

diff --git a/builtin-mktree.c b/builtin-mktree.c
index 2b3145b..133ab4b 100644
--- a/builtin-mktree.c
+++ b/builtin-mktree.c
@@ -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);
-- 
1.6.3.9.g6345d

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 4/6] mktree: do not barf on a submodule commit
  2009-05-10 18:49           ` [PATCH 3/6] builtin-mktree.c: use a helper function to handle one line of input Junio C Hamano
@ 2009-05-10 18:49             ` Junio C Hamano
  2009-05-10 18:49               ` [PATCH 5/6] t1010: add mktree test Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 18:49 UTC (permalink / raw)
  To: git

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.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin-mktree.c |    9 ++++++++-
 1 files changed, 8 insertions(+), 1 deletions(-)

diff --git a/builtin-mktree.c b/builtin-mktree.c
index 133ab4b..17cdb3d 100644
--- a/builtin-mktree.c
+++ b/builtin-mktree.c
@@ -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));
-- 
1.6.3.9.g6345d

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 5/6] t1010: add mktree test
  2009-05-10 18:49             ` [PATCH 4/6] mktree: do not barf on a submodule commit Junio C Hamano
@ 2009-05-10 18:49               ` Junio C Hamano
  2009-05-10 18:49                 ` [PATCH 6/6] mktree --missing: allow missing objects Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 18:49 UTC (permalink / raw)
  To: git

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

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 t/t1010-mktree.sh |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 61 insertions(+), 0 deletions(-)
 create mode 100755 t/t1010-mktree.sh

diff --git a/t/t1010-mktree.sh b/t/t1010-mktree.sh
new file mode 100755
index 0000000..4d9b138
--- /dev/null
+++ b/t/t1010-mktree.sh
@@ -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
-- 
1.6.3.9.g6345d

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 6/6] mktree --missing: allow missing objects
  2009-05-10 18:49               ` [PATCH 5/6] t1010: add mktree test Junio C Hamano
@ 2009-05-10 18:49                 ` Junio C Hamano
  2009-05-10 22:13                   ` René Scharfe
  0 siblings, 1 reply; 16+ messages in thread
From: Junio C Hamano @ 2009-05-10 18:49 UTC (permalink / raw)
  To: git

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

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: questions about git-mktree - [PATCH] proposed '--batch' option
  2009-05-10 17:10   ` Junio C Hamano
  2009-05-10 18:49     ` [PATCH 0/6] Modernize mktree somewhat Junio C Hamano
@ 2009-05-10 18:54     ` Josh Micich
  1 sibling, 0 replies; 16+ messages in thread
From: Josh Micich @ 2009-05-10 18:54 UTC (permalink / raw)
  To: git

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

^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH 6/6] mktree --missing: allow missing objects
  2009-05-10 18:49                 ` [PATCH 6/6] mktree --missing: allow missing objects Junio C Hamano
@ 2009-05-10 22:13                   ` René Scharfe
  2009-05-11  0:29                     ` Junio C Hamano
  0 siblings, 1 reply; 16+ messages in thread
From: René Scharfe @ 2009-05-10 22:13 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

Junio C Hamano schrieb:
> -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));

Don't you need this, above?

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

René

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 6/6] mktree --missing: allow missing objects
  2009-05-10 22:13                   ` René Scharfe
@ 2009-05-11  0:29                     ` Junio C Hamano
  0 siblings, 0 replies; 16+ messages in thread
From: Junio C Hamano @ 2009-05-11  0:29 UTC (permalink / raw)
  To: René Scharfe; +Cc: Junio C Hamano, git

René Scharfe <rene.scharfe@lsrfire.ath.cx> writes:

> Junio C Hamano schrieb:
>> -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));
>
> Don't you need this, above?
>
> -  	if (!S_ISGITLINK(mode))
> +	if (S_ISGITLINK(mode))

Agh; you are of course right.

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2009-05-11  0:30 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-05-10 13:41 questions about git-mktree Jon Seymour
2009-05-10 15:12 ` Uwe Kleine-König
2009-05-10 15:27   ` A Large Angry SCM
2009-05-10 15:31   ` Jakub Narebski
2009-05-10 16:39 ` Junio C Hamano
2009-05-10 17:10   ` Junio C Hamano
2009-05-10 18:49     ` [PATCH 0/6] Modernize mktree somewhat Junio C Hamano
2009-05-10 18:49       ` [PATCH 1/6] build-in git-mktree Junio C Hamano
2009-05-10 18:49         ` [PATCH 2/6] mktree: use parse-options Junio C Hamano
2009-05-10 18:49           ` [PATCH 3/6] builtin-mktree.c: use a helper function to handle one line of input Junio C Hamano
2009-05-10 18:49             ` [PATCH 4/6] mktree: do not barf on a submodule commit Junio C Hamano
2009-05-10 18:49               ` [PATCH 5/6] t1010: add mktree test Junio C Hamano
2009-05-10 18:49                 ` [PATCH 6/6] mktree --missing: allow missing objects Junio C Hamano
2009-05-10 22:13                   ` René Scharfe
2009-05-11  0:29                     ` Junio C Hamano
2009-05-10 18:54     ` questions about git-mktree - [PATCH] proposed '--batch' option Josh Micich

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).