Skip to content

Commit 20b630a

Browse files
pcloudsgitster
authored andcommitted
format-patch: pick up correct branch name from symbolic ref
find_branch_name() assumes to take refs/heads/<branch>. But we also have symbolic refs, such as HEAD, that can point to a valid branch in refs/heads and do not follow refs/heads/<branch> syntax. Remove the assumption and apply normal ref resolution. After all it would be confusing if rev machinery resolves a ref in one way and find_branch_name() another. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e216cc4 commit 20b630a

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

builtin/log.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,8 @@ static char *find_branch_name(struct rev_info *rev)
10141014
{
10151015
int i, positive = -1;
10161016
unsigned char branch_sha1[20];
1017-
struct strbuf buf = STRBUF_INIT;
1018-
const char *branch;
1017+
const char *ref;
1018+
char *full_ref, *branch = NULL;
10191019

10201020
for (i = 0; i < rev->cmdline.nr; i++) {
10211021
if (rev->cmdline.rev[i].flags & UNINTERESTING)
@@ -1027,16 +1027,13 @@ static char *find_branch_name(struct rev_info *rev)
10271027
}
10281028
if (positive < 0)
10291029
return NULL;
1030-
strbuf_addf(&buf, "refs/heads/%s", rev->cmdline.rev[positive].name);
1031-
branch = resolve_ref_unsafe(buf.buf, branch_sha1, 1, NULL);
1032-
if (!branch ||
1033-
prefixcmp(branch, "refs/heads/") ||
1034-
hashcmp(rev->cmdline.rev[positive].item->sha1, branch_sha1))
1035-
branch = NULL;
1036-
strbuf_release(&buf);
1037-
if (branch)
1038-
return xstrdup(rev->cmdline.rev[positive].name);
1039-
return NULL;
1030+
ref = rev->cmdline.rev[positive].name;
1031+
if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
1032+
!prefixcmp(full_ref, "refs/heads/") &&
1033+
!hashcmp(rev->cmdline.rev[positive].item->sha1, branch_sha1))
1034+
branch = xstrdup(full_ref + strlen("refs/heads/"));
1035+
free(full_ref);
1036+
return branch;
10401037
}
10411038

10421039
int cmd_format_patch(int argc, const char **argv, const char *prefix)

t/t4014-format-patch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,4 +912,18 @@ test_expect_success 'cover letter using branch description (3)' '
912912
grep hello actual >/dev/null
913913
'
914914

915+
test_expect_success 'cover letter using branch description (4)' '
916+
git checkout rebuild-1 &&
917+
test_config branch.rebuild-1.description hello &&
918+
git format-patch --stdout --cover-letter master.. >actual &&
919+
grep hello actual >/dev/null
920+
'
921+
922+
test_expect_success 'cover letter using branch description (5)' '
923+
git checkout rebuild-1 &&
924+
test_config branch.rebuild-1.description hello &&
925+
git format-patch --stdout --cover-letter -2 HEAD >actual &&
926+
grep hello actual >/dev/null
927+
'
928+
915929
test_done

0 commit comments

Comments
 (0)