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