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