1
#include "cache.h"
2
#include "commit.h"
3
-#include "interpolate.h"
3
#include "utf8.h"
4
#include "diff.h"
5
#include "revision.h"
282
return out;
283
}
284
286
-static void fill_person(struct interp *table, const char *msg, int len)
285
+static void format_person_part(struct strbuf *sb, char part,
286
+ const char *msg, int len)
287
{
288
int start, end, tz = 0;
289
unsigned long date;
295
start = end + 1;
296
while (end > 0 && isspace(msg[end - 1]))
297
end--;
298
- table[0].value = xmemdupz(msg, end);
298
+ if (part == 'n') { /* name */
299
+ strbuf_add(sb, msg, end);
300
+ return;
301
+ }
302
303
if (start >= len)
304
return;
310
if (end >= len)
311
return;
312
310
- table[1].value = xmemdupz(msg + start, end - start);
313
+ if (part == 'e') { /* email */
314
+ strbuf_add(sb, msg + start, end - start);
315
+ return;
316
+ }
317
318
/* parse date */
319
for (start = end + 1; start < len && isspace(msg[start]); start++)
324
if (msg + start == ep)
325
return;
326
321
- table[5].value = xmemdupz(msg + start, ep - (msg + start));
327
+ if (part == 't') { /* date, UNIX timestamp */
328
+ strbuf_add(sb, msg + start, ep - (msg + start));
329
+ return;
330
+ }
331
332
/* parse tz */
333
for (start = ep - msg + 1; start < len && isspace(msg[start]); start++)
338
tz = -tz;
339
}
340
332
- interp_set_entry(table, 2, show_date(date, tz, DATE_NORMAL));
333
- interp_set_entry(table, 3, show_date(date, tz, DATE_RFC2822));
334
- interp_set_entry(table, 4, show_date(date, tz, DATE_RELATIVE));
335
- interp_set_entry(table, 6, show_date(date, tz, DATE_ISO8601));
341
+ switch (part) {
342
+ case 'd': /* date */
343
+ strbuf_addstr(sb, show_date(date, tz, DATE_NORMAL));
344
+ return;
345
+ case 'D': /* date, RFC2822 style */
346
+ strbuf_addstr(sb, show_date(date, tz, DATE_RFC2822));
347
+ return;
348
+ case 'r': /* date, relative */
349
+ strbuf_addstr(sb, show_date(date, tz, DATE_RELATIVE));
350
+ return;
351
+ case 'i': /* date, ISO 8601 */
352
+ strbuf_addstr(sb, show_date(date, tz, DATE_ISO8601));
353
+ return;
354
+ }
355
}
356
338
-void format_commit_message(const struct commit *commit,
339
- const void *format, struct strbuf *sb)
357
+static void format_commit_item(struct strbuf *sb, const char *placeholder,
358
+ void *context)
359
{
341
- struct interp table[] = {
342
- { "%H" }, /* commit hash */
343
- { "%h" }, /* abbreviated commit hash */
344
- { "%T" }, /* tree hash */
345
- { "%t" }, /* abbreviated tree hash */
346
- { "%P" }, /* parent hashes */
347
- { "%p" }, /* abbreviated parent hashes */
348
- { "%an" }, /* author name */
349
- { "%ae" }, /* author email */
350
- { "%ad" }, /* author date */
351
- { "%aD" }, /* author date, RFC2822 style */
352
- { "%ar" }, /* author date, relative */
353
- { "%at" }, /* author date, UNIX timestamp */
354
- { "%ai" }, /* author date, ISO 8601 */
355
- { "%cn" }, /* committer name */
356
- { "%ce" }, /* committer email */
357
- { "%cd" }, /* committer date */
358
- { "%cD" }, /* committer date, RFC2822 style */
359
- { "%cr" }, /* committer date, relative */
360
- { "%ct" }, /* committer date, UNIX timestamp */
361
- { "%ci" }, /* committer date, ISO 8601 */
362
- { "%e" }, /* encoding */
363
- { "%s" }, /* subject */
364
- { "%b" }, /* body */
365
- { "%Cred" }, /* red */
366
- { "%Cgreen" }, /* green */
367
- { "%Cblue" }, /* blue */
368
- { "%Creset" }, /* reset color */
369
- { "%n" }, /* newline */
370
- { "%m" }, /* left/right/bottom */
371
- };
372
- enum interp_index {
373
- IHASH = 0, IHASH_ABBREV,
374
- ITREE, ITREE_ABBREV,
375
- IPARENTS, IPARENTS_ABBREV,
376
- IAUTHOR_NAME, IAUTHOR_EMAIL,
377
- IAUTHOR_DATE, IAUTHOR_DATE_RFC2822, IAUTHOR_DATE_RELATIVE,
378
- IAUTHOR_TIMESTAMP, IAUTHOR_ISO8601,
379
- ICOMMITTER_NAME, ICOMMITTER_EMAIL,
380
- ICOMMITTER_DATE, ICOMMITTER_DATE_RFC2822,
381
- ICOMMITTER_DATE_RELATIVE, ICOMMITTER_TIMESTAMP,
382
- ICOMMITTER_ISO8601,
383
- IENCODING,
384
- ISUBJECT,
385
- IBODY,
386
- IRED, IGREEN, IBLUE, IRESET_COLOR,
387
- INEWLINE,
388
- ILEFT_RIGHT,
389
- };
360
+ const struct commit *commit = context;
361
struct commit_list *p;
391
- char parents[1024];
392
- unsigned long len;
362
int i;
363
enum { HEADER, SUBJECT, BODY } state;
364
const char *msg = commit->buffer;
365
397
- if (ILEFT_RIGHT + 1 != ARRAY_SIZE(table))
398
- die("invalid interp table!");
399
-
366
/* these are independent of the commit */
401
- interp_set_entry(table, IRED, "\033[31m");
402
- interp_set_entry(table, IGREEN, "\033[32m");
403
- interp_set_entry(table, IBLUE, "\033[34m");
404
- interp_set_entry(table, IRESET_COLOR, "\033[m");
405
- interp_set_entry(table, INEWLINE, "\n");
367
+ switch (placeholder[0]) {
368
+ case 'C':
369
+ switch (placeholder[3]) {
370
+ case 'd': /* red */
371
+ strbuf_addstr(sb, "\033[31m");
372
+ return;
373
+ case 'e': /* green */
374
+ strbuf_addstr(sb, "\033[32m");
375
+ return;
376
+ case 'u': /* blue */
377
+ strbuf_addstr(sb, "\033[34m");
378
+ return;
379
+ case 's': /* reset color */
380
+ strbuf_addstr(sb, "\033[m");
381
+ return;
382
+ }
383
+ case 'n': /* newline */
384
+ strbuf_addch(sb, '\n');
385
+ return;
386
+ }
387
388
/* these depend on the commit */
389
if (!commit->object.parsed)
390
parse_object(commit->object.sha1);
410
- interp_set_entry(table, IHASH, sha1_to_hex(commit->object.sha1));
411
- interp_set_entry(table, IHASH_ABBREV,
412
- find_unique_abbrev(commit->object.sha1,
413
- DEFAULT_ABBREV));
414
- interp_set_entry(table, ITREE, sha1_to_hex(commit->tree->object.sha1));
415
- interp_set_entry(table, ITREE_ABBREV,
416
- find_unique_abbrev(commit->tree->object.sha1,
417
- DEFAULT_ABBREV));
418
- interp_set_entry(table, ILEFT_RIGHT,
419
- (commit->object.flags & BOUNDARY)
420
- ? "-"
421
- : (commit->object.flags & SYMMETRIC_LEFT)
422
- ? "<"
423
- : ">");
424
-
425
- parents[1] = 0;
426
- for (i = 0, p = commit->parents;
427
- p && i < sizeof(parents) - 1;
428
- p = p->next)
429
- i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
430
- sha1_to_hex(p->item->object.sha1));
431
- interp_set_entry(table, IPARENTS, parents + 1);
432
-
433
- parents[1] = 0;
434
- for (i = 0, p = commit->parents;
435
- p && i < sizeof(parents) - 1;
436
- p = p->next)
437
- i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
438
- find_unique_abbrev(p->item->object.sha1,
439
- DEFAULT_ABBREV));
440
- interp_set_entry(table, IPARENTS_ABBREV, parents + 1);
391
392
+ switch (placeholder[0]) {
393
+ case 'H': /* commit hash */
394
+ strbuf_addstr(sb, sha1_to_hex(commit->object.sha1));
395
+ return;
396
+ case 'h': /* abbreviated commit hash */
397
+ strbuf_addstr(sb, find_unique_abbrev(commit->object.sha1,
398
+ DEFAULT_ABBREV));
399
+ return;
400
+ case 'T': /* tree hash */
401
+ strbuf_addstr(sb, sha1_to_hex(commit->tree->object.sha1));
402
+ return;
403
+ case 't': /* abbreviated tree hash */
404
+ strbuf_addstr(sb, find_unique_abbrev(commit->tree->object.sha1,
405
+ DEFAULT_ABBREV));
406
+ return;
407
+ case 'P': /* parent hashes */
408
+ for (p = commit->parents; p; p = p->next) {
409
+ if (p != commit->parents)
410
+ strbuf_addch(sb, ' ');
411
+ strbuf_addstr(sb, sha1_to_hex(p->item->object.sha1));
412
+ }
413
+ return;
414
+ case 'p': /* abbreviated parent hashes */
415
+ for (p = commit->parents; p; p = p->next) {
416
+ if (p != commit->parents)
417
+ strbuf_addch(sb, ' ');
418
+ strbuf_addstr(sb, find_unique_abbrev(
419
+ p->item->object.sha1, DEFAULT_ABBREV));
420
+ }
421
+ return;
422
+ case 'm': /* left/right/bottom */
423
+ strbuf_addch(sb, (commit->object.flags & BOUNDARY)
424
+ ? '-'
425
+ : (commit->object.flags & SYMMETRIC_LEFT)
426
+ ? '<'
427
+ : '>');
428
+ return;
429
+ }
430
+
431
+ /* For the rest we have to parse the commit header. */
432
for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
433
int eol;
434
for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
435
; /* do nothing */
436
437
if (state == SUBJECT) {
448
- table[ISUBJECT].value = xmemdupz(msg + i, eol - i);
438
+ if (placeholder[0] == 's') {
439
+ strbuf_add(sb, msg + i, eol - i);
440
+ return;
441
+ }
442
i = eol;
443
}
444
if (i == eol) {
446
/* strip empty lines */
447
while (msg[eol + 1] == '\n')
448
eol++;
456
- } else if (!prefixcmp(msg + i, "author "))
457
- fill_person(table + IAUTHOR_NAME,
458
- msg + i + 7, eol - i - 7);
459
- else if (!prefixcmp(msg + i, "committer "))
460
- fill_person(table + ICOMMITTER_NAME,
461
- msg + i + 10, eol - i - 10);
462
- else if (!prefixcmp(msg + i, "encoding "))
463
- table[IENCODING].value =
464
- xmemdupz(msg + i + 9, eol - i - 9);
449
+ } else if (!prefixcmp(msg + i, "author ")) {
450
+ if (placeholder[0] == 'a') {
451
+ format_person_part(sb, placeholder[1],
452
+ msg + i + 7, eol - i - 7);
453
+ return;
454
+ }
455
+ } else if (!prefixcmp(msg + i, "committer ")) {
456
+ if (placeholder[0] == 'c') {
457
+ format_person_part(sb, placeholder[1],
458
+ msg + i + 10, eol - i - 10);
459
+ return;
460
+ }
461
+ } else if (!prefixcmp(msg + i, "encoding ")) {
462
+ if (placeholder[0] == 'e') {
463
+ strbuf_add(sb, msg + i + 9, eol - i - 9);
464
+ return;
465
+ }
466
+ }
467
i = eol;
468
}
467
- if (msg[i])
468
- table[IBODY].value = xstrdup(msg + i);
469
-
470
- len = interpolate(sb->buf + sb->len, strbuf_avail(sb),
471
- format, table, ARRAY_SIZE(table));
472
- if (len > strbuf_avail(sb)) {
473
- strbuf_grow(sb, len);
474
- interpolate(sb->buf + sb->len, strbuf_avail(sb) + 1,
475
- format, table, ARRAY_SIZE(table));
476
- }
477
- strbuf_setlen(sb, sb->len + len);
478
- interp_clear_table(table, ARRAY_SIZE(table));
469
+ if (msg[i] && placeholder[0] == 'b') /* body */
470
+ strbuf_addstr(sb, msg + i);
471
+}
472
+
473
+void format_commit_message(const struct commit *commit,
474
+ const void *format, struct strbuf *sb)
475
+{
476
+ const char *placeholders[] = {
477
+ "H", /* commit hash */
478
+ "h", /* abbreviated commit hash */
479
+ "T", /* tree hash */
480
+ "t", /* abbreviated tree hash */
481
+ "P", /* parent hashes */
482
+ "p", /* abbreviated parent hashes */
483
+ "an", /* author name */
484
+ "ae", /* author email */
485
+ "ad", /* author date */
486
+ "aD", /* author date, RFC2822 style */
487
+ "ar", /* author date, relative */
488
+ "at", /* author date, UNIX timestamp */
489
+ "ai", /* author date, ISO 8601 */
490
+ "cn", /* committer name */
491
+ "ce", /* committer email */
492
+ "cd", /* committer date */
493
+ "cD", /* committer date, RFC2822 style */
494
+ "cr", /* committer date, relative */
495
+ "ct", /* committer date, UNIX timestamp */
496
+ "ci", /* committer date, ISO 8601 */
497
+ "e", /* encoding */
498
+ "s", /* subject */
499
+ "b", /* body */
500
+ "Cred", /* red */
501
+ "Cgreen", /* green */
502
+ "Cblue", /* blue */
503
+ "Creset", /* reset color */
504
+ "n", /* newline */
505
+ "m", /* left/right/bottom */
506
+ NULL
507
+ };
508
+ strbuf_expand(sb, format, placeholders, format_commit_item, (void *)commit);
509
}
510
511
static void pp_header(enum cmit_fmt fmt,