Skip to content

Commit 662cc30

Browse files
peffgitster
authored andcommitted
format-patch: print in-body "From" only when needed
Commit a908047 taught format-patch the "--from" option, which places the author ident into an in-body from header, and uses the committer ident in the rfc822 from header. The documentation claims that it will omit the in-body header when it is the same as the rfc822 header, but the code never implemented that behavior. This patch completes the feature by comparing the two idents and doing nothing when they are the same (this is the same as simply omitting the in-body header, as the two are by definition indistinguishable in this case). This makes it reasonable to turn on "--from" all the time (if it matches your particular workflow), rather than only using it when exporting other people's patches. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent a908047 commit 662cc30

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

cache.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,15 @@ struct ident_split {
963963
*/
964964
extern int split_ident_line(struct ident_split *, const char *, int);
965965

966+
/*
967+
* Compare split idents for equality or strict ordering. Note that we
968+
* compare only the ident part of the line, ignoring any timestamp.
969+
*
970+
* Because there are two fields, we must choose one as the primary key; we
971+
* currently arbitrarily pick the email.
972+
*/
973+
extern int ident_cmp(const struct ident_split *, const struct ident_split *);
974+
966975
struct checkout {
967976
const char *base_dir;
968977
int base_dir_len;

ident.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,3 +402,32 @@ int git_ident_config(const char *var, const char *value, void *data)
402402

403403
return 0;
404404
}
405+
406+
static int buf_cmp(const char *a_begin, const char *a_end,
407+
const char *b_begin, const char *b_end)
408+
{
409+
int a_len = a_end - a_begin;
410+
int b_len = b_end - b_begin;
411+
int min = a_len < b_len ? a_len : b_len;
412+
int cmp;
413+
414+
cmp = memcmp(a_begin, b_begin, min);
415+
if (cmp)
416+
return cmp;
417+
418+
return a_len - b_len;
419+
}
420+
421+
int ident_cmp(const struct ident_split *a,
422+
const struct ident_split *b)
423+
{
424+
int cmp;
425+
426+
cmp = buf_cmp(a->mail_begin, a->mail_end,
427+
b->mail_begin, b->mail_end);
428+
if (cmp)
429+
return cmp;
430+
431+
return buf_cmp(a->name_begin, a->name_end,
432+
b->name_begin, b->name_end);
433+
}

pretty.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ void pp_user_info(struct pretty_print_context *pp,
432432
map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
433433

434434
if (pp->fmt == CMIT_FMT_EMAIL) {
435-
if (pp->from_ident) {
435+
if (pp->from_ident && ident_cmp(pp->from_ident, &ident)) {
436436
struct strbuf buf = STRBUF_INIT;
437437

438438
strbuf_addstr(&buf, "From: ");

t/t4014-format-patch.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,16 @@ test_expect_success '--from uses committer ident' '
10001000
test_cmp expect patch.head
10011001
'
10021002

1003+
test_expect_success '--from omits redundant in-body header' '
1004+
git format-patch -1 --stdout --from="A U Thor <[email protected]>" >patch &&
1005+
cat >expect <<-\EOF &&
1006+
From: A U Thor <[email protected]>
1007+
1008+
EOF
1009+
sed -ne "/^From:/p; /^$/p; /^---$/q" <patch >patch.head &&
1010+
test_cmp expect patch.head
1011+
'
1012+
10031013
test_expect_success 'in-body headers trigger content encoding' '
10041014
GIT_AUTHOR_NAME="éxötìc" test_commit exotic &&
10051015
test_when_finished "git reset --hard HEAD^" &&

0 commit comments

Comments
 (0)