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