]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/mktree.c
treewide: remove unnecessary cache.h inclusion from a few headers
[thirdparty/git.git] / builtin / mktree.c
CommitLineData
83f50539
JH
1/*
2 * GIT - the stupid content tracker
3 *
633e3556 4 * Copyright (c) Junio C Hamano, 2006, 2009
83f50539 5 */
633e3556 6#include "builtin.h"
36bf1958 7#include "alloc.h"
41771fa4 8#include "hex.h"
83f50539 9#include "quote.h"
8e440259 10#include "tree.h"
1fdee85c 11#include "parse-options.h"
cbd53a21 12#include "object-store.h"
83f50539
JH
13
14static struct treeent {
15 unsigned mode;
83eb0802 16 struct object_id oid;
83f50539
JH
17 int len;
18 char name[FLEX_ARRAY];
19} **entries;
20static int alloc, used;
21
83eb0802 22static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
83f50539
JH
23{
24 struct treeent *ent;
96ffc06f 25 size_t len = strlen(path);
83f50539
JH
26 if (strchr(path, '/'))
27 die("path %s contains slash", path);
28
96ffc06f 29 FLEX_ALLOC_MEM(ent, name, path, len);
83f50539
JH
30 ent->mode = mode;
31 ent->len = len;
83eb0802 32 oidcpy(&ent->oid, oid);
96ffc06f
JK
33
34 ALLOC_GROW(entries, used + 1, alloc);
35 entries[used++] = ent;
83f50539
JH
36}
37
38static int ent_compare(const void *a_, const void *b_)
39{
40 struct treeent *a = *(struct treeent **)a_;
41 struct treeent *b = *(struct treeent **)b_;
42 return base_name_compare(a->name, a->len, a->mode,
43 b->name, b->len, b->mode);
44}
45
a09c985e 46static void write_tree(struct object_id *oid)
83f50539 47{
d52bc661
PH
48 struct strbuf buf;
49 size_t size;
83f50539
JH
50 int i;
51
9ed0d8d6 52 QSORT(entries, used, ent_compare);
83f50539
JH
53 for (size = i = 0; i < used; i++)
54 size += 32 + entries[i]->len;
83f50539 55
f1696ee3 56 strbuf_init(&buf, size);
83f50539
JH
57 for (i = 0; i < used; i++) {
58 struct treeent *ent = entries[i];
d52bc661 59 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
83eb0802 60 strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
83f50539 61 }
d52bc661 62
c80d226a 63 write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
c444c165 64 strbuf_release(&buf);
83f50539
JH
65}
66
1fdee85c 67static const char *mktree_usage[] = {
959d670d 68 "git mktree [-z] [--missing] [--batch]",
1fdee85c
JH
69 NULL
70};
83f50539 71
be27fb7b 72static void mktree_line(char *buf, int nul_term_line, int allow_missing)
fe0bb5f7
JH
73{
74 char *ptr, *ntr;
83eb0802 75 const char *p;
fe0bb5f7 76 unsigned mode;
31c8221a
JM
77 enum object_type mode_type; /* object type derived from mode */
78 enum object_type obj_type; /* object type derived from sha */
817b0f60 79 struct object_info oi = OBJECT_INFO_INIT;
43e61e71 80 char *path, *to_free = NULL;
83eb0802 81 struct object_id oid;
fe0bb5f7
JH
82
83 ptr = buf;
84 /*
85 * Read non-recursive ls-tree output format:
86 * mode SP type SP sha1 TAB name
87 */
88 mode = strtoul(ptr, &ntr, 8);
89 if (ptr == ntr || !ntr || *ntr != ' ')
90 die("input format error: %s", buf);
91 ptr = ntr + 1; /* type */
92 ntr = strchr(ptr, ' ');
83eb0802 93 if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
94 *p != '\t')
fe0bb5f7 95 die("input format error: %s", buf);
ad87b5dd
JH
96
97 /* It is perfectly normal if we do not have a commit from a submodule */
1c64e79a
JH
98 if (S_ISGITLINK(mode))
99 allow_missing = 1;
100
ad87b5dd 101
fe0bb5f7 102 *ntr++ = 0; /* now at the beginning of SHA1 */
fe0bb5f7 103
58ce21b8 104 path = (char *)p + 1; /* at the beginning of name */
b4df87b8 105 if (!nul_term_line && path[0] == '"') {
fe0bb5f7
JH
106 struct strbuf p_uq = STRBUF_INIT;
107 if (unquote_c_style(&p_uq, path, NULL))
108 die("invalid quoting");
43e61e71 109 path = to_free = strbuf_detach(&p_uq, NULL);
fe0bb5f7 110 }
31c8221a
JM
111
112 /*
113 * Object type is redundantly derivable three ways.
114 * These should all agree.
115 */
116 mode_type = object_type(mode);
117 if (mode_type != type_from_string(ptr)) {
118 die("entry '%s' object type (%s) doesn't match mode type (%s)",
debca9d2 119 path, ptr, type_name(mode_type));
31c8221a
JM
120 }
121
817b0f60
RO
122 /* Check the type of object identified by oid without fetching objects */
123 oi.typep = &obj_type;
124 if (oid_object_info_extended(the_repository, &oid, &oi,
125 OBJECT_INFO_LOOKUP_REPLACE |
126 OBJECT_INFO_QUICK |
127 OBJECT_INFO_SKIP_FETCH_OBJECT) < 0)
128 obj_type = -1;
129
31c8221a
JM
130 if (obj_type < 0) {
131 if (allow_missing) {
132 ; /* no problem - missing objects are presumed to be of the right type */
133 } else {
83eb0802 134 die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
31c8221a
JM
135 }
136 } else {
137 if (obj_type != mode_type) {
138 /*
139 * The object exists but is of the wrong type.
140 * This is a problem regardless of allow_missing
141 * because the new tree entry will never be correct.
142 */
143 die("entry '%s' object %s is a %s but specified type was (%s)",
83eb0802 144 path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
31c8221a
JM
145 }
146 }
147
83eb0802 148 append_to_tree(mode, &oid, path);
43e61e71 149 free(to_free);
fe0bb5f7
JH
150}
151
633e3556 152int cmd_mktree(int ac, const char **av, const char *prefix)
83f50539 153{
f285a2d7 154 struct strbuf sb = STRBUF_INIT;
a09c985e 155 struct object_id oid;
b4df87b8 156 int nul_term_line = 0;
1c64e79a 157 int allow_missing = 0;
f1cf2d8b
JM
158 int is_batch_mode = 0;
159 int got_eof = 0;
b4df87b8 160 strbuf_getline_fn getline_fn;
f1cf2d8b 161
1fdee85c 162 const struct option option[] = {
b4df87b8 163 OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
a6312810
NTND
164 OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
165 OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
1fdee85c
JH
166 OPT_END()
167 };
83f50539 168
37782920 169 ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
b4df87b8 170 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
83f50539 171
f1cf2d8b
JM
172 while (!got_eof) {
173 while (1) {
b4df87b8 174 if (getline_fn(&sb, stdin) == EOF) {
f1cf2d8b
JM
175 got_eof = 1;
176 break;
177 }
178 if (sb.buf[0] == '\0') {
179 /* empty lines denote tree boundaries in batch mode */
180 if (is_batch_mode)
181 break;
182 die("input format error: (blank line only valid in batch mode)");
183 }
be27fb7b 184 mktree_line(sb.buf, nul_term_line, allow_missing);
f1cf2d8b
JM
185 }
186 if (is_batch_mode && got_eof && used < 1) {
187 /*
188 * Execution gets here if the last tree entry is terminated with a
189 * new-line. The final new-line has been made optional to be
190 * consistent with the original non-batch behaviour of mktree.
191 */
192 ; /* skip creating an empty tree */
193 } else {
a09c985e
PO
194 write_tree(&oid);
195 puts(oid_to_hex(&oid));
f1cf2d8b
JM
196 fflush(stdout);
197 }
198 used=0; /* reset tree entry buffer for re-use in batch mode */
199 }
e6c019d0 200 strbuf_release(&sb);
338abb0f 201 return 0;
83f50539 202}