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