354
}
355
}
356
357
-static void format_commit_item(struct strbuf *sb, const char *placeholder,
358
- void *context)
357
+struct chunk {
358
+ size_t off;
359
+ size_t len;
360
+};
361
+
362
+struct format_commit_context {
363
+ const struct commit *commit;
364
+
365
+ /* These offsets are relative to the start of the commit message. */
366
+ int commit_header_parsed;
367
+ struct chunk subject;
368
+ struct chunk author;
369
+ struct chunk committer;
370
+ struct chunk encoding;
371
+ size_t body_off;
372
+};
373
+
374
+static void parse_commit_header(struct format_commit_context *context)
375
{
360
- const struct commit *commit = context;
361
- struct commit_list *p;
376
+ const char *msg = context->commit->buffer;
377
int i;
378
enum { HEADER, SUBJECT, BODY } state;
379
+
380
+ for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
381
+ int eol;
382
+ for (eol = i; msg[eol] && msg[eol] != '\n'; eol++)
383
+ ; /* do nothing */
384
+
385
+ if (state == SUBJECT) {
386
+ context->subject.off = i;
387
+ context->subject.len = eol - i;
388
+ i = eol;
389
+ }
390
+ if (i == eol) {
391
+ state++;
392
+ /* strip empty lines */
393
+ while (msg[eol + 1] == '\n')
394
+ eol++;
395
+ } else if (!prefixcmp(msg + i, "author ")) {
396
+ context->author.off = i + 7;
397
+ context->author.len = eol - i - 7;
398
+ } else if (!prefixcmp(msg + i, "committer ")) {
399
+ context->committer.off = i + 10;
400
+ context->committer.len = eol - i - 10;
401
+ } else if (!prefixcmp(msg + i, "encoding ")) {
402
+ context->encoding.off = i + 9;
403
+ context->encoding.len = eol - i - 9;
404
+ }
405
+ i = eol;
406
+ }
407
+ context->body_off = i;
408
+ context->commit_header_parsed = 1;
409
+}
410
+
411
+static void format_commit_item(struct strbuf *sb, const char *placeholder,
412
+ void *context)
413
+{
414
+ struct format_commit_context *c = context;
415
+ const struct commit *commit = c->commit;
416
const char *msg = commit->buffer;
417
+ struct commit_list *p;
418
419
/* these are independent of the commit */
420
switch (placeholder[0]) {
482
}
483
484
/* 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 */
485
+ if (!c->commit_header_parsed)
486
+ parse_commit_header(c);
487
437
- if (state == SUBJECT) {
438
- if (placeholder[0] == 's') {
439
- strbuf_add(sb, msg + i, eol - i);
440
- return;
441
- }
442
- i = eol;
443
- }
444
- if (i == eol) {
445
- state++;
446
- /* strip empty lines */
447
- while (msg[eol + 1] == '\n')
448
- eol++;
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;
488
+ switch (placeholder[0]) {
489
+ case 's':
490
+ strbuf_add(sb, msg + c->subject.off, c->subject.len);
491
+ return;
492
+ case 'a':
493
+ format_person_part(sb, placeholder[1],
494
+ msg + c->author.off, c->author.len);
495
+ return;
496
+ case 'c':
497
+ format_person_part(sb, placeholder[1],
498
+ msg + c->committer.off, c->committer.len);
499
+ return;
500
+ case 'e':
501
+ strbuf_add(sb, msg + c->encoding.off, c->encoding.len);
502
+ return;
503
+ case 'b':
504
+ strbuf_addstr(sb, msg + c->body_off);
505
+ return;
506
}
469
- if (msg[i] && placeholder[0] == 'b') /* body */
470
- strbuf_addstr(sb, msg + i);
507
}
508
509
void format_commit_message(const struct commit *commit,
541
"m", /* left/right/bottom */
542
NULL
543
};
508
- strbuf_expand(sb, format, placeholders, format_commit_item, (void *)commit);
544
+ struct format_commit_context context;
545
+
546
+ memset(&context, 0, sizeof(context));
547
+ context.commit = commit;
548
+ strbuf_expand(sb, format, placeholders, format_commit_item, &context);
549
}
550
551
static void pp_header(enum cmit_fmt fmt,