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

Nguyễn Thái Ngọc Duy committed Jan 3, 2013 at 23:16 UTC 5ee29aefac94d4ed11fc5101c89697f39057abb5
2 files changed +23 -3
builtin/log.c
+16 -3
@@ -1014,6 +1014,7 @@ static char *find_branch_name(struct rev_info *rev)
1014 {
1015 int i, positive = -1;
1016 unsigned char branch_sha1[20];
1017 + const unsigned char *tip_sha1;
1018 const char *ref;
1019 char *full_ref, *branch = NULL;
1020
@@ -1025,12 +1026,24 @@ static char *find_branch_name(struct rev_info *rev)
1026 else
1027 return NULL;
1028 }
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 {
1042 return NULL;
1030 - ref = rev->cmdline.rev[positive].name;
1043 + }
1044 if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
1045 !prefixcmp(full_ref, "refs/heads/") &&
1033 - !hashcmp(rev->cmdline.rev[positive].item->sha1, branch_sha1))
1046 + !hashcmp(tip_sha1, branch_sha1))
1047 branch = xstrdup(full_ref + strlen("refs/heads/"));
1048 free(full_ref);
1049 return branch;
t/t4014-format-patch.sh
+7
@@ -926,4 +926,11 @@ test_expect_success 'cover letter using branch description (5)' '
926 grep hello actual >/dev/null
927 '
928
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 +
936 test_done