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 <felipe.contreras@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Felipe Contreras committed
Apr 7, 2013 at 12:46 UTC
427a8ec5e75f4450620e5df3e1f1071f48c7772c
1 file changed
+16
-17
builtin/log.c
+16
-17
@@ -1052,15 +1052,6 @@ static char *find_branch_name(struct rev_info *rev)
1052
if (0 <= positive) {
1053
ref = rev->cmdline.rev[positive].name;
1054
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;
1055
} else {
1056
return NULL;
1057
}
@@ -1280,28 +1271,36 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix)
1271
}
1272
1273
if (rev.pending.nr == 1) {
1274
+ int check_head = 0;
1275
+
1276
if (rev.max_count < 0 && !rev.show_root_diff) {
1277
/*
1278
* This is traditional behaviour of "git format-patch
1279
* origin" that prepares what the origin side still
1280
* does not have.
1281
*/
1289
- unsigned char sha1[20];
1290
- const char *ref;
1291
-
1282
rev.pending.objects[0].item->flags |= UNINTERESTING;
1283
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;
1285
}
1286
/*
1287
* Otherwise, it is "format-patch -22 HEAD", and/or
1288
* "format-patch --root HEAD". The user wants
1289
* get_revision() to do the usual traversal.
1290
*/
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
+ }
1304
}
1305
1306
/*