6 #include "interpolate.h"
10 int save_commit_buffer
= 1;
12 const char *commit_type
= "commit";
14 static struct cmt_fmt_map
{
19 { "raw", 1, CMIT_FMT_RAW
},
20 { "medium", 1, CMIT_FMT_MEDIUM
},
21 { "short", 1, CMIT_FMT_SHORT
},
22 { "email", 1, CMIT_FMT_EMAIL
},
23 { "full", 5, CMIT_FMT_FULL
},
24 { "fuller", 5, CMIT_FMT_FULLER
},
25 { "oneline", 1, CMIT_FMT_ONELINE
},
26 { "format:", 7, CMIT_FMT_USERFORMAT
},
29 static char *user_format
;
31 enum cmit_fmt
get_commit_format(const char *arg
)
36 return CMIT_FMT_DEFAULT
;
39 if (!prefixcmp(arg
, "format:")) {
42 user_format
= xstrdup(arg
+ 7);
43 return CMIT_FMT_USERFORMAT
;
45 for (i
= 0; i
< ARRAY_SIZE(cmt_fmts
); i
++) {
46 if (!strncmp(arg
, cmt_fmts
[i
].n
, cmt_fmts
[i
].cmp_len
) &&
47 !strncmp(arg
, cmt_fmts
[i
].n
, strlen(arg
)))
51 die("invalid --pretty format: %s", arg
);
54 static struct commit
*check_commit(struct object
*obj
,
55 const unsigned char *sha1
,
58 if (obj
->type
!= OBJ_COMMIT
) {
60 error("Object %s is a %s, not a commit",
61 sha1_to_hex(sha1
), typename(obj
->type
));
64 return (struct commit
*) obj
;
67 struct commit
*lookup_commit_reference_gently(const unsigned char *sha1
,
70 struct object
*obj
= deref_tag(parse_object(sha1
), NULL
, 0);
74 return check_commit(obj
, sha1
, quiet
);
77 struct commit
*lookup_commit_reference(const unsigned char *sha1
)
79 return lookup_commit_reference_gently(sha1
, 0);
82 struct commit
*lookup_commit(const unsigned char *sha1
)
84 struct object
*obj
= lookup_object(sha1
);
86 return create_object(sha1
, OBJ_COMMIT
, alloc_commit_node());
88 obj
->type
= OBJ_COMMIT
;
89 return check_commit(obj
, sha1
, 0);
92 static unsigned long parse_commit_date(const char *buf
)
96 if (memcmp(buf
, "author", 6))
98 while (*buf
++ != '\n')
100 if (memcmp(buf
, "committer", 9))
102 while (*buf
++ != '>')
104 date
= strtoul(buf
, NULL
, 10);
105 if (date
== ULONG_MAX
)
110 static struct commit_graft
**commit_graft
;
111 static int commit_graft_alloc
, commit_graft_nr
;
113 static int commit_graft_pos(const unsigned char *sha1
)
117 hi
= commit_graft_nr
;
119 int mi
= (lo
+ hi
) / 2;
120 struct commit_graft
*graft
= commit_graft
[mi
];
121 int cmp
= hashcmp(sha1
, graft
->sha1
);
132 int register_commit_graft(struct commit_graft
*graft
, int ignore_dups
)
134 int pos
= commit_graft_pos(graft
->sha1
);
140 free(commit_graft
[pos
]);
141 commit_graft
[pos
] = graft
;
146 if (commit_graft_alloc
<= ++commit_graft_nr
) {
147 commit_graft_alloc
= alloc_nr(commit_graft_alloc
);
148 commit_graft
= xrealloc(commit_graft
,
149 sizeof(*commit_graft
) *
152 if (pos
< commit_graft_nr
)
153 memmove(commit_graft
+ pos
+ 1,
155 (commit_graft_nr
- pos
- 1) *
156 sizeof(*commit_graft
));
157 commit_graft
[pos
] = graft
;
161 struct commit_graft
*read_graft_line(char *buf
, int len
)
163 /* The format is just "Commit Parent1 Parent2 ...\n" */
165 struct commit_graft
*graft
= NULL
;
167 if (buf
[len
-1] == '\n')
169 if (buf
[0] == '#' || buf
[0] == '\0')
171 if ((len
+ 1) % 41) {
173 error("bad graft data: %s", buf
);
177 i
= (len
+ 1) / 41 - 1;
178 graft
= xmalloc(sizeof(*graft
) + 20 * i
);
179 graft
->nr_parent
= i
;
180 if (get_sha1_hex(buf
, graft
->sha1
))
182 for (i
= 40; i
< len
; i
+= 41) {
185 if (get_sha1_hex(buf
+ i
+ 1, graft
->parent
[i
/41]))
191 int read_graft_file(const char *graft_file
)
193 FILE *fp
= fopen(graft_file
, "r");
197 while (fgets(buf
, sizeof(buf
), fp
)) {
198 /* The format is just "Commit Parent1 Parent2 ...\n" */
199 int len
= strlen(buf
);
200 struct commit_graft
*graft
= read_graft_line(buf
, len
);
203 if (register_commit_graft(graft
, 1))
204 error("duplicate graft data: %s", buf
);
210 static void prepare_commit_graft(void)
212 static int commit_graft_prepared
;
215 if (commit_graft_prepared
)
217 graft_file
= get_graft_file();
218 read_graft_file(graft_file
);
219 /* make sure shallows are read */
220 is_repository_shallow();
221 commit_graft_prepared
= 1;
224 static struct commit_graft
*lookup_commit_graft(const unsigned char *sha1
)
227 prepare_commit_graft();
228 pos
= commit_graft_pos(sha1
);
231 return commit_graft
[pos
];
234 int write_shallow_commits(int fd
, int use_pack_protocol
)
237 for (i
= 0; i
< commit_graft_nr
; i
++)
238 if (commit_graft
[i
]->nr_parent
< 0) {
240 sha1_to_hex(commit_graft
[i
]->sha1
);
242 if (use_pack_protocol
)
243 packet_write(fd
, "shallow %s", hex
);
245 if (write_in_full(fd
, hex
, 40) != 40)
247 if (write_in_full(fd
, "\n", 1) != 1)
254 int unregister_shallow(const unsigned char *sha1
)
256 int pos
= commit_graft_pos(sha1
);
259 if (pos
+ 1 < commit_graft_nr
)
260 memcpy(commit_graft
+ pos
, commit_graft
+ pos
+ 1,
261 sizeof(struct commit_graft
*)
262 * (commit_graft_nr
- pos
- 1));
267 int parse_commit_buffer(struct commit
*item
, void *buffer
, unsigned long size
)
270 char *bufptr
= buffer
;
271 unsigned char parent
[20];
272 struct commit_list
**pptr
;
273 struct commit_graft
*graft
;
276 if (item
->object
.parsed
)
278 item
->object
.parsed
= 1;
280 if (tail
<= bufptr
+ 5 || memcmp(bufptr
, "tree ", 5))
281 return error("bogus commit object %s", sha1_to_hex(item
->object
.sha1
));
282 if (tail
<= bufptr
+ 45 || get_sha1_hex(bufptr
+ 5, parent
) < 0)
283 return error("bad tree pointer in commit %s",
284 sha1_to_hex(item
->object
.sha1
));
285 item
->tree
= lookup_tree(parent
);
288 bufptr
+= 46; /* "tree " + "hex sha1" + "\n" */
289 pptr
= &item
->parents
;
291 graft
= lookup_commit_graft(item
->object
.sha1
);
292 while (bufptr
+ 48 < tail
&& !memcmp(bufptr
, "parent ", 7)) {
293 struct commit
*new_parent
;
295 if (tail
<= bufptr
+ 48 ||
296 get_sha1_hex(bufptr
+ 7, parent
) ||
298 return error("bad parents in commit %s", sha1_to_hex(item
->object
.sha1
));
302 new_parent
= lookup_commit(parent
);
304 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
310 struct commit
*new_parent
;
311 for (i
= 0; i
< graft
->nr_parent
; i
++) {
312 new_parent
= lookup_commit(graft
->parent
[i
]);
315 pptr
= &commit_list_insert(new_parent
, pptr
)->next
;
319 item
->date
= parse_commit_date(bufptr
);
321 if (track_object_refs
) {
323 struct commit_list
*p
;
324 struct object_refs
*refs
= alloc_object_refs(n_refs
);
326 refs
->ref
[i
++] = &item
->tree
->object
;
327 for (p
= item
->parents
; p
; p
= p
->next
)
328 refs
->ref
[i
++] = &p
->item
->object
;
329 set_object_refs(&item
->object
, refs
);
335 int parse_commit(struct commit
*item
)
337 enum object_type type
;
342 if (item
->object
.parsed
)
344 buffer
= read_sha1_file(item
->object
.sha1
, &type
, &size
);
346 return error("Could not read %s",
347 sha1_to_hex(item
->object
.sha1
));
348 if (type
!= OBJ_COMMIT
) {
350 return error("Object %s not a commit",
351 sha1_to_hex(item
->object
.sha1
));
353 ret
= parse_commit_buffer(item
, buffer
, size
);
354 if (save_commit_buffer
&& !ret
) {
355 item
->buffer
= buffer
;
362 struct commit_list
*commit_list_insert(struct commit
*item
, struct commit_list
**list_p
)
364 struct commit_list
*new_list
= xmalloc(sizeof(struct commit_list
));
365 new_list
->item
= item
;
366 new_list
->next
= *list_p
;
371 void free_commit_list(struct commit_list
*list
)
374 struct commit_list
*temp
= list
;
380 struct commit_list
* insert_by_date(struct commit
*item
, struct commit_list
**list
)
382 struct commit_list
**pp
= list
;
383 struct commit_list
*p
;
384 while ((p
= *pp
) != NULL
) {
385 if (p
->item
->date
< item
->date
) {
390 return commit_list_insert(item
, pp
);
394 void sort_by_date(struct commit_list
**list
)
396 struct commit_list
*ret
= NULL
;
398 insert_by_date((*list
)->item
, &ret
);
399 *list
= (*list
)->next
;
404 struct commit
*pop_most_recent_commit(struct commit_list
**list
,
407 struct commit
*ret
= (*list
)->item
;
408 struct commit_list
*parents
= ret
->parents
;
409 struct commit_list
*old
= *list
;
411 *list
= (*list
)->next
;
415 struct commit
*commit
= parents
->item
;
416 parse_commit(commit
);
417 if (!(commit
->object
.flags
& mark
)) {
418 commit
->object
.flags
|= mark
;
419 insert_by_date(commit
, list
);
421 parents
= parents
->next
;
426 void clear_commit_marks(struct commit
*commit
, unsigned int mark
)
429 struct commit_list
*parents
;
431 if (!(mark
& commit
->object
.flags
))
434 commit
->object
.flags
&= ~mark
;
436 parents
= commit
->parents
;
440 while ((parents
= parents
->next
))
441 clear_commit_marks(parents
->item
, mark
);
443 commit
= commit
->parents
->item
;
448 * Generic support for pretty-printing the header
450 static int get_one_line(const char *msg
)
465 /* High bit set, or ISO-2022-INT */
466 int non_ascii(int ch
)
469 return ((ch
& 0x80) || (ch
== 0x1b));
472 static int is_rfc2047_special(char ch
)
474 return (non_ascii(ch
) || (ch
== '=') || (ch
== '?') || (ch
== '_'));
477 static void add_rfc2047(struct strbuf
*sb
, const char *line
, int len
,
478 const char *encoding
)
482 for (i
= 0; i
< len
; i
++) {
486 if ((i
+ 1 < len
) && (ch
== '=' && line
[i
+1] == '?'))
489 strbuf_add(sb
, line
, len
);
493 strbuf_grow(sb
, len
* 3 + strlen(encoding
) + 100);
494 strbuf_addf(sb
, "=?%s?q?", encoding
);
495 for (i
= last
= 0; i
< len
; i
++) {
496 unsigned ch
= line
[i
] & 0xFF;
498 * We encode ' ' using '=20' even though rfc2047
499 * allows using '_' for readability. Unfortunately,
500 * many programs do not understand this and just
501 * leave the underscore in place.
503 if (is_rfc2047_special(ch
) || ch
== ' ') {
504 strbuf_add(sb
, line
+ last
, i
- last
);
505 strbuf_addf(sb
, "=%02X", ch
);
509 strbuf_add(sb
, line
+ last
, len
- last
);
510 strbuf_addstr(sb
, "?=");
513 static void add_user_info(const char *what
, enum cmit_fmt fmt
, struct strbuf
*sb
,
514 const char *line
, enum date_mode dmode
,
515 const char *encoding
)
521 const char *filler
= " ";
523 if (fmt
== CMIT_FMT_ONELINE
)
525 date
= strchr(line
, '>');
528 namelen
= ++date
- line
;
529 time
= strtoul(date
, &date
, 10);
530 tz
= strtol(date
, NULL
, 10);
532 if (fmt
== CMIT_FMT_EMAIL
) {
533 char *name_tail
= strchr(line
, '<');
534 int display_name_length
;
537 while (line
< name_tail
&& isspace(name_tail
[-1]))
539 display_name_length
= name_tail
- line
;
541 strbuf_addstr(sb
, "From: ");
542 add_rfc2047(sb
, line
, display_name_length
, encoding
);
543 strbuf_add(sb
, name_tail
, namelen
- display_name_length
);
544 strbuf_addch(sb
, '\n');
546 strbuf_addf(sb
, "%s: %.*s%.*s\n", what
,
547 (fmt
== CMIT_FMT_FULLER
) ? 4 : 0,
548 filler
, namelen
, line
);
551 case CMIT_FMT_MEDIUM
:
552 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, dmode
));
555 strbuf_addf(sb
, "Date: %s\n", show_date(time
, tz
, DATE_RFC2822
));
557 case CMIT_FMT_FULLER
:
558 strbuf_addf(sb
, "%sDate: %s\n", what
, show_date(time
, tz
, dmode
));
566 static int is_empty_line(const char *line
, int *len_p
)
569 while (len
&& isspace(line
[len
-1]))
575 static void add_merge_info(enum cmit_fmt fmt
, struct strbuf
*sb
,
576 const struct commit
*commit
, int abbrev
)
578 struct commit_list
*parent
= commit
->parents
;
580 if ((fmt
== CMIT_FMT_ONELINE
) || (fmt
== CMIT_FMT_EMAIL
) ||
581 !parent
|| !parent
->next
)
584 strbuf_addstr(sb
, "Merge:");
587 struct commit
*p
= parent
->item
;
588 const char *hex
= NULL
;
591 hex
= find_unique_abbrev(p
->object
.sha1
, abbrev
);
593 hex
= sha1_to_hex(p
->object
.sha1
);
594 dots
= (abbrev
&& strlen(hex
) != 40) ? "..." : "";
595 parent
= parent
->next
;
597 strbuf_addf(sb
, " %s%s", hex
, dots
);
599 strbuf_addch(sb
, '\n');
602 static char *get_header(const struct commit
*commit
, const char *key
)
604 int key_len
= strlen(key
);
605 const char *line
= commit
->buffer
;
608 const char *eol
= strchr(line
, '\n'), *next
;
613 eol
= line
+ strlen(line
);
617 if (eol
- line
> key_len
&&
618 !strncmp(line
, key
, key_len
) &&
619 line
[key_len
] == ' ') {
620 return xmemdupz(line
+ key_len
+ 1, eol
- line
- key_len
- 1);
626 static char *replace_encoding_header(char *buf
, const char *encoding
)
632 /* guess if there is an encoding header before a \n\n */
633 while (strncmp(cp
, "encoding ", strlen("encoding "))) {
634 cp
= strchr(cp
, '\n');
635 if (!cp
|| *++cp
== '\n')
639 cp
= strchr(cp
, '\n');
641 return buf
; /* should not happen but be defensive */
642 len
= cp
+ 1 - (buf
+ start
);
644 strbuf_init(&tmp
, 0);
645 strbuf_attach(&tmp
, buf
, strlen(buf
), strlen(buf
) + 1);
646 if (is_encoding_utf8(encoding
)) {
647 /* we have re-coded to UTF-8; drop the header */
648 strbuf_remove(&tmp
, start
, len
);
650 /* just replaces XXXX in 'encoding XXXX\n' */
651 strbuf_splice(&tmp
, start
+ strlen("encoding "),
652 len
- strlen("encoding \n"),
653 encoding
, strlen(encoding
));
655 return strbuf_detach(&tmp
, NULL
);
658 static char *logmsg_reencode(const struct commit
*commit
,
659 const char *output_encoding
)
661 static const char *utf8
= "utf-8";
662 const char *use_encoding
;
666 if (!*output_encoding
)
668 encoding
= get_header(commit
, "encoding");
669 use_encoding
= encoding
? encoding
: utf8
;
670 if (!strcmp(use_encoding
, output_encoding
))
671 if (encoding
) /* we'll strip encoding header later */
672 out
= xstrdup(commit
->buffer
);
674 return NULL
; /* nothing to do */
676 out
= reencode_string(commit
->buffer
,
677 output_encoding
, use_encoding
);
679 out
= replace_encoding_header(out
, output_encoding
);
685 static void fill_person(struct interp
*table
, const char *msg
, int len
)
687 int start
, end
, tz
= 0;
692 for (end
= 0; end
< len
&& msg
[end
] != '<'; end
++)
695 while (end
> 0 && isspace(msg
[end
- 1]))
697 table
[0].value
= xmemdupz(msg
, end
);
703 for (end
= start
+ 1; end
< len
&& msg
[end
] != '>'; end
++)
709 table
[1].value
= xmemdupz(msg
+ start
, end
- start
);
712 for (start
= end
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
716 date
= strtoul(msg
+ start
, &ep
, 10);
717 if (msg
+ start
== ep
)
720 table
[5].value
= xmemdupz(msg
+ start
, ep
- (msg
+ start
));
723 for (start
= ep
- msg
+ 1; start
< len
&& isspace(msg
[start
]); start
++)
725 if (start
+ 1 < len
) {
726 tz
= strtoul(msg
+ start
+ 1, NULL
, 10);
727 if (msg
[start
] == '-')
731 interp_set_entry(table
, 2, show_date(date
, tz
, DATE_NORMAL
));
732 interp_set_entry(table
, 3, show_date(date
, tz
, DATE_RFC2822
));
733 interp_set_entry(table
, 4, show_date(date
, tz
, DATE_RELATIVE
));
734 interp_set_entry(table
, 6, show_date(date
, tz
, DATE_ISO8601
));
737 void format_commit_message(const struct commit
*commit
,
738 const void *format
, struct strbuf
*sb
)
740 struct interp table
[] = {
741 { "%H" }, /* commit hash */
742 { "%h" }, /* abbreviated commit hash */
743 { "%T" }, /* tree hash */
744 { "%t" }, /* abbreviated tree hash */
745 { "%P" }, /* parent hashes */
746 { "%p" }, /* abbreviated parent hashes */
747 { "%an" }, /* author name */
748 { "%ae" }, /* author email */
749 { "%ad" }, /* author date */
750 { "%aD" }, /* author date, RFC2822 style */
751 { "%ar" }, /* author date, relative */
752 { "%at" }, /* author date, UNIX timestamp */
753 { "%ai" }, /* author date, ISO 8601 */
754 { "%cn" }, /* committer name */
755 { "%ce" }, /* committer email */
756 { "%cd" }, /* committer date */
757 { "%cD" }, /* committer date, RFC2822 style */
758 { "%cr" }, /* committer date, relative */
759 { "%ct" }, /* committer date, UNIX timestamp */
760 { "%ci" }, /* committer date, ISO 8601 */
761 { "%e" }, /* encoding */
762 { "%s" }, /* subject */
764 { "%Cred" }, /* red */
765 { "%Cgreen" }, /* green */
766 { "%Cblue" }, /* blue */
767 { "%Creset" }, /* reset color */
768 { "%n" }, /* newline */
769 { "%m" }, /* left/right/bottom */
772 IHASH
= 0, IHASH_ABBREV
,
774 IPARENTS
, IPARENTS_ABBREV
,
775 IAUTHOR_NAME
, IAUTHOR_EMAIL
,
776 IAUTHOR_DATE
, IAUTHOR_DATE_RFC2822
, IAUTHOR_DATE_RELATIVE
,
777 IAUTHOR_TIMESTAMP
, IAUTHOR_ISO8601
,
778 ICOMMITTER_NAME
, ICOMMITTER_EMAIL
,
779 ICOMMITTER_DATE
, ICOMMITTER_DATE_RFC2822
,
780 ICOMMITTER_DATE_RELATIVE
, ICOMMITTER_TIMESTAMP
,
785 IRED
, IGREEN
, IBLUE
, IRESET_COLOR
,
789 struct commit_list
*p
;
793 enum { HEADER
, SUBJECT
, BODY
} state
;
794 const char *msg
= commit
->buffer
;
796 if (ILEFT_RIGHT
+ 1 != ARRAY_SIZE(table
))
797 die("invalid interp table!");
799 /* these are independent of the commit */
800 interp_set_entry(table
, IRED
, "\033[31m");
801 interp_set_entry(table
, IGREEN
, "\033[32m");
802 interp_set_entry(table
, IBLUE
, "\033[34m");
803 interp_set_entry(table
, IRESET_COLOR
, "\033[m");
804 interp_set_entry(table
, INEWLINE
, "\n");
806 /* these depend on the commit */
807 if (!commit
->object
.parsed
)
808 parse_object(commit
->object
.sha1
);
809 interp_set_entry(table
, IHASH
, sha1_to_hex(commit
->object
.sha1
));
810 interp_set_entry(table
, IHASH_ABBREV
,
811 find_unique_abbrev(commit
->object
.sha1
,
813 interp_set_entry(table
, ITREE
, sha1_to_hex(commit
->tree
->object
.sha1
));
814 interp_set_entry(table
, ITREE_ABBREV
,
815 find_unique_abbrev(commit
->tree
->object
.sha1
,
817 interp_set_entry(table
, ILEFT_RIGHT
,
818 (commit
->object
.flags
& BOUNDARY
)
820 : (commit
->object
.flags
& SYMMETRIC_LEFT
)
825 for (i
= 0, p
= commit
->parents
;
826 p
&& i
< sizeof(parents
) - 1;
828 i
+= snprintf(parents
+ i
, sizeof(parents
) - i
- 1, " %s",
829 sha1_to_hex(p
->item
->object
.sha1
));
830 interp_set_entry(table
, IPARENTS
, parents
+ 1);
833 for (i
= 0, p
= commit
->parents
;
834 p
&& i
< sizeof(parents
) - 1;
836 i
+= snprintf(parents
+ i
, sizeof(parents
) - i
- 1, " %s",
837 find_unique_abbrev(p
->item
->object
.sha1
,
839 interp_set_entry(table
, IPARENTS_ABBREV
, parents
+ 1);
841 for (i
= 0, state
= HEADER
; msg
[i
] && state
< BODY
; i
++) {
843 for (eol
= i
; msg
[eol
] && msg
[eol
] != '\n'; eol
++)
846 if (state
== SUBJECT
) {
847 table
[ISUBJECT
].value
= xmemdupz(msg
+ i
, eol
- i
);
852 /* strip empty lines */
853 while (msg
[eol
+ 1] == '\n')
855 } else if (!prefixcmp(msg
+ i
, "author "))
856 fill_person(table
+ IAUTHOR_NAME
,
857 msg
+ i
+ 7, eol
- i
- 7);
858 else if (!prefixcmp(msg
+ i
, "committer "))
859 fill_person(table
+ ICOMMITTER_NAME
,
860 msg
+ i
+ 10, eol
- i
- 10);
861 else if (!prefixcmp(msg
+ i
, "encoding "))
862 table
[IENCODING
].value
=
863 xmemdupz(msg
+ i
+ 9, eol
- i
- 9);
867 table
[IBODY
].value
= xstrdup(msg
+ i
);
869 len
= interpolate(sb
->buf
+ sb
->len
, strbuf_avail(sb
),
870 format
, table
, ARRAY_SIZE(table
));
871 if (len
> strbuf_avail(sb
)) {
872 strbuf_grow(sb
, len
);
873 interpolate(sb
->buf
+ sb
->len
, strbuf_avail(sb
) + 1,
874 format
, table
, ARRAY_SIZE(table
));
876 strbuf_setlen(sb
, sb
->len
+ len
);
877 interp_clear_table(table
, ARRAY_SIZE(table
));
880 static void pp_header(enum cmit_fmt fmt
,
882 enum date_mode dmode
,
883 const char *encoding
,
884 const struct commit
*commit
,
888 int parents_shown
= 0;
891 const char *line
= *msg_p
;
892 int linelen
= get_one_line(*msg_p
);
902 if (fmt
== CMIT_FMT_RAW
) {
903 strbuf_add(sb
, line
, linelen
);
907 if (!memcmp(line
, "parent ", 7)) {
909 die("bad parent line in commit");
913 if (!parents_shown
) {
914 struct commit_list
*parent
;
916 for (parent
= commit
->parents
, num
= 0;
918 parent
= parent
->next
, num
++)
920 /* with enough slop */
921 strbuf_grow(sb
, num
* 50 + 20);
922 add_merge_info(fmt
, sb
, commit
, abbrev
);
927 * MEDIUM == DEFAULT shows only author with dates.
928 * FULL shows both authors but not dates.
929 * FULLER shows both authors and dates.
931 if (!memcmp(line
, "author ", 7)) {
932 strbuf_grow(sb
, linelen
+ 80);
933 add_user_info("Author", fmt
, sb
, line
+ 7, dmode
, encoding
);
935 if (!memcmp(line
, "committer ", 10) &&
936 (fmt
== CMIT_FMT_FULL
|| fmt
== CMIT_FMT_FULLER
)) {
937 strbuf_grow(sb
, linelen
+ 80);
938 add_user_info("Commit", fmt
, sb
, line
+ 10, dmode
, encoding
);
943 static void pp_title_line(enum cmit_fmt fmt
,
947 const char *after_subject
,
948 const char *encoding
,
953 strbuf_init(&title
, 80);
956 const char *line
= *msg_p
;
957 int linelen
= get_one_line(line
);
960 if (!linelen
|| is_empty_line(line
, &linelen
))
963 strbuf_grow(&title
, linelen
+ 2);
965 if (fmt
== CMIT_FMT_EMAIL
) {
966 strbuf_addch(&title
, '\n');
968 strbuf_addch(&title
, ' ');
970 strbuf_add(&title
, line
, linelen
);
973 strbuf_grow(sb
, title
.len
+ 1024);
975 strbuf_addstr(sb
, subject
);
976 add_rfc2047(sb
, title
.buf
, title
.len
, encoding
);
978 strbuf_addbuf(sb
, &title
);
980 strbuf_addch(sb
, '\n');
982 if (plain_non_ascii
) {
983 const char *header_fmt
=
984 "MIME-Version: 1.0\n"
985 "Content-Type: text/plain; charset=%s\n"
986 "Content-Transfer-Encoding: 8bit\n";
987 strbuf_addf(sb
, header_fmt
, encoding
);
990 strbuf_addstr(sb
, after_subject
);
992 if (fmt
== CMIT_FMT_EMAIL
) {
993 strbuf_addch(sb
, '\n');
995 strbuf_release(&title
);
998 static void pp_remainder(enum cmit_fmt fmt
,
1005 const char *line
= *msg_p
;
1006 int linelen
= get_one_line(line
);
1012 if (is_empty_line(line
, &linelen
)) {
1015 if (fmt
== CMIT_FMT_SHORT
)
1020 strbuf_grow(sb
, linelen
+ indent
+ 20);
1022 memset(sb
->buf
+ sb
->len
, ' ', indent
);
1023 strbuf_setlen(sb
, sb
->len
+ indent
);
1025 strbuf_add(sb
, line
, linelen
);
1026 strbuf_addch(sb
, '\n');
1030 void pretty_print_commit(enum cmit_fmt fmt
, const struct commit
*commit
,
1031 struct strbuf
*sb
, int abbrev
,
1032 const char *subject
, const char *after_subject
,
1033 enum date_mode dmode
, int plain_non_ascii
)
1035 unsigned long beginning_of_body
;
1037 const char *msg
= commit
->buffer
;
1039 const char *encoding
;
1041 if (fmt
== CMIT_FMT_USERFORMAT
) {
1042 format_commit_message(commit
, user_format
, sb
);
1046 encoding
= (git_log_output_encoding
1047 ? git_log_output_encoding
1048 : git_commit_encoding
);
1051 reencoded
= logmsg_reencode(commit
, encoding
);
1056 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1059 /* After-subject is used to pass in Content-Type: multipart
1060 * MIME header; in that case we do not have to do the
1061 * plaintext content type even if the commit message has
1062 * non 7-bit ASCII character. Otherwise, check if we need
1063 * to say this is not a 7-bit ASCII.
1065 if (fmt
== CMIT_FMT_EMAIL
&& !after_subject
) {
1068 for (in_body
= i
= 0; (ch
= msg
[i
]); i
++) {
1070 /* author could be non 7-bit ASCII but
1071 * the log may be so; skip over the
1072 * header part first.
1074 if (ch
== '\n' && msg
[i
+1] == '\n')
1077 else if (non_ascii(ch
)) {
1078 plain_non_ascii
= 1;
1084 pp_header(fmt
, abbrev
, dmode
, encoding
, commit
, &msg
, sb
);
1085 if (fmt
!= CMIT_FMT_ONELINE
&& !subject
) {
1086 strbuf_addch(sb
, '\n');
1089 /* Skip excess blank lines at the beginning of body, if any... */
1091 int linelen
= get_one_line(msg
);
1095 if (!is_empty_line(msg
, &ll
))
1100 /* These formats treat the title line specially. */
1101 if (fmt
== CMIT_FMT_ONELINE
|| fmt
== CMIT_FMT_EMAIL
)
1102 pp_title_line(fmt
, &msg
, sb
, subject
,
1103 after_subject
, encoding
, plain_non_ascii
);
1105 beginning_of_body
= sb
->len
;
1106 if (fmt
!= CMIT_FMT_ONELINE
)
1107 pp_remainder(fmt
, &msg
, sb
, indent
);
1110 /* Make sure there is an EOLN for the non-oneline case */
1111 if (fmt
!= CMIT_FMT_ONELINE
)
1112 strbuf_addch(sb
, '\n');
1115 * The caller may append additional body text in e-mail
1116 * format. Make sure we did not strip the blank line
1117 * between the header and the body.
1119 if (fmt
== CMIT_FMT_EMAIL
&& sb
->len
<= beginning_of_body
)
1120 strbuf_addch(sb
, '\n');
1124 struct commit
*pop_commit(struct commit_list
**stack
)
1126 struct commit_list
*top
= *stack
;
1127 struct commit
*item
= top
? top
->item
: NULL
;
1137 * Performs an in-place topological sort on the list supplied.
1139 void sort_in_topological_order(struct commit_list
** list
, int lifo
)
1141 struct commit_list
*next
, *orig
= *list
;
1142 struct commit_list
*work
, **insert
;
1143 struct commit_list
**pptr
;
1149 /* Mark them and clear the indegree */
1150 for (next
= orig
; next
; next
= next
->next
) {
1151 struct commit
*commit
= next
->item
;
1152 commit
->object
.flags
|= TOPOSORT
;
1153 commit
->indegree
= 0;
1156 /* update the indegree */
1157 for (next
= orig
; next
; next
= next
->next
) {
1158 struct commit_list
* parents
= next
->item
->parents
;
1160 struct commit
*parent
= parents
->item
;
1162 if (parent
->object
.flags
& TOPOSORT
)
1164 parents
= parents
->next
;
1171 * tips are nodes not reachable from any other node in the list
1173 * the tips serve as a starting set for the work queue.
1177 for (next
= orig
; next
; next
= next
->next
) {
1178 struct commit
*commit
= next
->item
;
1180 if (!commit
->indegree
)
1181 insert
= &commit_list_insert(commit
, insert
)->next
;
1184 /* process the list in topological order */
1186 sort_by_date(&work
);
1191 struct commit
*commit
;
1192 struct commit_list
*parents
, *work_item
;
1195 work
= work_item
->next
;
1196 work_item
->next
= NULL
;
1198 commit
= work_item
->item
;
1199 for (parents
= commit
->parents
; parents
; parents
= parents
->next
) {
1200 struct commit
*parent
=parents
->item
;
1202 if (!(parent
->object
.flags
& TOPOSORT
))
1206 * parents are only enqueued for emission
1207 * when all their children have been emitted thereby
1208 * guaranteeing topological order.
1210 if (!--parent
->indegree
) {
1212 insert_by_date(parent
, &work
);
1214 commit_list_insert(parent
, &work
);
1218 * work_item is a commit all of whose children
1219 * have already been emitted. we can emit it now.
1221 commit
->object
.flags
&= ~TOPOSORT
;
1223 pptr
= &work_item
->next
;
1227 /* merge-base stuff */
1229 /* bits #0..15 in revision.h */
1230 #define PARENT1 (1u<<16)
1231 #define PARENT2 (1u<<17)
1232 #define STALE (1u<<18)
1233 #define RESULT (1u<<19)
1235 static const unsigned all_flags
= (PARENT1
| PARENT2
| STALE
| RESULT
);
1237 static struct commit
*interesting(struct commit_list
*list
)
1240 struct commit
*commit
= list
->item
;
1242 if (commit
->object
.flags
& STALE
)
1249 static struct commit_list
*merge_bases(struct commit
*one
, struct commit
*two
)
1251 struct commit_list
*list
= NULL
;
1252 struct commit_list
*result
= NULL
;
1255 /* We do not mark this even with RESULT so we do not
1256 * have to clean it up.
1258 return commit_list_insert(one
, &result
);
1263 one
->object
.flags
|= PARENT1
;
1264 two
->object
.flags
|= PARENT2
;
1265 insert_by_date(one
, &list
);
1266 insert_by_date(two
, &list
);
1268 while (interesting(list
)) {
1269 struct commit
*commit
;
1270 struct commit_list
*parents
;
1271 struct commit_list
*n
;
1274 commit
= list
->item
;
1279 flags
= commit
->object
.flags
& (PARENT1
| PARENT2
| STALE
);
1280 if (flags
== (PARENT1
| PARENT2
)) {
1281 if (!(commit
->object
.flags
& RESULT
)) {
1282 commit
->object
.flags
|= RESULT
;
1283 insert_by_date(commit
, &result
);
1285 /* Mark parents of a found merge stale */
1288 parents
= commit
->parents
;
1290 struct commit
*p
= parents
->item
;
1291 parents
= parents
->next
;
1292 if ((p
->object
.flags
& flags
) == flags
)
1295 p
->object
.flags
|= flags
;
1296 insert_by_date(p
, &list
);
1300 /* Clean up the result to remove stale ones */
1301 free_commit_list(list
);
1302 list
= result
; result
= NULL
;
1304 struct commit_list
*n
= list
->next
;
1305 if (!(list
->item
->object
.flags
& STALE
))
1306 insert_by_date(list
->item
, &result
);
1313 struct commit_list
*get_merge_bases(struct commit
*one
,
1314 struct commit
*two
, int cleanup
)
1316 struct commit_list
*list
;
1317 struct commit
**rslt
;
1318 struct commit_list
*result
;
1321 result
= merge_bases(one
, two
);
1324 if (!result
|| !result
->next
) {
1326 clear_commit_marks(one
, all_flags
);
1327 clear_commit_marks(two
, all_flags
);
1332 /* There are more than one */
1339 rslt
= xcalloc(cnt
, sizeof(*rslt
));
1340 for (list
= result
, i
= 0; list
; list
= list
->next
)
1341 rslt
[i
++] = list
->item
;
1342 free_commit_list(result
);
1344 clear_commit_marks(one
, all_flags
);
1345 clear_commit_marks(two
, all_flags
);
1346 for (i
= 0; i
< cnt
- 1; i
++) {
1347 for (j
= i
+1; j
< cnt
; j
++) {
1348 if (!rslt
[i
] || !rslt
[j
])
1350 result
= merge_bases(rslt
[i
], rslt
[j
]);
1351 clear_commit_marks(rslt
[i
], all_flags
);
1352 clear_commit_marks(rslt
[j
], all_flags
);
1353 for (list
= result
; list
; list
= list
->next
) {
1354 if (rslt
[i
] == list
->item
)
1356 if (rslt
[j
] == list
->item
)
1362 /* Surviving ones in rslt[] are the independent results */
1364 for (i
= 0; i
< cnt
; i
++) {
1366 insert_by_date(rslt
[i
], &result
);
1372 int in_merge_bases(struct commit
*commit
, struct commit
**reference
, int num
)
1374 struct commit_list
*bases
, *b
;
1378 bases
= get_merge_bases(commit
, *reference
, 1);
1381 for (b
= bases
; b
; b
= b
->next
) {
1382 if (!hashcmp(commit
->object
.sha1
, b
->item
->object
.sha1
)) {
1388 free_commit_list(bases
);