]> git.ipfire.org Git - thirdparty/git.git/blob - commit.c
Test suite for sliding window mmap implementation.
[thirdparty/git.git] / commit.c
1 #include "cache.h"
2 #include "tag.h"
3 #include "commit.h"
4 #include "pkt-line.h"
5 #include "utf8.h"
6
7 int save_commit_buffer = 1;
8
9 struct sort_node
10 {
11 /*
12 * the number of children of the associated commit
13 * that also occur in the list being sorted.
14 */
15 unsigned int indegree;
16
17 /*
18 * reference to original list item that we will re-use
19 * on output.
20 */
21 struct commit_list * list_item;
22
23 };
24
25 const char *commit_type = "commit";
26
27 struct cmt_fmt_map {
28 const char *n;
29 size_t cmp_len;
30 enum cmit_fmt v;
31 } cmt_fmts[] = {
32 { "raw", 1, CMIT_FMT_RAW },
33 { "medium", 1, CMIT_FMT_MEDIUM },
34 { "short", 1, CMIT_FMT_SHORT },
35 { "email", 1, CMIT_FMT_EMAIL },
36 { "full", 5, CMIT_FMT_FULL },
37 { "fuller", 5, CMIT_FMT_FULLER },
38 { "oneline", 1, CMIT_FMT_ONELINE },
39 };
40
41 enum cmit_fmt get_commit_format(const char *arg)
42 {
43 int i;
44
45 if (!arg || !*arg)
46 return CMIT_FMT_DEFAULT;
47 if (*arg == '=')
48 arg++;
49 for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) {
50 if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len))
51 return cmt_fmts[i].v;
52 }
53
54 die("invalid --pretty format: %s", arg);
55 }
56
57 static struct commit *check_commit(struct object *obj,
58 const unsigned char *sha1,
59 int quiet)
60 {
61 if (obj->type != OBJ_COMMIT) {
62 if (!quiet)
63 error("Object %s is a %s, not a commit",
64 sha1_to_hex(sha1), typename(obj->type));
65 return NULL;
66 }
67 return (struct commit *) obj;
68 }
69
70 struct commit *lookup_commit_reference_gently(const unsigned char *sha1,
71 int quiet)
72 {
73 struct object *obj = deref_tag(parse_object(sha1), NULL, 0);
74
75 if (!obj)
76 return NULL;
77 return check_commit(obj, sha1, quiet);
78 }
79
80 struct commit *lookup_commit_reference(const unsigned char *sha1)
81 {
82 return lookup_commit_reference_gently(sha1, 0);
83 }
84
85 struct commit *lookup_commit(const unsigned char *sha1)
86 {
87 struct object *obj = lookup_object(sha1);
88 if (!obj) {
89 struct commit *ret = alloc_commit_node();
90 created_object(sha1, &ret->object);
91 ret->object.type = OBJ_COMMIT;
92 return ret;
93 }
94 if (!obj->type)
95 obj->type = OBJ_COMMIT;
96 return check_commit(obj, sha1, 0);
97 }
98
99 static unsigned long parse_commit_date(const char *buf)
100 {
101 unsigned long date;
102
103 if (memcmp(buf, "author", 6))
104 return 0;
105 while (*buf++ != '\n')
106 /* nada */;
107 if (memcmp(buf, "committer", 9))
108 return 0;
109 while (*buf++ != '>')
110 /* nada */;
111 date = strtoul(buf, NULL, 10);
112 if (date == ULONG_MAX)
113 date = 0;
114 return date;
115 }
116
117 static struct commit_graft **commit_graft;
118 static int commit_graft_alloc, commit_graft_nr;
119
120 static int commit_graft_pos(const unsigned char *sha1)
121 {
122 int lo, hi;
123 lo = 0;
124 hi = commit_graft_nr;
125 while (lo < hi) {
126 int mi = (lo + hi) / 2;
127 struct commit_graft *graft = commit_graft[mi];
128 int cmp = hashcmp(sha1, graft->sha1);
129 if (!cmp)
130 return mi;
131 if (cmp < 0)
132 hi = mi;
133 else
134 lo = mi + 1;
135 }
136 return -lo - 1;
137 }
138
139 int register_commit_graft(struct commit_graft *graft, int ignore_dups)
140 {
141 int pos = commit_graft_pos(graft->sha1);
142
143 if (0 <= pos) {
144 if (ignore_dups)
145 free(graft);
146 else {
147 free(commit_graft[pos]);
148 commit_graft[pos] = graft;
149 }
150 return 1;
151 }
152 pos = -pos - 1;
153 if (commit_graft_alloc <= ++commit_graft_nr) {
154 commit_graft_alloc = alloc_nr(commit_graft_alloc);
155 commit_graft = xrealloc(commit_graft,
156 sizeof(*commit_graft) *
157 commit_graft_alloc);
158 }
159 if (pos < commit_graft_nr)
160 memmove(commit_graft + pos + 1,
161 commit_graft + pos,
162 (commit_graft_nr - pos - 1) *
163 sizeof(*commit_graft));
164 commit_graft[pos] = graft;
165 return 0;
166 }
167
168 struct commit_graft *read_graft_line(char *buf, int len)
169 {
170 /* The format is just "Commit Parent1 Parent2 ...\n" */
171 int i;
172 struct commit_graft *graft = NULL;
173
174 if (buf[len-1] == '\n')
175 buf[--len] = 0;
176 if (buf[0] == '#' || buf[0] == '\0')
177 return NULL;
178 if ((len + 1) % 41) {
179 bad_graft_data:
180 error("bad graft data: %s", buf);
181 free(graft);
182 return NULL;
183 }
184 i = (len + 1) / 41 - 1;
185 graft = xmalloc(sizeof(*graft) + 20 * i);
186 graft->nr_parent = i;
187 if (get_sha1_hex(buf, graft->sha1))
188 goto bad_graft_data;
189 for (i = 40; i < len; i += 41) {
190 if (buf[i] != ' ')
191 goto bad_graft_data;
192 if (get_sha1_hex(buf + i + 1, graft->parent[i/41]))
193 goto bad_graft_data;
194 }
195 return graft;
196 }
197
198 int read_graft_file(const char *graft_file)
199 {
200 FILE *fp = fopen(graft_file, "r");
201 char buf[1024];
202 if (!fp)
203 return -1;
204 while (fgets(buf, sizeof(buf), fp)) {
205 /* The format is just "Commit Parent1 Parent2 ...\n" */
206 int len = strlen(buf);
207 struct commit_graft *graft = read_graft_line(buf, len);
208 if (!graft)
209 continue;
210 if (register_commit_graft(graft, 1))
211 error("duplicate graft data: %s", buf);
212 }
213 fclose(fp);
214 return 0;
215 }
216
217 static void prepare_commit_graft(void)
218 {
219 static int commit_graft_prepared;
220 char *graft_file;
221
222 if (commit_graft_prepared)
223 return;
224 graft_file = get_graft_file();
225 read_graft_file(graft_file);
226 /* make sure shallows are read */
227 is_repository_shallow();
228 commit_graft_prepared = 1;
229 }
230
231 static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
232 {
233 int pos;
234 prepare_commit_graft();
235 pos = commit_graft_pos(sha1);
236 if (pos < 0)
237 return NULL;
238 return commit_graft[pos];
239 }
240
241 int write_shallow_commits(int fd, int use_pack_protocol)
242 {
243 int i, count = 0;
244 for (i = 0; i < commit_graft_nr; i++)
245 if (commit_graft[i]->nr_parent < 0) {
246 const char *hex =
247 sha1_to_hex(commit_graft[i]->sha1);
248 count++;
249 if (use_pack_protocol)
250 packet_write(fd, "shallow %s", hex);
251 else {
252 write(fd, hex, 40);
253 write(fd, "\n", 1);
254 }
255 }
256 return count;
257 }
258
259 int unregister_shallow(const unsigned char *sha1)
260 {
261 int pos = commit_graft_pos(sha1);
262 if (pos < 0)
263 return -1;
264 if (pos + 1 < commit_graft_nr)
265 memcpy(commit_graft + pos, commit_graft + pos + 1,
266 sizeof(struct commit_graft *)
267 * (commit_graft_nr - pos - 1));
268 commit_graft_nr--;
269 return 0;
270 }
271
272 int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
273 {
274 char *tail = buffer;
275 char *bufptr = buffer;
276 unsigned char parent[20];
277 struct commit_list **pptr;
278 struct commit_graft *graft;
279 unsigned n_refs = 0;
280
281 if (item->object.parsed)
282 return 0;
283 item->object.parsed = 1;
284 tail += size;
285 if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
286 return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
287 if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
288 return error("bad tree pointer in commit %s",
289 sha1_to_hex(item->object.sha1));
290 item->tree = lookup_tree(parent);
291 if (item->tree)
292 n_refs++;
293 bufptr += 46; /* "tree " + "hex sha1" + "\n" */
294 pptr = &item->parents;
295
296 graft = lookup_commit_graft(item->object.sha1);
297 while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
298 struct commit *new_parent;
299
300 if (tail <= bufptr + 48 ||
301 get_sha1_hex(bufptr + 7, parent) ||
302 bufptr[47] != '\n')
303 return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
304 bufptr += 48;
305 if (graft)
306 continue;
307 new_parent = lookup_commit(parent);
308 if (new_parent) {
309 pptr = &commit_list_insert(new_parent, pptr)->next;
310 n_refs++;
311 }
312 }
313 if (graft) {
314 int i;
315 struct commit *new_parent;
316 for (i = 0; i < graft->nr_parent; i++) {
317 new_parent = lookup_commit(graft->parent[i]);
318 if (!new_parent)
319 continue;
320 pptr = &commit_list_insert(new_parent, pptr)->next;
321 n_refs++;
322 }
323 }
324 item->date = parse_commit_date(bufptr);
325
326 if (track_object_refs) {
327 unsigned i = 0;
328 struct commit_list *p;
329 struct object_refs *refs = alloc_object_refs(n_refs);
330 if (item->tree)
331 refs->ref[i++] = &item->tree->object;
332 for (p = item->parents; p; p = p->next)
333 refs->ref[i++] = &p->item->object;
334 set_object_refs(&item->object, refs);
335 }
336
337 return 0;
338 }
339
340 int parse_commit(struct commit *item)
341 {
342 char type[20];
343 void *buffer;
344 unsigned long size;
345 int ret;
346
347 if (item->object.parsed)
348 return 0;
349 buffer = read_sha1_file(item->object.sha1, type, &size);
350 if (!buffer)
351 return error("Could not read %s",
352 sha1_to_hex(item->object.sha1));
353 if (strcmp(type, commit_type)) {
354 free(buffer);
355 return error("Object %s not a commit",
356 sha1_to_hex(item->object.sha1));
357 }
358 ret = parse_commit_buffer(item, buffer, size);
359 if (save_commit_buffer && !ret) {
360 item->buffer = buffer;
361 return 0;
362 }
363 free(buffer);
364 return ret;
365 }
366
367 struct commit_list *commit_list_insert(struct commit *item, struct commit_list **list_p)
368 {
369 struct commit_list *new_list = xmalloc(sizeof(struct commit_list));
370 new_list->item = item;
371 new_list->next = *list_p;
372 *list_p = new_list;
373 return new_list;
374 }
375
376 void free_commit_list(struct commit_list *list)
377 {
378 while (list) {
379 struct commit_list *temp = list;
380 list = temp->next;
381 free(temp);
382 }
383 }
384
385 struct commit_list * insert_by_date(struct commit *item, struct commit_list **list)
386 {
387 struct commit_list **pp = list;
388 struct commit_list *p;
389 while ((p = *pp) != NULL) {
390 if (p->item->date < item->date) {
391 break;
392 }
393 pp = &p->next;
394 }
395 return commit_list_insert(item, pp);
396 }
397
398
399 void sort_by_date(struct commit_list **list)
400 {
401 struct commit_list *ret = NULL;
402 while (*list) {
403 insert_by_date((*list)->item, &ret);
404 *list = (*list)->next;
405 }
406 *list = ret;
407 }
408
409 struct commit *pop_most_recent_commit(struct commit_list **list,
410 unsigned int mark)
411 {
412 struct commit *ret = (*list)->item;
413 struct commit_list *parents = ret->parents;
414 struct commit_list *old = *list;
415
416 *list = (*list)->next;
417 free(old);
418
419 while (parents) {
420 struct commit *commit = parents->item;
421 parse_commit(commit);
422 if (!(commit->object.flags & mark)) {
423 commit->object.flags |= mark;
424 insert_by_date(commit, list);
425 }
426 parents = parents->next;
427 }
428 return ret;
429 }
430
431 void clear_commit_marks(struct commit *commit, unsigned int mark)
432 {
433 struct commit_list *parents;
434
435 commit->object.flags &= ~mark;
436 parents = commit->parents;
437 while (parents) {
438 struct commit *parent = parents->item;
439
440 /* Have we already cleared this? */
441 if (mark & parent->object.flags)
442 clear_commit_marks(parent, mark);
443 parents = parents->next;
444 }
445 }
446
447 /*
448 * Generic support for pretty-printing the header
449 */
450 static int get_one_line(const char *msg, unsigned long len)
451 {
452 int ret = 0;
453
454 while (len--) {
455 char c = *msg++;
456 if (!c)
457 break;
458 ret++;
459 if (c == '\n')
460 break;
461 }
462 return ret;
463 }
464
465 static int is_rfc2047_special(char ch)
466 {
467 return ((ch & 0x80) || (ch == '=') || (ch == '?') || (ch == '_'));
468 }
469
470 static int add_rfc2047(char *buf, const char *line, int len)
471 {
472 char *bp = buf;
473 int i, needquote;
474 static const char q_utf8[] = "=?utf-8?q?";
475
476 for (i = needquote = 0; !needquote && i < len; i++) {
477 unsigned ch = line[i];
478 if (ch & 0x80)
479 needquote++;
480 if ((i + 1 < len) &&
481 (ch == '=' && line[i+1] == '?'))
482 needquote++;
483 }
484 if (!needquote)
485 return sprintf(buf, "%.*s", len, line);
486
487 memcpy(bp, q_utf8, sizeof(q_utf8)-1);
488 bp += sizeof(q_utf8)-1;
489 for (i = 0; i < len; i++) {
490 unsigned ch = line[i] & 0xFF;
491 if (is_rfc2047_special(ch)) {
492 sprintf(bp, "=%02X", ch);
493 bp += 3;
494 }
495 else if (ch == ' ')
496 *bp++ = '_';
497 else
498 *bp++ = ch;
499 }
500 memcpy(bp, "?=", 2);
501 bp += 2;
502 return bp - buf;
503 }
504
505 static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf,
506 const char *line, int relative_date)
507 {
508 char *date;
509 int namelen;
510 unsigned long time;
511 int tz, ret;
512 const char *filler = " ";
513
514 if (fmt == CMIT_FMT_ONELINE)
515 return 0;
516 date = strchr(line, '>');
517 if (!date)
518 return 0;
519 namelen = ++date - line;
520 time = strtoul(date, &date, 10);
521 tz = strtol(date, NULL, 10);
522
523 if (fmt == CMIT_FMT_EMAIL) {
524 char *name_tail = strchr(line, '<');
525 int display_name_length;
526 if (!name_tail)
527 return 0;
528 while (line < name_tail && isspace(name_tail[-1]))
529 name_tail--;
530 display_name_length = name_tail - line;
531 filler = "";
532 strcpy(buf, "From: ");
533 ret = strlen(buf);
534 ret += add_rfc2047(buf + ret, line, display_name_length);
535 memcpy(buf + ret, name_tail, namelen - display_name_length);
536 ret += namelen - display_name_length;
537 buf[ret++] = '\n';
538 }
539 else {
540 ret = sprintf(buf, "%s: %.*s%.*s\n", what,
541 (fmt == CMIT_FMT_FULLER) ? 4 : 0,
542 filler, namelen, line);
543 }
544 switch (fmt) {
545 case CMIT_FMT_MEDIUM:
546 ret += sprintf(buf + ret, "Date: %s\n",
547 show_date(time, tz, relative_date));
548 break;
549 case CMIT_FMT_EMAIL:
550 ret += sprintf(buf + ret, "Date: %s\n",
551 show_rfc2822_date(time, tz));
552 break;
553 case CMIT_FMT_FULLER:
554 ret += sprintf(buf + ret, "%sDate: %s\n", what,
555 show_date(time, tz, relative_date));
556 break;
557 default:
558 /* notin' */
559 break;
560 }
561 return ret;
562 }
563
564 static int is_empty_line(const char *line, int *len_p)
565 {
566 int len = *len_p;
567 while (len && isspace(line[len-1]))
568 len--;
569 *len_p = len;
570 return !len;
571 }
572
573 static int add_merge_info(enum cmit_fmt fmt, char *buf, const struct commit *commit, int abbrev)
574 {
575 struct commit_list *parent = commit->parents;
576 int offset;
577
578 if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) ||
579 !parent || !parent->next)
580 return 0;
581
582 offset = sprintf(buf, "Merge:");
583
584 while (parent) {
585 struct commit *p = parent->item;
586 const char *hex = NULL;
587 const char *dots;
588 if (abbrev)
589 hex = find_unique_abbrev(p->object.sha1, abbrev);
590 if (!hex)
591 hex = sha1_to_hex(p->object.sha1);
592 dots = (abbrev && strlen(hex) != 40) ? "..." : "";
593 parent = parent->next;
594
595 offset += sprintf(buf + offset, " %s%s", hex, dots);
596 }
597 buf[offset++] = '\n';
598 return offset;
599 }
600
601 static char *get_header(const struct commit *commit, const char *key)
602 {
603 int key_len = strlen(key);
604 const char *line = commit->buffer;
605
606 for (;;) {
607 const char *eol = strchr(line, '\n'), *next;
608
609 if (line == eol)
610 return NULL;
611 if (!eol) {
612 eol = line + strlen(line);
613 next = NULL;
614 } else
615 next = eol + 1;
616 if (!strncmp(line, key, key_len) && line[key_len] == ' ') {
617 int len = eol - line - key_len;
618 char *ret = xmalloc(len);
619 memcpy(ret, line + key_len + 1, len - 1);
620 ret[len - 1] = '\0';
621 return ret;
622 }
623 line = next;
624 }
625 }
626
627 static char *logmsg_reencode(const struct commit *commit)
628 {
629 char *encoding;
630 char *out;
631 char *output_encoding = (git_log_output_encoding
632 ? git_log_output_encoding
633 : git_commit_encoding);
634
635 if (!output_encoding)
636 return NULL;
637 encoding = get_header(commit, "encoding");
638 if (!encoding || !strcmp(encoding, output_encoding)) {
639 free(encoding);
640 return NULL;
641 }
642 out = reencode_string(commit->buffer, output_encoding, encoding);
643 free(encoding);
644 if (!out)
645 return NULL;
646 return out;
647 }
648
649 unsigned long pretty_print_commit(enum cmit_fmt fmt,
650 const struct commit *commit,
651 unsigned long len,
652 char *buf, unsigned long space,
653 int abbrev, const char *subject,
654 const char *after_subject,
655 int relative_date)
656 {
657 int hdr = 1, body = 0;
658 unsigned long offset = 0;
659 int indent = 4;
660 int parents_shown = 0;
661 const char *msg = commit->buffer;
662 int plain_non_ascii = 0;
663 char *reencoded = logmsg_reencode(commit);
664
665 if (reencoded)
666 msg = reencoded;
667
668 if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
669 indent = 0;
670
671 /* After-subject is used to pass in Content-Type: multipart
672 * MIME header; in that case we do not have to do the
673 * plaintext content type even if the commit message has
674 * non 7-bit ASCII character. Otherwise, check if we need
675 * to say this is not a 7-bit ASCII.
676 */
677 if (fmt == CMIT_FMT_EMAIL && !after_subject) {
678 int i, ch, in_body;
679
680 for (in_body = i = 0; (ch = msg[i]) && i < len; i++) {
681 if (!in_body) {
682 /* author could be non 7-bit ASCII but
683 * the log may be so; skip over the
684 * header part first.
685 */
686 if (ch == '\n' &&
687 i + 1 < len && msg[i+1] == '\n')
688 in_body = 1;
689 }
690 else if (ch & 0x80) {
691 plain_non_ascii = 1;
692 break;
693 }
694 }
695 }
696
697 for (;;) {
698 const char *line = msg;
699 int linelen = get_one_line(msg, len);
700
701 if (!linelen)
702 break;
703
704 /*
705 * We want some slop for indentation and a possible
706 * final "...". Thus the "+ 20".
707 */
708 if (offset + linelen + 20 > space) {
709 memcpy(buf + offset, " ...\n", 8);
710 offset += 8;
711 break;
712 }
713
714 msg += linelen;
715 len -= linelen;
716 if (hdr) {
717 if (linelen == 1) {
718 hdr = 0;
719 if ((fmt != CMIT_FMT_ONELINE) && !subject)
720 buf[offset++] = '\n';
721 continue;
722 }
723 if (fmt == CMIT_FMT_RAW) {
724 memcpy(buf + offset, line, linelen);
725 offset += linelen;
726 continue;
727 }
728 if (!memcmp(line, "parent ", 7)) {
729 if (linelen != 48)
730 die("bad parent line in commit");
731 continue;
732 }
733
734 if (!parents_shown) {
735 offset += add_merge_info(fmt, buf + offset,
736 commit, abbrev);
737 parents_shown = 1;
738 continue;
739 }
740 /*
741 * MEDIUM == DEFAULT shows only author with dates.
742 * FULL shows both authors but not dates.
743 * FULLER shows both authors and dates.
744 */
745 if (!memcmp(line, "author ", 7))
746 offset += add_user_info("Author", fmt,
747 buf + offset,
748 line + 7,
749 relative_date);
750 if (!memcmp(line, "committer ", 10) &&
751 (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER))
752 offset += add_user_info("Commit", fmt,
753 buf + offset,
754 line + 10,
755 relative_date);
756 continue;
757 }
758
759 if (!subject)
760 body = 1;
761
762 if (is_empty_line(line, &linelen)) {
763 if (!body)
764 continue;
765 if (subject)
766 continue;
767 if (fmt == CMIT_FMT_SHORT)
768 break;
769 }
770
771 if (subject) {
772 int slen = strlen(subject);
773 memcpy(buf + offset, subject, slen);
774 offset += slen;
775 offset += add_rfc2047(buf + offset, line, linelen);
776 }
777 else {
778 memset(buf + offset, ' ', indent);
779 memcpy(buf + offset + indent, line, linelen);
780 offset += linelen + indent;
781 }
782 buf[offset++] = '\n';
783 if (fmt == CMIT_FMT_ONELINE)
784 break;
785 if (subject && plain_non_ascii) {
786 static const char header[] =
787 "Content-Type: text/plain; charset=UTF-8\n"
788 "Content-Transfer-Encoding: 8bit\n";
789 memcpy(buf + offset, header, sizeof(header)-1);
790 offset += sizeof(header)-1;
791 }
792 if (after_subject) {
793 int slen = strlen(after_subject);
794 if (slen > space - offset - 1)
795 slen = space - offset - 1;
796 memcpy(buf + offset, after_subject, slen);
797 offset += slen;
798 after_subject = NULL;
799 }
800 subject = NULL;
801 }
802 while (offset && isspace(buf[offset-1]))
803 offset--;
804 /* Make sure there is an EOLN for the non-oneline case */
805 if (fmt != CMIT_FMT_ONELINE)
806 buf[offset++] = '\n';
807 /*
808 * make sure there is another EOLN to separate the headers from whatever
809 * body the caller appends if we haven't already written a body
810 */
811 if (fmt == CMIT_FMT_EMAIL && !body)
812 buf[offset++] = '\n';
813 buf[offset] = '\0';
814
815 free(reencoded);
816 return offset;
817 }
818
819 struct commit *pop_commit(struct commit_list **stack)
820 {
821 struct commit_list *top = *stack;
822 struct commit *item = top ? top->item : NULL;
823
824 if (top) {
825 *stack = top->next;
826 free(top);
827 }
828 return item;
829 }
830
831 int count_parents(struct commit * commit)
832 {
833 int count;
834 struct commit_list * parents = commit->parents;
835 for (count = 0; parents; parents = parents->next,count++)
836 ;
837 return count;
838 }
839
840 void topo_sort_default_setter(struct commit *c, void *data)
841 {
842 c->util = data;
843 }
844
845 void *topo_sort_default_getter(struct commit *c)
846 {
847 return c->util;
848 }
849
850 /*
851 * Performs an in-place topological sort on the list supplied.
852 */
853 void sort_in_topological_order(struct commit_list ** list, int lifo)
854 {
855 sort_in_topological_order_fn(list, lifo, topo_sort_default_setter,
856 topo_sort_default_getter);
857 }
858
859 void sort_in_topological_order_fn(struct commit_list ** list, int lifo,
860 topo_sort_set_fn_t setter,
861 topo_sort_get_fn_t getter)
862 {
863 struct commit_list * next = *list;
864 struct commit_list * work = NULL, **insert;
865 struct commit_list ** pptr = list;
866 struct sort_node * nodes;
867 struct sort_node * next_nodes;
868 int count = 0;
869
870 /* determine the size of the list */
871 while (next) {
872 next = next->next;
873 count++;
874 }
875
876 if (!count)
877 return;
878 /* allocate an array to help sort the list */
879 nodes = xcalloc(count, sizeof(*nodes));
880 /* link the list to the array */
881 next_nodes = nodes;
882 next=*list;
883 while (next) {
884 next_nodes->list_item = next;
885 setter(next->item, next_nodes);
886 next_nodes++;
887 next = next->next;
888 }
889 /* update the indegree */
890 next=*list;
891 while (next) {
892 struct commit_list * parents = next->item->parents;
893 while (parents) {
894 struct commit * parent=parents->item;
895 struct sort_node * pn = (struct sort_node *) getter(parent);
896
897 if (pn)
898 pn->indegree++;
899 parents=parents->next;
900 }
901 next=next->next;
902 }
903 /*
904 * find the tips
905 *
906 * tips are nodes not reachable from any other node in the list
907 *
908 * the tips serve as a starting set for the work queue.
909 */
910 next=*list;
911 insert = &work;
912 while (next) {
913 struct sort_node * node = (struct sort_node *) getter(next->item);
914
915 if (node->indegree == 0) {
916 insert = &commit_list_insert(next->item, insert)->next;
917 }
918 next=next->next;
919 }
920
921 /* process the list in topological order */
922 if (!lifo)
923 sort_by_date(&work);
924 while (work) {
925 struct commit * work_item = pop_commit(&work);
926 struct sort_node * work_node = (struct sort_node *) getter(work_item);
927 struct commit_list * parents = work_item->parents;
928
929 while (parents) {
930 struct commit * parent=parents->item;
931 struct sort_node * pn = (struct sort_node *) getter(parent);
932
933 if (pn) {
934 /*
935 * parents are only enqueued for emission
936 * when all their children have been emitted thereby
937 * guaranteeing topological order.
938 */
939 pn->indegree--;
940 if (!pn->indegree) {
941 if (!lifo)
942 insert_by_date(parent, &work);
943 else
944 commit_list_insert(parent, &work);
945 }
946 }
947 parents=parents->next;
948 }
949 /*
950 * work_item is a commit all of whose children
951 * have already been emitted. we can emit it now.
952 */
953 *pptr = work_node->list_item;
954 pptr = &(*pptr)->next;
955 *pptr = NULL;
956 setter(work_item, NULL);
957 }
958 free(nodes);
959 }
960
961 /* merge-rebase stuff */
962
963 /* bits #0..15 in revision.h */
964 #define PARENT1 (1u<<16)
965 #define PARENT2 (1u<<17)
966 #define STALE (1u<<18)
967 #define RESULT (1u<<19)
968
969 static struct commit *interesting(struct commit_list *list)
970 {
971 while (list) {
972 struct commit *commit = list->item;
973 list = list->next;
974 if (commit->object.flags & STALE)
975 continue;
976 return commit;
977 }
978 return NULL;
979 }
980
981 static struct commit_list *merge_bases(struct commit *one, struct commit *two)
982 {
983 struct commit_list *list = NULL;
984 struct commit_list *result = NULL;
985
986 if (one == two)
987 /* We do not mark this even with RESULT so we do not
988 * have to clean it up.
989 */
990 return commit_list_insert(one, &result);
991
992 parse_commit(one);
993 parse_commit(two);
994
995 one->object.flags |= PARENT1;
996 two->object.flags |= PARENT2;
997 insert_by_date(one, &list);
998 insert_by_date(two, &list);
999
1000 while (interesting(list)) {
1001 struct commit *commit;
1002 struct commit_list *parents;
1003 struct commit_list *n;
1004 int flags;
1005
1006 commit = list->item;
1007 n = list->next;
1008 free(list);
1009 list = n;
1010
1011 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
1012 if (flags == (PARENT1 | PARENT2)) {
1013 if (!(commit->object.flags & RESULT)) {
1014 commit->object.flags |= RESULT;
1015 insert_by_date(commit, &result);
1016 }
1017 /* Mark parents of a found merge stale */
1018 flags |= STALE;
1019 }
1020 parents = commit->parents;
1021 while (parents) {
1022 struct commit *p = parents->item;
1023 parents = parents->next;
1024 if ((p->object.flags & flags) == flags)
1025 continue;
1026 parse_commit(p);
1027 p->object.flags |= flags;
1028 insert_by_date(p, &list);
1029 }
1030 }
1031
1032 /* Clean up the result to remove stale ones */
1033 list = result; result = NULL;
1034 while (list) {
1035 struct commit_list *n = list->next;
1036 if (!(list->item->object.flags & STALE))
1037 insert_by_date(list->item, &result);
1038 free(list);
1039 list = n;
1040 }
1041 return result;
1042 }
1043
1044 struct commit_list *get_merge_bases(struct commit *one,
1045 struct commit *two,
1046 int cleanup)
1047 {
1048 const unsigned all_flags = (PARENT1 | PARENT2 | STALE | RESULT);
1049 struct commit_list *list;
1050 struct commit **rslt;
1051 struct commit_list *result;
1052 int cnt, i, j;
1053
1054 result = merge_bases(one, two);
1055 if (one == two)
1056 return result;
1057 if (!result || !result->next) {
1058 if (cleanup) {
1059 clear_commit_marks(one, all_flags);
1060 clear_commit_marks(two, all_flags);
1061 }
1062 return result;
1063 }
1064
1065 /* There are more than one */
1066 cnt = 0;
1067 list = result;
1068 while (list) {
1069 list = list->next;
1070 cnt++;
1071 }
1072 rslt = xcalloc(cnt, sizeof(*rslt));
1073 for (list = result, i = 0; list; list = list->next)
1074 rslt[i++] = list->item;
1075 free_commit_list(result);
1076
1077 clear_commit_marks(one, all_flags);
1078 clear_commit_marks(two, all_flags);
1079 for (i = 0; i < cnt - 1; i++) {
1080 for (j = i+1; j < cnt; j++) {
1081 if (!rslt[i] || !rslt[j])
1082 continue;
1083 result = merge_bases(rslt[i], rslt[j]);
1084 clear_commit_marks(rslt[i], all_flags);
1085 clear_commit_marks(rslt[j], all_flags);
1086 for (list = result; list; list = list->next) {
1087 if (rslt[i] == list->item)
1088 rslt[i] = NULL;
1089 if (rslt[j] == list->item)
1090 rslt[j] = NULL;
1091 }
1092 }
1093 }
1094
1095 /* Surviving ones in rslt[] are the independent results */
1096 result = NULL;
1097 for (i = 0; i < cnt; i++) {
1098 if (rslt[i])
1099 insert_by_date(rslt[i], &result);
1100 }
1101 free(rslt);
1102 return result;
1103 }
1104
1105 int in_merge_bases(struct commit *rev1, struct commit *rev2)
1106 {
1107 struct commit_list *bases, *b;
1108 int ret = 0;
1109
1110 bases = get_merge_bases(rev1, rev2, 1);
1111 for (b = bases; b; b = b->next) {
1112 if (!hashcmp(rev1->object.sha1, b->item->object.sha1)) {
1113 ret = 1;
1114 break;
1115 }
1116 }
1117
1118 free_commit_list(bases);
1119 return ret;
1120 }