5 int save_commit_buffer
= 1;
10 * the number of children of the associated commit
11 * that also occur in the list being sorted.
13 unsigned int indegree
;
16 * reference to original list item that we will re-use
19 struct commit_list
* list_item
;
23 const char *commit_type
= "commit";
30 { "raw", 1, CMIT_FMT_RAW
},
31 { "medium", 1, CMIT_FMT_MEDIUM
},
32 { "short", 1, CMIT_FMT_SHORT
},
33 { "email", 1, CMIT_FMT_EMAIL
},
34 { "full", 5, CMIT_FMT_FULL
},
35 { "fuller", 5, CMIT_FMT_FULLER
},
36 { "oneline", 1, CMIT_FMT_ONELINE
},
39 enum cmit_fmt
get_commit_format(const char *arg
)
44 return CMIT_FMT_DEFAULT
;
47 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
48 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
))
52 die("invalid --pretty format: %s", arg
);
55 static struct commit
*check_commit(struct object
*obj
,
56 const unsigned char *sha1
,
59 if (obj
->type
!= OBJ_COMMIT
) {
61 error("Object %s is a %s, not a commit",
62 sha1_to_hex(sha1
), typename(obj
->type
));
65 return (struct commit
*) obj
;
68 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
71 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
75 return check_commit(obj
, sha1
, quiet
);
78 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
80 return lookup_commit_reference_gently(sha1
, 0);
83 struct commit
*lookup_commit(const unsigned char *sha1
)
85 struct object
*obj
= lookup_object(sha1
);
87 struct commit
*ret
= alloc_commit_node();
88 created_object(sha1
, &ret
->object
);
89 ret
->object
.type
= OBJ_COMMIT
;
93 obj
->type
= OBJ_COMMIT
;
94 return check_commit(obj
, sha1
, 0);
97 static unsigned long parse_commit_date(const char *buf
)
101 if (memcmp(buf
, "author", 6))
103 while (*buf
++ != '\n')
105 if (memcmp(buf
, "committer", 9))
107 while (*buf
++ != '>')
109 date
= strtoul(buf
, NULL
, 10);
110 if (date
== ULONG_MAX
)
115 static struct commit_graft
**commit_graft
;
116 static int commit_graft_alloc
, commit_graft_nr
;
118 static int commit_graft_pos(const unsigned char *sha1
)
122 hi
= commit_graft_nr
;
124 int mi
= (lo
+ hi
) / 2;
125 struct commit_graft
*graft
= commit_graft
[mi
];
126 int cmp
= hashcmp(sha1
, graft
->sha1
);
137 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
139 int pos
= commit_graft_pos(graft
->sha1
);
145 free(commit_graft
[pos
]);
146 commit_graft
[pos
] = graft
;
151 if (commit_graft_alloc
<= ++commit_graft_nr
) {
152 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
153 commit_graft
= xrealloc(commit_graft
,
154 sizeof(*commit_graft
) *
157 if (pos
< commit_graft_nr
)
158 memmove(commit_graft
+ pos
+ 1,
160 (commit_graft_nr
- pos
- 1) *
161 sizeof(*commit_graft
));
162 commit_graft
[pos
] = graft
;
166 struct commit_graft
*read_graft_line(char *buf
, int len
)
168 /* The format is just "Commit Parent1 Parent2 ...\n" */
170 struct commit_graft
*graft
= NULL
;
172 if (buf
[len
-1] == '\n')
174 if (buf
[0] == '#' || buf
[0] == '\0')
176 if ((len
+ 1) % 41) {
178 error("bad graft data: %s", buf
);
182 i
= (len
+ 1) / 41 - 1;
183 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
184 graft
->nr_parent
= i
;
185 if (get_sha1_hex(buf
, graft
->sha1
))
187 for (i
= 40; i
< len
; i
+= 41) {
190 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
196 int read_graft_file(const char *graft_file
)
198 FILE *fp
= fopen(graft_file
, "r");
202 while (fgets(buf
, sizeof(buf
), fp
)) {
203 /* The format is just "Commit Parent1 Parent2 ...\n" */
204 int len
= strlen(buf
);
205 struct commit_graft
*graft
= read_graft_line(buf
, len
);
208 if (register_commit_graft(graft
, 1))
209 error("duplicate graft data: %s", buf
);
215 static void prepare_commit_graft(void)
217 static int commit_graft_prepared
;
220 if (commit_graft_prepared
)
222 graft_file
= get_graft_file();
223 read_graft_file(graft_file
);
224 commit_graft_prepared
= 1;
227 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
230 prepare_commit_graft();
231 pos
= commit_graft_pos(sha1
);
234 return commit_graft
[pos
];
237 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
240 char *bufptr
= buffer
;
241 unsigned char parent
[20];
242 struct commit_list
**pptr
;
243 struct commit_graft
*graft
;
246 if (item
->object
.parsed
)
248 item
->object
.parsed
= 1;
250 if (tail
<= bufptr
+ 5 || memcmp(bufptr
, "tree ", 5))
251 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
252 if (tail
<= bufptr
+ 45 || get_sha1_hex(bufptr
+ 5, parent
) < 0)
253 return error("bad tree pointer in commit %s",
254 sha1_to_hex(item
->object
.sha1
));
255 item
->tree
= lookup_tree(parent
);
258 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
259 pptr
= &item
->parents
;
261 graft
= lookup_commit_graft(item
->object
.sha1
);
262 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
263 struct commit
*new_parent
;
265 if (tail
<= bufptr
+ 48 ||
266 get_sha1_hex(bufptr
+ 7, parent
) ||
268 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
272 new_parent
= lookup_commit(parent
);
274 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
280 struct commit
*new_parent
;
281 for (i
= 0; i
< graft
->nr_parent
; i
++) {
282 new_parent
= lookup_commit(graft
->parent
[i
]);
285 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
289 item
->date
= parse_commit_date(bufptr
);
291 if (track_object_refs
) {
293 struct commit_list
*p
;
294 struct object_refs
*refs
= alloc_object_refs(n_refs
);
296 refs
->ref
[i
++] = &item
->tree
->object
;
297 for (p
= item
->parents
; p
; p
= p
->next
)
298 refs
->ref
[i
++] = &p
->item
->object
;
299 set_object_refs(&item
->object
, refs
);
305 int parse_commit(struct commit
*item
)
312 if (item
->object
.parsed
)
314 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
316 return error("Could not read %s",
317 sha1_to_hex(item
->object
.sha1
));
318 if (strcmp(type
, commit_type
)) {
320 return error("Object %s not a commit",
321 sha1_to_hex(item
->object
.sha1
));
323 ret
= parse_commit_buffer(item
, buffer
, size
);
324 if (save_commit_buffer
&& !ret
) {
325 item
->buffer
= buffer
;
332 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
334 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
335 new_list
->item
= item
;
336 new_list
->next
= *list_p
;
341 void free_commit_list(struct commit_list
*list
)
344 struct commit_list
*temp
= list
;
350 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
352 struct commit_list
**pp
= list
;
353 struct commit_list
*p
;
354 while ((p
= *pp
) != NULL
) {
355 if (p
->item
->date
< item
->date
) {
360 return commit_list_insert(item
, pp
);
364 void sort_by_date(struct commit_list
**list
)
366 struct commit_list
*ret
= NULL
;
368 insert_by_date((*list
)->item
, &ret
);
369 *list
= (*list
)->next
;
374 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
377 struct commit
*ret
= (*list
)->item
;
378 struct commit_list
*parents
= ret
->parents
;
379 struct commit_list
*old
= *list
;
381 *list
= (*list
)->next
;
385 struct commit
*commit
= parents
->item
;
386 parse_commit(commit
);
387 if (!(commit
->object
.flags
& mark
)) {
388 commit
->object
.flags
|= mark
;
389 insert_by_date(commit
, list
);
391 parents
= parents
->next
;
396 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
398 struct commit_list
*parents
;
400 commit
->object
.flags
&= ~mark
;
401 parents
= commit
->parents
;
403 struct commit
*parent
= parents
->item
;
405 /* Have we already cleared this? */
406 if (mark
& parent
->object
.flags
)
407 clear_commit_marks(parent
, mark
);
408 parents
= parents
->next
;
413 * Generic support for pretty-printing the header
415 static int get_one_line(const char *msg
, unsigned long len
)
430 static int is_rfc2047_special(char ch
)
432 return ((ch
& 0x80) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
435 static int add_rfc2047(char *buf
, const char *line
, int len
)
439 static const char q_utf8
[] = "=?utf-8?q?";
441 for (i
= needquote
= 0; !needquote
&& i
< len
; i
++) {
442 unsigned ch
= line
[i
];
446 (ch
== '=' && line
[i
+1] == '?'))
450 return sprintf(buf
, "%.*s", len
, line
);
452 memcpy(bp
, q_utf8
, sizeof(q_utf8
)-1);
453 bp
+= sizeof(q_utf8
)-1;
454 for (i
= 0; i
< len
; i
++) {
455 unsigned ch
= line
[i
] & 0xFF;
456 if (is_rfc2047_special(ch
)) {
457 sprintf(bp
, "=%02X", ch
);
470 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
,
471 const char *line
, int relative_date
)
477 const char *filler
= " ";
479 if (fmt
== CMIT_FMT_ONELINE
)
481 date
= strchr(line
, '>');
484 namelen
= ++date
- line
;
485 time
= strtoul(date
, &date
, 10);
486 tz
= strtol(date
, NULL
, 10);
488 if (fmt
== CMIT_FMT_EMAIL
) {
489 char *name_tail
= strchr(line
, '<');
490 int display_name_length
;
493 while (line
< name_tail
&& isspace(name_tail
[-1]))
495 display_name_length
= name_tail
- line
;
497 strcpy(buf
, "From: ");
499 ret
+= add_rfc2047(buf
+ ret
, line
, display_name_length
);
500 memcpy(buf
+ ret
, name_tail
, namelen
- display_name_length
);
501 ret
+= namelen
- display_name_length
;
505 ret
= sprintf(buf
, "%s: %.*s%.*s\n", what
,
506 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
507 filler
, namelen
, line
);
510 case CMIT_FMT_MEDIUM
:
511 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
512 show_date(time
, tz
, relative_date
));
515 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
516 show_rfc2822_date(time
, tz
));
518 case CMIT_FMT_FULLER
:
519 ret
+= sprintf(buf
+ ret
, "%sDate: %s\n", what
,
520 show_date(time
, tz
, relative_date
));
529 static int is_empty_line(const char *line
, int *len_p
)
532 while (len
&& isspace(line
[len
-1]))
538 static int add_merge_info(enum cmit_fmt fmt
, char *buf
, const struct commit
*commit
, int abbrev
)
540 struct commit_list
*parent
= commit
->parents
;
543 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
544 !parent
|| !parent
->next
)
547 offset
= sprintf(buf
, "Merge:");
550 struct commit
*p
= parent
->item
;
551 const char *hex
= abbrev
552 ? find_unique_abbrev(p
->object
.sha1
, abbrev
)
553 : sha1_to_hex(p
->object
.sha1
);
554 const char *dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
555 parent
= parent
->next
;
557 offset
+= sprintf(buf
+ offset
, " %s%s", hex
, dots
);
559 buf
[offset
++] = '\n';
563 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
564 unsigned long len
, char *buf
, unsigned long space
,
565 int abbrev
, const char *subject
,
566 const char *after_subject
, int relative_date
)
568 int hdr
= 1, body
= 0;
569 unsigned long offset
= 0;
571 int parents_shown
= 0;
572 const char *msg
= commit
->buffer
;
573 int plain_non_ascii
= 0;
575 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
578 /* After-subject is used to pass in Content-Type: multipart
579 * MIME header; in that case we do not have to do the
580 * plaintext content type even if the commit message has
581 * non 7-bit ASCII character. Otherwise, check if we need
582 * to say this is not a 7-bit ASCII.
584 if (fmt
== CMIT_FMT_EMAIL
&& !after_subject
) {
587 for (in_body
= i
= 0; (ch
= msg
[i
]) && i
< len
; i
++) {
589 /* author could be non 7-bit ASCII but
590 * the log may so; skip over the
594 i
+ 1 < len
&& msg
[i
+1] == '\n')
597 else if (ch
& 0x80) {
605 const char *line
= msg
;
606 int linelen
= get_one_line(msg
, len
);
612 * We want some slop for indentation and a possible
613 * final "...". Thus the "+ 20".
615 if (offset
+ linelen
+ 20 > space
) {
616 memcpy(buf
+ offset
, " ...\n", 8);
626 if ((fmt
!= CMIT_FMT_ONELINE
) && !subject
)
627 buf
[offset
++] = '\n';
630 if (fmt
== CMIT_FMT_RAW
) {
631 memcpy(buf
+ offset
, line
, linelen
);
635 if (!memcmp(line
, "parent ", 7)) {
637 die("bad parent line in commit");
641 if (!parents_shown
) {
642 offset
+= add_merge_info(fmt
, buf
+ offset
,
648 * MEDIUM == DEFAULT shows only author with dates.
649 * FULL shows both authors but not dates.
650 * FULLER shows both authors and dates.
652 if (!memcmp(line
, "author ", 7))
653 offset
+= add_user_info("Author", fmt
,
657 if (!memcmp(line
, "committer ", 10) &&
658 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
))
659 offset
+= add_user_info("Commit", fmt
,
669 if (is_empty_line(line
, &linelen
)) {
674 if (fmt
== CMIT_FMT_SHORT
)
679 int slen
= strlen(subject
);
680 memcpy(buf
+ offset
, subject
, slen
);
682 offset
+= add_rfc2047(buf
+ offset
, line
, linelen
);
685 memset(buf
+ offset
, ' ', indent
);
686 memcpy(buf
+ offset
+ indent
, line
, linelen
);
687 offset
+= linelen
+ indent
;
689 buf
[offset
++] = '\n';
690 if (fmt
== CMIT_FMT_ONELINE
)
692 if (subject
&& plain_non_ascii
) {
693 static const char header
[] =
694 "Content-Type: text/plain; charset=UTF-8\n"
695 "Content-Transfer-Encoding: 8bit\n";
696 memcpy(buf
+ offset
, header
, sizeof(header
)-1);
697 offset
+= sizeof(header
)-1;
700 int slen
= strlen(after_subject
);
701 if (slen
> space
- offset
- 1)
702 slen
= space
- offset
- 1;
703 memcpy(buf
+ offset
, after_subject
, slen
);
705 after_subject
= NULL
;
709 while (offset
&& isspace(buf
[offset
-1]))
711 /* Make sure there is an EOLN for the non-oneline case */
712 if (fmt
!= CMIT_FMT_ONELINE
)
713 buf
[offset
++] = '\n';
715 * make sure there is another EOLN to separate the headers from whatever
716 * body the caller appends if we haven't already written a body
718 if (fmt
== CMIT_FMT_EMAIL
&& !body
)
719 buf
[offset
++] = '\n';
724 struct commit
*pop_commit(struct commit_list
**stack
)
726 struct commit_list
*top
= *stack
;
727 struct commit
*item
= top
? top
->item
: NULL
;
736 int count_parents(struct commit
* commit
)
739 struct commit_list
* parents
= commit
->parents
;
740 for (count
= 0; parents
; parents
= parents
->next
,count
++)
745 void topo_sort_default_setter(struct commit
*c
, void *data
)
750 void *topo_sort_default_getter(struct commit
*c
)
756 * Performs an in-place topological sort on the list supplied.
758 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
760 sort_in_topological_order_fn(list
, lifo
, topo_sort_default_setter
,
761 topo_sort_default_getter
);
764 void sort_in_topological_order_fn(struct commit_list
** list
, int lifo
,
765 topo_sort_set_fn_t setter
,
766 topo_sort_get_fn_t getter
)
768 struct commit_list
* next
= *list
;
769 struct commit_list
* work
= NULL
, **insert
;
770 struct commit_list
** pptr
= list
;
771 struct sort_node
* nodes
;
772 struct sort_node
* next_nodes
;
775 /* determine the size of the list */
783 /* allocate an array to help sort the list */
784 nodes
= xcalloc(count
, sizeof(*nodes
));
785 /* link the list to the array */
789 next_nodes
->list_item
= next
;
790 setter(next
->item
, next_nodes
);
794 /* update the indegree */
797 struct commit_list
* parents
= next
->item
->parents
;
799 struct commit
* parent
=parents
->item
;
800 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
804 parents
=parents
->next
;
811 * tips are nodes not reachable from any other node in the list
813 * the tips serve as a starting set for the work queue.
818 struct sort_node
* node
= (struct sort_node
*) getter(next
->item
);
820 if (node
->indegree
== 0) {
821 insert
= &commit_list_insert(next
->item
, insert
)->next
;
826 /* process the list in topological order */
830 struct commit
* work_item
= pop_commit(&work
);
831 struct sort_node
* work_node
= (struct sort_node
*) getter(work_item
);
832 struct commit_list
* parents
= work_item
->parents
;
835 struct commit
* parent
=parents
->item
;
836 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
840 * parents are only enqueued for emission
841 * when all their children have been emitted thereby
842 * guaranteeing topological order.
847 insert_by_date(parent
, &work
);
849 commit_list_insert(parent
, &work
);
852 parents
=parents
->next
;
855 * work_item is a commit all of whose children
856 * have already been emitted. we can emit it now.
858 *pptr
= work_node
->list_item
;
859 pptr
= &(*pptr
)->next
;
861 setter(work_item
, NULL
);
866 /* merge-rebase stuff */
868 /* bits #0..7 in revision.h */
869 #define PARENT1 (1u<< 8)
870 #define PARENT2 (1u<< 9)
871 #define STALE (1u<<10)
872 #define RESULT (1u<<11)
874 static struct commit
*interesting(struct commit_list
*list
)
877 struct commit
*commit
= list
->item
;
879 if (commit
->object
.flags
& STALE
)
886 static struct commit_list
*merge_bases(struct commit
*one
, struct commit
*two
)
888 struct commit_list
*list
= NULL
;
889 struct commit_list
*result
= NULL
;
892 /* We do not mark this even with RESULT so we do not
893 * have to clean it up.
895 return commit_list_insert(one
, &result
);
900 one
->object
.flags
|= PARENT1
;
901 two
->object
.flags
|= PARENT2
;
902 insert_by_date(one
, &list
);
903 insert_by_date(two
, &list
);
905 while (interesting(list
)) {
906 struct commit
*commit
;
907 struct commit_list
*parents
;
908 struct commit_list
*n
;
916 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
917 if (flags
== (PARENT1
| PARENT2
)) {
918 if (!(commit
->object
.flags
& RESULT
)) {
919 commit
->object
.flags
|= RESULT
;
920 insert_by_date(commit
, &result
);
922 /* Mark parents of a found merge stale */
925 parents
= commit
->parents
;
927 struct commit
*p
= parents
->item
;
928 parents
= parents
->next
;
929 if ((p
->object
.flags
& flags
) == flags
)
932 p
->object
.flags
|= flags
;
933 insert_by_date(p
, &list
);
937 /* Clean up the result to remove stale ones */
938 list
= result
; result
= NULL
;
940 struct commit_list
*n
= list
->next
;
941 if (!(list
->item
->object
.flags
& STALE
))
942 insert_by_date(list
->item
, &result
);
949 struct commit_list
*get_merge_bases(struct commit
*one
,
953 const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
954 struct commit_list
*list
;
955 struct commit
**rslt
;
956 struct commit_list
*result
;
959 result
= merge_bases(one
, two
);
962 if (!result
|| !result
->next
) {
964 clear_commit_marks(one
, all_flags
);
965 clear_commit_marks(two
, all_flags
);
970 /* There are more than one */
977 rslt
= xcalloc(cnt
, sizeof(*rslt
));
978 for (list
= result
, i
= 0; list
; list
= list
->next
)
979 rslt
[i
++] = list
->item
;
980 free_commit_list(result
);
982 clear_commit_marks(one
, all_flags
);
983 clear_commit_marks(two
, all_flags
);
984 for (i
= 0; i
< cnt
- 1; i
++) {
985 for (j
= i
+1; j
< cnt
; j
++) {
986 if (!rslt
[i
] || !rslt
[j
])
988 result
= merge_bases(rslt
[i
], rslt
[j
]);
989 clear_commit_marks(rslt
[i
], all_flags
);
990 clear_commit_marks(rslt
[j
], all_flags
);
991 for (list
= result
; list
; list
= list
->next
) {
992 if (rslt
[i
] == list
->item
)
994 if (rslt
[j
] == list
->item
)
1000 /* Surviving ones in rslt[] are the independent results */
1002 for (i
= 0; i
< cnt
; i
++) {
1004 insert_by_date(rslt
[i
], &result
);