Skip to content

Commit 128b0c0

Browse files
committed
Merge branch 'jc/mktree'
* jc/mktree: mktree: validate entry type in input mktree --batch: build more than one tree object mktree --missing: updated usage message and man page mktree --missing: allow missing objects t1010: add mktree test mktree: do not barf on a submodule commit builtin-mktree.c: use a helper function to handle one line of input mktree: use parse-options build-in git-mktree
2 parents 22cdab5 + 31c8221 commit 128b0c0

File tree

7 files changed

+280
-135
lines changed

7 files changed

+280
-135
lines changed

Documentation/git-mktree.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,32 @@ git-mktree - Build a tree-object from ls-tree formatted text
88

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

1313
DESCRIPTION
1414
-----------
15-
Reads standard input in non-recursive `ls-tree` output format,
16-
and creates a tree object. The object name of the tree object
15+
Reads standard input in non-recursive `ls-tree` output format, and creates
16+
a tree object. The order of the tree entries is normalised by mktree so
17+
pre-sorting the input is not required. The object name of the tree object
1718
built is written to the standard output.
1819

1920
OPTIONS
2021
-------
2122
-z::
2223
Read the NUL-terminated `ls-tree -z` output instead.
2324

25+
--missing::
26+
Allow missing objects. The default behaviour (without this option)
27+
is to verify that each tree entry's sha1 identifies an existing
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+
2437
Author
2538
------
2639
Written by Junio C Hamano <[email protected]>

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ PROGRAMS += git-index-pack$X
336336
PROGRAMS += git-merge-index$X
337337
PROGRAMS += git-merge-tree$X
338338
PROGRAMS += git-mktag$X
339-
PROGRAMS += git-mktree$X
340339
PROGRAMS += git-pack-redundant$X
341340
PROGRAMS += git-patch-id$X
342341
PROGRAMS += git-shell$X
@@ -590,6 +589,7 @@ BUILTIN_OBJS += builtin-merge-base.o
590589
BUILTIN_OBJS += builtin-merge-file.o
591590
BUILTIN_OBJS += builtin-merge-ours.o
592591
BUILTIN_OBJS += builtin-merge-recursive.o
592+
BUILTIN_OBJS += builtin-mktree.o
593593
BUILTIN_OBJS += builtin-mv.o
594594
BUILTIN_OBJS += builtin-name-rev.o
595595
BUILTIN_OBJS += builtin-pack-objects.o

builtin-mktree.c

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*
2+
* GIT - the stupid content tracker
3+
*
4+
* Copyright (c) Junio C Hamano, 2006, 2009
5+
*/
6+
#include "builtin.h"
7+
#include "quote.h"
8+
#include "tree.h"
9+
#include "parse-options.h"
10+
11+
static struct treeent {
12+
unsigned mode;
13+
unsigned char sha1[20];
14+
int len;
15+
char name[FLEX_ARRAY];
16+
} **entries;
17+
static int alloc, used;
18+
19+
static void append_to_tree(unsigned mode, unsigned char *sha1, char *path)
20+
{
21+
struct treeent *ent;
22+
int len = strlen(path);
23+
if (strchr(path, '/'))
24+
die("path %s contains slash", path);
25+
26+
if (alloc <= used) {
27+
alloc = alloc_nr(used);
28+
entries = xrealloc(entries, sizeof(*entries) * alloc);
29+
}
30+
ent = entries[used++] = xmalloc(sizeof(**entries) + len + 1);
31+
ent->mode = mode;
32+
ent->len = len;
33+
hashcpy(ent->sha1, sha1);
34+
memcpy(ent->name, path, len+1);
35+
}
36+
37+
static int ent_compare(const void *a_, const void *b_)
38+
{
39+
struct treeent *a = *(struct treeent **)a_;
40+
struct treeent *b = *(struct treeent **)b_;
41+
return base_name_compare(a->name, a->len, a->mode,
42+
b->name, b->len, b->mode);
43+
}
44+
45+
static void write_tree(unsigned char *sha1)
46+
{
47+
struct strbuf buf;
48+
size_t size;
49+
int i;
50+
51+
qsort(entries, used, sizeof(*entries), ent_compare);
52+
for (size = i = 0; i < used; i++)
53+
size += 32 + entries[i]->len;
54+
55+
strbuf_init(&buf, size);
56+
for (i = 0; i < used; i++) {
57+
struct treeent *ent = entries[i];
58+
strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
59+
strbuf_add(&buf, ent->sha1, 20);
60+
}
61+
62+
write_sha1_file(buf.buf, buf.len, tree_type, sha1);
63+
}
64+
65+
static const char *mktree_usage[] = {
66+
"git mktree [-z] [--missing] [--batch]",
67+
NULL
68+
};
69+
70+
static void mktree_line(char *buf, size_t len, int line_termination, int allow_missing)
71+
{
72+
char *ptr, *ntr;
73+
unsigned mode;
74+
enum object_type mode_type; /* object type derived from mode */
75+
enum object_type obj_type; /* object type derived from sha */
76+
char *path;
77+
unsigned char sha1[20];
78+
79+
ptr = buf;
80+
/*
81+
* Read non-recursive ls-tree output format:
82+
* mode SP type SP sha1 TAB name
83+
*/
84+
mode = strtoul(ptr, &ntr, 8);
85+
if (ptr == ntr || !ntr || *ntr != ' ')
86+
die("input format error: %s", buf);
87+
ptr = ntr + 1; /* type */
88+
ntr = strchr(ptr, ' ');
89+
if (!ntr || buf + len <= ntr + 40 ||
90+
ntr[41] != '\t' ||
91+
get_sha1_hex(ntr + 1, sha1))
92+
die("input format error: %s", buf);
93+
94+
/* It is perfectly normal if we do not have a commit from a submodule */
95+
if (S_ISGITLINK(mode))
96+
allow_missing = 1;
97+
98+
99+
*ntr++ = 0; /* now at the beginning of SHA1 */
100+
101+
path = ntr + 41; /* at the beginning of name */
102+
if (line_termination && path[0] == '"') {
103+
struct strbuf p_uq = STRBUF_INIT;
104+
if (unquote_c_style(&p_uq, path, NULL))
105+
die("invalid quoting");
106+
path = strbuf_detach(&p_uq, NULL);
107+
}
108+
109+
/*
110+
* Object type is redundantly derivable three ways.
111+
* These should all agree.
112+
*/
113+
mode_type = object_type(mode);
114+
if (mode_type != type_from_string(ptr)) {
115+
die("entry '%s' object type (%s) doesn't match mode type (%s)",
116+
path, ptr, typename(mode_type));
117+
}
118+
119+
/* Check the type of object identified by sha1 */
120+
obj_type = sha1_object_info(sha1, NULL);
121+
if (obj_type < 0) {
122+
if (allow_missing) {
123+
; /* no problem - missing objects are presumed to be of the right type */
124+
} else {
125+
die("entry '%s' object %s is unavailable", path, sha1_to_hex(sha1));
126+
}
127+
} else {
128+
if (obj_type != mode_type) {
129+
/*
130+
* The object exists but is of the wrong type.
131+
* This is a problem regardless of allow_missing
132+
* because the new tree entry will never be correct.
133+
*/
134+
die("entry '%s' object %s is a %s but specified type was (%s)",
135+
path, sha1_to_hex(sha1), typename(obj_type), typename(mode_type));
136+
}
137+
}
138+
139+
append_to_tree(mode, sha1, path);
140+
}
141+
142+
int cmd_mktree(int ac, const char **av, const char *prefix)
143+
{
144+
struct strbuf sb = STRBUF_INIT;
145+
unsigned char sha1[20];
146+
int line_termination = '\n';
147+
int allow_missing = 0;
148+
int is_batch_mode = 0;
149+
int got_eof = 0;
150+
151+
const struct option option[] = {
152+
OPT_SET_INT('z', NULL, &line_termination, "input is NUL terminated", '\0'),
153+
OPT_SET_INT( 0 , "missing", &allow_missing, "allow missing objects", 1),
154+
OPT_SET_INT( 0 , "batch", &is_batch_mode, "allow creation of more than one tree", 1),
155+
OPT_END()
156+
};
157+
158+
ac = parse_options(ac, av, option, mktree_usage, 0);
159+
160+
while (!got_eof) {
161+
while (1) {
162+
if (strbuf_getline(&sb, stdin, line_termination) == EOF) {
163+
got_eof = 1;
164+
break;
165+
}
166+
if (sb.buf[0] == '\0') {
167+
/* empty lines denote tree boundaries in batch mode */
168+
if (is_batch_mode)
169+
break;
170+
die("input format error: (blank line only valid in batch mode)");
171+
}
172+
mktree_line(sb.buf, sb.len, line_termination, allow_missing);
173+
}
174+
if (is_batch_mode && got_eof && used < 1) {
175+
/*
176+
* Execution gets here if the last tree entry is terminated with a
177+
* new-line. The final new-line has been made optional to be
178+
* consistent with the original non-batch behaviour of mktree.
179+
*/
180+
; /* skip creating an empty tree */
181+
} else {
182+
write_tree(sha1);
183+
puts(sha1_to_hex(sha1));
184+
fflush(stdout);
185+
}
186+
used=0; /* reset tree entry buffer for re-use in batch mode */
187+
}
188+
strbuf_release(&sb);
189+
exit(0);
190+
}

builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ extern int cmd_merge_base(int argc, const char **argv, const char *prefix);
7272
extern int cmd_merge_ours(int argc, const char **argv, const char *prefix);
7373
extern int cmd_merge_file(int argc, const char **argv, const char *prefix);
7474
extern int cmd_merge_recursive(int argc, const char **argv, const char *prefix);
75+
extern int cmd_mktree(int argc, const char **argv, const char *prefix);
7576
extern int cmd_mv(int argc, const char **argv, const char *prefix);
7677
extern int cmd_name_rev(int argc, const char **argv, const char *prefix);
7778
extern int cmd_pack_objects(int argc, const char **argv, const char *prefix);

git.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ static void handle_internal_command(int argc, const char **argv)
327327
{ "merge-ours", cmd_merge_ours, RUN_SETUP },
328328
{ "merge-recursive", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
329329
{ "merge-subtree", cmd_merge_recursive, RUN_SETUP | NEED_WORK_TREE },
330+
{ "mktree", cmd_mktree, RUN_SETUP },
330331
{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
331332
{ "name-rev", cmd_name_rev, RUN_SETUP },
332333
{ "pack-objects", cmd_pack_objects, RUN_SETUP },

mktree.c

Lines changed: 0 additions & 131 deletions
This file was deleted.

0 commit comments

Comments
 (0)