Skip to content

Commit 427a8ec

Browse files
felipecgitster
authored andcommitted
format-patch: refactor branch name calculation
By moving the part that relies on rev->pending earlier, where we are already checking the special case where there's only one ref. Signed-off-by: Felipe Contreras <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 80d35ca commit 427a8ec

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

builtin/log.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,15 +1052,6 @@ static char *find_branch_name(struct rev_info *rev)
10521052
if (0 <= positive) {
10531053
ref = rev->cmdline.rev[positive].name;
10541054
tip_sha1 = rev->cmdline.rev[positive].item->sha1;
1055-
} else if (!rev->cmdline.nr && rev->pending.nr == 1 &&
1056-
!strcmp(rev->pending.objects[0].name, "HEAD")) {
1057-
/*
1058-
* No actual ref from command line, but "HEAD" from
1059-
* rev->def was added in setup_revisions()
1060-
* e.g. format-patch --cover-letter -12
1061-
*/
1062-
ref = "HEAD";
1063-
tip_sha1 = rev->pending.objects[0].item->sha1;
10641055
} else {
10651056
return NULL;
10661057
}
@@ -1280,28 +1271,36 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
12801271
}
12811272

12821273
if (rev.pending.nr == 1) {
1274+
int check_head = 0;
1275+
12831276
if (rev.max_count < 0 && !rev.show_root_diff) {
12841277
/*
12851278
* This is traditional behaviour of "git format-patch
12861279
* origin" that prepares what the origin side still
12871280
* does not have.
12881281
*/
1289-
unsigned char sha1[20];
1290-
const char *ref;
1291-
12921282
rev.pending.objects[0].item->flags |= UNINTERESTING;
12931283
add_head_to_pending(&rev);
1294-
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
1295-
if (ref && !prefixcmp(ref, "refs/heads/"))
1296-
branch_name = xstrdup(ref + strlen("refs/heads/"));
1297-
else
1298-
branch_name = xstrdup(""); /* no branch */
1284+
check_head = 1;
12991285
}
13001286
/*
13011287
* Otherwise, it is "format-patch -22 HEAD", and/or
13021288
* "format-patch --root HEAD". The user wants
13031289
* get_revision() to do the usual traversal.
13041290
*/
1291+
1292+
if (!strcmp(rev.pending.objects[0].name, "HEAD"))
1293+
check_head = 1;
1294+
1295+
if (check_head) {
1296+
unsigned char sha1[20];
1297+
const char *ref;
1298+
ref = resolve_ref_unsafe("HEAD", sha1, 1, NULL);
1299+
if (ref && !prefixcmp(ref, "refs/heads/"))
1300+
branch_name = xstrdup(ref + strlen("refs/heads/"));
1301+
else
1302+
branch_name = xstrdup(""); /* no branch */
1303+
}
13051304
}
13061305

13071306
/*

0 commit comments

Comments
 (0)