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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 3, 2013 at 21:03 UTC 20b630aae924e35c24ee3d082362c36bee1c97b3
2 files changed +23 -12
builtin/log.c
+9 -12
@@ -1014,8 +1014,8 @@ static char *find_branch_name(struct rev_info *rev)
1014 {
1015 int i, positive = -1;
1016 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;
1019
1020 for (i = 0; i < rev->cmdline.nr; i++) {
1021 if (rev->cmdline.rev[i].flags & UNINTERESTING)
@@ -1027,16 +1027,13 @@ static char *find_branch_name(struct rev_info *rev)
1027 }
1028 if (positive < 0)
1029 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;
1037 }
1038
1039 int cmd_format_patch(int argc, const char **argv, const char *prefix)
t/t4014-format-patch.sh
+14
@@ -912,4 +912,18 @@ test_expect_success 'cover letter using branch description (3)' '
912 grep hello actual >/dev/null
913 '
914
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 +
929 test_done