hash.h: move some oid-related declarations from cache.h
[git/debian.git] / builtin / mktree.c
blobec721ffb947fc584f02188c4fe813ef8e3a57131
1 /*
2 * GIT - the stupid content tracker
4 * Copyright (c) Junio C Hamano, 2006, 2009
5 */
6 #include "builtin.h"
7 #include "alloc.h"
8 #include "quote.h"
9 #include "tree.h"
10 #include "parse-options.h"
11 #include "object-store.h"
13 static struct treeent {
14 unsigned mode;
15 struct object_id oid;
16 int len;
17 char name[FLEX_ARRAY];
18 } **entries;
19 static int alloc, used;
21 static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
23 struct treeent *ent;
24 size_t len = strlen(path);
25 if (strchr(path, '/'))
26 die("path %s contains slash", path);
28 FLEX_ALLOC_MEM(ent, name, path, len);
29 ent->mode = mode;
30 ent->len = len;
31 oidcpy(&ent->oid, oid);
33 ALLOC_GROW(entries, used + 1, alloc);
34 entries[used++] = ent;
37 static int ent_compare(const void *a_, const void *b_)
39 struct treeent *a = *(struct treeent **)a_;
40 struct treeent *b = *(struct treeent **)b_;
41 return base_name_compare(a->name, a->len, a->mode,
42 b->name, b->len, b->mode);
45 static void write_tree(struct object_id *oid)
47 struct strbuf buf;
48 size_t size;
49 int i;
51 QSORT(entries, used, ent_compare);
52 for (size = i = 0; i < used; i++)
53 size += 32 + entries[i]->len;
55 strbuf_init(&buf, size);
56 for (i = 0; i < used; i++) {
57 struct treeent *ent = entries[i];
58 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
59 strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
62 write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
63 strbuf_release(&buf);
66 static const char *mktree_usage[] = {
67 "git mktree [-z] [--missing] [--batch]",
68 NULL
71 static void mktree_line(char *buf, int nul_term_line, int allow_missing)
73 char *ptr, *ntr;
74 const char *p;
75 unsigned mode;
76 enum object_type mode_type; /* object type derived from mode */
77 enum object_type obj_type; /* object type derived from sha */
78 struct object_info oi = OBJECT_INFO_INIT;
79 char *path, *to_free = NULL;
80 struct object_id oid;
82 ptr = buf;
84 * Read non-recursive ls-tree output format:
85 * mode SP type SP sha1 TAB name
87 mode = strtoul(ptr, &ntr, 8);
88 if (ptr == ntr || !ntr || *ntr != ' ')
89 die("input format error: %s", buf);
90 ptr = ntr + 1; /* type */
91 ntr = strchr(ptr, ' ');
92 if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
93 *p != '\t')
94 die("input format error: %s", buf);
96 /* It is perfectly normal if we do not have a commit from a submodule */
97 if (S_ISGITLINK(mode))
98 allow_missing = 1;
101 *ntr++ = 0; /* now at the beginning of SHA1 */
103 path = (char *)p + 1; /* at the beginning of name */
104 if (!nul_term_line && path[0] == '"') {
105 struct strbuf p_uq = STRBUF_INIT;
106 if (unquote_c_style(&p_uq, path, NULL))
107 die("invalid quoting");
108 path = to_free = strbuf_detach(&p_uq, NULL);
112 * Object type is redundantly derivable three ways.
113 * These should all agree.
115 mode_type = object_type(mode);
116 if (mode_type != type_from_string(ptr)) {
117 die("entry '%s' object type (%s) doesn't match mode type (%s)",
118 path, ptr, type_name(mode_type));
121 /* Check the type of object identified by oid without fetching objects */
122 oi.typep = &obj_type;
123 if (oid_object_info_extended(the_repository, &oid, &oi,
124 OBJECT_INFO_LOOKUP_REPLACE |
125 OBJECT_INFO_QUICK |
126 OBJECT_INFO_SKIP_FETCH_OBJECT) < 0)
127 obj_type = -1;
129 if (obj_type < 0) {
130 if (allow_missing) {
131 ; /* no problem - missing objects are presumed to be of the right type */
132 } else {
133 die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
135 } else {
136 if (obj_type != mode_type) {
138 * The object exists but is of the wrong type.
139 * This is a problem regardless of allow_missing
140 * because the new tree entry will never be correct.
142 die("entry '%s' object %s is a %s but specified type was (%s)",
143 path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
147 append_to_tree(mode, &oid, path);
148 free(to_free);
151 int cmd_mktree(int ac, const char **av, const char *prefix)
153 struct strbuf sb = STRBUF_INIT;
154 struct object_id oid;
155 int nul_term_line = 0;
156 int allow_missing = 0;
157 int is_batch_mode = 0;
158 int got_eof = 0;
159 strbuf_getline_fn getline_fn;
161 const struct option option[] = {
162 OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
163 OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1),
164 OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1),
165 OPT_END()
168 ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
169 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
171 while (!got_eof) {
172 while (1) {
173 if (getline_fn(&sb, stdin) == EOF) {
174 got_eof = 1;
175 break;
177 if (sb.buf[0] == '\0') {
178 /* empty lines denote tree boundaries in batch mode */
179 if (is_batch_mode)
180 break;
181 die("input format error: (blank line only valid in batch mode)");
183 mktree_line(sb.buf, nul_term_line, allow_missing);
185 if (is_batch_mode && got_eof && used < 1) {
187 * Execution gets here if the last tree entry is terminated with a
188 * new-line. The final new-line has been made optional to be
189 * consistent with the original non-batch behaviour of mktree.
191 ; /* skip creating an empty tree */
192 } else {
193 write_tree(&oid);
194 puts(oid_to_hex(&oid));
195 fflush(stdout);
197 used=0; /* reset tree entry buffer for re-use in batch mode */
199 strbuf_release(&sb);
200 return 0;