Proposed patch for mktree [0/3]

4 messages, 1 author, 2016-06-15 · open the first message on its own page

Proposed patch for mktree [0/3]

From: Josh Micich <hidden>
Date: 2016-06-15 22:46:46

Here is a series of 3 patches to git-mktree in addition to the recent work done 
on pu.

The first is just small fixes after the work on the '--missing' option.
The second patch adds a '--batch' option to mktree (similar to '--batch' in 
cat-file) that allows multiple tree objects to be created interactively by a 
single process.
Finally there are some improvements to the validation of the type of object 
identified by the tree entry sha1 (previously the sha1 was only used to check 
for existence, not type).

-josh


Josh Micich (3):
  --missing option for mktree: re-added strbuf_release(&p_uq), Updated man page
  added --batch option to mk-tree
  improved validation of entry type in mktree

 Documentation/git-mktree.txt |   16 ++++++++--
 builtin-mktree.c             |   63 +++++++++++++++++++++++++++++++----------
 2 files changed, 60 insertions(+), 19 deletions(-)

[PATCH 3/3] improved validation of entry type in mktree

From: Josh Micich <hidden>
Date: 2016-06-15 22:46:46

Previously mktree would accept tree entries which had a mismatch between the 
declared type and the actual type of object identified by the sha.

Signed-off-by: Josh Micich <redacted>
---
 builtin-mktree.c |   34 +++++++++++++++++++++-------------
 1 files changed, 21 insertions(+), 13 deletions(-)
diff --git a/builtin-mktree.c b/builtin-mktree.c
index a56c917..a37954a 100644
--- a/builtin-mktree.c
+++ b/builtin-mktree.c
@@ -71,7 +71,7 @@ static void mktree_line(char *buf, size_t len, int 
line_termination, int allow_m
 {
 	char *ptr, *ntr;
 	unsigned mode;
-	enum object_type type;
+	enum object_type mode_type;
 	char *path;
 	unsigned char sha1[20];
 
@@ -94,29 +94,37 @@ static void mktree_line(char *buf, size_t len, int 
line_termination, int allow_m
 	if (S_ISGITLINK(mode))
 		allow_missing = 1;
 
-	if (!allow_missing)
-		type = sha1_object_info(sha1, NULL);
-	else
-		type = object_type(mode);
-
-	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 */
+	struct strbuf p_uq = STRBUF_INIT;
 	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);
-		strbuf_release(&p_uq);
-		return;
 	}
+
+	mode_type = object_type(mode);
+	if (mode_type != type_from_string(ptr)) {
+		die("entry '%s' object type (%s) doesn't match mode type (%s)", 
path, ptr, typename(mode_type));
+	}
+
+	enum object_type obj_type = sha1_object_info(sha1, NULL);
+	if (obj_type < 0) {
+		if (!allow_missing) {
+			die("entry '%s' object %s is unavailable", path, 
sha1_to_hex(sha1));
+		}
+	} else {
+		if (obj_type != mode_type) {
+			die("entry '%s' object %s is a %s but specified type 
was (%s)",
+				path, sha1_to_hex(sha1), typename(obj_type), 
typename(mode_type));
+		}
+	}
+	
 	append_to_tree(mode, sha1, path);
+	strbuf_release(&p_uq); // safe to call on unused empty buffer
 }
 
 int cmd_mktree(int ac, const char **av, const char *prefix)
-- 
1.6.3.165.g2cce5.dirty

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

From: Josh Micich <hidden>
Date: 2016-06-15 22:46:46

This option enables creation of many tree objects with a single process

Signed-off-by: Josh Micich <redacted>
---
 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

[PATCH 1/3] '--missing' option for mktree: re-added strbuf_release(&p_uq), Updated man page

From: Josh Micich <hidden>
Date: 2016-06-15 22:46:46

Re-added call to strbuf_release(&p_uq) that got lost in earlier changes.
Updated mktree_usage msg.

Updated man page to explain new '--missing' option.
Also clarified sorting behaviour.

Signed-off-by: Josh Micich <redacted>
---
 Documentation/git-mktree.txt |   11 ++++++++---
 builtin-mktree.c             |    5 ++++-
 2 files changed, 12 insertions(+), 4 deletions(-)
diff --git a/Documentation/git-mktree.txt b/Documentation/git-mktree.txt
index af19f06..0461062 100644
--- a/Documentation/git-mktree.txt
+++ b/Documentation/git-mktree.txt
@@ -8,18 +8,23 @@ git-mktree - Build a tree-object from ls-tree formatted text
 
 SYNOPSIS
 --------
-'git mktree' [-z]
+'git mktree' [-z] [--missing]
 
 DESCRIPTION
 -----------
-Reads standard input in non-recursive `ls-tree` output format,
-and creates a tree object.  The object name of the tree object
+Reads standard input in non-recursive `ls-tree` output format, 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.
+--missing::
+	Allow missing objects.  The default behaviour (without this option)
+	is to verify that each tree entry's sha1 identifies an existing
+	object.
 
 Author
 ------
diff --git a/builtin-mktree.c b/builtin-mktree.c
index e1c9a27..db647ce 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]",
+	"git mktree [-z] [--missing]",
 	NULL
 };
 
@@ -112,6 +112,9 @@ static void mktree_line(char *buf, size_t len, int 
line_termination, int allow_m
 		if (unquote_c_style(&p_uq, path, NULL))
 			die("invalid quoting");
 		path = strbuf_detach(&p_uq, NULL);
+		append_to_tree(mode, sha1, path);
+		strbuf_release(&p_uq);
+		return;
 	}
 	append_to_tree(mode, sha1, path);
 }
-- 
1.6.3.165.g2cce5.dirty
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help