Merge branch 'nd/format-patch-cover-letter-stat-width'
[git.git] / builtin / mktree.c
blob94e82b8504f7da5bed50d81771cefe3445b98ece
1 /*
2 * GIT - the stupid content tracker
4 * Copyright (c) Junio C Hamano, 2006, 2009
5 */
6 #include "builtin.h"
7 #include "quote.h"
8 #include "tree.h"
9 #include "parse-options.h"
10 #include "object-store.h"
12 static struct treeent {
13 unsigned mode;
14 struct object_id oid;
15 int len;
16 char name[FLEX_ARRAY];
17 } **entries;
18 static int alloc, used;
20 static void append_to_tree(unsigned mode, struct object_id *oid, char *path)
22 struct treeent *ent;
23 size_t len = strlen(path);
24 if (strchr(path, '/'))
25 die("path %s contains slash", path);
27 FLEX_ALLOC_MEM(ent, name, path, len);
28 ent->mode = mode;
29 ent->len = len;
30 oidcpy(&ent->oid, oid);
32 ALLOC_GROW(entries, used + 1, alloc);
33 entries[used++] = ent;
36 static int ent_compare(const void *a_, const void *b_)
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);
44 static void write_tree(struct object_id *oid)
46 struct strbuf buf;
47 size_t size;
48 int i;
50 QSORT(entries, used, ent_compare);
51 for (size = i = 0; i < used; i++)
52 size += 32 + entries[i]->len;
54 strbuf_init(&buf, size);
55 for (i = 0; i < used; i++) {
56 struct treeent *ent = entries[i];
57 strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0');
58 strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz);
61 write_object_file(buf.buf, buf.len, tree_type, oid);
62 strbuf_release(&buf);
65 static const char *mktree_usage[] = {
66 N_("git mktree [-z] [--missing] [--batch]"),
67 NULL
70 static void mktree_line(char *buf, size_t len, int nul_term_line, int allow_missing)
72 char *ptr, *ntr;
73 const char *p;
74 unsigned mode;
75 enum object_type mode_type; /* object type derived from mode */
76 enum object_type obj_type; /* object type derived from sha */
77 char *path, *to_free = NULL;
78 struct object_id oid;
80 ptr = buf;
82 * Read non-recursive ls-tree output format:
83 * mode SP type SP sha1 TAB name
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, ' ');
90 if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) ||
91 *p != '\t')
92 die("input format error: %s", buf);
94 /* It is perfectly normal if we do not have a commit from a submodule */
95 if (S_ISGITLINK(mode))
96 allow_missing = 1;
99 *ntr++ = 0; /* now at the beginning of SHA1 */
101 path = (char *)p + 1; /* at the beginning of name */
102 if (!nul_term_line && path[0] == '"') {
103 struct strbuf p_uq = STRBUF_INIT;
104 if (unquote_c_style(&p_uq, path, NULL))
105 die("invalid quoting");
106 path = to_free = strbuf_detach(&p_uq, NULL);
110 * Object type is redundantly derivable three ways.
111 * These should all agree.
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)",
116 path, ptr, type_name(mode_type));
119 /* Check the type of object identified by sha1 */
120 obj_type = oid_object_info(the_repository, &oid, NULL);
121 if (obj_type < 0) {
122 if (allow_missing) {
123 ; /* no problem - missing objects are presumed to be of the right type */
124 } else {
125 die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid));
127 } else {
128 if (obj_type != mode_type) {
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.
134 die("entry '%s' object %s is a %s but specified type was (%s)",
135 path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type));
139 append_to_tree(mode, &oid, path);
140 free(to_free);
143 int cmd_mktree(int ac, const char **av, const char *prefix)
145 struct strbuf sb = STRBUF_INIT;
146 struct object_id oid;
147 int nul_term_line = 0;
148 int allow_missing = 0;
149 int is_batch_mode = 0;
150 int got_eof = 0;
151 strbuf_getline_fn getline_fn;
153 const struct option option[] = {
154 OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")),
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),
157 OPT_END()
160 ac = parse_options(ac, av, prefix, option, mktree_usage, 0);
161 getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf;
163 while (!got_eof) {
164 while (1) {
165 if (getline_fn(&sb, stdin) == EOF) {
166 got_eof = 1;
167 break;
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)");
175 mktree_line(sb.buf, sb.len, nul_term_line, allow_missing);
177 if (is_batch_mode && got_eof && used < 1) {
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.
183 ; /* skip creating an empty tree */
184 } else {
185 write_tree(&oid);
186 puts(oid_to_hex(&oid));
187 fflush(stdout);
189 used=0; /* reset tree entry buffer for re-use in batch mode */
191 strbuf_release(&sb);
192 exit(0);