| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "commit.h" |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 3 | #include "utf8.h" |
| 4 | #include "diff.h" |
| 5 | #include "revision.h" |
| Johannes Schindelin | c455c87 | 2008-07-21 18:03:49 | [diff] [blame] | 6 | #include "string-list.h" |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 7 | #include "mailmap.h" |
| René Scharfe | 3b3d443 | 2008-09-04 21:40:03 | [diff] [blame] | 8 | #include "log-tree.h" |
| Jeff King | c002922 | 2009-01-17 15:38:46 | [diff] [blame] | 9 | #include "color.h" |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 10 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 11 | static char *user_format; |
| 12 | |
| Nanako Shiraishi | 3640754 | 2009-02-24 09:59:15 | [diff] [blame] | 13 | static void save_user_format(struct rev_info *rev, const char *cp, int is_tformat) |
| 14 | { |
| 15 | free(user_format); |
| 16 | user_format = xstrdup(cp); |
| 17 | if (is_tformat) |
| 18 | rev->use_terminator = 1; |
| 19 | rev->commit_format = CMIT_FMT_USERFORMAT; |
| 20 | } |
| 21 | |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 22 | void get_commit_format(const char *arg, struct rev_info *rev) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 23 | { |
| 24 | int i; |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 25 | static struct cmt_fmt_map { |
| 26 | const char *n; |
| 27 | size_t cmp_len; |
| 28 | enum cmit_fmt v; |
| 29 | } cmt_fmts[] = { |
| 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 }, |
| 37 | }; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 38 | |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 39 | rev->use_terminator = 0; |
| 40 | if (!arg || !*arg) { |
| 41 | rev->commit_format = CMIT_FMT_DEFAULT; |
| 42 | return; |
| 43 | } |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 44 | if (!prefixcmp(arg, "format:") || !prefixcmp(arg, "tformat:")) { |
| Nanako Shiraishi | 3640754 | 2009-02-24 09:59:15 | [diff] [blame] | 45 | save_user_format(rev, strchr(arg, ':') + 1, arg[0] == 't'); |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 46 | return; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 47 | } |
| 48 | for (i = 0; i < ARRAY_SIZE(cmt_fmts); i++) { |
| 49 | if (!strncmp(arg, cmt_fmts[i].n, cmt_fmts[i].cmp_len) && |
| Junio C Hamano | 4da45be | 2008-04-08 00:11:34 | [diff] [blame] | 50 | !strncmp(arg, cmt_fmts[i].n, strlen(arg))) { |
| 51 | if (cmt_fmts[i].v == CMIT_FMT_ONELINE) |
| 52 | rev->use_terminator = 1; |
| 53 | rev->commit_format = cmt_fmts[i].v; |
| 54 | return; |
| 55 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 56 | } |
| Nanako Shiraishi | 3640754 | 2009-02-24 09:59:15 | [diff] [blame] | 57 | if (strchr(arg, '%')) { |
| 58 | save_user_format(rev, arg, 1); |
| 59 | return; |
| 60 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 61 | |
| 62 | die("invalid --pretty format: %s", arg); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Generic support for pretty-printing the header |
| 67 | */ |
| 68 | static int get_one_line(const char *msg) |
| 69 | { |
| 70 | int ret = 0; |
| 71 | |
| 72 | for (;;) { |
| 73 | char c = *msg++; |
| 74 | if (!c) |
| 75 | break; |
| 76 | ret++; |
| 77 | if (c == '\n') |
| 78 | break; |
| 79 | } |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | /* High bit set, or ISO-2022-INT */ |
| 84 | int non_ascii(int ch) |
| 85 | { |
| René Scharfe | c2e9364 | 2009-03-07 13:06:49 | [diff] [blame] | 86 | return !isascii(ch) || ch == '\033'; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static int is_rfc2047_special(char ch) |
| 90 | { |
| 91 | return (non_ascii(ch) || (ch == '=') || (ch == '?') || (ch == '_')); |
| 92 | } |
| 93 | |
| 94 | static void add_rfc2047(struct strbuf *sb, const char *line, int len, |
| 95 | const char *encoding) |
| 96 | { |
| 97 | int i, last; |
| 98 | |
| 99 | for (i = 0; i < len; i++) { |
| 100 | int ch = line[i]; |
| 101 | if (non_ascii(ch)) |
| 102 | goto needquote; |
| 103 | if ((i + 1 < len) && (ch == '=' && line[i+1] == '?')) |
| 104 | goto needquote; |
| 105 | } |
| 106 | strbuf_add(sb, line, len); |
| 107 | return; |
| 108 | |
| 109 | needquote: |
| 110 | strbuf_grow(sb, len * 3 + strlen(encoding) + 100); |
| 111 | strbuf_addf(sb, "=?%s?q?", encoding); |
| 112 | for (i = last = 0; i < len; i++) { |
| 113 | unsigned ch = line[i] & 0xFF; |
| 114 | /* |
| 115 | * We encode ' ' using '=20' even though rfc2047 |
| 116 | * allows using '_' for readability. Unfortunately, |
| 117 | * many programs do not understand this and just |
| 118 | * leave the underscore in place. |
| 119 | */ |
| 120 | if (is_rfc2047_special(ch) || ch == ' ') { |
| 121 | strbuf_add(sb, line + last, i - last); |
| 122 | strbuf_addf(sb, "=%02X", ch); |
| 123 | last = i + 1; |
| 124 | } |
| 125 | } |
| 126 | strbuf_add(sb, line + last, len - last); |
| 127 | strbuf_addstr(sb, "?="); |
| 128 | } |
| 129 | |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 130 | void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, |
| 131 | const char *line, enum date_mode dmode, |
| 132 | const char *encoding) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 133 | { |
| 134 | char *date; |
| 135 | int namelen; |
| 136 | unsigned long time; |
| 137 | int tz; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 138 | |
| 139 | if (fmt == CMIT_FMT_ONELINE) |
| 140 | return; |
| 141 | date = strchr(line, '>'); |
| 142 | if (!date) |
| 143 | return; |
| 144 | namelen = ++date - line; |
| 145 | time = strtoul(date, &date, 10); |
| 146 | tz = strtol(date, NULL, 10); |
| 147 | |
| 148 | if (fmt == CMIT_FMT_EMAIL) { |
| 149 | char *name_tail = strchr(line, '<'); |
| 150 | int display_name_length; |
| 151 | if (!name_tail) |
| 152 | return; |
| 153 | while (line < name_tail && isspace(name_tail[-1])) |
| 154 | name_tail--; |
| 155 | display_name_length = name_tail - line; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 156 | strbuf_addstr(sb, "From: "); |
| 157 | add_rfc2047(sb, line, display_name_length, encoding); |
| 158 | strbuf_add(sb, name_tail, namelen - display_name_length); |
| 159 | strbuf_addch(sb, '\n'); |
| 160 | } else { |
| 161 | strbuf_addf(sb, "%s: %.*s%.*s\n", what, |
| 162 | (fmt == CMIT_FMT_FULLER) ? 4 : 0, |
| Benjamin Kramer | 8e76bf3 | 2009-03-13 12:51:33 | [diff] [blame] | 163 | " ", namelen, line); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 164 | } |
| 165 | switch (fmt) { |
| 166 | case CMIT_FMT_MEDIUM: |
| 167 | strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode)); |
| 168 | break; |
| 169 | case CMIT_FMT_EMAIL: |
| 170 | strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822)); |
| 171 | break; |
| 172 | case CMIT_FMT_FULLER: |
| 173 | strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode)); |
| 174 | break; |
| 175 | default: |
| 176 | /* notin' */ |
| 177 | break; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static int is_empty_line(const char *line, int *len_p) |
| 182 | { |
| 183 | int len = *len_p; |
| 184 | while (len && isspace(line[len-1])) |
| 185 | len--; |
| 186 | *len_p = len; |
| 187 | return !len; |
| 188 | } |
| 189 | |
| René Scharfe | a010966 | 2008-12-27 00:32:49 | [diff] [blame] | 190 | static const char *skip_empty_lines(const char *msg) |
| 191 | { |
| 192 | for (;;) { |
| 193 | int linelen = get_one_line(msg); |
| 194 | int ll = linelen; |
| 195 | if (!linelen) |
| 196 | break; |
| 197 | if (!is_empty_line(msg, &ll)) |
| 198 | break; |
| 199 | msg += linelen; |
| 200 | } |
| 201 | return msg; |
| 202 | } |
| 203 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 204 | static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb, |
| 205 | const struct commit *commit, int abbrev) |
| 206 | { |
| 207 | struct commit_list *parent = commit->parents; |
| 208 | |
| 209 | if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || |
| 210 | !parent || !parent->next) |
| 211 | return; |
| 212 | |
| 213 | strbuf_addstr(sb, "Merge:"); |
| 214 | |
| 215 | while (parent) { |
| 216 | struct commit *p = parent->item; |
| 217 | const char *hex = NULL; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 218 | if (abbrev) |
| 219 | hex = find_unique_abbrev(p->object.sha1, abbrev); |
| 220 | if (!hex) |
| 221 | hex = sha1_to_hex(p->object.sha1); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 222 | parent = parent->next; |
| 223 | |
| Thomas Rast | 7fcda92 | 2009-02-13 22:10:41 | [diff] [blame] | 224 | strbuf_addf(sb, " %s", hex); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 225 | } |
| 226 | strbuf_addch(sb, '\n'); |
| 227 | } |
| 228 | |
| 229 | static char *get_header(const struct commit *commit, const char *key) |
| 230 | { |
| 231 | int key_len = strlen(key); |
| 232 | const char *line = commit->buffer; |
| 233 | |
| 234 | for (;;) { |
| 235 | const char *eol = strchr(line, '\n'), *next; |
| 236 | |
| 237 | if (line == eol) |
| 238 | return NULL; |
| 239 | if (!eol) { |
| 240 | eol = line + strlen(line); |
| 241 | next = NULL; |
| 242 | } else |
| 243 | next = eol + 1; |
| 244 | if (eol - line > key_len && |
| 245 | !strncmp(line, key, key_len) && |
| 246 | line[key_len] == ' ') { |
| 247 | return xmemdupz(line + key_len + 1, eol - line - key_len - 1); |
| 248 | } |
| 249 | line = next; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | static char *replace_encoding_header(char *buf, const char *encoding) |
| 254 | { |
| Brandon Casey | f285a2d | 2008-10-09 19:12:12 | [diff] [blame] | 255 | struct strbuf tmp = STRBUF_INIT; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 256 | size_t start, len; |
| 257 | char *cp = buf; |
| 258 | |
| 259 | /* guess if there is an encoding header before a \n\n */ |
| 260 | while (strncmp(cp, "encoding ", strlen("encoding "))) { |
| 261 | cp = strchr(cp, '\n'); |
| 262 | if (!cp || *++cp == '\n') |
| 263 | return buf; |
| 264 | } |
| 265 | start = cp - buf; |
| 266 | cp = strchr(cp, '\n'); |
| 267 | if (!cp) |
| 268 | return buf; /* should not happen but be defensive */ |
| 269 | len = cp + 1 - (buf + start); |
| 270 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 271 | strbuf_attach(&tmp, buf, strlen(buf), strlen(buf) + 1); |
| 272 | if (is_encoding_utf8(encoding)) { |
| 273 | /* we have re-coded to UTF-8; drop the header */ |
| 274 | strbuf_remove(&tmp, start, len); |
| 275 | } else { |
| 276 | /* just replaces XXXX in 'encoding XXXX\n' */ |
| 277 | strbuf_splice(&tmp, start + strlen("encoding "), |
| 278 | len - strlen("encoding \n"), |
| 279 | encoding, strlen(encoding)); |
| 280 | } |
| 281 | return strbuf_detach(&tmp, NULL); |
| 282 | } |
| 283 | |
| 284 | static char *logmsg_reencode(const struct commit *commit, |
| 285 | const char *output_encoding) |
| 286 | { |
| Brandon Casey | 330db18 | 2009-05-18 23:44:39 | [diff] [blame] | 287 | static const char *utf8 = "UTF-8"; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 288 | const char *use_encoding; |
| 289 | char *encoding; |
| 290 | char *out; |
| 291 | |
| 292 | if (!*output_encoding) |
| 293 | return NULL; |
| 294 | encoding = get_header(commit, "encoding"); |
| 295 | use_encoding = encoding ? encoding : utf8; |
| 296 | if (!strcmp(use_encoding, output_encoding)) |
| 297 | if (encoding) /* we'll strip encoding header later */ |
| 298 | out = xstrdup(commit->buffer); |
| 299 | else |
| 300 | return NULL; /* nothing to do */ |
| 301 | else |
| 302 | out = reencode_string(commit->buffer, |
| 303 | output_encoding, use_encoding); |
| 304 | if (out) |
| 305 | out = replace_encoding_header(out, output_encoding); |
| 306 | |
| 307 | free(encoding); |
| 308 | return out; |
| 309 | } |
| 310 | |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 311 | static int mailmap_name(char *email, int email_len, char *name, int name_len) |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 312 | { |
| Johannes Schindelin | c455c87 | 2008-07-21 18:03:49 | [diff] [blame] | 313 | static struct string_list *mail_map; |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 314 | if (!mail_map) { |
| 315 | mail_map = xcalloc(1, sizeof(*mail_map)); |
| Marius Storm-Olsen | d551a48 | 2009-02-08 14:34:27 | [diff] [blame] | 316 | read_mailmap(mail_map, NULL); |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 317 | } |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 318 | return mail_map->nr && map_user(mail_map, email, email_len, name, name_len); |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 319 | } |
| 320 | |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 321 | static size_t format_person_part(struct strbuf *sb, char part, |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 322 | const char *msg, int len, enum date_mode dmode) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 323 | { |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 324 | /* currently all placeholders have same length */ |
| 325 | const int placeholder_len = 2; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 326 | int start, end, tz = 0; |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 327 | unsigned long date = 0; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 328 | char *ep; |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 329 | const char *name_start, *name_end, *mail_start, *mail_end, *msg_end = msg+len; |
| 330 | char person_name[1024]; |
| 331 | char person_mail[1024]; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 332 | |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 333 | /* advance 'end' to point to email start delimiter */ |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 334 | for (end = 0; end < len && msg[end] != '<'; end++) |
| 335 | ; /* do nothing */ |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 336 | |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 337 | /* |
| 338 | * When end points at the '<' that we found, it should have |
| 339 | * matching '>' later, which means 'end' must be strictly |
| 340 | * below len - 1. |
| 341 | */ |
| 342 | if (end >= len - 2) |
| 343 | goto skip; |
| 344 | |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 345 | /* Seek for both name and email part */ |
| 346 | name_start = msg; |
| 347 | name_end = msg+end; |
| 348 | while (name_end > name_start && isspace(*(name_end-1))) |
| 349 | name_end--; |
| 350 | mail_start = msg+end+1; |
| 351 | mail_end = mail_start; |
| 352 | while (mail_end < msg_end && *mail_end != '>') |
| 353 | mail_end++; |
| 354 | if (mail_end == msg_end) |
| 355 | goto skip; |
| 356 | end = mail_end-msg; |
| 357 | |
| 358 | if (part == 'N' || part == 'E') { /* mailmap lookup */ |
| 359 | strlcpy(person_name, name_start, name_end-name_start+1); |
| 360 | strlcpy(person_mail, mail_start, mail_end-mail_start+1); |
| 361 | mailmap_name(person_mail, sizeof(person_mail), person_name, sizeof(person_name)); |
| 362 | name_start = person_name; |
| 363 | name_end = name_start + strlen(person_name); |
| 364 | mail_start = person_mail; |
| 365 | mail_end = mail_start + strlen(person_mail); |
| 366 | } |
| Johannes Schindelin | e0cbc39 | 2008-07-11 23:28:18 | [diff] [blame] | 367 | if (part == 'n' || part == 'N') { /* name */ |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 368 | strbuf_add(sb, name_start, name_end-name_start); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 369 | return placeholder_len; |
| 370 | } |
| Marius Storm-Olsen | d20d654 | 2009-02-08 14:34:30 | [diff] [blame] | 371 | if (part == 'e' || part == 'E') { /* email */ |
| 372 | strbuf_add(sb, mail_start, mail_end-mail_start); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 373 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 374 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 375 | |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 376 | /* advance 'start' to point to date start delimiter */ |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 377 | for (start = end + 1; start < len && isspace(msg[start]); start++) |
| 378 | ; /* do nothing */ |
| 379 | if (start >= len) |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 380 | goto skip; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 381 | date = strtoul(msg + start, &ep, 10); |
| 382 | if (msg + start == ep) |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 383 | goto skip; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 384 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 385 | if (part == 't') { /* date, UNIX timestamp */ |
| 386 | strbuf_add(sb, msg + start, ep - (msg + start)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 387 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 388 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 389 | |
| 390 | /* parse tz */ |
| 391 | for (start = ep - msg + 1; start < len && isspace(msg[start]); start++) |
| 392 | ; /* do nothing */ |
| 393 | if (start + 1 < len) { |
| 394 | tz = strtoul(msg + start + 1, NULL, 10); |
| 395 | if (msg[start] == '-') |
| 396 | tz = -tz; |
| 397 | } |
| 398 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 399 | switch (part) { |
| 400 | case 'd': /* date */ |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 401 | strbuf_addstr(sb, show_date(date, tz, dmode)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 402 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 403 | case 'D': /* date, RFC2822 style */ |
| 404 | strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 405 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 406 | case 'r': /* date, relative */ |
| 407 | strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 408 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 409 | case 'i': /* date, ISO 8601 */ |
| 410 | strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 411 | return placeholder_len; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 412 | } |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 413 | |
| 414 | skip: |
| 415 | /* |
| 416 | * bogus commit, 'sb' cannot be updated, but we still need to |
| 417 | * compute a valid return value. |
| 418 | */ |
| 419 | if (part == 'n' || part == 'e' || part == 't' || part == 'd' |
| 420 | || part == 'D' || part == 'r' || part == 'i') |
| 421 | return placeholder_len; |
| 422 | |
| 423 | return 0; /* unknown placeholder */ |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 424 | } |
| 425 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 426 | struct chunk { |
| 427 | size_t off; |
| 428 | size_t len; |
| 429 | }; |
| 430 | |
| 431 | struct format_commit_context { |
| 432 | const struct commit *commit; |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 433 | enum date_mode dmode; |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 434 | unsigned commit_header_parsed:1; |
| 435 | unsigned commit_message_parsed:1; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 436 | |
| 437 | /* These offsets are relative to the start of the commit message. */ |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 438 | struct chunk author; |
| 439 | struct chunk committer; |
| 440 | struct chunk encoding; |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 441 | size_t message_off; |
| 442 | size_t subject_off; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 443 | size_t body_off; |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 444 | |
| 445 | /* The following ones are relative to the result struct strbuf. */ |
| 446 | struct chunk abbrev_commit_hash; |
| 447 | struct chunk abbrev_tree_hash; |
| 448 | struct chunk abbrev_parent_hashes; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 449 | }; |
| 450 | |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 451 | static int add_again(struct strbuf *sb, struct chunk *chunk) |
| 452 | { |
| 453 | if (chunk->len) { |
| 454 | strbuf_adddup(sb, chunk->off, chunk->len); |
| 455 | return 1; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * We haven't seen this chunk before. Our caller is surely |
| 460 | * going to add it the hard way now. Remember the most likely |
| 461 | * start of the to-be-added chunk: the current end of the |
| 462 | * struct strbuf. |
| 463 | */ |
| 464 | chunk->off = sb->len; |
| 465 | return 0; |
| 466 | } |
| 467 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 468 | static void parse_commit_header(struct format_commit_context *context) |
| 469 | { |
| 470 | const char *msg = context->commit->buffer; |
| 471 | int i; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 472 | |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 473 | for (i = 0; msg[i]; i++) { |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 474 | int eol; |
| 475 | for (eol = i; msg[eol] && msg[eol] != '\n'; eol++) |
| 476 | ; /* do nothing */ |
| 477 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 478 | if (i == eol) { |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 479 | break; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 480 | } else if (!prefixcmp(msg + i, "author ")) { |
| 481 | context->author.off = i + 7; |
| 482 | context->author.len = eol - i - 7; |
| 483 | } else if (!prefixcmp(msg + i, "committer ")) { |
| 484 | context->committer.off = i + 10; |
| 485 | context->committer.len = eol - i - 10; |
| 486 | } else if (!prefixcmp(msg + i, "encoding ")) { |
| 487 | context->encoding.off = i + 9; |
| 488 | context->encoding.len = eol - i - 9; |
| 489 | } |
| 490 | i = eol; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 491 | } |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 492 | context->message_off = i; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 493 | context->commit_header_parsed = 1; |
| 494 | } |
| 495 | |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 496 | static int istitlechar(char c) |
| 497 | { |
| 498 | return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || |
| 499 | (c >= '0' && c <= '9') || c == '.' || c == '_'; |
| 500 | } |
| 501 | |
| 502 | static void format_sanitized_subject(struct strbuf *sb, const char *msg) |
| 503 | { |
| 504 | size_t trimlen; |
| Stephen Boyd | 871d21d | 2009-03-31 23:24:38 | [diff] [blame] | 505 | size_t start_len = sb->len; |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 506 | int space = 2; |
| 507 | |
| 508 | for (; *msg && *msg != '\n'; msg++) { |
| 509 | if (istitlechar(*msg)) { |
| 510 | if (space == 1) |
| 511 | strbuf_addch(sb, '-'); |
| 512 | space = 0; |
| 513 | strbuf_addch(sb, *msg); |
| 514 | if (*msg == '.') |
| 515 | while (*(msg+1) == '.') |
| 516 | msg++; |
| 517 | } else |
| 518 | space |= 1; |
| 519 | } |
| 520 | |
| 521 | /* trim any trailing '.' or '-' characters */ |
| 522 | trimlen = 0; |
| Stephen Boyd | 871d21d | 2009-03-31 23:24:38 | [diff] [blame] | 523 | while (sb->len - trimlen > start_len && |
| 524 | (sb->buf[sb->len - 1 - trimlen] == '.' |
| 525 | || sb->buf[sb->len - 1 - trimlen] == '-')) |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 526 | trimlen++; |
| 527 | strbuf_remove(sb, sb->len - trimlen, trimlen); |
| 528 | } |
| 529 | |
| René Scharfe | cec0871 | 2009-01-06 20:41:06 | [diff] [blame] | 530 | const char *format_subject(struct strbuf *sb, const char *msg, |
| 531 | const char *line_separator) |
| René Scharfe | 88c4473 | 2008-12-27 00:39:35 | [diff] [blame] | 532 | { |
| 533 | int first = 1; |
| 534 | |
| 535 | for (;;) { |
| 536 | const char *line = msg; |
| 537 | int linelen = get_one_line(line); |
| 538 | |
| 539 | msg += linelen; |
| 540 | if (!linelen || is_empty_line(line, &linelen)) |
| 541 | break; |
| 542 | |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 543 | if (!sb) |
| 544 | continue; |
| René Scharfe | 88c4473 | 2008-12-27 00:39:35 | [diff] [blame] | 545 | strbuf_grow(sb, linelen + 2); |
| 546 | if (!first) |
| 547 | strbuf_addstr(sb, line_separator); |
| 548 | strbuf_add(sb, line, linelen); |
| 549 | first = 0; |
| 550 | } |
| 551 | return msg; |
| 552 | } |
| 553 | |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 554 | static void parse_commit_message(struct format_commit_context *c) |
| 555 | { |
| 556 | const char *msg = c->commit->buffer + c->message_off; |
| 557 | const char *start = c->commit->buffer; |
| 558 | |
| 559 | msg = skip_empty_lines(msg); |
| 560 | c->subject_off = msg - start; |
| 561 | |
| 562 | msg = format_subject(NULL, msg, NULL); |
| 563 | msg = skip_empty_lines(msg); |
| 564 | c->body_off = msg - start; |
| 565 | |
| 566 | c->commit_message_parsed = 1; |
| 567 | } |
| 568 | |
| René Scharfe | 3b3d443 | 2008-09-04 21:40:03 | [diff] [blame] | 569 | static void format_decoration(struct strbuf *sb, const struct commit *commit) |
| 570 | { |
| 571 | struct name_decoration *d; |
| 572 | const char *prefix = " ("; |
| 573 | |
| 574 | load_ref_decorations(); |
| 575 | d = lookup_decoration(&name_decoration, &commit->object); |
| 576 | while (d) { |
| 577 | strbuf_addstr(sb, prefix); |
| 578 | prefix = ", "; |
| 579 | strbuf_addstr(sb, d->name); |
| 580 | d = d->next; |
| 581 | } |
| 582 | if (prefix[0] == ',') |
| 583 | strbuf_addch(sb, ')'); |
| 584 | } |
| 585 | |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 586 | static size_t format_commit_item(struct strbuf *sb, const char *placeholder, |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 587 | void *context) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 588 | { |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 589 | struct format_commit_context *c = context; |
| 590 | const struct commit *commit = c->commit; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 591 | const char *msg = commit->buffer; |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 592 | struct commit_list *p; |
| Govind Salinas | 42c8c74 | 2008-03-21 15:05:06 | [diff] [blame] | 593 | int h1, h2; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 594 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 595 | /* these are independent of the commit */ |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 596 | switch (placeholder[0]) { |
| 597 | case 'C': |
| Jeff King | c002922 | 2009-01-17 15:38:46 | [diff] [blame] | 598 | if (placeholder[1] == '(') { |
| 599 | const char *end = strchr(placeholder + 2, ')'); |
| 600 | char color[COLOR_MAXLEN]; |
| 601 | if (!end) |
| 602 | return 0; |
| 603 | color_parse_mem(placeholder + 2, |
| 604 | end - (placeholder + 2), |
| 605 | "--pretty format", color); |
| 606 | strbuf_addstr(sb, color); |
| 607 | return end - placeholder + 1; |
| 608 | } |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 609 | if (!prefixcmp(placeholder + 1, "red")) { |
| Arjen Laarhoven | dc6ebd4 | 2009-02-13 21:53:40 | [diff] [blame] | 610 | strbuf_addstr(sb, GIT_COLOR_RED); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 611 | return 4; |
| 612 | } else if (!prefixcmp(placeholder + 1, "green")) { |
| Arjen Laarhoven | dc6ebd4 | 2009-02-13 21:53:40 | [diff] [blame] | 613 | strbuf_addstr(sb, GIT_COLOR_GREEN); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 614 | return 6; |
| 615 | } else if (!prefixcmp(placeholder + 1, "blue")) { |
| Arjen Laarhoven | dc6ebd4 | 2009-02-13 21:53:40 | [diff] [blame] | 616 | strbuf_addstr(sb, GIT_COLOR_BLUE); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 617 | return 5; |
| 618 | } else if (!prefixcmp(placeholder + 1, "reset")) { |
| Arjen Laarhoven | dc6ebd4 | 2009-02-13 21:53:40 | [diff] [blame] | 619 | strbuf_addstr(sb, GIT_COLOR_RESET); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 620 | return 6; |
| 621 | } else |
| 622 | return 0; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 623 | case 'n': /* newline */ |
| 624 | strbuf_addch(sb, '\n'); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 625 | return 1; |
| Govind Salinas | 42c8c74 | 2008-03-21 15:05:06 | [diff] [blame] | 626 | case 'x': |
| 627 | /* %x00 == NUL, %x0a == LF, etc. */ |
| 628 | if (0 <= (h1 = hexval_table[0xff & placeholder[1]]) && |
| 629 | h1 <= 16 && |
| 630 | 0 <= (h2 = hexval_table[0xff & placeholder[2]]) && |
| 631 | h2 <= 16) { |
| 632 | strbuf_addch(sb, (h1<<4)|h2); |
| 633 | return 3; |
| 634 | } else |
| 635 | return 0; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 636 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 637 | |
| 638 | /* these depend on the commit */ |
| 639 | if (!commit->object.parsed) |
| 640 | parse_object(commit->object.sha1); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 641 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 642 | switch (placeholder[0]) { |
| 643 | case 'H': /* commit hash */ |
| 644 | strbuf_addstr(sb, sha1_to_hex(commit->object.sha1)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 645 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 646 | case 'h': /* abbreviated commit hash */ |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 647 | if (add_again(sb, &c->abbrev_commit_hash)) |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 648 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 649 | strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1, |
| 650 | DEFAULT_ABBREV)); |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 651 | c->abbrev_commit_hash.len = sb->len - c->abbrev_commit_hash.off; |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 652 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 653 | case 'T': /* tree hash */ |
| 654 | strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1)); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 655 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 656 | case 't': /* abbreviated tree hash */ |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 657 | if (add_again(sb, &c->abbrev_tree_hash)) |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 658 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 659 | strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1, |
| 660 | DEFAULT_ABBREV)); |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 661 | c->abbrev_tree_hash.len = sb->len - c->abbrev_tree_hash.off; |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 662 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 663 | case 'P': /* parent hashes */ |
| 664 | for (p = commit->parents; p; p = p->next) { |
| 665 | if (p != commit->parents) |
| 666 | strbuf_addch(sb, ' '); |
| 667 | strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1)); |
| 668 | } |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 669 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 670 | case 'p': /* abbreviated parent hashes */ |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 671 | if (add_again(sb, &c->abbrev_parent_hashes)) |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 672 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 673 | for (p = commit->parents; p; p = p->next) { |
| 674 | if (p != commit->parents) |
| 675 | strbuf_addch(sb, ' '); |
| 676 | strbuf_addstr(sb, find_unique_abbrev( |
| 677 | p->item->object.sha1, DEFAULT_ABBREV)); |
| 678 | } |
| René Scharfe | b9c6232 | 2007-11-10 11:18:26 | [diff] [blame] | 679 | c->abbrev_parent_hashes.len = sb->len - |
| 680 | c->abbrev_parent_hashes.off; |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 681 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 682 | case 'm': /* left/right/bottom */ |
| 683 | strbuf_addch(sb, (commit->object.flags & BOUNDARY) |
| 684 | ? '-' |
| 685 | : (commit->object.flags & SYMMETRIC_LEFT) |
| 686 | ? '<' |
| 687 | : '>'); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 688 | return 1; |
| René Scharfe | 3b3d443 | 2008-09-04 21:40:03 | [diff] [blame] | 689 | case 'd': |
| 690 | format_decoration(sb, commit); |
| 691 | return 1; |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 692 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 693 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 694 | /* For the rest we have to parse the commit header. */ |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 695 | if (!c->commit_header_parsed) |
| 696 | parse_commit_header(c); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 697 | |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 698 | switch (placeholder[0]) { |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 699 | case 'a': /* author ... */ |
| 700 | return format_person_part(sb, placeholder[1], |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 701 | msg + c->author.off, c->author.len, |
| 702 | c->dmode); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 703 | case 'c': /* committer ... */ |
| 704 | return format_person_part(sb, placeholder[1], |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 705 | msg + c->committer.off, c->committer.len, |
| 706 | c->dmode); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 707 | case 'e': /* encoding */ |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 708 | strbuf_add(sb, msg + c->encoding.off, c->encoding.len); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 709 | return 1; |
| René Scharfe | f53bd74 | 2008-12-27 00:49:21 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /* Now we need to parse the commit message. */ |
| 713 | if (!c->commit_message_parsed) |
| 714 | parse_commit_message(c); |
| 715 | |
| 716 | switch (placeholder[0]) { |
| 717 | case 's': /* subject */ |
| 718 | format_subject(sb, msg + c->subject_off, " "); |
| 719 | return 1; |
| Stephen Boyd | 46d164b | 2009-03-23 02:14:01 | [diff] [blame] | 720 | case 'f': /* sanitized subject */ |
| 721 | format_sanitized_subject(sb, msg + c->subject_off); |
| 722 | return 1; |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 723 | case 'b': /* body */ |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 724 | strbuf_addstr(sb, msg + c->body_off); |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 725 | return 1; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 726 | } |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 727 | return 0; /* unknown placeholder */ |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 728 | } |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 729 | |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 730 | void format_commit_message(const struct commit *commit, |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 731 | const void *format, struct strbuf *sb, |
| 732 | enum date_mode dmode) |
| René Scharfe | cde75e5 | 2007-11-09 00:49:42 | [diff] [blame] | 733 | { |
| René Scharfe | f29d595 | 2007-11-10 11:14:20 | [diff] [blame] | 734 | struct format_commit_context context; |
| 735 | |
| 736 | memset(&context, 0, sizeof(context)); |
| 737 | context.commit = commit; |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 738 | context.dmode = dmode; |
| Marco Costalba | c3a670d | 2008-02-09 14:40:19 | [diff] [blame] | 739 | strbuf_expand(sb, format, format_commit_item, &context); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | static void pp_header(enum cmit_fmt fmt, |
| 743 | int abbrev, |
| 744 | enum date_mode dmode, |
| 745 | const char *encoding, |
| 746 | const struct commit *commit, |
| 747 | const char **msg_p, |
| 748 | struct strbuf *sb) |
| 749 | { |
| 750 | int parents_shown = 0; |
| 751 | |
| 752 | for (;;) { |
| 753 | const char *line = *msg_p; |
| 754 | int linelen = get_one_line(*msg_p); |
| 755 | |
| 756 | if (!linelen) |
| 757 | return; |
| 758 | *msg_p += linelen; |
| 759 | |
| 760 | if (linelen == 1) |
| 761 | /* End of header */ |
| 762 | return; |
| 763 | |
| 764 | if (fmt == CMIT_FMT_RAW) { |
| 765 | strbuf_add(sb, line, linelen); |
| 766 | continue; |
| 767 | } |
| 768 | |
| 769 | if (!memcmp(line, "parent ", 7)) { |
| 770 | if (linelen != 48) |
| 771 | die("bad parent line in commit"); |
| 772 | continue; |
| 773 | } |
| 774 | |
| 775 | if (!parents_shown) { |
| 776 | struct commit_list *parent; |
| 777 | int num; |
| 778 | for (parent = commit->parents, num = 0; |
| 779 | parent; |
| 780 | parent = parent->next, num++) |
| 781 | ; |
| 782 | /* with enough slop */ |
| 783 | strbuf_grow(sb, num * 50 + 20); |
| 784 | add_merge_info(fmt, sb, commit, abbrev); |
| 785 | parents_shown = 1; |
| 786 | } |
| 787 | |
| 788 | /* |
| 789 | * MEDIUM == DEFAULT shows only author with dates. |
| 790 | * FULL shows both authors but not dates. |
| 791 | * FULLER shows both authors and dates. |
| 792 | */ |
| 793 | if (!memcmp(line, "author ", 7)) { |
| 794 | strbuf_grow(sb, linelen + 80); |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 795 | pp_user_info("Author", fmt, sb, line + 7, dmode, encoding); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 796 | } |
| 797 | if (!memcmp(line, "committer ", 10) && |
| 798 | (fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) { |
| 799 | strbuf_grow(sb, linelen + 80); |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 800 | pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | } |
| 804 | |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 805 | void pp_title_line(enum cmit_fmt fmt, |
| 806 | const char **msg_p, |
| 807 | struct strbuf *sb, |
| 808 | const char *subject, |
| 809 | const char *after_subject, |
| 810 | const char *encoding, |
| Junio C Hamano | 267123b | 2008-03-15 07:09:20 | [diff] [blame] | 811 | int need_8bit_cte) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 812 | { |
| René Scharfe | 88c4473 | 2008-12-27 00:39:35 | [diff] [blame] | 813 | const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " "; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 814 | struct strbuf title; |
| 815 | |
| 816 | strbuf_init(&title, 80); |
| René Scharfe | 88c4473 | 2008-12-27 00:39:35 | [diff] [blame] | 817 | *msg_p = format_subject(&title, *msg_p, line_separator); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 818 | |
| 819 | strbuf_grow(sb, title.len + 1024); |
| 820 | if (subject) { |
| 821 | strbuf_addstr(sb, subject); |
| 822 | add_rfc2047(sb, title.buf, title.len, encoding); |
| 823 | } else { |
| 824 | strbuf_addbuf(sb, &title); |
| 825 | } |
| 826 | strbuf_addch(sb, '\n'); |
| 827 | |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 828 | if (need_8bit_cte > 0) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 829 | const char *header_fmt = |
| 830 | "MIME-Version: 1.0\n" |
| 831 | "Content-Type: text/plain; charset=%s\n" |
| 832 | "Content-Transfer-Encoding: 8bit\n"; |
| 833 | strbuf_addf(sb, header_fmt, encoding); |
| 834 | } |
| 835 | if (after_subject) { |
| 836 | strbuf_addstr(sb, after_subject); |
| 837 | } |
| 838 | if (fmt == CMIT_FMT_EMAIL) { |
| 839 | strbuf_addch(sb, '\n'); |
| 840 | } |
| 841 | strbuf_release(&title); |
| 842 | } |
| 843 | |
| Daniel Barkalow | b02bd65 | 2008-02-19 03:56:08 | [diff] [blame] | 844 | void pp_remainder(enum cmit_fmt fmt, |
| 845 | const char **msg_p, |
| 846 | struct strbuf *sb, |
| 847 | int indent) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 848 | { |
| 849 | int first = 1; |
| 850 | for (;;) { |
| 851 | const char *line = *msg_p; |
| 852 | int linelen = get_one_line(line); |
| 853 | *msg_p += linelen; |
| 854 | |
| 855 | if (!linelen) |
| 856 | break; |
| 857 | |
| 858 | if (is_empty_line(line, &linelen)) { |
| 859 | if (first) |
| 860 | continue; |
| 861 | if (fmt == CMIT_FMT_SHORT) |
| 862 | break; |
| 863 | } |
| 864 | first = 0; |
| 865 | |
| 866 | strbuf_grow(sb, linelen + indent + 20); |
| 867 | if (indent) { |
| 868 | memset(sb->buf + sb->len, ' ', indent); |
| 869 | strbuf_setlen(sb, sb->len + indent); |
| 870 | } |
| 871 | strbuf_add(sb, line, linelen); |
| 872 | strbuf_addch(sb, '\n'); |
| 873 | } |
| 874 | } |
| 875 | |
| Alexander Gavrilov | 69cd8f6 | 2008-10-21 20:55:57 | [diff] [blame] | 876 | char *reencode_commit_message(const struct commit *commit, const char **encoding_p) |
| 877 | { |
| 878 | const char *encoding; |
| 879 | |
| 880 | encoding = (git_log_output_encoding |
| 881 | ? git_log_output_encoding |
| 882 | : git_commit_encoding); |
| 883 | if (!encoding) |
| Brandon Casey | 330db18 | 2009-05-18 23:44:39 | [diff] [blame] | 884 | encoding = "UTF-8"; |
| Alexander Gavrilov | 69cd8f6 | 2008-10-21 20:55:57 | [diff] [blame] | 885 | if (encoding_p) |
| 886 | *encoding_p = encoding; |
| 887 | return logmsg_reencode(commit, encoding); |
| 888 | } |
| 889 | |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 890 | void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 891 | struct strbuf *sb, int abbrev, |
| 892 | const char *subject, const char *after_subject, |
| 893 | enum date_mode dmode, int need_8bit_cte) |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 894 | { |
| 895 | unsigned long beginning_of_body; |
| 896 | int indent = 4; |
| 897 | const char *msg = commit->buffer; |
| 898 | char *reencoded; |
| 899 | const char *encoding; |
| 900 | |
| 901 | if (fmt == CMIT_FMT_USERFORMAT) { |
| Jeff King | d36f867 | 2008-08-29 00:54:59 | [diff] [blame] | 902 | format_commit_message(commit, user_format, sb, dmode); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 903 | return; |
| 904 | } |
| 905 | |
| Alexander Gavrilov | 69cd8f6 | 2008-10-21 20:55:57 | [diff] [blame] | 906 | reencoded = reencode_commit_message(commit, &encoding); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 907 | if (reencoded) { |
| 908 | msg = reencoded; |
| 909 | } |
| 910 | |
| 911 | if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) |
| 912 | indent = 0; |
| 913 | |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 914 | /* |
| 915 | * We need to check and emit Content-type: to mark it |
| 916 | * as 8-bit if we haven't done so. |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 917 | */ |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 918 | if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) { |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 919 | int i, ch, in_body; |
| 920 | |
| 921 | for (in_body = i = 0; (ch = msg[i]); i++) { |
| 922 | if (!in_body) { |
| 923 | /* author could be non 7-bit ASCII but |
| 924 | * the log may be so; skip over the |
| 925 | * header part first. |
| 926 | */ |
| 927 | if (ch == '\n' && msg[i+1] == '\n') |
| 928 | in_body = 1; |
| 929 | } |
| 930 | else if (non_ascii(ch)) { |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 931 | need_8bit_cte = 1; |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 932 | break; |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | pp_header(fmt, abbrev, dmode, encoding, commit, &msg, sb); |
| 938 | if (fmt != CMIT_FMT_ONELINE && !subject) { |
| 939 | strbuf_addch(sb, '\n'); |
| 940 | } |
| 941 | |
| 942 | /* Skip excess blank lines at the beginning of body, if any... */ |
| René Scharfe | a010966 | 2008-12-27 00:32:49 | [diff] [blame] | 943 | msg = skip_empty_lines(msg); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 944 | |
| 945 | /* These formats treat the title line specially. */ |
| 946 | if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) |
| 947 | pp_title_line(fmt, &msg, sb, subject, |
| Junio C Hamano | 6bf4f1b | 2008-03-15 00:10:09 | [diff] [blame] | 948 | after_subject, encoding, need_8bit_cte); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 949 | |
| 950 | beginning_of_body = sb->len; |
| 951 | if (fmt != CMIT_FMT_ONELINE) |
| 952 | pp_remainder(fmt, &msg, sb, indent); |
| 953 | strbuf_rtrim(sb); |
| 954 | |
| 955 | /* Make sure there is an EOLN for the non-oneline case */ |
| 956 | if (fmt != CMIT_FMT_ONELINE) |
| 957 | strbuf_addch(sb, '\n'); |
| 958 | |
| 959 | /* |
| 960 | * The caller may append additional body text in e-mail |
| 961 | * format. Make sure we did not strip the blank line |
| 962 | * between the header and the body. |
| 963 | */ |
| 964 | if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body) |
| 965 | strbuf_addch(sb, '\n'); |
| Johannes Schindelin | 93fc05e | 2007-11-04 19:15:06 | [diff] [blame] | 966 | free(reencoded); |
| 967 | } |