7 int save_commit_buffer
= 1;
12 * the number of children of the associated commit
13 * that also occur in the list being sorted.
15 unsigned int indegree
;
18 * reference to original list item that we will re-use
21 struct commit_list
* list_item
;
25 const char *commit_type
= "commit";
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
},
41 enum cmit_fmt
get_commit_format(const char *arg
)
46 return CMIT_FMT_DEFAULT
;
49 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
50 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
51 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
)))
55 die("invalid --pretty format: %s", arg
);
58 static struct commit
*check_commit(struct object
*obj
,
59 const unsigned char *sha1
,
62 if (obj
->type
!= OBJ_COMMIT
) {
64 error("Object %s is a %s, not a commit",
65 sha1_to_hex(sha1
), typename(obj
->type
));
68 return (struct commit
*) obj
;
71 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
74 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
78 return check_commit(obj
, sha1
, quiet
);
81 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
83 return lookup_commit_reference_gently(sha1
, 0);
86 struct commit
*lookup_commit(const unsigned char *sha1
)
88 struct object
*obj
= lookup_object(sha1
);
90 struct commit
*ret
= alloc_commit_node();
91 created_object(sha1
, &ret
->object
);
92 ret
->object
.type
= OBJ_COMMIT
;
96 obj
->type
= OBJ_COMMIT
;
97 return check_commit(obj
, sha1
, 0);
100 static unsigned long parse_commit_date(const char *buf
)
104 if (memcmp(buf
, "author", 6))
106 while (*buf
++ != '\n')
108 if (memcmp(buf
, "committer", 9))
110 while (*buf
++ != '>')
112 date
= strtoul(buf
, NULL
, 10);
113 if (date
== ULONG_MAX
)
118 static struct commit_graft
**commit_graft
;
119 static int commit_graft_alloc
, commit_graft_nr
;
121 static int commit_graft_pos(const unsigned char *sha1
)
125 hi
= commit_graft_nr
;
127 int mi
= (lo
+ hi
) / 2;
128 struct commit_graft
*graft
= commit_graft
[mi
];
129 int cmp
= hashcmp(sha1
, graft
->sha1
);
140 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
142 int pos
= commit_graft_pos(graft
->sha1
);
148 free(commit_graft
[pos
]);
149 commit_graft
[pos
] = graft
;
154 if (commit_graft_alloc
<= ++commit_graft_nr
) {
155 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
156 commit_graft
= xrealloc(commit_graft
,
157 sizeof(*commit_graft
) *
160 if (pos
< commit_graft_nr
)
161 memmove(commit_graft
+ pos
+ 1,
163 (commit_graft_nr
- pos
- 1) *
164 sizeof(*commit_graft
));
165 commit_graft
[pos
] = graft
;
169 struct commit_graft
*read_graft_line(char *buf
, int len
)
171 /* The format is just "Commit Parent1 Parent2 ...\n" */
173 struct commit_graft
*graft
= NULL
;
175 if (buf
[len
-1] == '\n')
177 if (buf
[0] == '#' || buf
[0] == '\0')
179 if ((len
+ 1) % 41) {
181 error("bad graft data: %s", buf
);
185 i
= (len
+ 1) / 41 - 1;
186 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
187 graft
->nr_parent
= i
;
188 if (get_sha1_hex(buf
, graft
->sha1
))
190 for (i
= 40; i
< len
; i
+= 41) {
193 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
199 int read_graft_file(const char *graft_file
)
201 FILE *fp
= fopen(graft_file
, "r");
205 while (fgets(buf
, sizeof(buf
), fp
)) {
206 /* The format is just "Commit Parent1 Parent2 ...\n" */
207 int len
= strlen(buf
);
208 struct commit_graft
*graft
= read_graft_line(buf
, len
);
211 if (register_commit_graft(graft
, 1))
212 error("duplicate graft data: %s", buf
);
218 static void prepare_commit_graft(void)
220 static int commit_graft_prepared
;
223 if (commit_graft_prepared
)
225 graft_file
= get_graft_file();
226 read_graft_file(graft_file
);
227 /* make sure shallows are read */
228 is_repository_shallow();
229 commit_graft_prepared
= 1;
232 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
235 prepare_commit_graft();
236 pos
= commit_graft_pos(sha1
);
239 return commit_graft
[pos
];
242 int write_shallow_commits(int fd
, int use_pack_protocol
)
245 for (i
= 0; i
< commit_graft_nr
; i
++)
246 if (commit_graft
[i
]->nr_parent
< 0) {
248 sha1_to_hex(commit_graft
[i
]->sha1
);
250 if (use_pack_protocol
)
251 packet_write(fd
, "shallow %s", hex
);
253 if (write_in_full(fd
, hex
, 40) != 40)
255 if (write_in_full(fd
, "\n", 1) != 1)
262 int unregister_shallow(const unsigned char *sha1
)
264 int pos
= commit_graft_pos(sha1
);
267 if (pos
+ 1 < commit_graft_nr
)
268 memcpy(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
269 sizeof(struct commit_graft
*)
270 * (commit_graft_nr
- pos
- 1));
275 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
278 char *bufptr
= buffer
;
279 unsigned char parent
[20];
280 struct commit_list
**pptr
;
281 struct commit_graft
*graft
;
284 if (item
->object
.parsed
)
286 item
->object
.parsed
= 1;
288 if (tail
<= bufptr
+ 5 || memcmp(bufptr
, "tree ", 5))
289 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
290 if (tail
<= bufptr
+ 45 || get_sha1_hex(bufptr
+ 5, parent
) < 0)
291 return error("bad tree pointer in commit %s",
292 sha1_to_hex(item
->object
.sha1
));
293 item
->tree
= lookup_tree(parent
);
296 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
297 pptr
= &item
->parents
;
299 graft
= lookup_commit_graft(item
->object
.sha1
);
300 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
301 struct commit
*new_parent
;
303 if (tail
<= bufptr
+ 48 ||
304 get_sha1_hex(bufptr
+ 7, parent
) ||
306 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
310 new_parent
= lookup_commit(parent
);
312 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
318 struct commit
*new_parent
;
319 for (i
= 0; i
< graft
->nr_parent
; i
++) {
320 new_parent
= lookup_commit(graft
->parent
[i
]);
323 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
327 item
->date
= parse_commit_date(bufptr
);
329 if (track_object_refs
) {
331 struct commit_list
*p
;
332 struct object_refs
*refs
= alloc_object_refs(n_refs
);
334 refs
->ref
[i
++] = &item
->tree
->object
;
335 for (p
= item
->parents
; p
; p
= p
->next
)
336 refs
->ref
[i
++] = &p
->item
->object
;
337 set_object_refs(&item
->object
, refs
);
343 int parse_commit(struct commit
*item
)
345 enum object_type type
;
350 if (item
->object
.parsed
)
352 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
354 return error("Could not read %s",
355 sha1_to_hex(item
->object
.sha1
));
356 if (type
!= OBJ_COMMIT
) {
358 return error("Object %s not a commit",
359 sha1_to_hex(item
->object
.sha1
));
361 ret
= parse_commit_buffer(item
, buffer
, size
);
362 if (save_commit_buffer
&& !ret
) {
363 item
->buffer
= buffer
;
370 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
372 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
373 new_list
->item
= item
;
374 new_list
->next
= *list_p
;
379 void free_commit_list(struct commit_list
*list
)
382 struct commit_list
*temp
= list
;
388 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
390 struct commit_list
**pp
= list
;
391 struct commit_list
*p
;
392 while ((p
= *pp
) != NULL
) {
393 if (p
->item
->date
< item
->date
) {
398 return commit_list_insert(item
, pp
);
402 void sort_by_date(struct commit_list
**list
)
404 struct commit_list
*ret
= NULL
;
406 insert_by_date((*list
)->item
, &ret
);
407 *list
= (*list
)->next
;
412 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
415 struct commit
*ret
= (*list
)->item
;
416 struct commit_list
*parents
= ret
->parents
;
417 struct commit_list
*old
= *list
;
419 *list
= (*list
)->next
;
423 struct commit
*commit
= parents
->item
;
424 parse_commit(commit
);
425 if (!(commit
->object
.flags
& mark
)) {
426 commit
->object
.flags
|= mark
;
427 insert_by_date(commit
, list
);
429 parents
= parents
->next
;
434 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
436 struct commit_list
*parents
;
438 commit
->object
.flags
&= ~mark
;
439 parents
= commit
->parents
;
441 struct commit
*parent
= parents
->item
;
443 /* Have we already cleared this? */
444 if (mark
& parent
->object
.flags
)
445 clear_commit_marks(parent
, mark
);
446 parents
= parents
->next
;
451 * Generic support for pretty-printing the header
453 static int get_one_line(const char *msg
, unsigned long len
)
468 /* High bit set, or ISO-2022-INT */
469 static int non_ascii(int ch
)
472 return ((ch
& 0x80) || (ch
== 0x1b));
475 static int is_rfc2047_special(char ch
)
477 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
480 static int add_rfc2047(char *buf
, const char *line
, int len
,
481 const char *encoding
)
485 char q_encoding
[128];
486 const char *q_encoding_fmt
= "=?%s?q?";
488 for (i
= needquote
= 0; !needquote
&& i
< len
; i
++) {
493 (ch
== '=' && line
[i
+1] == '?'))
497 return sprintf(buf
, "%.*s", len
, line
);
499 i
= snprintf(q_encoding
, sizeof(q_encoding
), q_encoding_fmt
, encoding
);
500 if (sizeof(q_encoding
) < i
)
501 die("Insanely long encoding name %s", encoding
);
502 memcpy(bp
, q_encoding
, i
);
504 for (i
= 0; i
< len
; i
++) {
505 unsigned ch
= line
[i
] & 0xFF;
506 if (is_rfc2047_special(ch
)) {
507 sprintf(bp
, "=%02X", ch
);
520 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
,
521 const char *line
, int relative_date
,
522 const char *encoding
)
528 const char *filler
= " ";
530 if (fmt
== CMIT_FMT_ONELINE
)
532 date
= strchr(line
, '>');
535 namelen
= ++date
- line
;
536 time
= strtoul(date
, &date
, 10);
537 tz
= strtol(date
, NULL
, 10);
539 if (fmt
== CMIT_FMT_EMAIL
) {
540 char *name_tail
= strchr(line
, '<');
541 int display_name_length
;
544 while (line
< name_tail
&& isspace(name_tail
[-1]))
546 display_name_length
= name_tail
- line
;
548 strcpy(buf
, "From: ");
550 ret
+= add_rfc2047(buf
+ ret
, line
, display_name_length
,
552 memcpy(buf
+ ret
, name_tail
, namelen
- display_name_length
);
553 ret
+= namelen
- display_name_length
;
557 ret
= sprintf(buf
, "%s: %.*s%.*s\n", what
,
558 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
559 filler
, namelen
, line
);
562 case CMIT_FMT_MEDIUM
:
563 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
564 show_date(time
, tz
, relative_date
));
567 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
568 show_rfc2822_date(time
, tz
));
570 case CMIT_FMT_FULLER
:
571 ret
+= sprintf(buf
+ ret
, "%sDate: %s\n", what
,
572 show_date(time
, tz
, relative_date
));
581 static int is_empty_line(const char *line
, int *len_p
)
584 while (len
&& isspace(line
[len
-1]))
590 static int add_merge_info(enum cmit_fmt fmt
, char *buf
, const struct commit
*commit
, int abbrev
)
592 struct commit_list
*parent
= commit
->parents
;
595 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
596 !parent
|| !parent
->next
)
599 offset
= sprintf(buf
, "Merge:");
602 struct commit
*p
= parent
->item
;
603 const char *hex
= NULL
;
606 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
608 hex
= sha1_to_hex(p
->object
.sha1
);
609 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
610 parent
= parent
->next
;
612 offset
+= sprintf(buf
+ offset
, " %s%s", hex
, dots
);
614 buf
[offset
++] = '\n';
618 static char *get_header(const struct commit
*commit
, const char *key
)
620 int key_len
= strlen(key
);
621 const char *line
= commit
->buffer
;
624 const char *eol
= strchr(line
, '\n'), *next
;
629 eol
= line
+ strlen(line
);
633 if (!strncmp(line
, key
, key_len
) && line
[key_len
] == ' ') {
634 int len
= eol
- line
- key_len
;
635 char *ret
= xmalloc(len
);
636 memcpy(ret
, line
+ key_len
+ 1, len
- 1);
644 static char *replace_encoding_header(char *buf
, char *encoding
)
646 char *encoding_header
= strstr(buf
, "\nencoding ");
647 char *end_of_encoding_header
;
648 int encoding_header_pos
;
649 int encoding_header_len
;
652 int buflen
= strlen(buf
) + 1;
654 if (!encoding_header
)
655 return buf
; /* should not happen but be defensive */
657 end_of_encoding_header
= strchr(encoding_header
, '\n');
658 if (!end_of_encoding_header
)
659 return buf
; /* should not happen but be defensive */
660 end_of_encoding_header
++;
662 encoding_header_len
= end_of_encoding_header
- encoding_header
;
663 encoding_header_pos
= encoding_header
- buf
;
665 if (is_encoding_utf8(encoding
)) {
666 /* we have re-coded to UTF-8; drop the header */
667 memmove(encoding_header
, end_of_encoding_header
,
668 buflen
- (encoding_header_pos
+ encoding_header_len
));
671 new_len
= strlen(encoding
);
672 need_len
= new_len
+ strlen("encoding \n");
673 if (encoding_header_len
< need_len
) {
674 buf
= xrealloc(buf
, buflen
+ (need_len
- encoding_header_len
));
675 encoding_header
= buf
+ encoding_header_pos
;
676 end_of_encoding_header
= encoding_header
+ encoding_header_len
;
678 memmove(end_of_encoding_header
+ (need_len
- encoding_header_len
),
679 end_of_encoding_header
,
680 buflen
- (encoding_header_pos
+ encoding_header_len
));
681 memcpy(encoding_header
+ 9, encoding
, strlen(encoding
));
682 encoding_header
[9 + new_len
] = '\n';
686 static char *logmsg_reencode(const struct commit
*commit
,
687 char *output_encoding
)
691 char *utf8
= "utf-8";
693 if (!*output_encoding
)
695 encoding
= get_header(commit
, "encoding");
698 if (!strcmp(encoding
, output_encoding
))
699 out
= strdup(commit
->buffer
);
701 out
= reencode_string(commit
->buffer
,
702 output_encoding
, encoding
);
704 out
= replace_encoding_header(out
, output_encoding
);
706 if (encoding
!= utf8
)
713 unsigned long pretty_print_commit(enum cmit_fmt fmt
,
714 const struct commit
*commit
,
716 char *buf
, unsigned long space
,
717 int abbrev
, const char *subject
,
718 const char *after_subject
,
721 int hdr
= 1, body
= 0, seen_title
= 0;
722 unsigned long offset
= 0;
724 int parents_shown
= 0;
725 const char *msg
= commit
->buffer
;
726 int plain_non_ascii
= 0;
730 encoding
= (git_log_output_encoding
731 ? git_log_output_encoding
732 : git_commit_encoding
);
735 reencoded
= logmsg_reencode(commit
, encoding
);
739 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
742 /* After-subject is used to pass in Content-Type: multipart
743 * MIME header; in that case we do not have to do the
744 * plaintext content type even if the commit message has
745 * non 7-bit ASCII character. Otherwise, check if we need
746 * to say this is not a 7-bit ASCII.
748 if (fmt
== CMIT_FMT_EMAIL
&& !after_subject
) {
751 for (in_body
= i
= 0; (ch
= msg
[i
]) && i
< len
; i
++) {
753 /* author could be non 7-bit ASCII but
754 * the log may be so; skip over the
758 i
+ 1 < len
&& msg
[i
+1] == '\n')
761 else if (non_ascii(ch
)) {
769 const char *line
= msg
;
770 int linelen
= get_one_line(msg
, len
);
776 * We want some slop for indentation and a possible
777 * final "...". Thus the "+ 20".
779 if (offset
+ linelen
+ 20 > space
) {
780 memcpy(buf
+ offset
, " ...\n", 8);
790 if ((fmt
!= CMIT_FMT_ONELINE
) && !subject
)
791 buf
[offset
++] = '\n';
794 if (fmt
== CMIT_FMT_RAW
) {
795 memcpy(buf
+ offset
, line
, linelen
);
799 if (!memcmp(line
, "parent ", 7)) {
801 die("bad parent line in commit");
805 if (!parents_shown
) {
806 offset
+= add_merge_info(fmt
, buf
+ offset
,
812 * MEDIUM == DEFAULT shows only author with dates.
813 * FULL shows both authors but not dates.
814 * FULLER shows both authors and dates.
816 if (!memcmp(line
, "author ", 7))
817 offset
+= add_user_info("Author", fmt
,
822 if (!memcmp(line
, "committer ", 10) &&
823 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
))
824 offset
+= add_user_info("Commit", fmt
,
835 if (is_empty_line(line
, &linelen
)) {
842 if (fmt
== CMIT_FMT_SHORT
)
848 int slen
= strlen(subject
);
849 memcpy(buf
+ offset
, subject
, slen
);
851 offset
+= add_rfc2047(buf
+ offset
, line
, linelen
,
855 memset(buf
+ offset
, ' ', indent
);
856 memcpy(buf
+ offset
+ indent
, line
, linelen
);
857 offset
+= linelen
+ indent
;
859 buf
[offset
++] = '\n';
860 if (fmt
== CMIT_FMT_ONELINE
)
862 if (subject
&& plain_non_ascii
) {
865 const char *header_fmt
=
866 "Content-Type: text/plain; charset=%s\n"
867 "Content-Transfer-Encoding: 8bit\n";
868 sz
= snprintf(header
, sizeof(header
), header_fmt
,
870 if (sizeof(header
) < sz
)
871 die("Encoding name %s too long", encoding
);
872 memcpy(buf
+ offset
, header
, sz
);
876 int slen
= strlen(after_subject
);
877 if (slen
> space
- offset
- 1)
878 slen
= space
- offset
- 1;
879 memcpy(buf
+ offset
, after_subject
, slen
);
881 after_subject
= NULL
;
885 while (offset
&& isspace(buf
[offset
-1]))
887 /* Make sure there is an EOLN for the non-oneline case */
888 if (fmt
!= CMIT_FMT_ONELINE
)
889 buf
[offset
++] = '\n';
891 * make sure there is another EOLN to separate the headers from whatever
892 * body the caller appends if we haven't already written a body
894 if (fmt
== CMIT_FMT_EMAIL
&& !body
)
895 buf
[offset
++] = '\n';
902 struct commit
*pop_commit(struct commit_list
**stack
)
904 struct commit_list
*top
= *stack
;
905 struct commit
*item
= top
? top
->item
: NULL
;
914 int count_parents(struct commit
* commit
)
917 struct commit_list
* parents
= commit
->parents
;
918 for (count
= 0; parents
; parents
= parents
->next
,count
++)
923 void topo_sort_default_setter(struct commit
*c
, void *data
)
928 void *topo_sort_default_getter(struct commit
*c
)
934 * Performs an in-place topological sort on the list supplied.
936 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
938 sort_in_topological_order_fn(list
, lifo
, topo_sort_default_setter
,
939 topo_sort_default_getter
);
942 void sort_in_topological_order_fn(struct commit_list
** list
, int lifo
,
943 topo_sort_set_fn_t setter
,
944 topo_sort_get_fn_t getter
)
946 struct commit_list
* next
= *list
;
947 struct commit_list
* work
= NULL
, **insert
;
948 struct commit_list
** pptr
= list
;
949 struct sort_node
* nodes
;
950 struct sort_node
* next_nodes
;
953 /* determine the size of the list */
961 /* allocate an array to help sort the list */
962 nodes
= xcalloc(count
, sizeof(*nodes
));
963 /* link the list to the array */
967 next_nodes
->list_item
= next
;
968 setter(next
->item
, next_nodes
);
972 /* update the indegree */
975 struct commit_list
* parents
= next
->item
->parents
;
977 struct commit
* parent
=parents
->item
;
978 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
982 parents
=parents
->next
;
989 * tips are nodes not reachable from any other node in the list
991 * the tips serve as a starting set for the work queue.
996 struct sort_node
* node
= (struct sort_node
*) getter(next
->item
);
998 if (node
->indegree
== 0) {
999 insert
= &commit_list_insert(next
->item
, insert
)->next
;
1004 /* process the list in topological order */
1006 sort_by_date(&work
);
1008 struct commit
* work_item
= pop_commit(&work
);
1009 struct sort_node
* work_node
= (struct sort_node
*) getter(work_item
);
1010 struct commit_list
* parents
= work_item
->parents
;
1013 struct commit
* parent
=parents
->item
;
1014 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
1018 * parents are only enqueued for emission
1019 * when all their children have been emitted thereby
1020 * guaranteeing topological order.
1023 if (!pn
->indegree
) {
1025 insert_by_date(parent
, &work
);
1027 commit_list_insert(parent
, &work
);
1030 parents
=parents
->next
;
1033 * work_item is a commit all of whose children
1034 * have already been emitted. we can emit it now.
1036 *pptr
= work_node
->list_item
;
1037 pptr
= &(*pptr
)->next
;
1039 setter(work_item
, NULL
);
1044 /* merge-base stuff */
1046 /* bits #0..15 in revision.h */
1047 #define PARENT1 (1u<<16)
1048 #define PARENT2 (1u<<17)
1049 #define STALE (1u<<18)
1050 #define RESULT (1u<<19)
1052 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
1054 static struct commit
*interesting(struct commit_list
*list
)
1057 struct commit
*commit
= list
->item
;
1059 if (commit
->object
.flags
& STALE
)
1066 static struct commit_list
*merge_bases(struct commit
*one
, struct commit
*two
)
1068 struct commit_list
*list
= NULL
;
1069 struct commit_list
*result
= NULL
;
1072 /* We do not mark this even with RESULT so we do not
1073 * have to clean it up.
1075 return commit_list_insert(one
, &result
);
1080 one
->object
.flags
|= PARENT1
;
1081 two
->object
.flags
|= PARENT2
;
1082 insert_by_date(one
, &list
);
1083 insert_by_date(two
, &list
);
1085 while (interesting(list
)) {
1086 struct commit
*commit
;
1087 struct commit_list
*parents
;
1088 struct commit_list
*n
;
1091 commit
= list
->item
;
1096 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
1097 if (flags
== (PARENT1
| PARENT2
)) {
1098 if (!(commit
->object
.flags
& RESULT
)) {
1099 commit
->object
.flags
|= RESULT
;
1100 insert_by_date(commit
, &result
);
1102 /* Mark parents of a found merge stale */
1105 parents
= commit
->parents
;
1107 struct commit
*p
= parents
->item
;
1108 parents
= parents
->next
;
1109 if ((p
->object
.flags
& flags
) == flags
)
1112 p
->object
.flags
|= flags
;
1113 insert_by_date(p
, &list
);
1117 /* Clean up the result to remove stale ones */
1118 free_commit_list(list
);
1119 list
= result
; result
= NULL
;
1121 struct commit_list
*n
= list
->next
;
1122 if (!(list
->item
->object
.flags
& STALE
))
1123 insert_by_date(list
->item
, &result
);
1130 struct commit_list
*get_merge_bases(struct commit
*one
,
1134 struct commit_list
*list
;
1135 struct commit
**rslt
;
1136 struct commit_list
*result
;
1139 result
= merge_bases(one
, two
);
1142 if (!result
|| !result
->next
) {
1144 clear_commit_marks(one
, all_flags
);
1145 clear_commit_marks(two
, all_flags
);
1150 /* There are more than one */
1157 rslt
= xcalloc(cnt
, sizeof(*rslt
));
1158 for (list
= result
, i
= 0; list
; list
= list
->next
)
1159 rslt
[i
++] = list
->item
;
1160 free_commit_list(result
);
1162 clear_commit_marks(one
, all_flags
);
1163 clear_commit_marks(two
, all_flags
);
1164 for (i
= 0; i
< cnt
- 1; i
++) {
1165 for (j
= i
+1; j
< cnt
; j
++) {
1166 if (!rslt
[i
] || !rslt
[j
])
1168 result
= merge_bases(rslt
[i
], rslt
[j
]);
1169 clear_commit_marks(rslt
[i
], all_flags
);
1170 clear_commit_marks(rslt
[j
], all_flags
);
1171 for (list
= result
; list
; list
= list
->next
) {
1172 if (rslt
[i
] == list
->item
)
1174 if (rslt
[j
] == list
->item
)
1180 /* Surviving ones in rslt[] are the independent results */
1182 for (i
= 0; i
< cnt
; i
++) {
1184 insert_by_date(rslt
[i
], &result
);
1190 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
1192 struct commit_list
*bases
, *b
;
1196 bases
= get_merge_bases(commit
, *reference
, 1);
1199 for (b
= bases
; b
; b
= b
->next
) {
1200 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
1206 free_commit_list(bases
);