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
!= TYPE_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
= TYPE_COMMIT
;
93 obj
->type
= TYPE_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
= memcmp(sha1
, graft
->sha1
, 20);
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 void free_commit_grafts(void)
168 int pos
= commit_graft_nr
;
170 free(commit_graft
[pos
--]);
174 struct commit_graft
*read_graft_line(char *buf
, int len
)
176 /* The format is just "Commit Parent1 Parent2 ...\n" */
178 struct commit_graft
*graft
= NULL
;
180 if (buf
[len
-1] == '\n')
182 if (buf
[0] == '#' || buf
[0] == '\0')
184 if ((len
+ 1) % 41) {
186 error("bad graft data: %s", buf
);
190 i
= (len
+ 1) / 41 - 1;
191 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
192 graft
->nr_parent
= i
;
193 if (get_sha1_hex(buf
, graft
->sha1
))
195 for (i
= 40; i
< len
; i
+= 41) {
198 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
204 int read_graft_file(const char *graft_file
)
206 FILE *fp
= fopen(graft_file
, "r");
210 while (fgets(buf
, sizeof(buf
), fp
)) {
211 /* The format is just "Commit Parent1 Parent2 ...\n" */
212 int len
= strlen(buf
);
213 struct commit_graft
*graft
= read_graft_line(buf
, len
);
216 if (register_commit_graft(graft
, 1))
217 error("duplicate graft data: %s", buf
);
223 static void prepare_commit_graft(void)
225 static int commit_graft_prepared
;
226 static char *last_graft_file
;
227 char *graft_file
= get_graft_file();
229 if (last_graft_file
) {
230 if (!strcmp(graft_file
, last_graft_file
))
232 free_commit_grafts();
235 free(last_graft_file
);
236 last_graft_file
= strdup(graft_file
);
238 read_graft_file(graft_file
);
239 commit_graft_prepared
= 1;
242 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
245 prepare_commit_graft();
246 pos
= commit_graft_pos(sha1
);
249 return commit_graft
[pos
];
252 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
255 char *bufptr
= buffer
;
256 unsigned char parent
[20];
257 struct commit_list
**pptr
;
258 struct commit_graft
*graft
;
261 if (item
->object
.parsed
)
263 item
->object
.parsed
= 1;
265 if (tail
<= bufptr
+ 5 || memcmp(bufptr
, "tree ", 5))
266 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
267 if (tail
<= bufptr
+ 45 || get_sha1_hex(bufptr
+ 5, parent
) < 0)
268 return error("bad tree pointer in commit %s",
269 sha1_to_hex(item
->object
.sha1
));
270 item
->tree
= lookup_tree(parent
);
273 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
274 pptr
= &item
->parents
;
276 graft
= lookup_commit_graft(item
->object
.sha1
);
277 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
278 struct commit
*new_parent
;
280 if (tail
<= bufptr
+ 48 ||
281 get_sha1_hex(bufptr
+ 7, parent
) ||
283 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
287 new_parent
= lookup_commit(parent
);
289 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
295 struct commit
*new_parent
;
296 for (i
= 0; i
< graft
->nr_parent
; i
++) {
297 new_parent
= lookup_commit(graft
->parent
[i
]);
300 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
304 item
->date
= parse_commit_date(bufptr
);
306 if (track_object_refs
) {
308 struct commit_list
*p
;
309 struct object_refs
*refs
= alloc_object_refs(n_refs
);
311 refs
->ref
[i
++] = &item
->tree
->object
;
312 for (p
= item
->parents
; p
; p
= p
->next
)
313 refs
->ref
[i
++] = &p
->item
->object
;
314 set_object_refs(&item
->object
, refs
);
320 int parse_commit(struct commit
*item
)
327 if (item
->object
.parsed
)
329 buffer
= read_sha1_file(item
->object
.sha1
, type
, &size
);
331 return error("Could not read %s",
332 sha1_to_hex(item
->object
.sha1
));
333 if (strcmp(type
, commit_type
)) {
335 return error("Object %s not a commit",
336 sha1_to_hex(item
->object
.sha1
));
338 ret
= parse_commit_buffer(item
, buffer
, size
);
339 if (save_commit_buffer
&& !ret
) {
340 item
->buffer
= buffer
;
347 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
349 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
350 new_list
->item
= item
;
351 new_list
->next
= *list_p
;
356 void free_commit_list(struct commit_list
*list
)
359 struct commit_list
*temp
= list
;
365 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
367 struct commit_list
**pp
= list
;
368 struct commit_list
*p
;
369 while ((p
= *pp
) != NULL
) {
370 if (p
->item
->date
< item
->date
) {
375 return commit_list_insert(item
, pp
);
379 void sort_by_date(struct commit_list
**list
)
381 struct commit_list
*ret
= NULL
;
383 insert_by_date((*list
)->item
, &ret
);
384 *list
= (*list
)->next
;
389 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
392 struct commit
*ret
= (*list
)->item
;
393 struct commit_list
*parents
= ret
->parents
;
394 struct commit_list
*old
= *list
;
396 *list
= (*list
)->next
;
400 struct commit
*commit
= parents
->item
;
401 parse_commit(commit
);
402 if (!(commit
->object
.flags
& mark
)) {
403 commit
->object
.flags
|= mark
;
404 insert_by_date(commit
, list
);
406 parents
= parents
->next
;
411 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
413 struct commit_list
*parents
;
415 parents
= commit
->parents
;
416 commit
->object
.flags
&= ~mark
;
418 struct commit
*parent
= parents
->item
;
419 if (parent
&& parent
->object
.parsed
&&
420 (parent
->object
.flags
& mark
))
421 clear_commit_marks(parent
, mark
);
422 parents
= parents
->next
;
427 * Generic support for pretty-printing the header
429 static int get_one_line(const char *msg
, unsigned long len
)
444 static int is_rfc2047_special(char ch
)
446 return ((ch
& 0x80) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
449 static int add_rfc2047(char *buf
, const char *line
, int len
)
453 static const char q_utf8
[] = "=?utf-8?q?";
455 for (i
= needquote
= 0; !needquote
&& i
< len
; i
++) {
456 unsigned ch
= line
[i
];
460 (ch
== '=' && line
[i
+1] == '?'))
464 return sprintf(buf
, "%.*s", len
, line
);
466 memcpy(bp
, q_utf8
, sizeof(q_utf8
)-1);
467 bp
+= sizeof(q_utf8
)-1;
468 for (i
= 0; i
< len
; i
++) {
469 unsigned ch
= line
[i
] & 0xFF;
470 if (is_rfc2047_special(ch
)) {
471 sprintf(bp
, "=%02X", ch
);
484 static int add_user_info(const char *what
, enum cmit_fmt fmt
, char *buf
, const char *line
)
490 const char *filler
= " ";
492 if (fmt
== CMIT_FMT_ONELINE
)
494 date
= strchr(line
, '>');
497 namelen
= ++date
- line
;
498 time
= strtoul(date
, &date
, 10);
499 tz
= strtol(date
, NULL
, 10);
501 if (fmt
== CMIT_FMT_EMAIL
) {
502 char *name_tail
= strchr(line
, '<');
503 int display_name_length
;
506 while (line
< name_tail
&& isspace(name_tail
[-1]))
508 display_name_length
= name_tail
- line
;
510 strcpy(buf
, "From: ");
512 ret
+= add_rfc2047(buf
+ ret
, line
, display_name_length
);
513 memcpy(buf
+ ret
, name_tail
, namelen
- display_name_length
);
514 ret
+= namelen
- display_name_length
;
518 ret
= sprintf(buf
, "%s: %.*s%.*s\n", what
,
519 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
520 filler
, namelen
, line
);
523 case CMIT_FMT_MEDIUM
:
524 ret
+= sprintf(buf
+ ret
, "Date: %s\n", show_date(time
, tz
));
527 ret
+= sprintf(buf
+ ret
, "Date: %s\n",
528 show_rfc2822_date(time
, tz
));
530 case CMIT_FMT_FULLER
:
531 ret
+= sprintf(buf
+ ret
, "%sDate: %s\n", what
, show_date(time
, tz
));
540 static int is_empty_line(const char *line
, int *len_p
)
543 while (len
&& isspace(line
[len
-1]))
549 static int add_merge_info(enum cmit_fmt fmt
, char *buf
, const struct commit
*commit
, int abbrev
)
551 struct commit_list
*parent
= commit
->parents
;
554 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
555 !parent
|| !parent
->next
)
558 offset
= sprintf(buf
, "Merge:");
561 struct commit
*p
= parent
->item
;
562 const char *hex
= abbrev
563 ? find_unique_abbrev(p
->object
.sha1
, abbrev
)
564 : sha1_to_hex(p
->object
.sha1
);
565 const char *dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
566 parent
= parent
->next
;
568 offset
+= sprintf(buf
+ offset
, " %s%s", hex
, dots
);
570 buf
[offset
++] = '\n';
574 unsigned long pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
, unsigned long len
, char *buf
, unsigned long space
, int abbrev
, const char *subject
, const char *after_subject
)
576 int hdr
= 1, body
= 0;
577 unsigned long offset
= 0;
579 int parents_shown
= 0;
580 const char *msg
= commit
->buffer
;
581 int plain_non_ascii
= 0;
583 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
586 /* After-subject is used to pass in Content-Type: multipart
587 * MIME header; in that case we do not have to do the
588 * plaintext content type even if the commit message has
589 * non 7-bit ASCII character. Otherwise, check if we need
590 * to say this is not a 7-bit ASCII.
592 if (fmt
== CMIT_FMT_EMAIL
&& !after_subject
) {
595 for (in_body
= i
= 0; (ch
= msg
[i
]) && i
< len
; i
++) {
597 /* author could be non 7-bit ASCII but
598 * the log may so; skip over the
602 i
+ 1 < len
&& msg
[i
+1] == '\n')
605 else if (ch
& 0x80) {
613 const char *line
= msg
;
614 int linelen
= get_one_line(msg
, len
);
620 * We want some slop for indentation and a possible
621 * final "...". Thus the "+ 20".
623 if (offset
+ linelen
+ 20 > space
) {
624 memcpy(buf
+ offset
, " ...\n", 8);
634 if ((fmt
!= CMIT_FMT_ONELINE
) && !subject
)
635 buf
[offset
++] = '\n';
638 if (fmt
== CMIT_FMT_RAW
) {
639 memcpy(buf
+ offset
, line
, linelen
);
643 if (!memcmp(line
, "parent ", 7)) {
645 die("bad parent line in commit");
649 if (!parents_shown
) {
650 offset
+= add_merge_info(fmt
, buf
+ offset
,
656 * MEDIUM == DEFAULT shows only author with dates.
657 * FULL shows both authors but not dates.
658 * FULLER shows both authors and dates.
660 if (!memcmp(line
, "author ", 7))
661 offset
+= add_user_info("Author", fmt
,
664 if (!memcmp(line
, "committer ", 10) &&
665 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
))
666 offset
+= add_user_info("Commit", fmt
,
672 if (is_empty_line(line
, &linelen
)) {
677 if (fmt
== CMIT_FMT_SHORT
)
684 int slen
= strlen(subject
);
685 memcpy(buf
+ offset
, subject
, slen
);
687 offset
+= add_rfc2047(buf
+ offset
, line
, linelen
);
690 memset(buf
+ offset
, ' ', indent
);
691 memcpy(buf
+ offset
+ indent
, line
, linelen
);
692 offset
+= linelen
+ indent
;
694 buf
[offset
++] = '\n';
695 if (fmt
== CMIT_FMT_ONELINE
)
697 if (subject
&& plain_non_ascii
) {
698 static const char header
[] =
699 "Content-Type: text/plain; charset=UTF-8\n"
700 "Content-Transfer-Encoding: 8bit\n";
701 memcpy(buf
+ offset
, header
, sizeof(header
)-1);
702 offset
+= sizeof(header
)-1;
705 int slen
= strlen(after_subject
);
706 if (slen
> space
- offset
- 1)
707 slen
= space
- offset
- 1;
708 memcpy(buf
+ offset
, after_subject
, slen
);
710 after_subject
= NULL
;
714 while (offset
&& isspace(buf
[offset
-1]))
716 /* Make sure there is an EOLN for the non-oneline case */
717 if (fmt
!= CMIT_FMT_ONELINE
)
718 buf
[offset
++] = '\n';
723 struct commit
*pop_commit(struct commit_list
**stack
)
725 struct commit_list
*top
= *stack
;
726 struct commit
*item
= top
? top
->item
: NULL
;
735 int count_parents(struct commit
* commit
)
738 struct commit_list
* parents
= commit
->parents
;
739 for (count
=0;parents
; parents
=parents
->next
,count
++)
744 void topo_sort_default_setter(struct commit
*c
, void *data
)
749 void *topo_sort_default_getter(struct commit
*c
)
755 * Performs an in-place topological sort on the list supplied.
757 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
759 sort_in_topological_order_fn(list
, lifo
, topo_sort_default_setter
,
760 topo_sort_default_getter
);
763 void sort_in_topological_order_fn(struct commit_list
** list
, int lifo
,
764 topo_sort_set_fn_t setter
,
765 topo_sort_get_fn_t getter
)
767 struct commit_list
* next
= *list
;
768 struct commit_list
* work
= NULL
, **insert
;
769 struct commit_list
** pptr
= list
;
770 struct sort_node
* nodes
;
771 struct sort_node
* next_nodes
;
774 /* determine the size of the list */
782 /* allocate an array to help sort the list */
783 nodes
= xcalloc(count
, sizeof(*nodes
));
784 /* link the list to the array */
788 next_nodes
->list_item
= next
;
789 setter(next
->item
, next_nodes
);
793 /* update the indegree */
796 struct commit_list
* parents
= next
->item
->parents
;
798 struct commit
* parent
=parents
->item
;
799 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
803 parents
=parents
->next
;
810 * tips are nodes not reachable from any other node in the list
812 * the tips serve as a starting set for the work queue.
817 struct sort_node
* node
= (struct sort_node
*) getter(next
->item
);
819 if (node
->indegree
== 0) {
820 insert
= &commit_list_insert(next
->item
, insert
)->next
;
825 /* process the list in topological order */
829 struct commit
* work_item
= pop_commit(&work
);
830 struct sort_node
* work_node
= (struct sort_node
*) getter(work_item
);
831 struct commit_list
* parents
= work_item
->parents
;
834 struct commit
* parent
=parents
->item
;
835 struct sort_node
* pn
= (struct sort_node
*) getter(parent
);
839 * parents are only enqueued for emission
840 * when all their children have been emitted thereby
841 * guaranteeing topological order.
846 insert_by_date(parent
, &work
);
848 commit_list_insert(parent
, &work
);
851 parents
=parents
->next
;
854 * work_item is a commit all of whose children
855 * have already been emitted. we can emit it now.
857 *pptr
= work_node
->list_item
;
858 pptr
= &(*pptr
)->next
;
860 setter(work_item
, NULL
);