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
This commit is contained in:
Junio C Hamano 2011-05-31 12:19:11 -07:00
commit f67d2e82d6
13 changed files with 174 additions and 92 deletions

View File

@ -399,9 +399,7 @@ static void add_verbose_info(struct strbuf *out, struct ref_item *item,
struct commit *commit = item->commit;
if (commit && !parse_commit(commit)) {
struct pretty_print_context ctx = {0};
pretty_print_commit(CMIT_FMT_ONELINE, commit,
&subject, &ctx);
pp_commit_easy(CMIT_FMT_ONELINE, commit, &subject);
sub = subject.buf;
}

View File

@ -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)
{
struct strbuf sb = STRBUF_INIT;
struct pretty_print_context ctx = {0};
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,
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
strbuf_release(&sb);
@ -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)
{
struct pretty_print_context ctx = { 0 };
parse_commit(commit);
strbuf_addstr(sb, " ");
strbuf_addstr(sb,
find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
strbuf_addch(sb, ' ');
pretty_print_commit(CMIT_FMT_ONELINE, commit, sb, &ctx);
pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
strbuf_addch(sb, '\n');
}

View File

@ -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)
{
struct strbuf out = STRBUF_INIT;
struct pretty_print_context pp = {0};
pp_user_info("Tagger", rev->commit_format, &out, buf, rev->date_mode,
get_log_output_encoding());
pp.fmt = rev->commit_format;
pp.date_mode = rev->date_mode;
pp_user_info(&pp, "Tagger", &out, buf, get_log_output_encoding());
printf("%s", out.buf);
strbuf_release(&out);
}
@ -762,10 +764,8 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
int quiet)
{
const char *committer;
const char *subject_start = NULL;
const char *body = "*** SUBJECT HERE ***\n\n*** BLURB HERE ***\n";
const char *msg;
const char *extra_headers = rev->extra_headers;
struct shortlog log;
struct strbuf sb = STRBUF_INIT;
int i;
@ -773,6 +773,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
struct diff_options opts;
int need_8bit_cte = 0;
struct commit *commit = NULL;
struct pretty_print_context pp = {0};
if (rev->commit_format != CMIT_FMT_EMAIL)
die(_("Cover letter needs email format"));
@ -804,7 +805,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
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);
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,
need_8bit_cte = 1;
msg = body;
pp_user_info(NULL, CMIT_FMT_EMAIL, &sb, committer, DATE_RFC2822,
encoding);
pp_title_line(CMIT_FMT_EMAIL, &msg, &sb, subject_start, extra_headers,
encoding, need_8bit_cte);
pp_remainder(CMIT_FMT_EMAIL, &msg, &sb, 0);
pp.fmt = CMIT_FMT_EMAIL;
pp.date_mode = DATE_RFC2822;
pp_user_info(&pp, NULL, &sb, committer, encoding);
pp_title_line(&pp, &msg, &sb, encoding, need_8bit_cte);
pp_remainder(&pp, &msg, &sb, 0);
printf("%s\n", sb.buf);
strbuf_release(&sb);
@ -1180,6 +1181,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
die (_("-n and -k are mutually exclusive."));
if (keep_subject && subject_prefix)
die (_("--subject-prefix and -k are mutually exclusive."));
rev.preserve_subject = keep_subject;
argc = setup_revisions(argc, argv, &rev, &s_r_opt);
if (argc > 1)
@ -1410,8 +1412,7 @@ static void print_commit(char sign, struct commit *commit, int verbose,
find_unique_abbrev(commit->object.sha1, abbrev));
} else {
struct strbuf buf = STRBUF_INIT;
struct pretty_print_context ctx = {0};
pretty_print_commit(CMIT_FMT_ONELINE, commit, &buf, &ctx);
pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
printf("%c %s %s\n", sign,
find_unique_abbrev(commit->object.sha1, abbrev),
buf.buf);

View File

@ -400,7 +400,7 @@ static int read_one_header_line(struct strbuf *line, FILE *in)
break;
if (strbuf_getline(&continuation, in, '\n'))
break;
continuation.buf[0] = '\n';
continuation.buf[0] = ' ';
strbuf_rtrim(&continuation);
strbuf_addbuf(line, &continuation);
}

View File

@ -339,13 +339,14 @@ static void squash_message(void)
ctx.abbrev = rev.abbrev;
ctx.date_mode = rev.date_mode;
ctx.fmt = rev.commit_format;
strbuf_addstr(&out, "Squashed commit of the following:\n");
while ((commit = get_revision(&rev)) != NULL) {
strbuf_addch(&out, '\n');
strbuf_addf(&out, "commit %s\n",
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)
die_errno(_("Writing SQUASH_MSG"));

View File

@ -104,7 +104,8 @@ static void show_commit(struct commit *commit, void *data)
struct pretty_print_context ctx = {0};
ctx.abbrev = revs->abbrev;
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 (buf.len) {
if (revs->commit_format != CMIT_FMT_ONELINE)

View File

@ -138,9 +138,8 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
const char *author = NULL, *buffer;
struct strbuf buf = 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;
while (*buffer && *buffer != '\n') {
const char *eol = strchr(buffer, '\n');
@ -159,11 +158,12 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
sha1_to_hex(commit->object.sha1));
if (log->user_format) {
struct pretty_print_context ctx = {0};
ctx.fmt = CMIT_FMT_USERFORMAT;
ctx.abbrev = log->abbrev;
ctx.subject = "";
ctx.after_subject = "";
ctx.date_mode = DATE_NORMAL;
pretty_print_commit(CMIT_FMT_USERFORMAT, commit, &ufbuf, &ctx);
pretty_print_commit(&ctx, commit, &ufbuf);
buffer = ufbuf.buf;
} else if (*buffer) {
buffer++;

View File

@ -283,8 +283,7 @@ static void show_one_commit(struct commit *commit, int no_name)
struct commit_name *name = commit->util;
if (commit->object.parsed) {
struct pretty_print_context ctx = {0};
pretty_print_commit(CMIT_FMT_ONELINE, commit, &pretty, &ctx);
pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
pretty_str = pretty.buf;
}
if (!prefixcmp(pretty_str, "[PATCH] "))

View File

@ -69,9 +69,11 @@ enum cmit_fmt {
};
struct pretty_print_context {
enum cmit_fmt fmt;
int abbrev;
const char *subject;
const char *after_subject;
int preserve_subject;
enum date_mode date_mode;
int need_8bit_cte;
int show_notes;
@ -96,20 +98,20 @@ extern void userformat_find_requirements(const char *fmt, struct userformat_want
extern void format_commit_message(const struct commit *commit,
const char *format, struct strbuf *sb,
const struct pretty_print_context *context);
extern void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *sb,
const struct pretty_print_context *context);
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
const char *line, enum date_mode dmode,
const char *encoding);
void pp_title_line(enum cmit_fmt fmt,
extern void pretty_print_commit(const struct pretty_print_context *pp,
const struct commit *commit,
struct strbuf *sb);
extern void pp_commit_easy(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *sb);
void pp_user_info(const struct pretty_print_context *pp,
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,
struct strbuf *sb,
const char *subject,
const char *after_subject,
const char *encoding,
int need_8bit_cte);
void pp_remainder(enum cmit_fmt fmt,
void pp_remainder(const struct pretty_print_context *pp,
const char **msg_p,
struct strbuf *sb,
int indent);

View File

@ -485,8 +485,10 @@ void show_log(struct rev_info *opt)
ctx.date_mode = opt->date_mode;
ctx.abbrev = opt->diffopt.abbrev;
ctx.after_subject = extra_headers;
ctx.preserve_subject = opt->preserve_subject;
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)
append_signoff(&msgbuf, opt->add_signoff);

107
pretty.c
View File

@ -318,16 +318,16 @@ needquote:
strbuf_addstr(sb, "?=");
}
void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
const char *line, enum date_mode dmode,
const char *encoding)
void pp_user_info(const struct pretty_print_context *pp,
const char *what, struct strbuf *sb,
const char *line, const char *encoding)
{
char *date;
int namelen;
unsigned long time;
int tz;
if (fmt == CMIT_FMT_ONELINE)
if (pp->fmt == CMIT_FMT_ONELINE)
return;
date = strchr(line, '>');
if (!date)
@ -336,7 +336,7 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
time = strtoul(date, &date, 10);
tz = strtol(date, NULL, 10);
if (fmt == CMIT_FMT_EMAIL) {
if (pp->fmt == CMIT_FMT_EMAIL) {
char *name_tail = strchr(line, '<');
int display_name_length;
int final_line;
@ -366,18 +366,18 @@ void pp_user_info(const char *what, enum cmit_fmt fmt, struct strbuf *sb,
strbuf_addch(sb, '\n');
} else {
strbuf_addf(sb, "%s: %.*s%.*s\n", what,
(fmt == CMIT_FMT_FULLER) ? 4 : 0,
(pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
" ", namelen, line);
}
switch (fmt) {
switch (pp->fmt) {
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;
case CMIT_FMT_EMAIL:
strbuf_addf(sb, "Date: %s\n", show_date(time, tz, DATE_RFC2822));
break;
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;
default:
/* notin' */
@ -408,12 +408,12 @@ static const char *skip_empty_lines(const char *msg)
return msg;
}
static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
const struct commit *commit, int abbrev)
static void add_merge_info(const struct pretty_print_context *pp,
struct strbuf *sb, const struct commit *commit)
{
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)
return;
@ -422,8 +422,8 @@ static void add_merge_info(enum cmit_fmt fmt, struct strbuf *sb,
while (parent) {
struct commit *p = parent->item;
const char *hex = NULL;
if (abbrev)
hex = find_unique_abbrev(p->object.sha1, abbrev);
if (pp->abbrev)
hex = find_unique_abbrev(p->object.sha1, pp->abbrev);
if (!hex)
hex = sha1_to_hex(p->object.sha1);
parent = parent->next;
@ -1116,9 +1116,7 @@ void format_commit_message(const struct commit *commit,
free(context.message);
}
static void pp_header(enum cmit_fmt fmt,
int abbrev,
enum date_mode dmode,
static void pp_header(const struct pretty_print_context *pp,
const char *encoding,
const struct commit *commit,
const char **msg_p,
@ -1138,7 +1136,7 @@ static void pp_header(enum cmit_fmt fmt,
/* End of header */
return;
if (fmt == CMIT_FMT_RAW) {
if (pp->fmt == CMIT_FMT_RAW) {
strbuf_add(sb, line, linelen);
continue;
}
@ -1158,7 +1156,7 @@ static void pp_header(enum cmit_fmt fmt,
;
/* with enough slop */
strbuf_grow(sb, num * 50 + 20);
add_merge_info(fmt, sb, commit, abbrev);
add_merge_info(pp, sb, commit);
parents_shown = 1;
}
@ -1169,32 +1167,31 @@ static void pp_header(enum cmit_fmt fmt,
*/
if (!memcmp(line, "author ", 7)) {
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) &&
(fmt == CMIT_FMT_FULL || fmt == CMIT_FMT_FULLER)) {
(pp->fmt == CMIT_FMT_FULL || pp->fmt == CMIT_FMT_FULLER)) {
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,
struct strbuf *sb,
const char *subject,
const char *after_subject,
const char *encoding,
int need_8bit_cte)
{
struct strbuf title;
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);
if (subject) {
strbuf_addstr(sb, subject);
if (pp->subject) {
strbuf_addstr(sb, pp->subject);
add_rfc2047(sb, title.buf, title.len, encoding);
} else {
strbuf_addbuf(sb, &title);
@ -1208,16 +1205,16 @@ void pp_title_line(enum cmit_fmt fmt,
"Content-Transfer-Encoding: 8bit\n";
strbuf_addf(sb, header_fmt, encoding);
}
if (after_subject) {
strbuf_addstr(sb, after_subject);
if (pp->after_subject) {
strbuf_addstr(sb, pp->after_subject);
}
if (fmt == CMIT_FMT_EMAIL) {
if (pp->fmt == CMIT_FMT_EMAIL) {
strbuf_addch(sb, '\n');
}
strbuf_release(&title);
}
void pp_remainder(enum cmit_fmt fmt,
void pp_remainder(const struct pretty_print_context *pp,
const char **msg_p,
struct strbuf *sb,
int indent)
@ -1234,7 +1231,7 @@ void pp_remainder(enum cmit_fmt fmt,
if (is_empty_line(line, &linelen)) {
if (first)
continue;
if (fmt == CMIT_FMT_SHORT)
if (pp->fmt == CMIT_FMT_SHORT)
break;
}
first = 0;
@ -1259,19 +1256,19 @@ char *reencode_commit_message(const struct commit *commit, const char **encoding
return logmsg_reencode(commit, encoding);
}
void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
struct strbuf *sb,
const struct pretty_print_context *context)
void pretty_print_commit(const struct pretty_print_context *pp,
const struct commit *commit,
struct strbuf *sb)
{
unsigned long beginning_of_body;
int indent = 4;
const char *msg = commit->buffer;
char *reencoded;
const char *encoding;
int need_8bit_cte = context->need_8bit_cte;
int need_8bit_cte = pp->need_8bit_cte;
if (fmt == CMIT_FMT_USERFORMAT) {
format_commit_message(commit, user_format, sb, context);
if (pp->fmt == CMIT_FMT_USERFORMAT) {
format_commit_message(commit, user_format, sb, pp);
return;
}
@ -1280,14 +1277,14 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
msg = reencoded;
}
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
indent = 0;
/*
* We need to check and emit Content-type: to mark it
* 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;
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,
}
}
pp_header(fmt, context->abbrev, context->date_mode, encoding,
commit, &msg, sb);
if (fmt != CMIT_FMT_ONELINE && !context->subject) {
pp_header(pp, encoding, commit, &msg, sb);
if (pp->fmt != CMIT_FMT_ONELINE && !pp->subject) {
strbuf_addch(sb, '\n');
}
@ -1316,17 +1312,16 @@ void pretty_print_commit(enum cmit_fmt fmt, const struct commit *commit,
msg = skip_empty_lines(msg);
/* These formats treat the title line specially. */
if (fmt == CMIT_FMT_ONELINE || fmt == CMIT_FMT_EMAIL)
pp_title_line(fmt, &msg, sb, context->subject,
context->after_subject, encoding, need_8bit_cte);
if (pp->fmt == CMIT_FMT_ONELINE || pp->fmt == CMIT_FMT_EMAIL)
pp_title_line(pp, &msg, sb, encoding, need_8bit_cte);
beginning_of_body = sb->len;
if (fmt != CMIT_FMT_ONELINE)
pp_remainder(fmt, &msg, sb, indent);
if (pp->fmt != CMIT_FMT_ONELINE)
pp_remainder(pp, &msg, sb, indent);
strbuf_rtrim(sb);
/* 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');
/*
@ -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
* 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');
if (context->show_notes)
if (pp->show_notes)
format_display_notes(commit->object.sha1, sb, encoding,
NOTES_SHOW_HEADER | NOTES_INDENT);
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);
}

View File

@ -94,7 +94,8 @@ struct rev_info {
abbrev_commit_given:1,
use_terminator:1,
missing_newline:1,
date_mode_explicit:1;
date_mode_explicit:1,
preserve_subject:1;
unsigned int disable_stdin:1;
enum date_mode date_mode;

77
t/t4152-am-subjects.sh Executable file
View File

@ -0,0 +1,77 @@
#!/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