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