]>
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" |
f394e093 | 7 | #include "gettext.h" |
41771fa4 | 8 | #include "hex.h" |
83f50539 | 9 | #include "quote.h" |
a034e910 | 10 | #include "strbuf.h" |
8e440259 | 11 | #include "tree.h" |
1fdee85c | 12 | #include "parse-options.h" |
a034e910 | 13 | #include "object-store-ll.h" |
83f50539 JH |
14 | |
15 | static struct treeent { | |
16 | unsigned mode; | |
83eb0802 | 17 | struct object_id oid; |
83f50539 JH |
18 | int len; |
19 | char name[FLEX_ARRAY]; | |
20 | } **entries; | |
21 | static int alloc, used; | |
22 | ||
83eb0802 | 23 | static void append_to_tree(unsigned mode, struct object_id *oid, char *path) |
83f50539 JH |
24 | { |
25 | struct treeent *ent; | |
96ffc06f | 26 | size_t len = strlen(path); |
83f50539 JH |
27 | if (strchr(path, '/')) |
28 | die("path %s contains slash", path); | |
29 | ||
96ffc06f | 30 | FLEX_ALLOC_MEM(ent, name, path, len); |
83f50539 JH |
31 | ent->mode = mode; |
32 | ent->len = len; | |
83eb0802 | 33 | oidcpy(&ent->oid, oid); |
96ffc06f JK |
34 | |
35 | ALLOC_GROW(entries, used + 1, alloc); | |
36 | entries[used++] = ent; | |
83f50539 JH |
37 | } |
38 | ||
39 | static int ent_compare(const void *a_, const void *b_) | |
40 | { | |
41 | struct treeent *a = *(struct treeent **)a_; | |
42 | struct treeent *b = *(struct treeent **)b_; | |
43 | return base_name_compare(a->name, a->len, a->mode, | |
44 | b->name, b->len, b->mode); | |
45 | } | |
46 | ||
a09c985e | 47 | static void write_tree(struct object_id *oid) |
83f50539 | 48 | { |
d52bc661 PH |
49 | struct strbuf buf; |
50 | size_t size; | |
83f50539 JH |
51 | int i; |
52 | ||
9ed0d8d6 | 53 | QSORT(entries, used, ent_compare); |
83f50539 JH |
54 | for (size = i = 0; i < used; i++) |
55 | size += 32 + entries[i]->len; | |
83f50539 | 56 | |
f1696ee3 | 57 | strbuf_init(&buf, size); |
83f50539 JH |
58 | for (i = 0; i < used; i++) { |
59 | struct treeent *ent = entries[i]; | |
d52bc661 | 60 | strbuf_addf(&buf, "%o %s%c", ent->mode, ent->name, '\0'); |
83eb0802 | 61 | strbuf_add(&buf, ent->oid.hash, the_hash_algo->rawsz); |
83f50539 | 62 | } |
d52bc661 | 63 | |
c80d226a | 64 | write_object_file(buf.buf, buf.len, OBJ_TREE, oid); |
c444c165 | 65 | strbuf_release(&buf); |
83f50539 JH |
66 | } |
67 | ||
1fdee85c | 68 | static const char *mktree_usage[] = { |
959d670d | 69 | "git mktree [-z] [--missing] [--batch]", |
1fdee85c JH |
70 | NULL |
71 | }; | |
83f50539 | 72 | |
be27fb7b | 73 | static void mktree_line(char *buf, int nul_term_line, int allow_missing) |
fe0bb5f7 JH |
74 | { |
75 | char *ptr, *ntr; | |
83eb0802 | 76 | const char *p; |
fe0bb5f7 | 77 | unsigned mode; |
31c8221a JM |
78 | enum object_type mode_type; /* object type derived from mode */ |
79 | enum object_type obj_type; /* object type derived from sha */ | |
817b0f60 | 80 | struct object_info oi = OBJECT_INFO_INIT; |
43e61e71 | 81 | char *path, *to_free = NULL; |
83eb0802 | 82 | struct object_id oid; |
fe0bb5f7 JH |
83 | |
84 | ptr = buf; | |
85 | /* | |
86 | * Read non-recursive ls-tree output format: | |
87 | * mode SP type SP sha1 TAB name | |
88 | */ | |
89 | mode = strtoul(ptr, &ntr, 8); | |
90 | if (ptr == ntr || !ntr || *ntr != ' ') | |
91 | die("input format error: %s", buf); | |
92 | ptr = ntr + 1; /* type */ | |
93 | ntr = strchr(ptr, ' '); | |
83eb0802 | 94 | if (!ntr || parse_oid_hex(ntr + 1, &oid, &p) || |
95 | *p != '\t') | |
fe0bb5f7 | 96 | die("input format error: %s", buf); |
ad87b5dd JH |
97 | |
98 | /* It is perfectly normal if we do not have a commit from a submodule */ | |
1c64e79a JH |
99 | if (S_ISGITLINK(mode)) |
100 | allow_missing = 1; | |
101 | ||
ad87b5dd | 102 | |
fe0bb5f7 | 103 | *ntr++ = 0; /* now at the beginning of SHA1 */ |
fe0bb5f7 | 104 | |
58ce21b8 | 105 | path = (char *)p + 1; /* at the beginning of name */ |
b4df87b8 | 106 | if (!nul_term_line && path[0] == '"') { |
fe0bb5f7 JH |
107 | struct strbuf p_uq = STRBUF_INIT; |
108 | if (unquote_c_style(&p_uq, path, NULL)) | |
109 | die("invalid quoting"); | |
43e61e71 | 110 | path = to_free = strbuf_detach(&p_uq, NULL); |
fe0bb5f7 | 111 | } |
31c8221a JM |
112 | |
113 | /* | |
114 | * Object type is redundantly derivable three ways. | |
115 | * These should all agree. | |
116 | */ | |
117 | mode_type = object_type(mode); | |
118 | if (mode_type != type_from_string(ptr)) { | |
119 | die("entry '%s' object type (%s) doesn't match mode type (%s)", | |
debca9d2 | 120 | path, ptr, type_name(mode_type)); |
31c8221a JM |
121 | } |
122 | ||
817b0f60 RO |
123 | /* Check the type of object identified by oid without fetching objects */ |
124 | oi.typep = &obj_type; | |
125 | if (oid_object_info_extended(the_repository, &oid, &oi, | |
126 | OBJECT_INFO_LOOKUP_REPLACE | | |
127 | OBJECT_INFO_QUICK | | |
128 | OBJECT_INFO_SKIP_FETCH_OBJECT) < 0) | |
129 | obj_type = -1; | |
130 | ||
31c8221a JM |
131 | if (obj_type < 0) { |
132 | if (allow_missing) { | |
133 | ; /* no problem - missing objects are presumed to be of the right type */ | |
134 | } else { | |
83eb0802 | 135 | die("entry '%s' object %s is unavailable", path, oid_to_hex(&oid)); |
31c8221a JM |
136 | } |
137 | } else { | |
138 | if (obj_type != mode_type) { | |
139 | /* | |
140 | * The object exists but is of the wrong type. | |
141 | * This is a problem regardless of allow_missing | |
142 | * because the new tree entry will never be correct. | |
143 | */ | |
144 | die("entry '%s' object %s is a %s but specified type was (%s)", | |
83eb0802 | 145 | path, oid_to_hex(&oid), type_name(obj_type), type_name(mode_type)); |
31c8221a JM |
146 | } |
147 | } | |
148 | ||
83eb0802 | 149 | append_to_tree(mode, &oid, path); |
43e61e71 | 150 | free(to_free); |
fe0bb5f7 JH |
151 | } |
152 | ||
633e3556 | 153 | int cmd_mktree(int ac, const char **av, const char *prefix) |
83f50539 | 154 | { |
f285a2d7 | 155 | struct strbuf sb = STRBUF_INIT; |
a09c985e | 156 | struct object_id oid; |
b4df87b8 | 157 | int nul_term_line = 0; |
1c64e79a | 158 | int allow_missing = 0; |
f1cf2d8b JM |
159 | int is_batch_mode = 0; |
160 | int got_eof = 0; | |
b4df87b8 | 161 | strbuf_getline_fn getline_fn; |
f1cf2d8b | 162 | |
1fdee85c | 163 | const struct option option[] = { |
b4df87b8 | 164 | OPT_BOOL('z', NULL, &nul_term_line, N_("input is NUL terminated")), |
a6312810 NTND |
165 | OPT_SET_INT( 0 , "missing", &allow_missing, N_("allow missing objects"), 1), |
166 | OPT_SET_INT( 0 , "batch", &is_batch_mode, N_("allow creation of more than one tree"), 1), | |
1fdee85c JH |
167 | OPT_END() |
168 | }; | |
83f50539 | 169 | |
37782920 | 170 | ac = parse_options(ac, av, prefix, option, mktree_usage, 0); |
b4df87b8 | 171 | getline_fn = nul_term_line ? strbuf_getline_nul : strbuf_getline_lf; |
83f50539 | 172 | |
f1cf2d8b JM |
173 | while (!got_eof) { |
174 | while (1) { | |
b4df87b8 | 175 | if (getline_fn(&sb, stdin) == EOF) { |
f1cf2d8b JM |
176 | got_eof = 1; |
177 | break; | |
178 | } | |
179 | if (sb.buf[0] == '\0') { | |
180 | /* empty lines denote tree boundaries in batch mode */ | |
181 | if (is_batch_mode) | |
182 | break; | |
183 | die("input format error: (blank line only valid in batch mode)"); | |
184 | } | |
be27fb7b | 185 | mktree_line(sb.buf, nul_term_line, allow_missing); |
f1cf2d8b JM |
186 | } |
187 | if (is_batch_mode && got_eof && used < 1) { | |
188 | /* | |
189 | * Execution gets here if the last tree entry is terminated with a | |
190 | * new-line. The final new-line has been made optional to be | |
191 | * consistent with the original non-batch behaviour of mktree. | |
192 | */ | |
193 | ; /* skip creating an empty tree */ | |
194 | } else { | |
a09c985e PO |
195 | write_tree(&oid); |
196 | puts(oid_to_hex(&oid)); | |
f1cf2d8b JM |
197 | fflush(stdout); |
198 | } | |
199 | used=0; /* reset tree entry buffer for re-use in batch mode */ | |
200 | } | |
e6c019d0 | 201 | strbuf_release(&sb); |
338abb0f | 202 | return 0; |
83f50539 | 203 | } |