Skip to content

Commit f1cf2d8

Browse files
micichgitster
authored andcommitted
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>
1 parent e01662b commit f1cf2d8

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

Documentation/git-mktree.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-mktree - Build a tree-object from ls-tree formatted text
88

99
SYNOPSIS
1010
--------
11-
'git mktree' [-z] [--missing]
11+
'git mktree' [-z] [--missing] [--batch]
1212

1313
DESCRIPTION
1414
-----------
@@ -28,6 +28,12 @@ OPTIONS
2828
object. This option has no effect on the treatment of gitlink entries
2929
(aka "submodules") which are always allowed to be missing.
3030

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+
3137
Author
3238
------
3339
Written by Junio C Hamano <gitster@pobox.com>

builtin-mktree.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static void write_tree(unsigned char *sha1)
6363
}
6464

6565
static const char *mktree_usage[] = {
66-
"git mktree [-z] [--missing]",
66+
"git mktree [-z] [--missing] [--batch]",
6767
NULL
6868
};
6969

@@ -122,20 +122,46 @@ int cmd_mktree(int ac, const char **av, const char *prefix)
122122
unsigned char sha1[20];
123123
int line_termination = '\n';
124124
int allow_missing = 0;
125+
int is_batch_mode = 0;
126+
int got_eof = 0;
127+
125128
const struct option option[] = {
126129
OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
127130
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),
128132
OPT_END()
129133
};
130134

131135
ac = parse_options(ac, av, option, mktree_usage, 0);
132136

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+
}
136165
strbuf_release(&sb);
137-
138-
write_tree(sha1);
139-
puts(sha1_to_hex(sha1));
140166
exit(0);
141167
}

0 commit comments

Comments
 (0)