builtin/commit.c: extract ignore_non_trailer() helper function

Extract a helper function from prepare_to_commit() to determine where to place a new Signed-off-by: line, which is essentially the true "end" of the log message, ignoring the trailing "Conflicts:" line and everything below it. The detection _should_ make sure the "Conflicts:" line it finds is truly the conflict hint block by checking everything that follows is a HT indented pathname to avoid false positive, but this logic will be revamped in a later patch to ignore comments and blanks anyway, so it is left as-is in this step. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Oct 28, 2014 at 12:44 UTC 073bd75e177aa9f1fc42b152a38e251c7156dfe0
1 file changed +33 -26
builtin/commit.c
+33 -26
@@ -593,6 +593,37 @@ static char *cut_ident_timestamp_part(char *string)
593 return ket;
594 }
595
596 +/*
597 + * Inspect sb and determine the true "end" of the log message, in
598 + * order to find where to put a new Signed-off-by: line. Ignored are
599 + * trailing "Conflict:" block.
600 + *
601 + * Returns the number of bytes from the tail to ignore, to be fed as
602 + * the second parameter to append_signoff().
603 + */
604 +static int ignore_non_trailer(struct strbuf *sb)
605 +{
606 + int ignore_footer = 0;
607 + int i, eol, previous = 0;
608 + const char *nl;
609 +
610 + for (i = 0; i < sb->len; i++) {
611 + nl = memchr(sb->buf + i, '\n', sb->len - i);
612 + if (nl)
613 + eol = nl - sb->buf;
614 + else
615 + eol = sb->len;
616 + if (!prefixcmp(sb->buf + previous, "\nConflicts:\n")) {
617 + ignore_footer = sb->len - previous;
618 + break;
619 + }
620 + while (i < eol)
621 + i++;
622 + previous = eol;
623 + }
624 + return ignore_footer;
625 +}
626 +
627 static int prepare_to_commit(const char *index_file, const char *prefix,
628 struct commit *current_head,
629 struct wt_status *s,
@@ -718,32 +749,8 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
749 if (clean_message_contents)
750 stripspace(&sb, 0);
751
721 - if (signoff) {
722 - /*
723 - * See if we have a Conflicts: block at the end. If yes, count
724 - * its size, so we can ignore it.
725 - */
726 - int ignore_footer = 0;
727 - int i, eol, previous = 0;
728 - const char *nl;
729 -
730 - for (i = 0; i < sb.len; i++) {
731 - nl = memchr(sb.buf + i, '\n', sb.len - i);
732 - if (nl)
733 - eol = nl - sb.buf;
734 - else
735 - eol = sb.len;
736 - if (!prefixcmp(sb.buf + previous, "\nConflicts:\n")) {
737 - ignore_footer = sb.len - previous;
738 - break;
739 - }
740 - while (i < eol)
741 - i++;
742 - previous = eol;
743 - }
744 -
745 - append_signoff(&sb, ignore_footer, 0);
746 - }
752 + if (signoff)
753 + append_signoff(&sb, ignore_non_trailer(&sb), 0);
754
755 if (fwrite(sb.buf, 1, sb.len, s->fp) < sb.len)
756 die_errno(_("could not write commit template"));