Skip to content

Commit da212ea

Browse files
committed
Merge branch 'jk/format-patch-from' into maint
"format-patch --from=<whom>" forgot to omit unnecessary in-body from line, i.e. when <whom> is the same as the real author. * jk/format-patch-from: format-patch: print in-body "From" only when needed
2 parents 77bc430 + 662cc30 commit da212ea

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
@@ -966,6 +966,15 @@ struct ident_split {
966966
*/
967967
extern int split_ident_line(struct ident_split *, const char *, int);
968968

969+
/*
970+
* Compare split idents for equality or strict ordering. Note that we
971+
* compare only the ident part of the line, ignoring any timestamp.
972+
*
973+
* Because there are two fields, we must choose one as the primary key; we
974+
* currently arbitrarily pick the email.
975+
*/
976+
extern int ident_cmp(const struct ident_split *, const struct ident_split *);
977+
969978
struct checkout {
970979
const char *base_dir;
971980
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)