doc: remove manpage-base-url workaround
[git.git] / builtin / mktree.c
blob848c7b47476fab78c15984178654f62e8b74cfd9
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 "hex.h"
9 #include "quote.h"
10 #include "tree.h"
11 #include "parse-options.h"
12 #include "object-store.h"
14 static struct treeent {
15 unsigned mode;
16 struct object_id oid;
17 int len;
18 char name[FLEX_ARRAY];
19 } **entries;
20 static int alloc, used;
22 static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
24 struct treeent *ent;
25 size_t len = strlen(path);
26 if (strchr(path, '/'))
27 die("path %s contains slash", path);
29 FLEX_ALLOC_MEM(ent, name, path, len);
30 ent->mode = mode;
31 ent->len = len;
32 oidcpy(&ent->oid, oid);
34 ALLOC_GROW(entries, used + 1, alloc);
35 entries[used++] = ent;
38 static int ent_compare(const void *a_, const void *b_)
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);
46 static void write_tree(struct object_id *oid)
48 struct strbuf buf;
49 size_t size;
50 int i;
52 QSORT(entries, used, ent_compare);
53 for (size = i = 0; i < used; i++)
54 size += 32 + entries[i]->len;
56 strbuf_init(&buf, size);
57 for (i = 0; i < used; i++) {
58 struct treeent *ent = entries[i];
59 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
60 strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
63 write_object_file(buf.buf, buf.len, OBJ_TREE, oid);
64 strbuf_release(&buf);
67 static const char *mktree_usage[] = {
68 "git mktree [-z] [--missing] [--batch]",
69 NULL
72 static void mktree_line(char *buf, int nul_term_line, int allow_missing)
74 char *ptr, *ntr;
75 const char *p;
76 unsigned mode;
77 enum object_type mode_type; /* object type derived from mode */
78 enum object_type obj_type; /* object type derived from sha */
79 struct object_info oi = OBJECT_INFO_INIT;
80 char *path, *to_free = NULL;
81 struct object_id oid;
83 ptr = buf;
85 * Read non-recursive ls-tree output format:
86 * mode SP type SP sha1 TAB name
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, ' ');
93 if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
94 *p != '\t')
95 die("input format error: %s", buf);
97 /* It is perfectly normal if we do not have a commit from a submodule */
98 if (S_ISGITLINK(mode))
99 allow_missing = 1;
102 *ntr++ = 0; /* now at the beginning of SHA1 */
104 path = (char *)p + 1; /* at the beginning of name */
105 if (!nul_term_line && path[0] == '"') {
106 struct strbuf p_uq = STRBUF_INIT;
107 if (unquote_c_style(&p_uq, path, NULL))
108 die("invalid quoting");
109 path = to_free = strbuf_detach(&p_uq, NULL);
113 * Object type is redundantly derivable three ways.
114 * These should all agree.
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)",
119 path, ptr, type_name(mode_type));
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;
130 if (obj_type < 0) {
131 if (allow_missing) {
132 ; /* no problem - missing objects are presumed to be of the right type */
133 } else {
134 die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
136 } else {
137 if (obj_type != mode_type) {
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.
143 die("entry '%s' object %s is a %s but specified type was (%s)",
144 path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
148 append_to_tree(mode, &oid, path);
149 free(to_free);
152 int cmd_mktree(int ac, const char **av, const char *prefix)
154 struct strbuf sb = STRBUF_INIT;
155 struct object_id oid;
156 int nul_term_line = 0;
157 int allow_missing = 0;
158 int is_batch_mode = 0;
159 int got_eof = 0;
160 strbuf_getline_fn getline_fn;
162 const struct option option[] = {
163 OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
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),
166 OPT_END()
169 ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
170 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
172 while (!got_eof) {
173 while (1) {
174 if (getline_fn(&sb, stdin) == EOF) {
175 got_eof = 1;
176 break;
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)");
184 mktree_line(sb.buf, nul_term_line, allow_missing);
186 if (is_batch_mode && got_eof && used < 1) {
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.
192 ; /* skip creating an empty tree */
193 } else {
194 write_tree(&oid);
195 puts(oid_to_hex(&oid));
196 fflush(stdout);
198 used=0; /* reset tree entry buffer for re-use in batch mode */
200 strbuf_release(&sb);
201 return 0;