]>
Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
961784ee | 2 | #include "tag.h" |
175785e5 | 3 | #include "commit.h" |
177722b3 | 4 | #include "commit-graph.h" |
ed09aef0 | 5 | #include "pkt-line.h" |
52883fbd | 6 | #include "utf8.h" |
199c45bf JH |
7 | #include "diff.h" |
8 | #include "revision.h" | |
a97a7468 | 9 | #include "notes.h" |
14ba97f8 | 10 | #include "alloc.h" |
ba3c69a9 | 11 | #include "gpg-interface.h" |
46905893 | 12 | #include "mergesort.h" |
a84b794a | 13 | #include "commit-slab.h" |
da24b104 | 14 | #include "prio-queue.h" |
0a9136f3 | 15 | #include "sha1-lookup.h" |
d76650b8 | 16 | #include "wt-status.h" |
f9f99b3f | 17 | #include "advice.h" |
175785e5 | 18 | |
82a75299 JH |
19 | static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **); |
20 | ||
60ab26de LT |
21 | int save_commit_buffer = 1; |
22 | ||
175785e5 DB |
23 | const char *commit_type = "commit"; |
24 | ||
bc83266a | 25 | struct commit *lookup_commit_reference_gently(const struct object_id *oid, |
f76412ed | 26 | int quiet) |
961784ee | 27 | { |
c251c83d | 28 | struct object *obj = deref_tag(parse_object(oid), NULL, 0); |
961784ee LT |
29 | |
30 | if (!obj) | |
31 | return NULL; | |
8ff226a9 | 32 | return object_as_type(obj, OBJ_COMMIT, quiet); |
f76412ed JH |
33 | } |
34 | ||
bc83266a | 35 | struct commit *lookup_commit_reference(const struct object_id *oid) |
f76412ed | 36 | { |
bc83266a | 37 | return lookup_commit_reference_gently(oid, 0); |
961784ee LT |
38 | } |
39 | ||
bc83266a | 40 | struct commit *lookup_commit_or_die(const struct object_id *oid, const char *ref_name) |
baf18fc2 | 41 | { |
bc83266a | 42 | struct commit *c = lookup_commit_reference(oid); |
baf18fc2 NTND |
43 | if (!c) |
44 | die(_("could not parse %s"), ref_name); | |
bc83266a | 45 | if (oidcmp(oid, &c->object.oid)) { |
baf18fc2 | 46 | warning(_("%s %s is not a commit!"), |
bc83266a | 47 | ref_name, oid_to_hex(oid)); |
baf18fc2 NTND |
48 | } |
49 | return c; | |
50 | } | |
51 | ||
bc83266a | 52 | struct commit *lookup_commit(const struct object_id *oid) |
175785e5 | 53 | { |
bc83266a | 54 | struct object *obj = lookup_object(oid->hash); |
d36f51c1 | 55 | if (!obj) |
68f95d38 | 56 | return create_object(the_repository, oid->hash, |
8ba0e5ec | 57 | alloc_commit_node(the_repository)); |
8ff226a9 | 58 | return object_as_type(obj, OBJ_COMMIT, 0); |
175785e5 DB |
59 | } |
60 | ||
a6fa5992 PN |
61 | struct commit *lookup_commit_reference_by_name(const char *name) |
62 | { | |
7683e2e6 | 63 | struct object_id oid; |
a6fa5992 PN |
64 | struct commit *commit; |
65 | ||
e82caf38 | 66 | if (get_oid_committish(name, &oid)) |
a6fa5992 | 67 | return NULL; |
bc83266a | 68 | commit = lookup_commit_reference(&oid); |
5e7d4d3e | 69 | if (parse_commit(commit)) |
a6fa5992 PN |
70 | return NULL; |
71 | return commit; | |
72 | } | |
73 | ||
dddbad72 | 74 | static timestamp_t parse_commit_date(const char *buf, const char *tail) |
175785e5 | 75 | { |
0a617799 | 76 | const char *dateptr; |
175785e5 | 77 | |
0a617799 MK |
78 | if (buf + 6 >= tail) |
79 | return 0; | |
175785e5 DB |
80 | if (memcmp(buf, "author", 6)) |
81 | return 0; | |
0a617799 | 82 | while (buf < tail && *buf++ != '\n') |
175785e5 | 83 | /* nada */; |
0a617799 MK |
84 | if (buf + 9 >= tail) |
85 | return 0; | |
175785e5 DB |
86 | if (memcmp(buf, "committer", 9)) |
87 | return 0; | |
0a617799 | 88 | while (buf < tail && *buf++ != '>') |
175785e5 | 89 | /* nada */; |
0a617799 MK |
90 | if (buf >= tail) |
91 | return 0; | |
92 | dateptr = buf; | |
93 | while (buf < tail && *buf++ != '\n') | |
94 | /* nada */; | |
95 | if (buf >= tail) | |
96 | return 0; | |
1aeb7e75 JS |
97 | /* dateptr < buf && buf[-1] == '\n', so parsing will stop at buf-1 */ |
98 | return parse_timestamp(dateptr, NULL, 10); | |
175785e5 DB |
99 | } |
100 | ||
5040f17e | 101 | static struct commit_graft **commit_graft; |
5da5c8f4 JH |
102 | static int commit_graft_alloc, commit_graft_nr; |
103 | ||
0a9136f3 DD |
104 | static const unsigned char *commit_graft_sha1_access(size_t index, void *table) |
105 | { | |
106 | struct commit_graft **commit_graft_table = table; | |
7683e2e6 | 107 | return commit_graft_table[index]->oid.hash; |
0a9136f3 DD |
108 | } |
109 | ||
5da5c8f4 JH |
110 | static int commit_graft_pos(const unsigned char *sha1) |
111 | { | |
0a9136f3 DD |
112 | return sha1_pos(sha1, commit_graft, commit_graft_nr, |
113 | commit_graft_sha1_access); | |
5da5c8f4 JH |
114 | } |
115 | ||
5040f17e JH |
116 | int register_commit_graft(struct commit_graft *graft, int ignore_dups) |
117 | { | |
7683e2e6 | 118 | int pos = commit_graft_pos(graft->oid.hash); |
a6080a0a | 119 | |
5040f17e JH |
120 | if (0 <= pos) { |
121 | if (ignore_dups) | |
122 | free(graft); | |
123 | else { | |
124 | free(commit_graft[pos]); | |
125 | commit_graft[pos] = graft; | |
126 | } | |
127 | return 1; | |
128 | } | |
129 | pos = -pos - 1; | |
d6e82b57 DD |
130 | ALLOC_GROW(commit_graft, commit_graft_nr + 1, commit_graft_alloc); |
131 | commit_graft_nr++; | |
5040f17e | 132 | if (pos < commit_graft_nr) |
f919ffeb SG |
133 | MOVE_ARRAY(commit_graft + pos + 1, commit_graft + pos, |
134 | commit_graft_nr - pos - 1); | |
5040f17e JH |
135 | commit_graft[pos] = graft; |
136 | return 0; | |
137 | } | |
138 | ||
9a934032 | 139 | struct commit_graft *read_graft_line(struct strbuf *line) |
5040f17e JH |
140 | { |
141 | /* The format is just "Commit Parent1 Parent2 ...\n" */ | |
cfa5bf16 PO |
142 | int i, phase; |
143 | const char *tail = NULL; | |
5040f17e | 144 | struct commit_graft *graft = NULL; |
cfa5bf16 | 145 | struct object_id dummy_oid, *oid; |
5040f17e | 146 | |
9a934032 PO |
147 | strbuf_rtrim(line); |
148 | if (!line->len || line->buf[0] == '#') | |
5bc4ce58 | 149 | return NULL; |
cfa5bf16 PO |
150 | /* |
151 | * phase 0 verifies line, counts hashes in line and allocates graft | |
152 | * phase 1 fills graft | |
153 | */ | |
154 | for (phase = 0; phase < 2; phase++) { | |
155 | oid = graft ? &graft->oid : &dummy_oid; | |
156 | if (parse_oid_hex(line->buf, oid, &tail)) | |
5040f17e | 157 | goto bad_graft_data; |
cfa5bf16 PO |
158 | for (i = 0; *tail != '\0'; i++) { |
159 | oid = graft ? &graft->parent[i] : &dummy_oid; | |
160 | if (!isspace(*tail++) || parse_oid_hex(tail, oid, &tail)) | |
161 | goto bad_graft_data; | |
162 | } | |
163 | if (!graft) { | |
164 | graft = xmalloc(st_add(sizeof(*graft), | |
165 | st_mult(sizeof(struct object_id), i))); | |
166 | graft->nr_parent = i; | |
167 | } | |
5040f17e JH |
168 | } |
169 | return graft; | |
df5d43be RT |
170 | |
171 | bad_graft_data: | |
9a934032 | 172 | error("bad graft data: %s", line->buf); |
cfa5bf16 | 173 | assert(!graft); |
df5d43be | 174 | return NULL; |
5040f17e JH |
175 | } |
176 | ||
c5ae6439 | 177 | static int read_graft_file(const char *graft_file) |
5da5c8f4 | 178 | { |
e9d983f1 | 179 | FILE *fp = fopen_or_warn(graft_file, "r"); |
e228c173 | 180 | struct strbuf buf = STRBUF_INIT; |
5040f17e JH |
181 | if (!fp) |
182 | return -1; | |
f9f99b3f JS |
183 | if (advice_graft_file_deprecated) |
184 | advise(_("Support for <GIT_DIR>/info/grafts is deprecated\n" | |
185 | "and will be removed in a future Git version.\n" | |
186 | "\n" | |
187 | "Please use \"git replace --convert-graft-file\"\n" | |
188 | "to convert the grafts into replace refs.\n" | |
189 | "\n" | |
190 | "Turn this message off by running\n" | |
191 | "\"git config advice.graftFileDeprecated false\"")); | |
e228c173 | 192 | while (!strbuf_getwholeline(&buf, fp, '\n')) { |
5da5c8f4 | 193 | /* The format is just "Commit Parent1 Parent2 ...\n" */ |
9a934032 | 194 | struct commit_graft *graft = read_graft_line(&buf); |
5bc4ce58 JH |
195 | if (!graft) |
196 | continue; | |
5040f17e | 197 | if (register_commit_graft(graft, 1)) |
e228c173 | 198 | error("duplicate graft data: %s", buf.buf); |
5da5c8f4 JH |
199 | } |
200 | fclose(fp); | |
e228c173 | 201 | strbuf_release(&buf); |
5040f17e JH |
202 | return 0; |
203 | } | |
204 | ||
205 | static void prepare_commit_graft(void) | |
206 | { | |
207 | static int commit_graft_prepared; | |
208 | char *graft_file; | |
209 | ||
210 | if (commit_graft_prepared) | |
211 | return; | |
14a9bd28 JK |
212 | if (!startup_info->have_repository) |
213 | return; | |
214 | ||
5040f17e JH |
215 | graft_file = get_graft_file(); |
216 | read_graft_file(graft_file); | |
ed09aef0 JS |
217 | /* make sure shallows are read */ |
218 | is_repository_shallow(); | |
5040f17e | 219 | commit_graft_prepared = 1; |
5da5c8f4 JH |
220 | } |
221 | ||
8b65a34c | 222 | struct commit_graft *lookup_commit_graft(const struct object_id *oid) |
5da5c8f4 JH |
223 | { |
224 | int pos; | |
5040f17e | 225 | prepare_commit_graft(); |
8b65a34c | 226 | pos = commit_graft_pos(oid->hash); |
5da5c8f4 JH |
227 | if (pos < 0) |
228 | return NULL; | |
229 | return commit_graft[pos]; | |
230 | } | |
231 | ||
09d46644 | 232 | int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data) |
ed09aef0 | 233 | { |
09d46644 NTND |
234 | int i, ret; |
235 | for (i = ret = 0; i < commit_graft_nr && !ret; i++) | |
236 | ret = fn(commit_graft[i], cb_data); | |
237 | return ret; | |
ed09aef0 JS |
238 | } |
239 | ||
e92b848c | 240 | int unregister_shallow(const struct object_id *oid) |
f53514bc | 241 | { |
e92b848c | 242 | int pos = commit_graft_pos(oid->hash); |
f53514bc JS |
243 | if (pos < 0) |
244 | return -1; | |
245 | if (pos + 1 < commit_graft_nr) | |
f331ab9d RS |
246 | MOVE_ARRAY(commit_graft + pos, commit_graft + pos + 1, |
247 | commit_graft_nr - pos - 1); | |
f53514bc JS |
248 | commit_graft_nr--; |
249 | return 0; | |
250 | } | |
251 | ||
8597ea3a JK |
252 | struct commit_buffer { |
253 | void *buffer; | |
254 | unsigned long size; | |
255 | }; | |
256 | define_commit_slab(buffer_slab, struct commit_buffer); | |
c1b3c71f JK |
257 | static struct buffer_slab buffer_slab = COMMIT_SLAB_INIT(1, buffer_slab); |
258 | ||
8597ea3a | 259 | void set_commit_buffer(struct commit *commit, void *buffer, unsigned long size) |
66c2827e | 260 | { |
8597ea3a JK |
261 | struct commit_buffer *v = buffer_slab_at(&buffer_slab, commit); |
262 | v->buffer = buffer; | |
263 | v->size = size; | |
66c2827e JK |
264 | } |
265 | ||
8597ea3a | 266 | const void *get_cached_commit_buffer(const struct commit *commit, unsigned long *sizep) |
152ff1cc | 267 | { |
862e730e JH |
268 | struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit); |
269 | if (!v) { | |
270 | if (sizep) | |
271 | *sizep = 0; | |
272 | return NULL; | |
273 | } | |
8597ea3a JK |
274 | if (sizep) |
275 | *sizep = v->size; | |
276 | return v->buffer; | |
152ff1cc JK |
277 | } |
278 | ||
8597ea3a | 279 | const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep) |
152ff1cc | 280 | { |
8597ea3a | 281 | const void *ret = get_cached_commit_buffer(commit, sizep); |
152ff1cc JK |
282 | if (!ret) { |
283 | enum object_type type; | |
284 | unsigned long size; | |
b4f5aca4 | 285 | ret = read_object_file(&commit->object.oid, &type, &size); |
152ff1cc JK |
286 | if (!ret) |
287 | die("cannot read commit object %s", | |
f2fd0760 | 288 | oid_to_hex(&commit->object.oid)); |
152ff1cc JK |
289 | if (type != OBJ_COMMIT) |
290 | die("expected commit for %s, got %s", | |
debca9d2 | 291 | oid_to_hex(&commit->object.oid), type_name(type)); |
8597ea3a JK |
292 | if (sizep) |
293 | *sizep = size; | |
152ff1cc JK |
294 | } |
295 | return ret; | |
296 | } | |
297 | ||
298 | void unuse_commit_buffer(const struct commit *commit, const void *buffer) | |
299 | { | |
862e730e JH |
300 | struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit); |
301 | if (!(v && v->buffer == buffer)) | |
152ff1cc JK |
302 | free((void *)buffer); |
303 | } | |
304 | ||
0fb370da JK |
305 | void free_commit_buffer(struct commit *commit) |
306 | { | |
862e730e JH |
307 | struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit); |
308 | if (v) { | |
6a83d902 | 309 | FREE_AND_NULL(v->buffer); |
862e730e JH |
310 | v->size = 0; |
311 | } | |
0fb370da JK |
312 | } |
313 | ||
5bb03de1 DS |
314 | struct tree *get_commit_tree(const struct commit *commit) |
315 | { | |
7b8a21db DS |
316 | if (commit->maybe_tree || !commit->object.parsed) |
317 | return commit->maybe_tree; | |
318 | ||
319 | if (commit->graph_pos == COMMIT_NOT_FROM_GRAPH) | |
320 | BUG("commit has NULL tree, but was not loaded from commit-graph"); | |
321 | ||
322 | return get_commit_tree_in_graph(commit); | |
5bb03de1 DS |
323 | } |
324 | ||
325 | struct object_id *get_commit_tree_oid(const struct commit *commit) | |
326 | { | |
327 | return &get_commit_tree(commit)->object.oid; | |
328 | } | |
329 | ||
14ba97f8 SB |
330 | void release_commit_memory(struct commit *c) |
331 | { | |
11024058 | 332 | c->maybe_tree = NULL; |
14ba97f8 SB |
333 | c->index = 0; |
334 | free_commit_buffer(c); | |
335 | free_commit_list(c->parents); | |
336 | /* TODO: what about commit->util? */ | |
337 | ||
338 | c->object.parsed = 0; | |
339 | } | |
340 | ||
8597ea3a | 341 | const void *detach_commit_buffer(struct commit *commit, unsigned long *sizep) |
0fb370da | 342 | { |
862e730e | 343 | struct commit_buffer *v = buffer_slab_peek(&buffer_slab, commit); |
8597ea3a JK |
344 | void *ret; |
345 | ||
862e730e JH |
346 | if (!v) { |
347 | if (sizep) | |
348 | *sizep = 0; | |
349 | return NULL; | |
350 | } | |
8597ea3a JK |
351 | ret = v->buffer; |
352 | if (sizep) | |
353 | *sizep = v->size; | |
354 | ||
355 | v->buffer = NULL; | |
356 | v->size = 0; | |
0fb370da JK |
357 | return ret; |
358 | } | |
359 | ||
e2838d85 | 360 | int parse_commit_buffer(struct commit *item, const void *buffer, unsigned long size, int check_graph) |
175785e5 | 361 | { |
cf7b1cad NTND |
362 | const char *tail = buffer; |
363 | const char *bufptr = buffer; | |
7683e2e6 | 364 | struct object_id parent; |
6c88be16 | 365 | struct commit_list **pptr; |
5da5c8f4 | 366 | struct commit_graft *graft; |
7683e2e6 | 367 | const int tree_entry_len = GIT_SHA1_HEXSZ + 5; |
368 | const int parent_entry_len = GIT_SHA1_HEXSZ + 7; | |
bd2c39f5 | 369 | |
175785e5 DB |
370 | if (item->object.parsed) |
371 | return 0; | |
372 | item->object.parsed = 1; | |
3b44f15a | 373 | tail += size; |
7683e2e6 | 374 | if (tail <= bufptr + tree_entry_len + 1 || memcmp(bufptr, "tree ", 5) || |
375 | bufptr[tree_entry_len] != '\n') | |
f2fd0760 | 376 | return error("bogus commit object %s", oid_to_hex(&item->object.oid)); |
26ea3e7d | 377 | if (get_oid_hex(bufptr + 5, &parent) < 0) |
bd2afde8 | 378 | return error("bad tree pointer in commit %s", |
f2fd0760 | 379 | oid_to_hex(&item->object.oid)); |
891435d5 | 380 | item->maybe_tree = lookup_tree(&parent); |
7683e2e6 | 381 | bufptr += tree_entry_len + 1; /* "tree " + "hex sha1" + "\n" */ |
6c88be16 | 382 | pptr = &item->parents; |
5da5c8f4 | 383 | |
8b65a34c | 384 | graft = lookup_commit_graft(&item->object.oid); |
7683e2e6 | 385 | while (bufptr + parent_entry_len < tail && !memcmp(bufptr, "parent ", 7)) { |
f7cc77d7 LT |
386 | struct commit *new_parent; |
387 | ||
7683e2e6 | 388 | if (tail <= bufptr + parent_entry_len + 1 || |
26ea3e7d | 389 | get_oid_hex(bufptr + 7, &parent) || |
7683e2e6 | 390 | bufptr[parent_entry_len] != '\n') |
f2fd0760 | 391 | return error("bad parents in commit %s", oid_to_hex(&item->object.oid)); |
7683e2e6 | 392 | bufptr += parent_entry_len + 1; |
7f3140cd JS |
393 | /* |
394 | * The clone is shallow if nr_parent < 0, and we must | |
395 | * not traverse its real parents even when we unhide them. | |
396 | */ | |
397 | if (graft && (graft->nr_parent < 0 || grafts_replace_parents)) | |
5da5c8f4 | 398 | continue; |
bc83266a | 399 | new_parent = lookup_commit(&parent); |
39468def | 400 | if (new_parent) |
6c88be16 | 401 | pptr = &commit_list_insert(new_parent, pptr)->next; |
5da5c8f4 JH |
402 | } |
403 | if (graft) { | |
404 | int i; | |
405 | struct commit *new_parent; | |
406 | for (i = 0; i < graft->nr_parent; i++) { | |
bc83266a | 407 | new_parent = lookup_commit(&graft->parent[i]); |
5da5c8f4 JH |
408 | if (!new_parent) |
409 | continue; | |
410 | pptr = &commit_list_insert(new_parent, pptr)->next; | |
5da5c8f4 | 411 | } |
175785e5 | 412 | } |
0a617799 | 413 | item->date = parse_commit_date(bufptr, tail); |
27dedf0c | 414 | |
e2838d85 DS |
415 | if (check_graph) |
416 | load_commit_graph_info(item); | |
417 | ||
175785e5 DB |
418 | return 0; |
419 | } | |
420 | ||
9cc2b07a | 421 | int parse_commit_gently(struct commit *item, int quiet_on_missing) |
bd2c39f5 | 422 | { |
21666f1a | 423 | enum object_type type; |
bd2c39f5 NP |
424 | void *buffer; |
425 | unsigned long size; | |
426 | int ret; | |
427 | ||
9786f68b MK |
428 | if (!item) |
429 | return -1; | |
bd2c39f5 NP |
430 | if (item->object.parsed) |
431 | return 0; | |
177722b3 DS |
432 | if (parse_commit_in_graph(item)) |
433 | return 0; | |
b4f5aca4 | 434 | buffer = read_object_file(&item->object.oid, &type, &size); |
bd2c39f5 | 435 | if (!buffer) |
9cc2b07a JK |
436 | return quiet_on_missing ? -1 : |
437 | error("Could not read %s", | |
f2fd0760 | 438 | oid_to_hex(&item->object.oid)); |
21666f1a | 439 | if (type != OBJ_COMMIT) { |
bd2c39f5 NP |
440 | free(buffer); |
441 | return error("Object %s not a commit", | |
f2fd0760 | 442 | oid_to_hex(&item->object.oid)); |
bd2c39f5 | 443 | } |
e2838d85 | 444 | ret = parse_commit_buffer(item, buffer, size, 0); |
60ab26de | 445 | if (save_commit_buffer && !ret) { |
8597ea3a | 446 | set_commit_buffer(item, buffer, size); |
3ff1fbbb LT |
447 | return 0; |
448 | } | |
bd2c39f5 NP |
449 | free(buffer); |
450 | return ret; | |
451 | } | |
452 | ||
7059dccc JK |
453 | void parse_commit_or_die(struct commit *item) |
454 | { | |
455 | if (parse_commit(item)) | |
456 | die("unable to parse commit %s", | |
f2fd0760 | 457 | item ? oid_to_hex(&item->object.oid) : "(null)"); |
7059dccc JK |
458 | } |
459 | ||
11af2aae CC |
460 | int find_commit_subject(const char *commit_buffer, const char **subject) |
461 | { | |
462 | const char *eol; | |
463 | const char *p = commit_buffer; | |
464 | ||
465 | while (*p && (*p != '\n' || p[1] != '\n')) | |
466 | p++; | |
467 | if (*p) { | |
4e1b06da | 468 | p = skip_blank_lines(p + 2); |
9c9b03b1 | 469 | eol = strchrnul(p, '\n'); |
11af2aae CC |
470 | } else |
471 | eol = p; | |
472 | ||
473 | *subject = p; | |
474 | ||
475 | return eol - p; | |
476 | } | |
477 | ||
ac5155ef | 478 | struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p) |
dd97f850 | 479 | { |
812666c8 | 480 | struct commit_list *new_list = xmalloc(sizeof(struct commit_list)); |
dd97f850 DB |
481 | new_list->item = item; |
482 | new_list->next = *list_p; | |
483 | *list_p = new_list; | |
ac5155ef | 484 | return new_list; |
dd97f850 DB |
485 | } |
486 | ||
65319475 MV |
487 | unsigned commit_list_count(const struct commit_list *l) |
488 | { | |
489 | unsigned c = 0; | |
490 | for (; l; l = l->next ) | |
491 | c++; | |
492 | return c; | |
493 | } | |
494 | ||
53d00b39 TR |
495 | struct commit_list *copy_commit_list(struct commit_list *list) |
496 | { | |
497 | struct commit_list *head = NULL; | |
498 | struct commit_list **pp = &head; | |
499 | while (list) { | |
cb979dbd | 500 | pp = commit_list_append(list->item, pp); |
53d00b39 TR |
501 | list = list->next; |
502 | } | |
503 | return head; | |
504 | } | |
505 | ||
175785e5 DB |
506 | void free_commit_list(struct commit_list *list) |
507 | { | |
e510ab89 RS |
508 | while (list) |
509 | pop_commit(&list); | |
175785e5 | 510 | } |
dd97f850 | 511 | |
47e44ed1 | 512 | struct commit_list * commit_list_insert_by_date(struct commit *item, struct commit_list **list) |
dd97f850 DB |
513 | { |
514 | struct commit_list **pp = list; | |
515 | struct commit_list *p; | |
516 | while ((p = *pp) != NULL) { | |
517 | if (p->item->date < item->date) { | |
518 | break; | |
519 | } | |
520 | pp = &p->next; | |
521 | } | |
f755494c | 522 | return commit_list_insert(item, pp); |
dd97f850 DB |
523 | } |
524 | ||
46905893 RS |
525 | static int commit_list_compare_by_date(const void *a, const void *b) |
526 | { | |
dddbad72 JS |
527 | timestamp_t a_date = ((const struct commit_list *)a)->item->date; |
528 | timestamp_t b_date = ((const struct commit_list *)b)->item->date; | |
46905893 RS |
529 | if (a_date < b_date) |
530 | return 1; | |
531 | if (a_date > b_date) | |
532 | return -1; | |
533 | return 0; | |
534 | } | |
535 | ||
536 | static void *commit_list_get_next(const void *a) | |
537 | { | |
538 | return ((const struct commit_list *)a)->next; | |
539 | } | |
540 | ||
541 | static void commit_list_set_next(void *a, void *next) | |
542 | { | |
543 | ((struct commit_list *)a)->next = next; | |
544 | } | |
a6080a0a | 545 | |
47e44ed1 | 546 | void commit_list_sort_by_date(struct commit_list **list) |
dd97f850 | 547 | { |
7365c95d JH |
548 | *list = llist_mergesort(*list, commit_list_get_next, commit_list_set_next, |
549 | commit_list_compare_by_date); | |
dd97f850 DB |
550 | } |
551 | ||
58e28af6 DB |
552 | struct commit *pop_most_recent_commit(struct commit_list **list, |
553 | unsigned int mark) | |
dd97f850 | 554 | { |
e510ab89 | 555 | struct commit *ret = pop_commit(list); |
dd97f850 | 556 | struct commit_list *parents = ret->parents; |
dd97f850 DB |
557 | |
558 | while (parents) { | |
4056c091 | 559 | struct commit *commit = parents->item; |
dec38c81 | 560 | if (!parse_commit(commit) && !(commit->object.flags & mark)) { |
58e28af6 | 561 | commit->object.flags |= mark; |
47e44ed1 | 562 | commit_list_insert_by_date(commit, list); |
4056c091 | 563 | } |
dd97f850 DB |
564 | parents = parents->next; |
565 | } | |
566 | return ret; | |
567 | } | |
e3bc7a3b | 568 | |
941ba8db NTND |
569 | static void clear_commit_marks_1(struct commit_list **plist, |
570 | struct commit *commit, unsigned int mark) | |
f8f9c73c | 571 | { |
60fcc2e6 JS |
572 | while (commit) { |
573 | struct commit_list *parents; | |
f8f9c73c | 574 | |
60fcc2e6 JS |
575 | if (!(mark & commit->object.flags)) |
576 | return; | |
58ecf5c1 | 577 | |
60fcc2e6 JS |
578 | commit->object.flags &= ~mark; |
579 | ||
580 | parents = commit->parents; | |
581 | if (!parents) | |
582 | return; | |
583 | ||
584 | while ((parents = parents->next)) | |
941ba8db | 585 | commit_list_insert(parents->item, plist); |
60fcc2e6 JS |
586 | |
587 | commit = commit->parents->item; | |
f8f9c73c JH |
588 | } |
589 | } | |
590 | ||
e895cb51 | 591 | void clear_commit_marks_many(int nr, struct commit **commit, unsigned int mark) |
941ba8db NTND |
592 | { |
593 | struct commit_list *list = NULL; | |
e895cb51 JH |
594 | |
595 | while (nr--) { | |
07f7d55a | 596 | clear_commit_marks_1(&list, *commit, mark); |
e895cb51 JH |
597 | commit++; |
598 | } | |
941ba8db NTND |
599 | while (list) |
600 | clear_commit_marks_1(&list, pop_commit(&list), mark); | |
601 | } | |
602 | ||
e895cb51 JH |
603 | void clear_commit_marks(struct commit *commit, unsigned int mark) |
604 | { | |
605 | clear_commit_marks_many(1, &commit, mark); | |
606 | } | |
607 | ||
a3437b8c JS |
608 | struct commit *pop_commit(struct commit_list **stack) |
609 | { | |
610 | struct commit_list *top = *stack; | |
611 | struct commit *item = top ? top->item : NULL; | |
612 | ||
613 | if (top) { | |
614 | *stack = top->next; | |
615 | free(top); | |
616 | } | |
617 | return item; | |
618 | } | |
619 | ||
a84b794a JH |
620 | /* |
621 | * Topological sort support | |
622 | */ | |
96c4f4a3 | 623 | |
a84b794a JH |
624 | /* count number of children that have not been emitted */ |
625 | define_commit_slab(indegree_slab, int); | |
96c4f4a3 | 626 | |
81c6b38b JH |
627 | /* record author-date for each commit object */ |
628 | define_commit_slab(author_date_slab, unsigned long); | |
629 | ||
630 | static void record_author_date(struct author_date_slab *author_date, | |
631 | struct commit *commit) | |
632 | { | |
8597ea3a | 633 | const char *buffer = get_commit_buffer(commit, NULL); |
81c6b38b | 634 | struct ident_split ident; |
ea5517f0 JK |
635 | const char *ident_line; |
636 | size_t ident_len; | |
81c6b38b | 637 | char *date_end; |
dddbad72 | 638 | timestamp_t date; |
81c6b38b | 639 | |
ea5517f0 JK |
640 | ident_line = find_commit_header(buffer, "author", &ident_len); |
641 | if (!ident_line) | |
642 | goto fail_exit; /* no author line */ | |
643 | if (split_ident_line(&ident, ident_line, ident_len) || | |
644 | !ident.date_begin || !ident.date_end) | |
645 | goto fail_exit; /* malformed "author" line */ | |
81c6b38b | 646 | |
1aeb7e75 | 647 | date = parse_timestamp(ident.date_begin, &date_end, 10); |
81c6b38b JH |
648 | if (date_end != ident.date_end) |
649 | goto fail_exit; /* malformed date */ | |
650 | *(author_date_slab_at(author_date, commit)) = date; | |
651 | ||
652 | fail_exit: | |
ba41c1c9 | 653 | unuse_commit_buffer(commit, buffer); |
81c6b38b JH |
654 | } |
655 | ||
656 | static int compare_commits_by_author_date(const void *a_, const void *b_, | |
657 | void *cb_data) | |
658 | { | |
659 | const struct commit *a = a_, *b = b_; | |
660 | struct author_date_slab *author_date = cb_data; | |
dddbad72 JS |
661 | timestamp_t a_date = *(author_date_slab_at(author_date, a)); |
662 | timestamp_t b_date = *(author_date_slab_at(author_date, b)); | |
81c6b38b JH |
663 | |
664 | /* newer commits with larger date first */ | |
665 | if (a_date < b_date) | |
666 | return 1; | |
667 | else if (a_date > b_date) | |
668 | return -1; | |
669 | return 0; | |
670 | } | |
671 | ||
3afc679b DS |
672 | int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void *unused) |
673 | { | |
674 | const struct commit *a = a_, *b = b_; | |
675 | ||
676 | /* newer commits first */ | |
677 | if (a->generation < b->generation) | |
678 | return 1; | |
679 | else if (a->generation > b->generation) | |
680 | return -1; | |
681 | ||
682 | /* use date as a heuristic when generations are equal */ | |
683 | if (a->date < b->date) | |
684 | return 1; | |
685 | else if (a->date > b->date) | |
686 | return -1; | |
687 | return 0; | |
688 | } | |
689 | ||
727377ff | 690 | int compare_commits_by_commit_date(const void *a_, const void *b_, void *unused) |
da24b104 JH |
691 | { |
692 | const struct commit *a = a_, *b = b_; | |
693 | /* newer commits with larger date first */ | |
694 | if (a->date < b->date) | |
695 | return 1; | |
696 | else if (a->date > b->date) | |
697 | return -1; | |
698 | return 0; | |
699 | } | |
700 | ||
ab580ace JS |
701 | /* |
702 | * Performs an in-place topological sort on the list supplied. | |
703 | */ | |
da24b104 | 704 | void sort_in_topological_order(struct commit_list **list, enum rev_sort_order sort_order) |
6b6dcfc2 | 705 | { |
23c17d4a | 706 | struct commit_list *next, *orig = *list; |
23c17d4a | 707 | struct commit_list **pptr; |
a84b794a | 708 | struct indegree_slab indegree; |
da24b104 JH |
709 | struct prio_queue queue; |
710 | struct commit *commit; | |
81c6b38b | 711 | struct author_date_slab author_date; |
a6080a0a | 712 | |
23c17d4a | 713 | if (!orig) |
7d6fb370 | 714 | return; |
23c17d4a LT |
715 | *list = NULL; |
716 | ||
a84b794a | 717 | init_indegree_slab(&indegree); |
da24b104 | 718 | memset(&queue, '\0', sizeof(queue)); |
81c6b38b | 719 | |
da24b104 JH |
720 | switch (sort_order) { |
721 | default: /* REV_SORT_IN_GRAPH_ORDER */ | |
722 | queue.compare = NULL; | |
723 | break; | |
724 | case REV_SORT_BY_COMMIT_DATE: | |
725 | queue.compare = compare_commits_by_commit_date; | |
726 | break; | |
81c6b38b JH |
727 | case REV_SORT_BY_AUTHOR_DATE: |
728 | init_author_date_slab(&author_date); | |
729 | queue.compare = compare_commits_by_author_date; | |
730 | queue.cb_data = &author_date; | |
731 | break; | |
da24b104 | 732 | } |
96c4f4a3 | 733 | |
23c17d4a LT |
734 | /* Mark them and clear the indegree */ |
735 | for (next = orig; next; next = next->next) { | |
736 | struct commit *commit = next->item; | |
a84b794a | 737 | *(indegree_slab_at(&indegree, commit)) = 1; |
81c6b38b JH |
738 | /* also record the author dates, if needed */ |
739 | if (sort_order == REV_SORT_BY_AUTHOR_DATE) | |
740 | record_author_date(&author_date, commit); | |
ab580ace | 741 | } |
23c17d4a | 742 | |
ab580ace | 743 | /* update the indegree */ |
23c17d4a | 744 | for (next = orig; next; next = next->next) { |
da24b104 | 745 | struct commit_list *parents = next->item->parents; |
ab580ace | 746 | while (parents) { |
23c17d4a | 747 | struct commit *parent = parents->item; |
a84b794a | 748 | int *pi = indegree_slab_at(&indegree, parent); |
6b6dcfc2 | 749 | |
96c4f4a3 JK |
750 | if (*pi) |
751 | (*pi)++; | |
23c17d4a | 752 | parents = parents->next; |
ab580ace | 753 | } |
ab580ace | 754 | } |
23c17d4a | 755 | |
a6080a0a | 756 | /* |
674d1727 PH |
757 | * find the tips |
758 | * | |
759 | * tips are nodes not reachable from any other node in the list | |
760 | * | |
761 | * the tips serve as a starting set for the work queue. | |
762 | */ | |
23c17d4a LT |
763 | for (next = orig; next; next = next->next) { |
764 | struct commit *commit = next->item; | |
ab580ace | 765 | |
a84b794a | 766 | if (*(indegree_slab_at(&indegree, commit)) == 1) |
da24b104 | 767 | prio_queue_put(&queue, commit); |
ab580ace | 768 | } |
4c8725f1 | 769 | |
da24b104 JH |
770 | /* |
771 | * This is unfortunate; the initial tips need to be shown | |
772 | * in the order given from the revision traversal machinery. | |
773 | */ | |
774 | if (sort_order == REV_SORT_IN_GRAPH_ORDER) | |
775 | prio_queue_reverse(&queue); | |
776 | ||
777 | /* We no longer need the commit list */ | |
778 | free_commit_list(orig); | |
23c17d4a LT |
779 | |
780 | pptr = list; | |
781 | *list = NULL; | |
da24b104 JH |
782 | while ((commit = prio_queue_get(&queue)) != NULL) { |
783 | struct commit_list *parents; | |
23c17d4a | 784 | |
23c17d4a | 785 | for (parents = commit->parents; parents ; parents = parents->next) { |
cd2b8ae9 | 786 | struct commit *parent = parents->item; |
a84b794a | 787 | int *pi = indegree_slab_at(&indegree, parent); |
23c17d4a | 788 | |
96c4f4a3 | 789 | if (!*pi) |
23c17d4a LT |
790 | continue; |
791 | ||
792 | /* | |
793 | * parents are only enqueued for emission | |
794 | * when all their children have been emitted thereby | |
795 | * guaranteeing topological order. | |
796 | */ | |
da24b104 JH |
797 | if (--(*pi) == 1) |
798 | prio_queue_put(&queue, parent); | |
ab580ace JS |
799 | } |
800 | /* | |
da24b104 JH |
801 | * all children of commit have already been |
802 | * emitted. we can emit it now. | |
674d1727 | 803 | */ |
a84b794a | 804 | *(indegree_slab_at(&indegree, commit)) = 0; |
da24b104 JH |
805 | |
806 | pptr = &commit_list_insert(commit, pptr)->next; | |
ab580ace | 807 | } |
96c4f4a3 | 808 | |
a84b794a | 809 | clear_indegree_slab(&indegree); |
da24b104 | 810 | clear_prio_queue(&queue); |
81c6b38b JH |
811 | if (sort_order == REV_SORT_BY_AUTHOR_DATE) |
812 | clear_author_date_slab(&author_date); | |
ab580ace | 813 | } |
7c6f8aaf | 814 | |
1295228d | 815 | /* merge-base stuff */ |
7c6f8aaf | 816 | |
208acbfb | 817 | /* Remember to update object flag allocation in object.h */ |
577ed5c2 JH |
818 | #define PARENT1 (1u<<16) |
819 | #define PARENT2 (1u<<17) | |
820 | #define STALE (1u<<18) | |
821 | #define RESULT (1u<<19) | |
7c6f8aaf | 822 | |
1295228d JH |
823 | static const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT); |
824 | ||
73f43f22 | 825 | static int queue_has_nonstale(struct prio_queue *queue) |
7c6f8aaf | 826 | { |
73f43f22 JK |
827 | int i; |
828 | for (i = 0; i < queue->nr; i++) { | |
829 | struct commit *commit = queue->array[i].data; | |
830 | if (!(commit->object.flags & STALE)) | |
831 | return 1; | |
7c6f8aaf | 832 | } |
73f43f22 | 833 | return 0; |
7c6f8aaf JS |
834 | } |
835 | ||
d866924a | 836 | /* all input commits in one and twos[] must have been parsed! */ |
d7c1ec3e DS |
837 | static struct commit_list *paint_down_to_common(struct commit *one, int n, |
838 | struct commit **twos, | |
839 | int min_generation) | |
7c6f8aaf | 840 | { |
3afc679b | 841 | struct prio_queue queue = { compare_commits_by_gen_then_commit_date }; |
7c6f8aaf | 842 | struct commit_list *result = NULL; |
6a938648 | 843 | int i; |
d7c1ec3e | 844 | uint32_t last_gen = GENERATION_NUMBER_INFINITY; |
7c6f8aaf | 845 | |
f3249438 | 846 | one->object.flags |= PARENT1; |
73f43f22 JK |
847 | if (!n) { |
848 | commit_list_append(one, &result); | |
849 | return result; | |
850 | } | |
851 | prio_queue_put(&queue, one); | |
852 | ||
6a938648 JH |
853 | for (i = 0; i < n; i++) { |
854 | twos[i]->object.flags |= PARENT2; | |
73f43f22 | 855 | prio_queue_put(&queue, twos[i]); |
6a938648 | 856 | } |
7c6f8aaf | 857 | |
73f43f22 JK |
858 | while (queue_has_nonstale(&queue)) { |
859 | struct commit *commit = prio_queue_get(&queue); | |
7c6f8aaf | 860 | struct commit_list *parents; |
f3249438 | 861 | int flags; |
7c6f8aaf | 862 | |
d7c1ec3e DS |
863 | if (commit->generation > last_gen) |
864 | BUG("bad generation skip %8x > %8x at %s", | |
865 | commit->generation, last_gen, | |
866 | oid_to_hex(&commit->object.oid)); | |
867 | last_gen = commit->generation; | |
868 | ||
869 | if (commit->generation < min_generation) | |
870 | break; | |
871 | ||
f3249438 JH |
872 | flags = commit->object.flags & (PARENT1 | PARENT2 | STALE); |
873 | if (flags == (PARENT1 | PARENT2)) { | |
874 | if (!(commit->object.flags & RESULT)) { | |
875 | commit->object.flags |= RESULT; | |
47e44ed1 | 876 | commit_list_insert_by_date(commit, &result); |
f3249438 | 877 | } |
542ccefe JH |
878 | /* Mark parents of a found merge stale */ |
879 | flags |= STALE; | |
7c6f8aaf JS |
880 | } |
881 | parents = commit->parents; | |
882 | while (parents) { | |
883 | struct commit *p = parents->item; | |
884 | parents = parents->next; | |
885 | if ((p->object.flags & flags) == flags) | |
886 | continue; | |
172947e6 MK |
887 | if (parse_commit(p)) |
888 | return NULL; | |
7c6f8aaf | 889 | p->object.flags |= flags; |
73f43f22 | 890 | prio_queue_put(&queue, p); |
7c6f8aaf JS |
891 | } |
892 | } | |
893 | ||
73f43f22 | 894 | clear_prio_queue(&queue); |
da1f5156 JH |
895 | return result; |
896 | } | |
897 | ||
898 | static struct commit_list *merge_bases_many(struct commit *one, int n, struct commit **twos) | |
899 | { | |
900 | struct commit_list *list = NULL; | |
901 | struct commit_list *result = NULL; | |
902 | int i; | |
903 | ||
904 | for (i = 0; i < n; i++) { | |
905 | if (one == twos[i]) | |
906 | /* | |
907 | * We do not mark this even with RESULT so we do not | |
908 | * have to clean it up. | |
909 | */ | |
910 | return commit_list_insert(one, &result); | |
911 | } | |
912 | ||
913 | if (parse_commit(one)) | |
914 | return NULL; | |
915 | for (i = 0; i < n; i++) { | |
916 | if (parse_commit(twos[i])) | |
917 | return NULL; | |
918 | } | |
919 | ||
d7c1ec3e | 920 | list = paint_down_to_common(one, n, twos, 0); |
da1f5156 | 921 | |
f3249438 | 922 | while (list) { |
e510ab89 RS |
923 | struct commit *commit = pop_commit(&list); |
924 | if (!(commit->object.flags & STALE)) | |
925 | commit_list_insert_by_date(commit, &result); | |
f3249438 JH |
926 | } |
927 | return result; | |
928 | } | |
7c6f8aaf | 929 | |
5240c9d7 MV |
930 | struct commit_list *get_octopus_merge_bases(struct commit_list *in) |
931 | { | |
932 | struct commit_list *i, *j, *k, *ret = NULL; | |
5240c9d7 | 933 | |
6bc76725 VM |
934 | if (!in) |
935 | return ret; | |
936 | ||
937 | commit_list_insert(in->item, &ret); | |
938 | ||
939 | for (i = in->next; i; i = i->next) { | |
258c03eb | 940 | struct commit_list *new_commits = NULL, *end = NULL; |
6bc76725 VM |
941 | |
942 | for (j = ret; j; j = j->next) { | |
943 | struct commit_list *bases; | |
2ce406cc | 944 | bases = get_merge_bases(i->item, j->item); |
258c03eb BW |
945 | if (!new_commits) |
946 | new_commits = bases; | |
6bc76725 VM |
947 | else |
948 | end->next = bases; | |
949 | for (k = bases; k; k = k->next) | |
950 | end = k; | |
5240c9d7 | 951 | } |
258c03eb | 952 | ret = new_commits; |
5240c9d7 MV |
953 | } |
954 | return ret; | |
955 | } | |
956 | ||
94f0ced0 JH |
957 | static int remove_redundant(struct commit **array, int cnt) |
958 | { | |
959 | /* | |
960 | * Some commit in the array may be an ancestor of | |
961 | * another commit. Move such commit to the end of | |
962 | * the array, and return the number of commits that | |
963 | * are independent from each other. | |
964 | */ | |
965 | struct commit **work; | |
966 | unsigned char *redundant; | |
967 | int *filled_index; | |
968 | int i, j, filled; | |
969 | ||
970 | work = xcalloc(cnt, sizeof(*work)); | |
971 | redundant = xcalloc(cnt, 1); | |
b32fa95f | 972 | ALLOC_ARRAY(filled_index, cnt - 1); |
94f0ced0 | 973 | |
d866924a JH |
974 | for (i = 0; i < cnt; i++) |
975 | parse_commit(array[i]); | |
94f0ced0 JH |
976 | for (i = 0; i < cnt; i++) { |
977 | struct commit_list *common; | |
04bc8d1e | 978 | uint32_t min_generation = array[i]->generation; |
94f0ced0 JH |
979 | |
980 | if (redundant[i]) | |
981 | continue; | |
982 | for (j = filled = 0; j < cnt; j++) { | |
983 | if (i == j || redundant[j]) | |
984 | continue; | |
985 | filled_index[filled] = j; | |
986 | work[filled++] = array[j]; | |
04bc8d1e DS |
987 | |
988 | if (array[j]->generation < min_generation) | |
989 | min_generation = array[j]->generation; | |
94f0ced0 | 990 | } |
04bc8d1e DS |
991 | common = paint_down_to_common(array[i], filled, work, |
992 | min_generation); | |
94f0ced0 JH |
993 | if (array[i]->object.flags & PARENT2) |
994 | redundant[i] = 1; | |
995 | for (j = 0; j < filled; j++) | |
996 | if (work[j]->object.flags & PARENT1) | |
997 | redundant[filled_index[j]] = 1; | |
998 | clear_commit_marks(array[i], all_flags); | |
abc03512 | 999 | clear_commit_marks_many(filled, work, all_flags); |
94f0ced0 JH |
1000 | free_commit_list(common); |
1001 | } | |
1002 | ||
1003 | /* Now collect the result */ | |
45ccef87 | 1004 | COPY_ARRAY(work, array, cnt); |
94f0ced0 JH |
1005 | for (i = filled = 0; i < cnt; i++) |
1006 | if (!redundant[i]) | |
1007 | array[filled++] = work[i]; | |
1008 | for (j = filled, i = 0; i < cnt; i++) | |
1009 | if (redundant[i]) | |
1010 | array[j++] = work[i]; | |
1011 | free(work); | |
1012 | free(redundant); | |
1013 | free(filled_index); | |
1014 | return filled; | |
1015 | } | |
1016 | ||
2ce406cc JH |
1017 | static struct commit_list *get_merge_bases_many_0(struct commit *one, |
1018 | int n, | |
1019 | struct commit **twos, | |
1020 | int cleanup) | |
f3249438 | 1021 | { |
f3249438 JH |
1022 | struct commit_list *list; |
1023 | struct commit **rslt; | |
1024 | struct commit_list *result; | |
94f0ced0 | 1025 | int cnt, i; |
f3249438 | 1026 | |
6a938648 JH |
1027 | result = merge_bases_many(one, n, twos); |
1028 | for (i = 0; i < n; i++) { | |
1029 | if (one == twos[i]) | |
1030 | return result; | |
1031 | } | |
f3249438 JH |
1032 | if (!result || !result->next) { |
1033 | if (cleanup) { | |
1034 | clear_commit_marks(one, all_flags); | |
e895cb51 | 1035 | clear_commit_marks_many(n, twos, all_flags); |
7c6f8aaf | 1036 | } |
f3249438 | 1037 | return result; |
7c6f8aaf JS |
1038 | } |
1039 | ||
f3249438 | 1040 | /* There are more than one */ |
4bbaa1eb | 1041 | cnt = commit_list_count(result); |
f3249438 JH |
1042 | rslt = xcalloc(cnt, sizeof(*rslt)); |
1043 | for (list = result, i = 0; list; list = list->next) | |
1044 | rslt[i++] = list->item; | |
1045 | free_commit_list(result); | |
1046 | ||
1047 | clear_commit_marks(one, all_flags); | |
e895cb51 | 1048 | clear_commit_marks_many(n, twos, all_flags); |
7c6f8aaf | 1049 | |
94f0ced0 | 1050 | cnt = remove_redundant(rslt, cnt); |
f3249438 | 1051 | result = NULL; |
94f0ced0 JH |
1052 | for (i = 0; i < cnt; i++) |
1053 | commit_list_insert_by_date(rslt[i], &result); | |
f3249438 | 1054 | free(rslt); |
7c6f8aaf JS |
1055 | return result; |
1056 | } | |
2ecd2bbc | 1057 | |
2ce406cc JH |
1058 | struct commit_list *get_merge_bases_many(struct commit *one, |
1059 | int n, | |
1060 | struct commit **twos) | |
1061 | { | |
1062 | return get_merge_bases_many_0(one, n, twos, 1); | |
1063 | } | |
1064 | ||
1065 | struct commit_list *get_merge_bases_many_dirty(struct commit *one, | |
1066 | int n, | |
1067 | struct commit **twos) | |
1068 | { | |
1069 | return get_merge_bases_many_0(one, n, twos, 0); | |
1070 | } | |
1071 | ||
1072 | struct commit_list *get_merge_bases(struct commit *one, struct commit *two) | |
6a938648 | 1073 | { |
2ce406cc | 1074 | return get_merge_bases_many_0(one, 1, &two, 1); |
6a938648 JH |
1075 | } |
1076 | ||
a20efee9 | 1077 | /* |
41ccfdd9 | 1078 | * Is "commit" a descendant of one of the elements on the "with_commit" list? |
a20efee9 | 1079 | */ |
7fcdb36e JG |
1080 | int is_descendant_of(struct commit *commit, struct commit_list *with_commit) |
1081 | { | |
1082 | if (!with_commit) | |
1083 | return 1; | |
1084 | while (with_commit) { | |
1085 | struct commit *other; | |
1086 | ||
1087 | other = with_commit->item; | |
1088 | with_commit = with_commit->next; | |
a20efee9 | 1089 | if (in_merge_bases(other, commit)) |
7fcdb36e JG |
1090 | return 1; |
1091 | } | |
1092 | return 0; | |
1093 | } | |
1094 | ||
a20efee9 | 1095 | /* |
4c4b27e8 | 1096 | * Is "commit" an ancestor of one of the "references"? |
a20efee9 | 1097 | */ |
4c4b27e8 | 1098 | int in_merge_bases_many(struct commit *commit, int nr_reference, struct commit **reference) |
2ecd2bbc | 1099 | { |
6440fdba | 1100 | struct commit_list *bases; |
4c4b27e8 | 1101 | int ret = 0, i; |
f9b8908b | 1102 | uint32_t min_generation = GENERATION_NUMBER_INFINITY; |
2ecd2bbc | 1103 | |
4c4b27e8 | 1104 | if (parse_commit(commit)) |
6440fdba | 1105 | return ret; |
f9b8908b | 1106 | for (i = 0; i < nr_reference; i++) { |
4c4b27e8 JH |
1107 | if (parse_commit(reference[i])) |
1108 | return ret; | |
f9b8908b DS |
1109 | if (reference[i]->generation < min_generation) |
1110 | min_generation = reference[i]->generation; | |
1111 | } | |
1112 | ||
1113 | if (commit->generation > min_generation) | |
1114 | return ret; | |
2ecd2bbc | 1115 | |
d7c1ec3e | 1116 | bases = paint_down_to_common(commit, nr_reference, reference, commit->generation); |
6440fdba JH |
1117 | if (commit->object.flags & PARENT2) |
1118 | ret = 1; | |
b0f9e9ee | 1119 | clear_commit_marks(commit, all_flags); |
557899ff | 1120 | clear_commit_marks_many(nr_reference, reference, all_flags); |
2ecd2bbc JH |
1121 | free_commit_list(bases); |
1122 | return ret; | |
1123 | } | |
98cf9c3b | 1124 | |
4c4b27e8 JH |
1125 | /* |
1126 | * Is "commit" an ancestor of (i.e. reachable from) the "reference"? | |
1127 | */ | |
1128 | int in_merge_bases(struct commit *commit, struct commit *reference) | |
1129 | { | |
1130 | return in_merge_bases_many(commit, 1, &reference); | |
1131 | } | |
1132 | ||
98cf9c3b JH |
1133 | struct commit_list *reduce_heads(struct commit_list *heads) |
1134 | { | |
1135 | struct commit_list *p; | |
1136 | struct commit_list *result = NULL, **tail = &result; | |
f37d3c75 JH |
1137 | struct commit **array; |
1138 | int num_head, i; | |
98cf9c3b JH |
1139 | |
1140 | if (!heads) | |
1141 | return NULL; | |
1142 | ||
f37d3c75 JH |
1143 | /* Uniquify */ |
1144 | for (p = heads; p; p = p->next) | |
1145 | p->item->object.flags &= ~STALE; | |
1146 | for (p = heads, num_head = 0; p; p = p->next) { | |
1147 | if (p->item->object.flags & STALE) | |
711f6b29 | 1148 | continue; |
f37d3c75 JH |
1149 | p->item->object.flags |= STALE; |
1150 | num_head++; | |
1151 | } | |
c4a7b009 | 1152 | array = xcalloc(num_head, sizeof(*array)); |
f37d3c75 JH |
1153 | for (p = heads, i = 0; p; p = p->next) { |
1154 | if (p->item->object.flags & STALE) { | |
1155 | array[i++] = p->item; | |
1156 | p->item->object.flags &= ~STALE; | |
98cf9c3b | 1157 | } |
98cf9c3b | 1158 | } |
f37d3c75 JH |
1159 | num_head = remove_redundant(array, num_head); |
1160 | for (i = 0; i < num_head; i++) | |
1161 | tail = &commit_list_insert(array[i], tail)->next; | |
cb7b29eb | 1162 | free(array); |
98cf9c3b JH |
1163 | return result; |
1164 | } | |
40d52ff7 | 1165 | |
4da72644 MÅ |
1166 | void reduce_heads_replace(struct commit_list **heads) |
1167 | { | |
1168 | struct commit_list *result = reduce_heads(*heads); | |
1169 | free_commit_list(*heads); | |
1170 | *heads = result; | |
1171 | } | |
1172 | ||
ba3c69a9 JH |
1173 | static const char gpg_sig_header[] = "gpgsig"; |
1174 | static const int gpg_sig_header_len = sizeof(gpg_sig_header) - 1; | |
1175 | ||
1176 | static int do_sign_commit(struct strbuf *buf, const char *keyid) | |
1177 | { | |
1178 | struct strbuf sig = STRBUF_INIT; | |
1179 | int inspos, copypos; | |
3324dd8f | 1180 | const char *eoh; |
ba3c69a9 JH |
1181 | |
1182 | /* find the end of the header */ | |
3324dd8f JS |
1183 | eoh = strstr(buf->buf, "\n\n"); |
1184 | if (!eoh) | |
1185 | inspos = buf->len; | |
1186 | else | |
1187 | inspos = eoh - buf->buf + 1; | |
ba3c69a9 JH |
1188 | |
1189 | if (!keyid || !*keyid) | |
1190 | keyid = get_signing_key(); | |
1191 | if (sign_buffer(buf, &sig, keyid)) { | |
1192 | strbuf_release(&sig); | |
1193 | return -1; | |
1194 | } | |
1195 | ||
1196 | for (copypos = 0; sig.buf[copypos]; ) { | |
1197 | const char *bol = sig.buf + copypos; | |
1198 | const char *eol = strchrnul(bol, '\n'); | |
1199 | int len = (eol - bol) + !!*eol; | |
1200 | ||
1201 | if (!copypos) { | |
1202 | strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len); | |
1203 | inspos += gpg_sig_header_len; | |
1204 | } | |
1205 | strbuf_insert(buf, inspos++, " ", 1); | |
1206 | strbuf_insert(buf, inspos, bol, len); | |
1207 | inspos += len; | |
1208 | copypos += len; | |
1209 | } | |
1210 | strbuf_release(&sig); | |
1211 | return 0; | |
1212 | } | |
1213 | ||
218aa3a6 | 1214 | int parse_signed_commit(const struct commit *commit, |
0c37f1fc JH |
1215 | struct strbuf *payload, struct strbuf *signature) |
1216 | { | |
218aa3a6 | 1217 | |
0c37f1fc | 1218 | unsigned long size; |
218aa3a6 | 1219 | const char *buffer = get_commit_buffer(commit, &size); |
0c37f1fc | 1220 | int in_signature, saw_signature = -1; |
218aa3a6 | 1221 | const char *line, *tail; |
0c37f1fc JH |
1222 | |
1223 | line = buffer; | |
1224 | tail = buffer + size; | |
1225 | in_signature = 0; | |
1226 | saw_signature = 0; | |
1227 | while (line < tail) { | |
1228 | const char *sig = NULL; | |
218aa3a6 | 1229 | const char *next = memchr(line, '\n', tail - line); |
0c37f1fc JH |
1230 | |
1231 | next = next ? next + 1 : tail; | |
1232 | if (in_signature && line[0] == ' ') | |
1233 | sig = line + 1; | |
59556548 | 1234 | else if (starts_with(line, gpg_sig_header) && |
0c37f1fc JH |
1235 | line[gpg_sig_header_len] == ' ') |
1236 | sig = line + gpg_sig_header_len + 1; | |
1237 | if (sig) { | |
1238 | strbuf_add(signature, sig, next - sig); | |
1239 | saw_signature = 1; | |
1240 | in_signature = 1; | |
1241 | } else { | |
1242 | if (*line == '\n') | |
1243 | /* dump the whole remainder of the buffer */ | |
1244 | next = tail; | |
1245 | strbuf_add(payload, line, next - line); | |
1246 | in_signature = 0; | |
1247 | } | |
1248 | line = next; | |
1249 | } | |
218aa3a6 | 1250 | unuse_commit_buffer(commit, buffer); |
0c37f1fc JH |
1251 | return saw_signature; |
1252 | } | |
1253 | ||
0b05ab6f CC |
1254 | int remove_signature(struct strbuf *buf) |
1255 | { | |
1256 | const char *line = buf->buf; | |
1257 | const char *tail = buf->buf + buf->len; | |
1258 | int in_signature = 0; | |
1259 | const char *sig_start = NULL; | |
1260 | const char *sig_end = NULL; | |
1261 | ||
1262 | while (line < tail) { | |
1263 | const char *next = memchr(line, '\n', tail - line); | |
1264 | next = next ? next + 1 : tail; | |
1265 | ||
1266 | if (in_signature && line[0] == ' ') | |
1267 | sig_end = next; | |
1268 | else if (starts_with(line, gpg_sig_header) && | |
1269 | line[gpg_sig_header_len] == ' ') { | |
1270 | sig_start = line; | |
1271 | sig_end = next; | |
1272 | in_signature = 1; | |
1273 | } else { | |
1274 | if (*line == '\n') | |
1275 | /* dump the whole remainder of the buffer */ | |
1276 | next = tail; | |
1277 | in_signature = 0; | |
1278 | } | |
1279 | line = next; | |
1280 | } | |
1281 | ||
1282 | if (sig_start) | |
1283 | strbuf_remove(buf, sig_start - buf->buf, sig_end - sig_start); | |
1284 | ||
1285 | return sig_start != NULL; | |
1286 | } | |
1287 | ||
5231c633 JH |
1288 | static void handle_signed_tag(struct commit *parent, struct commit_extra_header ***tail) |
1289 | { | |
1290 | struct merge_remote_desc *desc; | |
1291 | struct commit_extra_header *mergetag; | |
1292 | char *buf; | |
1293 | unsigned long size, len; | |
1294 | enum object_type type; | |
1295 | ||
1296 | desc = merge_remote_util(parent); | |
1297 | if (!desc || !desc->obj) | |
1298 | return; | |
b4f5aca4 | 1299 | buf = read_object_file(&desc->obj->oid, &type, &size); |
5231c633 JH |
1300 | if (!buf || type != OBJ_TAG) |
1301 | goto free_return; | |
1302 | len = parse_signature(buf, size); | |
1303 | if (size == len) | |
1304 | goto free_return; | |
1305 | /* | |
1306 | * We could verify this signature and either omit the tag when | |
1307 | * it does not validate, but the integrator may not have the | |
1308 | * public key of the signer of the tag he is merging, while a | |
1309 | * later auditor may have it while auditing, so let's not run | |
1310 | * verify-signed-buffer here for now... | |
1311 | * | |
1312 | * if (verify_signed_buffer(buf, len, buf + len, size - len, ...)) | |
1313 | * warn("warning: signed tag unverified."); | |
1314 | */ | |
1315 | mergetag = xcalloc(1, sizeof(*mergetag)); | |
1316 | mergetag->key = xstrdup("mergetag"); | |
1317 | mergetag->value = buf; | |
1318 | mergetag->len = size; | |
1319 | ||
1320 | **tail = mergetag; | |
1321 | *tail = &mergetag->next; | |
1322 | return; | |
1323 | ||
1324 | free_return: | |
1325 | free(buf); | |
1326 | } | |
1327 | ||
434060ec | 1328 | int check_commit_signature(const struct commit *commit, struct signature_check *sigc) |
ffb6d7d5 SG |
1329 | { |
1330 | struct strbuf payload = STRBUF_INIT; | |
1331 | struct strbuf signature = STRBUF_INIT; | |
434060ec | 1332 | int ret = 1; |
ffb6d7d5 SG |
1333 | |
1334 | sigc->result = 'N'; | |
1335 | ||
218aa3a6 | 1336 | if (parse_signed_commit(commit, &payload, &signature) <= 0) |
ffb6d7d5 | 1337 | goto out; |
434060ec | 1338 | ret = check_signature(payload.buf, payload.len, signature.buf, |
1339 | signature.len, sigc); | |
ffb6d7d5 SG |
1340 | |
1341 | out: | |
ffb6d7d5 SG |
1342 | strbuf_release(&payload); |
1343 | strbuf_release(&signature); | |
434060ec | 1344 | |
1345 | return ret; | |
ffb6d7d5 SG |
1346 | } |
1347 | ||
1348 | ||
1349 | ||
5231c633 JH |
1350 | void append_merge_tag_headers(struct commit_list *parents, |
1351 | struct commit_extra_header ***tail) | |
1352 | { | |
1353 | while (parents) { | |
1354 | struct commit *parent = parents->item; | |
1355 | handle_signed_tag(parent, tail); | |
1356 | parents = parents->next; | |
1357 | } | |
1358 | } | |
1359 | ||
1360 | static void add_extra_header(struct strbuf *buffer, | |
1361 | struct commit_extra_header *extra) | |
1362 | { | |
1363 | strbuf_addstr(buffer, extra->key); | |
ed7a42a0 JH |
1364 | if (extra->len) |
1365 | strbuf_add_lines(buffer, " ", extra->value, extra->len); | |
1366 | else | |
1367 | strbuf_addch(buffer, '\n'); | |
1368 | } | |
1369 | ||
c871a1d1 JH |
1370 | struct commit_extra_header *read_commit_extra_headers(struct commit *commit, |
1371 | const char **exclude) | |
ed7a42a0 JH |
1372 | { |
1373 | struct commit_extra_header *extra = NULL; | |
1374 | unsigned long size; | |
218aa3a6 JK |
1375 | const char *buffer = get_commit_buffer(commit, &size); |
1376 | extra = read_commit_extra_header_lines(buffer, size, exclude); | |
1377 | unuse_commit_buffer(commit, buffer); | |
ed7a42a0 JH |
1378 | return extra; |
1379 | } | |
1380 | ||
fef461ea | 1381 | int for_each_mergetag(each_mergetag_fn fn, struct commit *commit, void *data) |
063da62b CC |
1382 | { |
1383 | struct commit_extra_header *extra, *to_free; | |
fef461ea | 1384 | int res = 0; |
063da62b CC |
1385 | |
1386 | to_free = read_commit_extra_headers(commit, NULL); | |
fef461ea | 1387 | for (extra = to_free; !res && extra; extra = extra->next) { |
063da62b CC |
1388 | if (strcmp(extra->key, "mergetag")) |
1389 | continue; /* not a merge tag */ | |
fef461ea | 1390 | res = fn(commit, extra, data); |
063da62b CC |
1391 | } |
1392 | free_commit_extra_headers(to_free); | |
fef461ea | 1393 | return res; |
063da62b CC |
1394 | } |
1395 | ||
ed7a42a0 JH |
1396 | static inline int standard_header_field(const char *field, size_t len) |
1397 | { | |
b072504c RS |
1398 | return ((len == 4 && !memcmp(field, "tree", 4)) || |
1399 | (len == 6 && !memcmp(field, "parent", 6)) || | |
1400 | (len == 6 && !memcmp(field, "author", 6)) || | |
1401 | (len == 9 && !memcmp(field, "committer", 9)) || | |
1402 | (len == 8 && !memcmp(field, "encoding", 8))); | |
ed7a42a0 JH |
1403 | } |
1404 | ||
c871a1d1 JH |
1405 | static int excluded_header_field(const char *field, size_t len, const char **exclude) |
1406 | { | |
1407 | if (!exclude) | |
1408 | return 0; | |
1409 | ||
1410 | while (*exclude) { | |
1411 | size_t xlen = strlen(*exclude); | |
b072504c | 1412 | if (len == xlen && !memcmp(field, *exclude, xlen)) |
c871a1d1 JH |
1413 | return 1; |
1414 | exclude++; | |
1415 | } | |
1416 | return 0; | |
1417 | } | |
1418 | ||
82a75299 JH |
1419 | static struct commit_extra_header *read_commit_extra_header_lines( |
1420 | const char *buffer, size_t size, | |
1421 | const char **exclude) | |
ed7a42a0 JH |
1422 | { |
1423 | struct commit_extra_header *extra = NULL, **tail = &extra, *it = NULL; | |
1424 | const char *line, *next, *eof, *eob; | |
1425 | struct strbuf buf = STRBUF_INIT; | |
1426 | ||
1427 | for (line = buffer, eob = line + size; | |
1428 | line < eob && *line != '\n'; | |
1429 | line = next) { | |
1430 | next = memchr(line, '\n', eob - line); | |
1431 | next = next ? next + 1 : eob; | |
1432 | if (*line == ' ') { | |
1433 | /* continuation */ | |
1434 | if (it) | |
1435 | strbuf_add(&buf, line + 1, next - (line + 1)); | |
1436 | continue; | |
1437 | } | |
1438 | if (it) | |
1439 | it->value = strbuf_detach(&buf, &it->len); | |
1440 | strbuf_reset(&buf); | |
1441 | it = NULL; | |
1442 | ||
50a01cc4 RS |
1443 | eof = memchr(line, ' ', next - line); |
1444 | if (!eof) | |
ed7a42a0 | 1445 | eof = next; |
b072504c RS |
1446 | else if (standard_header_field(line, eof - line) || |
1447 | excluded_header_field(line, eof - line, exclude)) | |
ed7a42a0 JH |
1448 | continue; |
1449 | ||
1450 | it = xcalloc(1, sizeof(*it)); | |
1451 | it->key = xmemdupz(line, eof-line); | |
1452 | *tail = it; | |
1453 | tail = &it->next; | |
1454 | if (eof + 1 < next) | |
1455 | strbuf_add(&buf, eof + 1, next - (eof + 1)); | |
1456 | } | |
1457 | if (it) | |
1458 | it->value = strbuf_detach(&buf, &it->len); | |
1459 | return extra; | |
5231c633 JH |
1460 | } |
1461 | ||
1462 | void free_commit_extra_headers(struct commit_extra_header *extra) | |
1463 | { | |
1464 | while (extra) { | |
1465 | struct commit_extra_header *next = extra->next; | |
1466 | free(extra->key); | |
1467 | free(extra->value); | |
1468 | free(extra); | |
1469 | extra = next; | |
1470 | } | |
1471 | } | |
1472 | ||
5078f344 PO |
1473 | int commit_tree(const char *msg, size_t msg_len, const struct object_id *tree, |
1474 | struct commit_list *parents, struct object_id *ret, | |
ba3c69a9 | 1475 | const char *author, const char *sign_commit) |
5231c633 JH |
1476 | { |
1477 | struct commit_extra_header *extra = NULL, **tail = &extra; | |
1478 | int result; | |
1479 | ||
1480 | append_merge_tag_headers(parents, &tail); | |
3ffefb54 | 1481 | result = commit_tree_extended(msg, msg_len, tree, parents, ret, |
ba3c69a9 | 1482 | author, sign_commit, extra); |
5231c633 JH |
1483 | free_commit_extra_headers(extra); |
1484 | return result; | |
1485 | } | |
1486 | ||
08a94a14 LT |
1487 | static int find_invalid_utf8(const char *buf, int len) |
1488 | { | |
1489 | int offset = 0; | |
e82bd6cc | 1490 | static const unsigned int max_codepoint[] = { |
1491 | 0x7f, 0x7ff, 0xffff, 0x10ffff | |
1492 | }; | |
08a94a14 LT |
1493 | |
1494 | while (len) { | |
1495 | unsigned char c = *buf++; | |
1496 | int bytes, bad_offset; | |
28110d4b | 1497 | unsigned int codepoint; |
e82bd6cc | 1498 | unsigned int min_val, max_val; |
08a94a14 LT |
1499 | |
1500 | len--; | |
1501 | offset++; | |
1502 | ||
1503 | /* Simple US-ASCII? No worries. */ | |
1504 | if (c < 0x80) | |
1505 | continue; | |
1506 | ||
1507 | bad_offset = offset-1; | |
1508 | ||
1509 | /* | |
1510 | * Count how many more high bits set: that's how | |
1511 | * many more bytes this sequence should have. | |
1512 | */ | |
1513 | bytes = 0; | |
1514 | while (c & 0x40) { | |
1515 | c <<= 1; | |
1516 | bytes++; | |
1517 | } | |
1518 | ||
28110d4b | 1519 | /* |
1520 | * Must be between 1 and 3 more bytes. Longer sequences result in | |
1521 | * codepoints beyond U+10FFFF, which are guaranteed never to exist. | |
1522 | */ | |
1523 | if (bytes < 1 || 3 < bytes) | |
08a94a14 LT |
1524 | return bad_offset; |
1525 | ||
1526 | /* Do we *have* that many bytes? */ | |
1527 | if (len < bytes) | |
1528 | return bad_offset; | |
1529 | ||
e82bd6cc | 1530 | /* |
1531 | * Place the encoded bits at the bottom of the value and compute the | |
1532 | * valid range. | |
1533 | */ | |
28110d4b | 1534 | codepoint = (c & 0x7f) >> bytes; |
e82bd6cc | 1535 | min_val = max_codepoint[bytes-1] + 1; |
1536 | max_val = max_codepoint[bytes]; | |
28110d4b | 1537 | |
08a94a14 LT |
1538 | offset += bytes; |
1539 | len -= bytes; | |
1540 | ||
1541 | /* And verify that they are good continuation bytes */ | |
1542 | do { | |
28110d4b | 1543 | codepoint <<= 6; |
1544 | codepoint |= *buf & 0x3f; | |
08a94a14 LT |
1545 | if ((*buf++ & 0xc0) != 0x80) |
1546 | return bad_offset; | |
1547 | } while (--bytes); | |
1548 | ||
e82bd6cc | 1549 | /* Reject codepoints that are out of range for the sequence length. */ |
1550 | if (codepoint < min_val || codepoint > max_val) | |
28110d4b | 1551 | return bad_offset; |
1552 | /* Surrogates are only for UTF-16 and cannot be encoded in UTF-8. */ | |
1553 | if ((codepoint & 0x1ff800) == 0xd800) | |
1554 | return bad_offset; | |
81050ac6 | 1555 | /* U+xxFFFE and U+xxFFFF are guaranteed non-characters. */ |
dc773a67 | 1556 | if ((codepoint & 0xfffe) == 0xfffe) |
81050ac6 PK |
1557 | return bad_offset; |
1558 | /* So are anything in the range U+FDD0..U+FDEF. */ | |
1559 | if (codepoint >= 0xfdd0 && codepoint <= 0xfdef) | |
28110d4b | 1560 | return bad_offset; |
08a94a14 LT |
1561 | } |
1562 | return -1; | |
1563 | } | |
1564 | ||
1565 | /* | |
1566 | * This verifies that the buffer is in proper utf8 format. | |
1567 | * | |
1568 | * If it isn't, it assumes any non-utf8 characters are Latin1, | |
1569 | * and does the conversion. | |
08a94a14 LT |
1570 | */ |
1571 | static int verify_utf8(struct strbuf *buf) | |
1572 | { | |
1573 | int ok = 1; | |
1574 | long pos = 0; | |
1575 | ||
1576 | for (;;) { | |
1577 | int bad; | |
1578 | unsigned char c; | |
1579 | unsigned char replace[2]; | |
1580 | ||
1581 | bad = find_invalid_utf8(buf->buf + pos, buf->len - pos); | |
1582 | if (bad < 0) | |
1583 | return ok; | |
1584 | pos += bad; | |
1585 | ok = 0; | |
1586 | c = buf->buf[pos]; | |
1587 | strbuf_remove(buf, pos, 1); | |
1588 | ||
1589 | /* We know 'c' must be in the range 128-255 */ | |
1590 | replace[0] = 0xc0 + (c >> 6); | |
1591 | replace[1] = 0x80 + (c & 0x3f); | |
1592 | strbuf_insert(buf, pos, replace, 2); | |
1593 | pos += 2; | |
1594 | } | |
1595 | } | |
1596 | ||
40d52ff7 | 1597 | static const char commit_utf8_warn[] = |
4fa4b315 VA |
1598 | N_("Warning: commit message did not conform to UTF-8.\n" |
1599 | "You may want to amend it after fixing the message, or set the config\n" | |
1600 | "variable i18n.commitencoding to the encoding your project uses.\n"); | |
40d52ff7 | 1601 | |
3ffefb54 | 1602 | int commit_tree_extended(const char *msg, size_t msg_len, |
5078f344 PO |
1603 | const struct object_id *tree, |
1604 | struct commit_list *parents, struct object_id *ret, | |
ba3c69a9 JH |
1605 | const char *author, const char *sign_commit, |
1606 | struct commit_extra_header *extra) | |
40d52ff7 JK |
1607 | { |
1608 | int result; | |
1609 | int encoding_is_utf8; | |
1610 | struct strbuf buffer; | |
1611 | ||
e816caa0 | 1612 | assert_oid_type(tree, OBJ_TREE); |
40d52ff7 | 1613 | |
3ffefb54 | 1614 | if (memchr(msg, '\0', msg_len)) |
37576c14 NTND |
1615 | return error("a NUL byte in commit log message not allowed."); |
1616 | ||
40d52ff7 JK |
1617 | /* Not having i18n.commitencoding is the same as having utf-8 */ |
1618 | encoding_is_utf8 = is_encoding_utf8(git_commit_encoding); | |
1619 | ||
1620 | strbuf_init(&buffer, 8192); /* should avoid reallocs for the headers */ | |
5078f344 | 1621 | strbuf_addf(&buffer, "tree %s\n", oid_to_hex(tree)); |
40d52ff7 JK |
1622 | |
1623 | /* | |
1624 | * NOTE! This ordering means that the same exact tree merged with a | |
1625 | * different order of parents will be a _different_ changeset even | |
1626 | * if everything else stays the same. | |
1627 | */ | |
1628 | while (parents) { | |
e510ab89 | 1629 | struct commit *parent = pop_commit(&parents); |
40d52ff7 | 1630 | strbuf_addf(&buffer, "parent %s\n", |
f2fd0760 | 1631 | oid_to_hex(&parent->object.oid)); |
40d52ff7 JK |
1632 | } |
1633 | ||
1634 | /* Person/date information */ | |
1635 | if (!author) | |
f9bc573f | 1636 | author = git_author_info(IDENT_STRICT); |
40d52ff7 | 1637 | strbuf_addf(&buffer, "author %s\n", author); |
f9bc573f | 1638 | strbuf_addf(&buffer, "committer %s\n", git_committer_info(IDENT_STRICT)); |
40d52ff7 JK |
1639 | if (!encoding_is_utf8) |
1640 | strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding); | |
5231c633 JH |
1641 | |
1642 | while (extra) { | |
1643 | add_extra_header(&buffer, extra); | |
1644 | extra = extra->next; | |
1645 | } | |
40d52ff7 JK |
1646 | strbuf_addch(&buffer, '\n'); |
1647 | ||
1648 | /* And add the comment */ | |
3ffefb54 | 1649 | strbuf_add(&buffer, msg, msg_len); |
40d52ff7 JK |
1650 | |
1651 | /* And check the encoding */ | |
08a94a14 | 1652 | if (encoding_is_utf8 && !verify_utf8(&buffer)) |
4fa4b315 | 1653 | fprintf(stderr, _(commit_utf8_warn)); |
40d52ff7 | 1654 | |
e505146d RS |
1655 | if (sign_commit && do_sign_commit(&buffer, sign_commit)) { |
1656 | result = -1; | |
1657 | goto out; | |
1658 | } | |
ba3c69a9 | 1659 | |
a09c985e | 1660 | result = write_object_file(buffer.buf, buffer.len, commit_type, ret); |
e505146d | 1661 | out: |
40d52ff7 JK |
1662 | strbuf_release(&buffer); |
1663 | return result; | |
1664 | } | |
ae8e4c9c | 1665 | |
e2e5ac23 NTND |
1666 | define_commit_slab(merge_desc_slab, struct merge_remote_desc *); |
1667 | static struct merge_desc_slab merge_desc_slab = COMMIT_SLAB_INIT(1, merge_desc_slab); | |
1668 | ||
1669 | struct merge_remote_desc *merge_remote_util(struct commit *commit) | |
1670 | { | |
1671 | return *merge_desc_slab_at(&merge_desc_slab, commit); | |
1672 | } | |
1673 | ||
beb518c9 RS |
1674 | void set_merge_remote_desc(struct commit *commit, |
1675 | const char *name, struct object *obj) | |
1676 | { | |
1677 | struct merge_remote_desc *desc; | |
5447a76a | 1678 | FLEX_ALLOC_STR(desc, name, name); |
beb518c9 | 1679 | desc->obj = obj; |
e2e5ac23 | 1680 | *merge_desc_slab_at(&merge_desc_slab, commit) = desc; |
beb518c9 RS |
1681 | } |
1682 | ||
ae8e4c9c JH |
1683 | struct commit *get_merge_parent(const char *name) |
1684 | { | |
1685 | struct object *obj; | |
1686 | struct commit *commit; | |
7683e2e6 | 1687 | struct object_id oid; |
e82caf38 | 1688 | if (get_oid(name, &oid)) |
ae8e4c9c | 1689 | return NULL; |
c251c83d | 1690 | obj = parse_object(&oid); |
ae8e4c9c | 1691 | commit = (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); |
e2e5ac23 | 1692 | if (commit && !merge_remote_util(commit)) |
beb518c9 | 1693 | set_merge_remote_desc(commit, name, obj); |
ae8e4c9c JH |
1694 | return commit; |
1695 | } | |
89b5f1d9 RS |
1696 | |
1697 | /* | |
1698 | * Append a commit to the end of the commit_list. | |
1699 | * | |
1700 | * next starts by pointing to the variable that holds the head of an | |
1701 | * empty commit_list, and is updated to point to the "next" field of | |
1702 | * the last item on the list as new commits are appended. | |
1703 | * | |
1704 | * Usage example: | |
1705 | * | |
1706 | * struct commit_list *list; | |
1707 | * struct commit_list **next = &list; | |
1708 | * | |
1709 | * next = commit_list_append(c1, next); | |
1710 | * next = commit_list_append(c2, next); | |
1711 | * assert(commit_list_count(list) == 2); | |
1712 | * return list; | |
1713 | */ | |
1714 | struct commit_list **commit_list_append(struct commit *commit, | |
1715 | struct commit_list **next) | |
1716 | { | |
258c03eb BW |
1717 | struct commit_list *new_commit = xmalloc(sizeof(struct commit_list)); |
1718 | new_commit->item = commit; | |
1719 | *next = new_commit; | |
1720 | new_commit->next = NULL; | |
1721 | return &new_commit->next; | |
89b5f1d9 | 1722 | } |
efc7df45 | 1723 | |
fe6eb7f2 JK |
1724 | const char *find_commit_header(const char *msg, const char *key, size_t *out_len) |
1725 | { | |
1726 | int key_len = strlen(key); | |
1727 | const char *line = msg; | |
1728 | ||
1729 | while (line) { | |
1730 | const char *eol = strchrnul(line, '\n'); | |
1731 | ||
1732 | if (line == eol) | |
1733 | return NULL; | |
1734 | ||
1735 | if (eol - line > key_len && | |
1736 | !strncmp(line, key, key_len) && | |
1737 | line[key_len] == ' ') { | |
1738 | *out_len = eol - line - key_len - 1; | |
1739 | return line + key_len + 1; | |
1740 | } | |
1741 | line = *eol ? eol + 1 : NULL; | |
1742 | } | |
1743 | return NULL; | |
1744 | } | |
0ed8a4e1 | 1745 | |
8c384589 | 1746 | /* |
710714aa | 1747 | * Inspect the given string and determine the true "end" of the log message, in |
8c384589 | 1748 | * order to find where to put a new Signed-off-by: line. Ignored are |
d76650b8 BM |
1749 | * trailing comment lines and blank lines. To support "git commit -s |
1750 | * --amend" on an existing commit, we also ignore "Conflicts:". To | |
1751 | * support "git commit -v", we truncate at cut lines. | |
8c384589 CC |
1752 | * |
1753 | * Returns the number of bytes from the tail to ignore, to be fed as | |
1754 | * the second parameter to append_signoff(). | |
1755 | */ | |
710714aa | 1756 | int ignore_non_trailer(const char *buf, size_t len) |
8c384589 CC |
1757 | { |
1758 | int boc = 0; | |
1759 | int bol = 0; | |
1760 | int in_old_conflicts_block = 0; | |
d76650b8 | 1761 | size_t cutoff = wt_status_locate_end(buf, len); |
8c384589 | 1762 | |
d76650b8 | 1763 | while (bol < cutoff) { |
710714aa | 1764 | const char *next_line = memchr(buf + bol, '\n', len - bol); |
8c384589 | 1765 | |
710714aa JT |
1766 | if (!next_line) |
1767 | next_line = buf + len; | |
8c384589 CC |
1768 | else |
1769 | next_line++; | |
1770 | ||
710714aa | 1771 | if (buf[bol] == comment_line_char || buf[bol] == '\n') { |
8c384589 CC |
1772 | /* is this the first of the run of comments? */ |
1773 | if (!boc) | |
1774 | boc = bol; | |
1775 | /* otherwise, it is just continuing */ | |
710714aa | 1776 | } else if (starts_with(buf + bol, "Conflicts:\n")) { |
8c384589 CC |
1777 | in_old_conflicts_block = 1; |
1778 | if (!boc) | |
1779 | boc = bol; | |
710714aa | 1780 | } else if (in_old_conflicts_block && buf[bol] == '\t') { |
8c384589 CC |
1781 | ; /* a pathname in the conflicts block */ |
1782 | } else if (boc) { | |
1783 | /* the previous was not trailing comment */ | |
1784 | boc = 0; | |
1785 | in_old_conflicts_block = 0; | |
1786 | } | |
710714aa | 1787 | bol = next_line - buf; |
8c384589 | 1788 | } |
d76650b8 | 1789 | return boc ? len - boc : len - cutoff; |
8c384589 | 1790 | } |