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