Skip to content

Commit 69aff62

Browse files
peffgitster
authored andcommitted
pretty: split oneline and email subject printing
The pp_title_line() function is used for two formats: the oneline format and the subject line of the email format. But most of the logic in the function does not make any sense for oneline; it is about special formatting of email headers. Lumping the two formats together made sense long ago in 4234a76 (Extend --pretty=oneline to cover the first paragraph, 2007-06-11), when there was a lot of manual logic to paste lines together. But later, 88c4473 (pretty: factor out format_subject(), 2008-12-27) pulled that logic into its own function. We can implement the oneline format by just calling that one function. This makes the intention of the code much more clear, as we know we only need to worry about those extra email options when dealing with actual email. While the intent here is cleanup, it is possible to trigger these cases in practice by running format-patch with an explicit --oneline option. But if you did, the results are basically nonsense. For example, with the preserve_subject flag: $ printf "%s\n" one two three | git commit --allow-empty -F - $ git format-patch -1 --stdout -k | grep ^Subject Subject: =?UTF-8?q?one=0Atwo=0Athree?= $ git format-patch -1 --stdout -k --oneline --no-signature 2af7fbe one two three Or with extra headers: $ git format-patch -1 --stdout --cc=me --oneline --no-signature 2af7fbe one two three Cc: me So I'd actually consider this to be an improvement, though you are probably crazy to use other formats with format-patch in the first place (arguably it should forbid non-email formats entirely, but that's a bigger change). As a bonus, it eliminates some pointless extra allocations for the oneline output. The email code, since it has to deal with wrapping, formats into an extra auxiliary buffer. The speedup is tiny, though like "rev-list --no-abbrev --format=oneline" seems to improve by a consistent 1-2% for me. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c7f6a53 commit 69aff62

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ static void prepare_cover_text(struct pretty_print_context *pp,
12971297
subject = subject_sb.buf;
12981298

12991299
do_pp:
1300-
pp_title_line(pp, &subject, sb, encoding, need_8bit_cte);
1300+
pp_email_subject(pp, &subject, sb, encoding, need_8bit_cte);
13011301
pp_remainder(pp, &body, sb, 0);
13021302

13031303
strbuf_release(&description_sb);

pretty.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,11 +2077,11 @@ static void pp_header(struct pretty_print_context *pp,
20772077
}
20782078
}
20792079

2080-
void pp_title_line(struct pretty_print_context *pp,
2081-
const char **msg_p,
2082-
struct strbuf *sb,
2083-
const char *encoding,
2084-
int need_8bit_cte)
2080+
void pp_email_subject(struct pretty_print_context *pp,
2081+
const char **msg_p,
2082+
struct strbuf *sb,
2083+
const char *encoding,
2084+
int need_8bit_cte)
20852085
{
20862086
static const int max_length = 78; /* per rfc2047 */
20872087
struct strbuf title;
@@ -2126,9 +2126,8 @@ void pp_title_line(struct pretty_print_context *pp,
21262126
if (pp->after_subject) {
21272127
strbuf_addstr(sb, pp->after_subject);
21282128
}
2129-
if (cmit_fmt_is_mail(pp->fmt)) {
2130-
strbuf_addch(sb, '\n');
2131-
}
2129+
2130+
strbuf_addch(sb, '\n');
21322131

21332132
if (pp->in_body_headers.nr) {
21342133
int i;
@@ -2328,8 +2327,11 @@ void pretty_print_commit(struct pretty_print_context *pp,
23282327
msg = skip_blank_lines(msg);
23292328

23302329
/* These formats treat the title line specially. */
2331-
if (pp->fmt == CMIT_FMT_ONELINE || cmit_fmt_is_mail(pp->fmt))
2332-
pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
2330+
if (pp->fmt == CMIT_FMT_ONELINE) {
2331+
msg = format_subject(sb, msg, " ");
2332+
strbuf_addch(sb, '\n');
2333+
} else if (cmit_fmt_is_mail(pp->fmt))
2334+
pp_email_subject(pp, &msg, sb, encoding, need_8bit_cte);
23332335

23342336
beginning_of_body = sb->len;
23352337
if (pp->fmt != CMIT_FMT_ONELINE)

pretty.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ void pp_user_info(struct pretty_print_context *pp, const char *what,
9696
const char *encoding);
9797

9898
/*
99-
* Format title line of commit message taken from "msg_p" and
99+
* Format subject line of commit message taken from "msg_p" and
100100
* put it into "sb".
101101
* First line of "msg_p" is also affected.
102102
*/
103-
void pp_title_line(struct pretty_print_context *pp, const char **msg_p,
104-
struct strbuf *sb, const char *encoding,
105-
int need_8bit_cte);
103+
void pp_email_subject(struct pretty_print_context *pp, const char **msg_p,
104+
struct strbuf *sb, const char *encoding,
105+
int need_8bit_cte);
106106

107107
/*
108108
* Get current state of commit message from "msg_p" and continue formatting

0 commit comments

Comments
 (0)