@matheusbsbs / matheus-bagliano / commits / f1cf2d8b14
Fork 0

mktree --batch: build more than one tree object

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

Josh Micich committed May 14, 2009 at 12:51 UTC f1cf2d8b1467b5973f249703b31e709eaede97ad
2 files changed +40 -8
Documentation/git-mktree.txt
+7 -1
@@ -8,7 +8,7 @@ git-mktree - Build a tree-object from ls-tree formatted text
8
9 SYNOPSIS
10 --------
11 -'git mktree' [-z] [--missing]
11 +'git mktree' [-z] [--missing] [--batch]
12
13 DESCRIPTION
14 -----------
@@ -28,6 +28,12 @@ OPTIONS
28 object. This option has no effect on the treatment of gitlink entries
29 (aka "submodules") which are always allowed to be missing.
30
31 +--batch::
32 + Allow building of more than one tree object before exiting. Each
33 + tree is separated by as single blank line. The final new-line is
34 + optional. Note - if the '-z' option is used, lines are terminated
35 + with NUL.
36 +
37 Author
38 ------
39 Written by Junio C Hamano <gitster@pobox.com>
builtin-mktree.c
+33 -7
@@ -63,7 +63,7 @@ static void write_tree(unsigned char *sha1)
63 }
64
65 static const char *mktree_usage[] = {
66 - "git mktree [-z] [--missing]",
66 + "git mktree [-z] [--missing] [--batch]",
67 NULL
68 };
69
@@ -122,20 +122,46 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
122 unsigned char sha1[20];
123 int line_termination = '\n';
124 int allow_missing = 0;
125 + int is_batch_mode = 0;
126 + int got_eof = 0;
127 +
128 const struct option option[] = {
129 OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
130 OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing objects", 1),
131 + OPT_SET_INT( 0 , "batch", &is_batch_mode, "allow creation of more than one tree", 1),
132 OPT_END()
133 };
134
135 ac = parse_options(ac, av, option, mktree_usage, 0);
136
133 - while (strbuf_getline(&sb, stdin, line_termination) != EOF)
134 - mktree_line(sb.buf, sb.len, line_termination, allow_missing);
135 -
137 + while (!got_eof) {
138 + while (1) {
139 + if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
140 + got_eof = 1;
141 + break;
142 + }
143 + if (sb.buf[0] == '\0') {
144 + /* empty lines denote tree boundaries in batch mode */
145 + if (is_batch_mode)
146 + break;
147 + die("input format error: (blank line only valid in batch mode)");
148 + }
149 + mktree_line(sb.buf, sb.len, line_termination, allow_missing);
150 + }
151 + if (is_batch_mode && got_eof && used < 1) {
152 + /*
153 + * Execution gets here if the last tree entry is terminated with a
154 + * new-line. The final new-line has been made optional to be
155 + * consistent with the original non-batch behaviour of mktree.
156 + */
157 + ; /* skip creating an empty tree */
158 + } else {
159 + write_tree(sha1);
160 + puts(sha1_to_hex(sha1));
161 + fflush(stdout);
162 + }
163 + used=0; /* reset tree entry buffer for re-use in batch mode */
164 + }
165 strbuf_release(&sb);
137 -
138 - write_tree(sha1);
139 - puts(sha1_to_hex(sha1));
166 exit(0);
167 }