Skip to content

Commit 5ee29ae

Browse files
pcloudsgitster
authored andcommitted
format-patch: pick up branch description when no ref is specified
We only try to get branch name in "format-patch origin" case or similar and not "format-patch -22" where HEAD is automatically added. Without correct branch name, branch description cannot be added. Make sure we always get branch name. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 20b630a commit 5ee29ae

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

builtin/log.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,7 @@ static char *find_branch_name(struct rev_info *rev)
10141014
{
10151015
int i, positive = -1;
10161016
unsigned char branch_sha1[20];
1017+
const unsigned char *tip_sha1;
10171018
const char *ref;
10181019
char *full_ref, *branch = NULL;
10191020

@@ -1025,12 +1026,24 @@ static char *find_branch_name(struct rev_info *rev)
10251026
else
10261027
return NULL;
10271028
}
1028-
if (positive < 0)
1029+
if (0 <= positive) {
1030+
ref = rev->cmdline.rev[positive].name;
1031+
tip_sha1 = rev->cmdline.rev[positive].item->sha1;
1032+
} else if (!rev->cmdline.nr && rev->pending.nr == 1 &&
1033+
!strcmp(rev->pending.objects[0].name, "HEAD")) {
1034+
/*
1035+
* No actual ref from command line, but "HEAD" from
1036+
* rev->def was added in setup_revisions()
1037+
* e.g. format-patch --cover-letter -12
1038+
*/
1039+
ref = "HEAD";
1040+
tip_sha1 = rev->pending.objects[0].item->sha1;
1041+
} else {
10291042
return NULL;
1030-
ref = rev->cmdline.rev[positive].name;
1043+
}
10311044
if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
10321045
!prefixcmp(full_ref, "refs/heads/") &&
1033-
!hashcmp(rev->cmdline.rev[positive].item->sha1, branch_sha1))
1046+
!hashcmp(tip_sha1, branch_sha1))
10341047
branch = xstrdup(full_ref + strlen("refs/heads/"));
10351048
free(full_ref);
10361049
return branch;

t/t4014-format-patch.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,4 +926,11 @@ test_expect_success 'cover letter using branch description (5)' '
926926
grep hello actual >/dev/null
927927
'
928928

929+
test_expect_success 'cover letter using branch description (6)' '
930+
git checkout rebuild-1 &&
931+
test_config branch.rebuild-1.description hello &&
932+
git format-patch --stdout --cover-letter -2 >actual &&
933+
grep hello actual >/dev/null
934+
'
935+
929936
test_done

0 commit comments

Comments
 (0)