Skip to content

Commit 88c4473

Browse files
René Scharfegitster
authored andcommitted
pretty: factor out format_subject()
The next patch will use it. In the version that was factored out, we can't rely on the len of the struct strbuf to find out if a line separator needs to be added, as it might already contain something. Add a guard variable ("first") instead. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a010966 commit 88c4473

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

pretty.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,28 @@ static void parse_commit_header(struct format_commit_context *context)
495495
context->commit_header_parsed = 1;
496496
}
497497

498+
static const char *format_subject(struct strbuf *sb, const char *msg,
499+
const char *line_separator)
500+
{
501+
int first = 1;
502+
503+
for (;;) {
504+
const char *line = msg;
505+
int linelen = get_one_line(line);
506+
507+
msg += linelen;
508+
if (!linelen || is_empty_line(line, &linelen))
509+
break;
510+
511+
strbuf_grow(sb, linelen + 2);
512+
if (!first)
513+
strbuf_addstr(sb, line_separator);
514+
strbuf_add(sb, line, linelen);
515+
first = 0;
516+
}
517+
return msg;
518+
}
519+
498520
static void format_decoration(struct strbuf *sb, const struct commit *commit)
499521
{
500522
struct name_decoration *d;
@@ -718,27 +740,11 @@ void pp_title_line(enum cmit_fmt fmt,
718740
const char *encoding,
719741
int need_8bit_cte)
720742
{
743+
const char *line_separator = (fmt == CMIT_FMT_EMAIL) ? "\n " : " ";
721744
struct strbuf title;
722745

723746
strbuf_init(&title, 80);
724-
725-
for (;;) {
726-
const char *line = *msg_p;
727-
int linelen = get_one_line(line);
728-
729-
*msg_p += linelen;
730-
if (!linelen || is_empty_line(line, &linelen))
731-
break;
732-
733-
strbuf_grow(&title, linelen + 2);
734-
if (title.len) {
735-
if (fmt == CMIT_FMT_EMAIL) {
736-
strbuf_addch(&title, '\n');
737-
}
738-
strbuf_addch(&title, ' ');
739-
}
740-
strbuf_add(&title, line, linelen);
741-
}
747+
*msg_p = format_subject(&title, *msg_p, line_separator);
742748

743749
strbuf_grow(sb, title.len + 1024);
744750
if (subject) {

0 commit comments

Comments
 (0)