Start the 2.46 cycle
[git.git] / builtin / mktree.c
blob9a22d4e27735d55265fbf1a16cec8aeb66c27883
1 /*
2 * GIT - the stupid content tracker
4 * Copyright (c) Junio C Hamano, 2006, 2009
5 */
6 #include "builtin.h"
7 #include "gettext.h"
8 #include "hex.h"
9 #include "quote.h"
10 #include "strbuf.h"
11 #include "tree.h"
12 #include "parse-options.h"
13 #include "object-store-ll.h"
15 static struct treeent {
16 unsigned mode;
17 struct object_id oid;
18 int len;
19 char name[FLEX_ARRAY];
20 } **entries;
21 static int alloc, used;
23 static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
25 struct treeent *ent;
26 size_t len = strlen(path);
27 if (strchr(path, '/'))
28 die("path %s contains slash", path);
30 FLEX_ALLOC_MEM(ent, name, path, len);
31 ent->mode = mode;
32 ent->len = len;
33 oidcpy(&ent->oid, oid);
35 ALLOC_GROW(entries, used + 1, alloc);
36 entries[used++] = ent;
39 static int ent_compare(const void *a_, const void *b_)
41 struct treeent *a = *(struct treeent **)a_;
42 struct treeent *b = *(struct treeent **)b_;
43 return base_name_compare(a->name, a->len, a->mode,
44 b->name, b->len, b->mode);
47 static void write_tree(struct object_id *oid)
49 struct strbuf buf;
50 size_t size;
51 int i;
53 QSORT(entries, used, ent_compare);
54 for (size = i = 0; i < used; i++)
55 size += 32 + entries[i]->len;
57 strbuf_init(&buf, size);
58 for (i = 0; i < used; i++) {
59 struct treeent *ent = entries[i];
60 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
61 strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
64 write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
65 strbuf_release(&buf);
68 static const char *mktree_usage[] = {
69 "git mktree [-z] [--missing] [--batch]",
70 NULL
73 static void mktree_line(char *buf, int nul_term_line, int allow_missing)
75 char *ptr, *ntr;
76 const char *p;
77 unsigned mode;
78 enum object_type mode_type; /* object type derived from mode */
79 enum object_type obj_type; /* object type derived from sha */
80 struct object_info oi = OBJECT_INFO_INIT;
81 char *path, *to_free = NULL;
82 struct object_id oid;
84 ptr = buf;
86 * Read non-recursive ls-tree output format:
87 * mode SP type SP sha1 TAB name
89 mode = strtoul(ptr, &ntr, 8);
90 if (ptr == ntr || !ntr || *ntr != ' ')
91 die("input format error: %s", buf);
92 ptr = ntr + 1; /* type */
93 ntr = strchr(ptr, ' ');
94 if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
95 *p != '\t')
96 die("input format error: %s", buf);
98 /* It is perfectly normal if we do not have a commit from a submodule */
99 if (S_ISGITLINK(mode))
100 allow_missing = 1;
103 *ntr++ = 0; /* now at the beginning of SHA1 */
105 path = (char *)p + 1; /* at the beginning of name */
106 if (!nul_term_line && path[0] == '"') {
107 struct strbuf p_uq = STRBUF_INIT;
108 if (unquote_c_style(&p_uq, path, NULL))
109 die("invalid quoting");
110 path = to_free = strbuf_detach(&p_uq, NULL);
114 * Object type is redundantly derivable three ways.
115 * These should all agree.
117 mode_type = object_type(mode);
118 if (mode_type != type_from_string(ptr)) {
119 die("entry '%s' object type (%s) doesn't match mode type (%s)",
120 path, ptr, type_name(mode_type));
123 /* Check the type of object identified by oid without fetching objects */
124 oi.typep = &obj_type;
125 if (oid_object_info_extended(the_repository, &oid, &oi,
126 OBJECT_INFO_LOOKUP_REPLACE |
127 OBJECT_INFO_QUICK |
128 OBJECT_INFO_SKIP_FETCH_OBJECT) < 0)
129 obj_type = -1;
131 if (obj_type < 0) {
132 if (allow_missing) {
133 ; /* no problem - missing objects are presumed to be of the right type */
134 } else {
135 die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
137 } else {
138 if (obj_type != mode_type) {
140 * The object exists but is of the wrong type.
141 * This is a problem regardless of allow_missing
142 * because the new tree entry will never be correct.
144 die("entry '%s' object %s is a %s but specified type was (%s)",
145 path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
149 append_to_tree(mode, &oid, path);
150 free(to_free);
153 int cmd_mktree(int ac, const char **av, const char *prefix)
155 struct strbuf sb = STRBUF_INIT;
156 struct object_id oid;
157 int nul_term_line = 0;
158 int allow_missing = 0;
159 int is_batch_mode = 0;
160 int got_eof = 0;
161 strbuf_getline_fn getline_fn;
163 const struct option option[] = {
164 OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
165 OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
166 OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
167 OPT_END()
170 ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
171 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
173 while (!got_eof) {
174 while (1) {
175 if (getline_fn(&sb, stdin) == EOF) {
176 got_eof = 1;
177 break;
179 if (sb.buf[0] == '\0') {
180 /* empty lines denote tree boundaries in batch mode */
181 if (is_batch_mode)
182 break;
183 die("input format error: (blank line only valid in batch mode)");
185 mktree_line(sb.buf, nul_term_line, allow_missing);
187 if (is_batch_mode && got_eof && used < 1) {
189 * Execution gets here if the last tree entry is terminated with a
190 * new-line. The final new-line has been made optional to be
191 * consistent with the original non-batch behaviour of mktree.
193 ; /* skip creating an empty tree */
194 } else {
195 write_tree(&oid);
196 puts(oid_to_hex(&oid));
197 fflush(stdout);
199 used=0; /* reset tree entry buffer for re-use in batch mode */
201 strbuf_release(&sb);
202 return 0;