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