sequencer.c: teach append_signoff to avoid adding a duplicate newline
Teach append_signoff to detect whether a blank line exists at the position that the signed-off-by line will be added, and refrain from adding an additional one if one already exists. Or, add an additional line if one is needed to make sure the new footer is separated from the message body by a blank line. Signed-off-by: Brandon Casey <bcasey@nvidia.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Brandon Casey committed
Feb 12, 2013 at 02:33 UTC
33f2f9ab4e5c343b21a9e602456e7facabd609a0
1 file changed
+13
-2
sequencer.c
+13
-2
@@ -1123,8 +1123,19 @@ void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
1123
else
1124
has_footer = has_conforming_footer(msgbuf, &sob, ignore_footer);
1125
1126
- if (!has_footer)
1127
- strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0, "\n", 1);
1126
+ if (!has_footer) {
1127
+ const char *append_newlines = NULL;
1128
+ size_t len = msgbuf->len - ignore_footer;
1129
+
1130
+ if (len && msgbuf->buf[len - 1] != '\n')
1131
+ append_newlines = "\n\n";
1132
+ else if (len > 1 && msgbuf->buf[len - 2] != '\n')
1133
+ append_newlines = "\n";
1134
+
1135
+ if (append_newlines)
1136
+ strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,
1137
+ append_newlines, strlen(append_newlines));
1138
+ }
1139
1140
if (has_footer != 3 && (!no_dup_sob || has_footer != 2))
1141
strbuf_splice(msgbuf, msgbuf->len - ignore_footer, 0,