提交 f67d2e82 编写于 作者: J Junio C Hamano

Merge branch 'jk/format-patch-am'

* jk/format-patch-am:
  format-patch: preserve subject newlines with -k
  clean up calling conventions for pretty.c functions
  pretty: add pp_commit_easy function for simple callers
  mailinfo: always clean up rfc822 header folding
  t: test subject handling in format-patch / am pipeline

Conflicts:
	builtin/branch.c
	builtin/log.c
	commit.h
...@@ -399,9 +399,7 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item, ...@@ -399,9 +399,7 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
struct commit *commit = item->commit; struct commit *commit = item->commit;
if (commit && !parse_commit(commit)) { if (commit && !parse_commit(commit)) {
struct pretty_print_context ctx = {0}; pp_commit_easy(CMIT_FMT_ONELINE, commit, &subject);
pretty_print_commit(CMIT_FMT_ONELINE, commit,
&subject, &ctx);
sub = subject.buf; sub = subject.buf;
} }
......
...@@ -306,9 +306,8 @@ static void show_local_changes(struct object *head, struct diff_options *opts) ...@@ -306,9 +306,8 @@ static void show_local_changes(struct object *head, struct diff_options *opts)
static void describe_detached_head(const char *msg, struct commit *commit) static void describe_detached_head(const char *msg, struct commit *commit)
{ {
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
struct pretty_print_context ctx = {0};
parse_commit(commit); parse_commit(commit);
pretty_print_commit(CMIT_FMT_ONELINE, commit, &sb, &ctx); pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
fprintf(stderr, "%s %s... %s\n", msg, fprintf(stderr, "%s %s... %s\n", msg,
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf); find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
strbuf_release(&sb); strbuf_release(&sb);
...@@ -623,14 +622,12 @@ static int clear_commit_marks_from_one_ref(const char *refname, ...@@ -623,14 +622,12 @@ static int clear_commit_marks_from_one_ref(const char *refname,
static void describe_one_orphan(struct strbuf *sb, struct commit *commit) static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
{ {
struct pretty_print_context ctx = { 0 };
parse_commit(commit); parse_commit(commit);
strbuf_addstr(sb, " "); strbuf_addstr(sb, " ");
strbuf_addstr(sb, strbuf_addstr(sb,
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV)); find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
strbuf_addch(sb, ' '); strbuf_addch(sb, ' ');
pretty_print_commit(CMIT_FMT_ONELINE, commit, sb, &ctx); pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
} }
......
...@@ -376,9 +376,11 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix) ...@@ -376,9 +376,11 @@ int cmd_whatchanged(int argc, const char **argv, const char *prefix)
static void show_tagger(char *buf, int len, struct rev_info *rev) static void show_tagger(char *buf, int len, struct rev_info *rev)
{ {
struct strbuf out = STRBUF_INIT; struct strbuf out = STRBUF_INIT;
struct pretty_print_context pp = {0};
pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode, pp.fmt = rev->commit_format;
get_log_output_encoding()); pp.date_mode = rev->date_mode;
pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
printf("%s", out.buf); printf("%s", out.buf);
strbuf_release(&out); strbuf_release(&out);
} }
...@@ -762,10 +764,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout, ...@@ -762,10 +764,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
int quiet) int quiet)
{ {
const char *committer; const char *committer;
const char *subject_start = NULL;
const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n"; const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
const char *msg; const char *msg;
const char *extra_headers = rev->extra_headers;
struct shortlog log; struct shortlog log;
struct strbuf sb = STRBUF_INIT; struct strbuf sb = STRBUF_INIT;
int i; int i;
...@@ -773,6 +773,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout, ...@@ -773,6 +773,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
struct diff_options opts; struct diff_options opts;
int need_8bit_cte = 0; int need_8bit_cte = 0;
struct commit *commit = NULL; struct commit *commit = NULL;
struct pretty_print_context pp = {0};
if (rev->commit_format != CMIT_FMT_EMAIL) if (rev->commit_format != CMIT_FMT_EMAIL)
die(_("Cover letter needs email format")); die(_("Cover letter needs email format"));
...@@ -804,7 +805,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout, ...@@ -804,7 +805,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
free(commit); free(commit);
} }
log_write_email_headers(rev, head, &subject_start, &extra_headers, log_write_email_headers(rev, head, &pp.subject, &pp.after_subject,
&need_8bit_cte); &need_8bit_cte);
for (i = 0; !need_8bit_cte && i < nr; i++) for (i = 0; !need_8bit_cte && i < nr; i++)
...@@ -812,11 +813,11 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout, ...@@ -812,11 +813,11 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
need_8bit_cte = 1; need_8bit_cte = 1;
msg = body; msg = body;
pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822, pp.fmt = CMIT_FMT_EMAIL;
encoding); pp.date_mode = DATE_RFC2822;
pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers, pp_user_info(&pp, NULL, &sb, committer, encoding);
encoding, need_8bit_cte); pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0); pp_remainder(&pp, &msg, &sb, 0);
printf("%s\n", sb.buf); printf("%s\n", sb.buf);
strbuf_release(&sb); strbuf_release(&sb);
...@@ -1180,6 +1181,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) ...@@ -1180,6 +1181,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
die (_("-n and -k are mutually exclusive.")); die (_("-n and -k are mutually exclusive."));
if (keep_subject && subject_prefix) if (keep_subject && subject_prefix)
die (_("--subject-prefix and -k are mutually exclusive.")); die (_("--subject-prefix and -k are mutually exclusive."));
rev.preserve_subject = keep_subject;
argc = setup_revisions(argc, argv, &rev, &s_r_opt); argc = setup_revisions(argc, argv, &rev, &s_r_opt);
if (argc > 1) if (argc > 1)
...@@ -1410,8 +1412,7 @@ static void print_commit(char sign, struct commit *commit, int verbose, ...@@ -1410,8 +1412,7 @@ static void print_commit(char sign, struct commit *commit, int verbose,
find_unique_abbrev(commit->object.sha1, abbrev)); find_unique_abbrev(commit->object.sha1, abbrev));
} else { } else {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct pretty_print_context ctx = {0}; pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
pretty_print_commit(CMIT_FMT_ONELINE, commit, &buf, &ctx);
printf("%c %s %s\n", sign, printf("%c %s %s\n", sign,
find_unique_abbrev(commit->object.sha1, abbrev), find_unique_abbrev(commit->object.sha1, abbrev),
buf.buf); buf.buf);
......
...@@ -400,7 +400,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in) ...@@ -400,7 +400,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
break; break;
if (strbuf_getline(&continuation, in, '\n')) if (strbuf_getline(&continuation, in, '\n'))
break; break;
continuation.buf[0] = '\n'; continuation.buf[0] = ' ';
strbuf_rtrim(&continuation); strbuf_rtrim(&continuation);
strbuf_addbuf(line, &continuation); strbuf_addbuf(line, &continuation);
} }
......
...@@ -339,13 +339,14 @@ static void squash_message(void) ...@@ -339,13 +339,14 @@ static void squash_message(void)
ctx.abbrev = rev.abbrev; ctx.abbrev = rev.abbrev;
ctx.date_mode = rev.date_mode; ctx.date_mode = rev.date_mode;
ctx.fmt = rev.commit_format;
strbuf_addstr(&out, "Squashed commit of the following:\n"); strbuf_addstr(&out, "Squashed commit of the following:\n");
while ((commit = get_revision(&rev)) != NULL) { while ((commit = get_revision(&rev)) != NULL) {
strbuf_addch(&out, '\n'); strbuf_addch(&out, '\n');
strbuf_addf(&out, "commit %s\n", strbuf_addf(&out, "commit %s\n",
sha1_to_hex(commit->object.sha1)); sha1_to_hex(commit->object.sha1));
pretty_print_commit(rev.commit_format, commit, &out, &ctx); pretty_print_commit(&ctx, commit, &out);
} }
if (write(fd, out.buf, out.len) < 0) if (write(fd, out.buf, out.len) < 0)
die_errno(_("Writing SQUASH_MSG")); die_errno(_("Writing SQUASH_MSG"));
......
...@@ -104,7 +104,8 @@ static void show_commit(struct commit *commit, void *data) ...@@ -104,7 +104,8 @@ static void show_commit(struct commit *commit, void *data)
struct pretty_print_context ctx = {0}; struct pretty_print_context ctx = {0};
ctx.abbrev = revs->abbrev; ctx.abbrev = revs->abbrev;
ctx.date_mode = revs->date_mode; ctx.date_mode = revs->date_mode;
pretty_print_commit(revs->commit_format, commit, &buf, &ctx); ctx.fmt = revs->commit_format;
pretty_print_commit(&ctx, commit, &buf);
if (revs->graph) { if (revs->graph) {
if (buf.len) { if (buf.len) {
if (revs->commit_format != CMIT_FMT_ONELINE) if (revs->commit_format != CMIT_FMT_ONELINE)
......
...@@ -138,9 +138,8 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit) ...@@ -138,9 +138,8 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
const char *author = NULL, *buffer; const char *author = NULL, *buffer;
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
struct strbuf ufbuf = STRBUF_INIT; struct strbuf ufbuf = STRBUF_INIT;
struct pretty_print_context ctx = {0};
pretty_print_commit(CMIT_FMT_RAW, commit, &buf, &ctx); pp_commit_easy(CMIT_FMT_RAW, commit, &buf);
buffer = buf.buf; buffer = buf.buf;
while (*buffer && *buffer != '\n') { while (*buffer && *buffer != '\n') {
const char *eol = strchr(buffer, '\n'); const char *eol = strchr(buffer, '\n');
...@@ -159,11 +158,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit) ...@@ -159,11 +158,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
sha1_to_hex(commit->object.sha1)); sha1_to_hex(commit->object.sha1));
if (log->user_format) { if (log->user_format) {
struct pretty_print_context ctx = {0}; struct pretty_print_context ctx = {0};
ctx.fmt = CMIT_FMT_USERFORMAT;
ctx.abbrev = log->abbrev; ctx.abbrev = log->abbrev;
ctx.subject = ""; ctx.subject = "";
ctx.after_subject = ""; ctx.after_subject = "";
ctx.date_mode = DATE_NORMAL; ctx.date_mode = DATE_NORMAL;
pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx); pretty_print_commit(&ctx, commit, &ufbuf);
buffer = ufbuf.buf; buffer = ufbuf.buf;
} else if (*buffer) { } else if (*buffer) {
buffer++; buffer++;
......
...@@ -283,8 +283,7 @@ static void show_one_commit(struct commit *commit, int no_name) ...@@ -283,8 +283,7 @@ static void show_one_commit(struct commit *commit, int no_name)
struct commit_name *name = commit->util; struct commit_name *name = commit->util;
if (commit->object.parsed) { if (commit->object.parsed) {
struct pretty_print_context ctx = {0}; pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
pretty_print_commit(CMIT_FMT_ONELINE, commit, &pretty, &ctx);
pretty_str = pretty.buf; pretty_str = pretty.buf;
} }
if (!prefixcmp(pretty_str, "[PATCH] ")) if (!prefixcmp(pretty_str, "[PATCH] "))
......
...@@ -69,9 +69,11 @@ enum cmit_fmt { ...@@ -69,9 +69,11 @@ enum cmit_fmt {
}; };
struct pretty_print_context { struct pretty_print_context {
enum cmit_fmt fmt;
int abbrev; int abbrev;
const char *subject; const char *subject;
const char *after_subject; const char *after_subject;
int preserve_subject;
enum date_mode date_mode; enum date_mode date_mode;
int need_8bit_cte; int need_8bit_cte;
int show_notes; int show_notes;
...@@ -96,20 +98,20 @@ extern void userformat_find_requirements(const char *fmt, struct userformat_want ...@@ -96,20 +98,20 @@ extern void userformat_find_requirements(const char *fmt, struct userformat_want
extern void format_commit_message(const struct commit *commit, extern void format_commit_message(const struct commit *commit,
const char *format, struct strbuf *sb, const char *format, struct strbuf *sb,
const struct pretty_print_context *context); const struct pretty_print_context *context);
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, extern void pretty_print_commit(const struct pretty_print_context *pp,
struct strbuf *sb, const struct commit *commit,
const struct pretty_print_context *context); struct strbuf *sb);
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
const char *line, enum date_mode dmode, struct strbuf *sb);
const char *encoding); void pp_user_info(const struct pretty_print_context *pp,
void pp_title_line(enum cmit_fmt fmt, const char *what, struct strbuf *sb,
const char *line, const char *encoding);
void pp_title_line(const struct pretty_print_context *pp,
const char **msg_p, const char **msg_p,
struct strbuf *sb, struct strbuf *sb,
const char *subject,
const char *after_subject,
const char *encoding, const char *encoding,
int need_8bit_cte); int need_8bit_cte);
void pp_remainder(enum cmit_fmt fmt, void pp_remainder(const struct pretty_print_context *pp,
const char **msg_p, const char **msg_p,
struct strbuf *sb, struct strbuf *sb,
int indent); int indent);
......
...@@ -485,8 +485,10 @@ void show_log(struct rev_info *opt) ...@@ -485,8 +485,10 @@ void show_log(struct rev_info *opt)
ctx.date_mode = opt->date_mode; ctx.date_mode = opt->date_mode;
ctx.abbrev = opt->diffopt.abbrev; ctx.abbrev = opt->diffopt.abbrev;
ctx.after_subject = extra_headers; ctx.after_subject = extra_headers;
ctx.preserve_subject = opt->preserve_subject;
ctx.reflog_info = opt->reflog_info; ctx.reflog_info = opt->reflog_info;
pretty_print_commit(opt->commit_format, commit, &msgbuf, &ctx); ctx.fmt = opt->commit_format;
pretty_print_commit(&ctx, commit, &msgbuf);
if (opt->add_signoff) if (opt->add_signoff)
append_signoff(&msgbuf, opt->add_signoff); append_signoff(&msgbuf, opt->add_signoff);
......
...@@ -318,16 +318,16 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len, ...@@ -318,16 +318,16 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
strbuf_addstr(sb, "?="); strbuf_addstr(sb, "?=");
} }
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, void pp_user_info(const struct pretty_print_context *pp,
const char *line, enum date_mode dmode, const char *what, struct strbuf *sb,
const char *encoding) const char *line, const char *encoding)
{ {
char *date; char *date;
int namelen; int namelen;
unsigned long time; unsigned long time;
int tz; int tz;
if (fmt == CMIT_FMT_ONELINE) if (pp->fmt == CMIT_FMT_ONELINE)
return; return;
date = strchr(line, '>'); date = strchr(line, '>');
if (!date) if (!date)
...@@ -336,7 +336,7 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, ...@@ -336,7 +336,7 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
time = strtoul(date, &date, 10); time = strtoul(date, &date, 10);
tz = strtol(date, NULL, 10); tz = strtol(date, NULL, 10);
if (fmt == CMIT_FMT_EMAIL) { if (pp->fmt == CMIT_FMT_EMAIL) {
char *name_tail = strchr(line, '<'); char *name_tail = strchr(line, '<');
int display_name_length; int display_name_length;
int final_line; int final_line;
...@@ -366,18 +366,18 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb, ...@@ -366,18 +366,18 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
} else { } else {
strbuf_addf(sb, "%s: %.*s%.*s\n", what, strbuf_addf(sb, "%s: %.*s%.*s\n", what,
(fmt == CMIT_FMT_FULLER) ? 4 : 0, (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
" ", namelen, line); " ", namelen, line);
} }
switch (fmt) { switch (pp->fmt) {
case CMIT_FMT_MEDIUM: case CMIT_FMT_MEDIUM:
strbuf_addf(sb, "Date: %s\n", show_date(time, tz, dmode)); strbuf_addf(sb, "Date: %s\n", show_date(time, tz, pp->date_mode));
break; break;
case CMIT_FMT_EMAIL: case CMIT_FMT_EMAIL:
strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822)); strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
break; break;
case CMIT_FMT_FULLER: case CMIT_FMT_FULLER:
strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, dmode)); strbuf_addf(sb, "%sDate: %s\n", what, show_date(time, tz, pp->date_mode));
break; break;
default: default:
/* notin' */ /* notin' */
...@@ -408,12 +408,12 @@ static const char *skip_empty_lines(const char *msg) ...@@ -408,12 +408,12 @@ static const char *skip_empty_lines(const char *msg)
return msg; return msg;
} }
static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb, static void add_merge_info(const struct pretty_print_context *pp,
const struct commit *commit, int abbrev) struct strbuf *sb, const struct commit *commit)
{ {
struct commit_list *parent = commit->parents; struct commit_list *parent = commit->parents;
if ((fmt == CMIT_FMT_ONELINE) || (fmt == CMIT_FMT_EMAIL) || if ((pp->fmt == CMIT_FMT_ONELINE) || (pp->fmt == CMIT_FMT_EMAIL) ||
!parent || !parent->next) !parent || !parent->next)
return; return;
...@@ -422,8 +422,8 @@ static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb, ...@@ -422,8 +422,8 @@ static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
while (parent) { while (parent) {
struct commit *p = parent->item; struct commit *p = parent->item;
const char *hex = NULL; const char *hex = NULL;
if (abbrev) if (pp->abbrev)
hex = find_unique_abbrev(p->object.sha1, abbrev); hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
if (!hex) if (!hex)
hex = sha1_to_hex(p->object.sha1); hex = sha1_to_hex(p->object.sha1);
parent = parent->next; parent = parent->next;
...@@ -1116,9 +1116,7 @@ void format_commit_message(const struct commit *commit, ...@@ -1116,9 +1116,7 @@ void format_commit_message(const struct commit *commit,
free(context.message); free(context.message);
} }
static void pp_header(enum cmit_fmt fmt, static void pp_header(const struct pretty_print_context *pp,
int abbrev,
enum date_mode dmode,
const char *encoding, const char *encoding,
const struct commit *commit, const struct commit *commit,
const char **msg_p, const char **msg_p,
...@@ -1138,7 +1136,7 @@ static void pp_header(enum cmit_fmt fmt, ...@@ -1138,7 +1136,7 @@ static void pp_header(enum cmit_fmt fmt,
/* End of header */ /* End of header */
return; return;
if (fmt == CMIT_FMT_RAW) { if (pp->fmt == CMIT_FMT_RAW) {
strbuf_add(sb, line, linelen); strbuf_add(sb, line, linelen);
continue; continue;
} }
...@@ -1158,7 +1156,7 @@ static void pp_header(enum cmit_fmt fmt, ...@@ -1158,7 +1156,7 @@ static void pp_header(enum cmit_fmt fmt,
; ;
/* with enough slop */ /* with enough slop */
strbuf_grow(sb, num * 50 + 20); strbuf_grow(sb, num * 50 + 20);
add_merge_info(fmt, sb, commit, abbrev); add_merge_info(pp, sb, commit);
parents_shown = 1; parents_shown = 1;
} }
...@@ -1169,32 +1167,31 @@ static void pp_header(enum cmit_fmt fmt, ...@@ -1169,32 +1167,31 @@ static void pp_header(enum cmit_fmt fmt,
*/ */
if (!memcmp(line, "author ", 7)) { if (!memcmp(line, "author ", 7)) {
strbuf_grow(sb, linelen + 80); strbuf_grow(sb, linelen + 80);
pp_user_info("Author", fmt, sb, line + 7, dmode, encoding); pp_user_info(pp, "Author", sb, line + 7, encoding);
} }
if (!memcmp(line, "committer ", 10) && if (!memcmp(line, "committer ", 10) &&
(fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) { (pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
strbuf_grow(sb, linelen + 80); strbuf_grow(sb, linelen + 80);
pp_user_info("Commit", fmt, sb, line + 10, dmode, encoding); pp_user_info(pp, "Commit", sb, line + 10, encoding);
} }
} }
} }
void pp_title_line(enum cmit_fmt fmt, void pp_title_line(const struct pretty_print_context *pp,
const char **msg_p, const char **msg_p,
struct strbuf *sb, struct strbuf *sb,
const char *subject,
const char *after_subject,
const char *encoding, const char *encoding,
int need_8bit_cte) int need_8bit_cte)
{ {
struct strbuf title; struct strbuf title;
strbuf_init(&title, 80); strbuf_init(&title, 80);
*msg_p = format_subject(&title, *msg_p, " "); *msg_p = format_subject(&title, *msg_p,
pp->preserve_subject ? "\n" : " ");
strbuf_grow(sb, title.len + 1024); strbuf_grow(sb, title.len + 1024);
if (subject) { if (pp->subject) {
strbuf_addstr(sb, subject); strbuf_addstr(sb, pp->subject);
add_rfc2047(sb, title.buf, title.len, encoding); add_rfc2047(sb, title.buf, title.len, encoding);
} else { } else {
strbuf_addbuf(sb, &title); strbuf_addbuf(sb, &title);
...@@ -1208,16 +1205,16 @@ void pp_title_line(enum cmit_fmt fmt, ...@@ -1208,16 +1205,16 @@ void pp_title_line(enum cmit_fmt fmt,
"Content-Transfer-Encoding: 8bit\n"; "Content-Transfer-Encoding: 8bit\n";
strbuf_addf(sb, header_fmt, encoding); strbuf_addf(sb, header_fmt, encoding);
} }
if (after_subject) { if (pp->after_subject) {
strbuf_addstr(sb, after_subject); strbuf_addstr(sb, pp->after_subject);
} }
if (fmt == CMIT_FMT_EMAIL) { if (pp->fmt == CMIT_FMT_EMAIL) {
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
} }
strbuf_release(&title); strbuf_release(&title);
} }
void pp_remainder(enum cmit_fmt fmt, void pp_remainder(const struct pretty_print_context *pp,
const char **msg_p, const char **msg_p,
struct strbuf *sb, struct strbuf *sb,
int indent) int indent)
...@@ -1234,7 +1231,7 @@ void pp_remainder(enum cmit_fmt fmt, ...@@ -1234,7 +1231,7 @@ void pp_remainder(enum cmit_fmt fmt,
if (is_empty_line(line, &linelen)) { if (is_empty_line(line, &linelen)) {
if (first) if (first)
continue; continue;
if (fmt == CMIT_FMT_SHORT) if (pp->fmt == CMIT_FMT_SHORT)
break; break;
} }
first = 0; first = 0;
...@@ -1259,19 +1256,19 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding ...@@ -1259,19 +1256,19 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding
return logmsg_reencode(commit, encoding); return logmsg_reencode(commit, encoding);
} }
void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, void pretty_print_commit(const struct pretty_print_context *pp,
struct strbuf *sb, const struct commit *commit,
const struct pretty_print_context *context) struct strbuf *sb)
{ {
unsigned long beginning_of_body; unsigned long beginning_of_body;
int indent = 4; int indent = 4;
const char *msg = commit->buffer; const char *msg = commit->buffer;
char *reencoded; char *reencoded;
const char *encoding; const char *encoding;
int need_8bit_cte = context->need_8bit_cte; int need_8bit_cte = pp->need_8bit_cte;
if (fmt == CMIT_FMT_USERFORMAT) { if (pp->fmt == CMIT_FMT_USERFORMAT) {
format_commit_message(commit, user_format, sb, context); format_commit_message(commit, user_format, sb, pp);
return; return;
} }
...@@ -1280,14 +1277,14 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, ...@@ -1280,14 +1277,14 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
msg = reencoded; msg = reencoded;
} }
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
indent = 0; indent = 0;
/* /*
* We need to check and emit Content-type: to mark it * We need to check and emit Content-type: to mark it
* as 8-bit if we haven't done so. * as 8-bit if we haven't done so.
*/ */
if (fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) { if (pp->fmt == CMIT_FMT_EMAIL && need_8bit_cte == 0) {
int i, ch, in_body; int i, ch, in_body;
for (in_body = i = 0; (ch = msg[i]); i++) { for (in_body = i = 0; (ch = msg[i]); i++) {
...@@ -1306,9 +1303,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, ...@@ -1306,9 +1303,8 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
} }
} }
pp_header(fmt, context->abbrev, context->date_mode, encoding, pp_header(pp, encoding, commit, &msg, sb);
commit, &msg, sb); if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
if (fmt != CMIT_FMT_ONELINE && !context->subject) {
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
} }
...@@ -1316,17 +1312,16 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, ...@@ -1316,17 +1312,16 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
msg = skip_empty_lines(msg); msg = skip_empty_lines(msg);
/* These formats treat the title line specially. */ /* These formats treat the title line specially. */
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL) if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
pp_title_line(fmt, &msg, sb, context->subject, pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
context->after_subject, encoding, need_8bit_cte);
beginning_of_body = sb->len; beginning_of_body = sb->len;
if (fmt != CMIT_FMT_ONELINE) if (pp->fmt != CMIT_FMT_ONELINE)
pp_remainder(fmt, &msg, sb, indent); pp_remainder(pp, &msg, sb, indent);
strbuf_rtrim(sb); strbuf_rtrim(sb);
/* Make sure there is an EOLN for the non-oneline case */ /* Make sure there is an EOLN for the non-oneline case */
if (fmt != CMIT_FMT_ONELINE) if (pp->fmt != CMIT_FMT_ONELINE)
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
/* /*
...@@ -1334,12 +1329,20 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit, ...@@ -1334,12 +1329,20 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
* format. Make sure we did not strip the blank line * format. Make sure we did not strip the blank line
* between the header and the body. * between the header and the body.
*/ */
if (fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body) if (pp->fmt == CMIT_FMT_EMAIL && sb->len <= beginning_of_body)
strbuf_addch(sb, '\n'); strbuf_addch(sb, '\n');
if (context->show_notes) if (pp->show_notes)
format_display_notes(commit->object.sha1, sb, encoding, format_display_notes(commit->object.sha1, sb, encoding,
NOTES_SHOW_HEADER | NOTES_INDENT); NOTES_SHOW_HEADER | NOTES_INDENT);
free(reencoded); free(reencoded);
} }
void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *sb)
{
struct pretty_print_context pp = {0};
pp.fmt = fmt;
pretty_print_commit(&pp, commit, sb);
}
...@@ -94,7 +94,8 @@ struct rev_info { ...@@ -94,7 +94,8 @@ struct rev_info {
abbrev_commit_given:1, abbrev_commit_given:1,
use_terminator:1, use_terminator:1,
missing_newline:1, missing_newline:1,
date_mode_explicit:1; date_mode_explicit:1,
preserve_subject:1;
unsigned int disable_stdin:1; unsigned int disable_stdin:1;
enum date_mode date_mode; enum date_mode date_mode;
......
#!/bin/sh
test_description='test subject preservation with format-patch | am'
. ./test-lib.sh
make_patches() {
type=$1
subject=$2
test_expect_success "create patches with $type subject" '
git reset --hard baseline &&
echo $type >file &&
git commit -a -m "$subject" &&
git format-patch -1 --stdout >$type.patch &&
git format-patch -1 --stdout -k >$type-k.patch
'
}
check_subject() {
git reset --hard baseline &&
git am $2 $1.patch &&
git log -1 --pretty=format:%B >actual &&
test_cmp expect actual
}
test_expect_success 'setup baseline commit' '
test_commit baseline file
'
SHORT_SUBJECT='short subject'
make_patches short "$SHORT_SUBJECT"
LONG_SUBJECT1='this is a long subject that is virtually guaranteed'
LONG_SUBJECT2='to require wrapping via format-patch if it is all'
LONG_SUBJECT3='going to appear on a single line'
LONG_SUBJECT="$LONG_SUBJECT1 $LONG_SUBJECT2 $LONG_SUBJECT3"
make_patches long "$LONG_SUBJECT"
MULTILINE_SUBJECT="$LONG_SUBJECT1
$LONG_SUBJECT2
$LONG_SUBJECT3"
make_patches multiline "$MULTILINE_SUBJECT"
echo "$SHORT_SUBJECT" >expect
test_expect_success 'short subject preserved (format-patch | am)' '
check_subject short
'
test_expect_success 'short subject preserved (format-patch -k | am)' '
check_subject short-k
'
test_expect_success 'short subject preserved (format-patch -k | am -k)' '
check_subject short-k -k
'
echo "$LONG_SUBJECT" >expect
test_expect_success 'long subject preserved (format-patch | am)' '
check_subject long
'
test_expect_success 'long subject preserved (format-patch -k | am)' '
check_subject long-k
'
test_expect_success 'long subject preserved (format-patch -k | am -k)' '
check_subject long-k -k
'
echo "$LONG_SUBJECT" >expect
test_expect_success 'multiline subject unwrapped (format-patch | am)' '
check_subject multiline
'
test_expect_success 'multiline subject unwrapped (format-patch -k | am)' '
check_subject multiline-k
'
echo "$MULTILINE_SUBJECT" >expect
test_expect_success 'multiline subject preserved (format-patch -k | am -k)' '
check_subject multiline-k -k
'
test_done
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册