git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Josh Micich <josh.micich@gmail.com>
To: git@vger.kernel.org
Cc: Sverre Rabbelier <srabbelier@gmail.com>,
	Junio C Hamano <gitster@pobox.com>,
	Josh Micich <josh.micich@gmail.com>
Subject: Re: [PATCH 2/3] added --batch option to mktree
Date: Thu, 14 May 2009 12:51:15 -0700	[thread overview]
Message-ID: <a644352c0905141251n168508eexa8f6ebf4d84c65bb@mail.gmail.com> (raw)
In-Reply-To: <fabb9a1e0905140325g2f376cf1lcaf206278a942c47@mail.gmail.com>

This option works in a similar way to the '--batch' option of 'git cat-file'.
It enables creation of many tree objects with a single process.

The change was motivated by performance considerations in applications that
need to create many tree objects. A non-rigorous test showed tree creation
times improved from (roughly) 200ms to 50ms.

Signed-off-by: Josh Micich <josh.micich@gmail.com>
---
 Documentation/git-mktree.txt |    8 +++++++-
 builtin-mktree.c             |   40 +++++++++++++++++++++++++++++++++-------
 2 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/Documentation/git-mktree.txt b/Documentation/git-mktree.txt
index 7336f48..81e3326 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
 -----------
@@ -28,6 +28,12 @@ OPTIONS
 	object.  This option has no effect on the treatment of gitlink entries
 	(aka "submodules") which are always allowed to be missing.

+--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
 ------
 Written by Junio C Hamano <gitster@pobox.com>
diff --git a/builtin-mktree.c b/builtin-mktree.c
index 5ff0475..73b0abb 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
 };

@@ -122,20 +122,46 @@ 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;
+	int got_eof = 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, "allow creation of 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);
-
+	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) {
+			/*
+			 * Execution gets here if the last tree entry is terminated with a
+			 * new-line.  The final new-line has been made optional to be
+			 * consistent with the original non-batch behaviour of mktree.
+			 */
+			; /* skip creating an empty tree */
+		} 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.1.181.gfc9b3

  reply	other threads:[~2009-05-14 19:51 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-14  5:06 Proposed patch for mktree [0/3] Josh Micich
2009-05-14  5:09 ` [PATCH 1/3] '--missing' option for mktree: re-added strbuf_release(&p_uq), Updated man page Josh Micich
2009-05-14  6:04   ` Junio C Hamano
2009-05-14 19:44     ` Josh Micich
2009-05-14  5:10 ` [PATCH 2/3] added --batch option to mktree Josh Micich
2009-05-14  6:18   ` Junio C Hamano
2009-05-14  9:24     ` Josh Micich
2009-05-14 10:25       ` Sverre Rabbelier
2009-05-14 19:51         ` Josh Micich [this message]
     [not found]     ` <a644352c0905140217h382a4d18h988b229c12577de3@mail.gmail.com>
2009-05-15  8:31       ` Junio C Hamano
2009-05-14  5:11 ` [PATCH 3/3] improved validation of entry type in mktree Josh Micich
2009-05-14  6:24   ` Junio C Hamano
2009-05-14 22:46     ` Josh Micich
2009-05-14 22:49       ` Josh Micich
2009-05-15  6:23       ` Jakub Narebski
2009-05-15 16:57         ` Josh Micich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a644352c0905141251n168508eexa8f6ebf4d84c65bb@mail.gmail.com \
    --to=josh.micich@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=srabbelier@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).