Skip to content

Commit d7ddad0

Browse files
felipecgitster
authored andcommitted
format-patch: trivial cleanups
Now that the cover-letter code has been shuffled, we can do some cleanups. Signed-off-by: Felipe Contreras <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2a4c260 commit d7ddad0

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

builtin/log.c

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,37 @@ static void add_branch_description(struct strbuf *buf, const char *branch_name)
810810
}
811811
}
812812

813+
static char *find_branch_name(struct rev_info *rev)
814+
{
815+
int i, positive = -1;
816+
unsigned char branch_sha1[20];
817+
const unsigned char *tip_sha1;
818+
const char *ref;
819+
char *full_ref, *branch = NULL;
820+
821+
for (i = 0; i < rev->cmdline.nr; i++) {
822+
if (rev->cmdline.rev[i].flags & UNINTERESTING)
823+
continue;
824+
if (positive < 0)
825+
positive = i;
826+
else
827+
return NULL;
828+
}
829+
if (positive < 0)
830+
return NULL;
831+
ref = rev->cmdline.rev[positive].name;
832+
tip_sha1 = rev->cmdline.rev[positive].item->sha1;
833+
if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
834+
!prefixcmp(full_ref, "refs/heads/") &&
835+
!hashcmp(tip_sha1, branch_sha1))
836+
branch = xstrdup(full_ref + strlen("refs/heads/"));
837+
free(full_ref);
838+
return branch;
839+
}
840+
813841
static void make_cover_letter(struct rev_info *rev, int use_stdout,
814842
struct commit *origin,
815-
int nr, struct commit **list, struct commit *head,
843+
int nr, struct commit **list,
816844
const char *branch_name,
817845
int quiet)
818846
{
@@ -826,6 +854,7 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
826854
struct diff_options opts;
827855
int need_8bit_cte = 0;
828856
struct pretty_print_context pp = {0};
857+
struct commit *head = list[0];
829858

830859
if (rev->commit_format != CMIT_FMT_EMAIL)
831860
die(_("Cover letter needs email format"));
@@ -843,6 +872,9 @@ static void make_cover_letter(struct rev_info *rev, int use_stdout,
843872
if (has_non_ascii(list[i]->buffer))
844873
need_8bit_cte = 1;
845874

875+
if (!branch_name)
876+
branch_name = find_branch_name(rev);
877+
846878
msg = body;
847879
pp.fmt = CMIT_FMT_EMAIL;
848880
pp.date_mode = DATE_RFC2822;
@@ -1049,36 +1081,6 @@ static int cc_callback(const struct option *opt, const char *arg, int unset)
10491081
return 0;
10501082
}
10511083

1052-
static char *find_branch_name(struct rev_info *rev)
1053-
{
1054-
int i, positive = -1;
1055-
unsigned char branch_sha1[20];
1056-
const unsigned char *tip_sha1;
1057-
const char *ref;
1058-
char *full_ref, *branch = NULL;
1059-
1060-
for (i = 0; i < rev->cmdline.nr; i++) {
1061-
if (rev->cmdline.rev[i].flags & UNINTERESTING)
1062-
continue;
1063-
if (positive < 0)
1064-
positive = i;
1065-
else
1066-
return NULL;
1067-
}
1068-
if (0 <= positive) {
1069-
ref = rev->cmdline.rev[positive].name;
1070-
tip_sha1 = rev->cmdline.rev[positive].item->sha1;
1071-
} else {
1072-
return NULL;
1073-
}
1074-
if (dwim_ref(ref, strlen(ref), branch_sha1, &full_ref) &&
1075-
!prefixcmp(full_ref, "refs/heads/") &&
1076-
!hashcmp(tip_sha1, branch_sha1))
1077-
branch = xstrdup(full_ref + strlen("refs/heads/"));
1078-
free(full_ref);
1079-
return branch;
1080-
}
1081-
10821084
int cmd_format_patch(int argc, const char **argv, const char *prefix)
10831085
{
10841086
struct commit *commit;
@@ -1093,7 +1095,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
10931095
int cover_letter = -1;
10941096
int boundary_count = 0;
10951097
int no_binary_diff = 0;
1096-
struct commit *origin = NULL, *head = NULL;
1098+
struct commit *origin = NULL;
10971099
const char *in_reply_to = NULL;
10981100
struct patch_ids ids;
10991101
struct strbuf buf = STRBUF_INIT;
@@ -1359,7 +1361,6 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
13591361
if (nr == 0)
13601362
/* nothing to do */
13611363
return 0;
1362-
head = list[0];
13631364
total = nr;
13641365
if (!keep_subject && auto_number && total > 1)
13651366
numbered = 1;
@@ -1383,10 +1384,8 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
13831384
if (cover_letter) {
13841385
if (thread)
13851386
gen_message_id(&rev, "cover");
1386-
if (!branch_name)
1387-
branch_name = find_branch_name(&rev);
13881387
make_cover_letter(&rev, use_stdout,
1389-
origin, nr, list, head, branch_name, quiet);
1388+
origin, nr, list, branch_name, quiet);
13901389
total++;
13911390
start_number--;
13921391
}

0 commit comments

Comments
 (0)