Skip to content

Commit 9bf0eed

Browse files
committed
Merge branch 'jc/maint-autofix-tag-in-head'
* jc/maint-autofix-tag-in-head: commit: reduce use of redundant global variables
2 parents 1d21112 + 06bb643 commit 9bf0eed

File tree

1 file changed

+45
-42
lines changed

1 file changed

+45
-42
lines changed

builtin/commit.c

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\
6262
"\n"
6363
"Otherwise, please use 'git reset'\n");
6464

65-
static unsigned char head_sha1[20];
66-
6765
static const char *use_message_buffer;
6866
static const char commit_editmsg[] = "COMMIT_EDITMSG";
6967
static struct lock_file index_lock; /* real index */
@@ -102,7 +100,7 @@ static enum {
102100
static char *cleanup_arg;
103101

104102
static enum commit_whence whence;
105-
static int use_editor = 1, initial_commit, include_status = 1;
103+
static int use_editor = 1, include_status = 1;
106104
static int show_ignored_in_status;
107105
static const char *only_include_assumed;
108106
static struct strbuf message;
@@ -296,13 +294,13 @@ static void add_remove_files(struct string_list *list)
296294
}
297295
}
298296

299-
static void create_base_index(void)
297+
static void create_base_index(const struct commit *current_head)
300298
{
301299
struct tree *tree;
302300
struct unpack_trees_options opts;
303301
struct tree_desc t;
304302

305-
if (initial_commit) {
303+
if (!current_head) {
306304
discard_cache();
307305
return;
308306
}
@@ -315,7 +313,7 @@ static void create_base_index(void)
315313
opts.dst_index = &the_index;
316314

317315
opts.fn = oneway_merge;
318-
tree = parse_tree_indirect(head_sha1);
316+
tree = parse_tree_indirect(current_head->object.sha1);
319317
if (!tree)
320318
die(_("failed to unpack HEAD tree object"));
321319
parse_tree(tree);
@@ -334,7 +332,8 @@ static void refresh_cache_or_die(int refresh_flags)
334332
die_resolve_conflict("commit");
335333
}
336334

337-
static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status)
335+
static char *prepare_index(int argc, const char **argv, const char *prefix,
336+
const struct commit *current_head, int is_status)
338337
{
339338
int fd;
340339
struct string_list partial;
@@ -450,7 +449,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
450449

451450
memset(&partial, 0, sizeof(partial));
452451
partial.strdup_strings = 1;
453-
if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
452+
if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec))
454453
exit(1);
455454

456455
discard_cache();
@@ -469,7 +468,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
469468
(uintmax_t) getpid()),
470469
LOCK_DIE_ON_ERROR);
471470

472-
create_base_index();
471+
create_base_index(current_head);
473472
add_remove_files(&partial);
474473
refresh_cache(REFRESH_QUIET);
475474

@@ -518,12 +517,9 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
518517
return s->commitable;
519518
}
520519

521-
static int is_a_merge(const unsigned char *sha1)
520+
static int is_a_merge(const struct commit *current_head)
522521
{
523-
struct commit *commit = lookup_commit(sha1);
524-
if (!commit || parse_commit(commit))
525-
die(_("could not parse HEAD commit"));
526-
return !!(commit->parents && commit->parents->next);
522+
return !!(current_head->parents && current_head->parents->next);
527523
}
528524

529525
static const char sign_off_header[] = "Signed-off-by: ";
@@ -627,6 +623,7 @@ static char *cut_ident_timestamp_part(char *string)
627623
}
628624

629625
static int prepare_to_commit(const char *index_file, const char *prefix,
626+
struct commit *current_head,
630627
struct wt_status *s,
631628
struct strbuf *author_ident)
632629
{
@@ -848,7 +845,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
848845
* empty due to conflict resolution, which the user should okay.
849846
*/
850847
if (!commitable && whence != FROM_MERGE && !allow_empty &&
851-
!(amend && is_a_merge(head_sha1))) {
848+
!(amend && is_a_merge(current_head))) {
852849
run_status(stdout, index_file, prefix, 0, s);
853850
if (amend)
854851
fputs(_(empty_amend_advice), stderr);
@@ -1006,6 +1003,7 @@ static const char *read_commit_message(const char *name)
10061003
static int parse_and_validate_options(int argc, const char *argv[],
10071004
const char * const usage[],
10081005
const char *prefix,
1006+
struct commit *current_head,
10091007
struct wt_status *s)
10101008
{
10111009
int f = 0;
@@ -1026,11 +1024,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
10261024
if (!use_editor)
10271025
setenv("GIT_EDITOR", ":", 1);
10281026

1029-
if (get_sha1("HEAD", head_sha1))
1030-
initial_commit = 1;
1031-
10321027
/* Sanity check options */
1033-
if (amend && initial_commit)
1028+
if (amend && !current_head)
10341029
die(_("You have nothing to amend."));
10351030
if (amend && whence != FROM_COMMIT)
10361031
die(_("You are in the middle of a %s -- cannot amend."), whence_s());
@@ -1102,12 +1097,12 @@ static int parse_and_validate_options(int argc, const char *argv[],
11021097
}
11031098

11041099
static int dry_run_commit(int argc, const char **argv, const char *prefix,
1105-
struct wt_status *s)
1100+
const struct commit *current_head, struct wt_status *s)
11061101
{
11071102
int commitable;
11081103
const char *index_file;
11091104

1110-
index_file = prepare_index(argc, argv, prefix, 1);
1105+
index_file = prepare_index(argc, argv, prefix, current_head, 1);
11111106
commitable = run_status(stdout, index_file, prefix, 0, s);
11121107
rollback_index_files();
11131108

@@ -1260,7 +1255,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
12601255
return 0;
12611256
}
12621257

1263-
static void print_summary(const char *prefix, const unsigned char *sha1)
1258+
static void print_summary(const char *prefix, const unsigned char *sha1,
1259+
int initial_commit)
12641260
{
12651261
struct rev_info rev;
12661262
struct commit *commit;
@@ -1382,12 +1378,13 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
13821378
struct strbuf author_ident = STRBUF_INIT;
13831379
const char *index_file, *reflog_msg;
13841380
char *nl, *p;
1385-
unsigned char commit_sha1[20];
1381+
unsigned char sha1[20];
13861382
struct ref_lock *ref_lock;
13871383
struct commit_list *parents = NULL, **pptr = &parents;
13881384
struct stat statbuf;
13891385
int allow_fast_forward = 1;
13901386
struct wt_status s;
1387+
struct commit *current_head = NULL;
13911388

13921389
if (argc == 2 && !strcmp(argv[1], "-h"))
13931390
usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1398,46 +1395,49 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
13981395

13991396
if (s.use_color == -1)
14001397
s.use_color = git_use_color_default;
1398+
if (get_sha1("HEAD", sha1))
1399+
current_head = NULL;
1400+
else {
1401+
current_head = lookup_commit(sha1);
1402+
if (!current_head || parse_commit(current_head))
1403+
die(_("could not parse HEAD commit"));
1404+
}
14011405
argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
1402-
prefix, &s);
1406+
prefix, current_head, &s);
14031407
if (dry_run) {
14041408
if (diff_use_color_default == -1)
14051409
diff_use_color_default = git_use_color_default;
1406-
return dry_run_commit(argc, argv, prefix, &s);
1410+
return dry_run_commit(argc, argv, prefix, current_head, &s);
14071411
}
1408-
index_file = prepare_index(argc, argv, prefix, 0);
1412+
index_file = prepare_index(argc, argv, prefix, current_head, 0);
14091413

14101414
/* Set up everything for writing the commit object. This includes
14111415
running hooks, writing the trees, and interacting with the user. */
1412-
if (!prepare_to_commit(index_file, prefix, &s, &author_ident)) {
1416+
if (!prepare_to_commit(index_file, prefix,
1417+
current_head, &s, &author_ident)) {
14131418
rollback_index_files();
14141419
return 1;
14151420
}
14161421

14171422
/* Determine parents */
14181423
reflog_msg = getenv("GIT_REFLOG_ACTION");
1419-
if (initial_commit) {
1424+
if (!current_head) {
14201425
if (!reflog_msg)
14211426
reflog_msg = "commit (initial)";
14221427
} else if (amend) {
14231428
struct commit_list *c;
1424-
struct commit *commit;
14251429

14261430
if (!reflog_msg)
14271431
reflog_msg = "commit (amend)";
1428-
commit = lookup_commit(head_sha1);
1429-
if (!commit || parse_commit(commit))
1430-
die(_("could not parse HEAD commit"));
1431-
1432-
for (c = commit->parents; c; c = c->next)
1432+
for (c = current_head->parents; c; c = c->next)
14331433
pptr = &commit_list_insert(c->item, pptr)->next;
14341434
} else if (whence == FROM_MERGE) {
14351435
struct strbuf m = STRBUF_INIT;
14361436
FILE *fp;
14371437

14381438
if (!reflog_msg)
14391439
reflog_msg = "commit (merge)";
1440-
pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
1440+
pptr = &commit_list_insert(current_head, pptr)->next;
14411441
fp = fopen(git_path("MERGE_HEAD"), "r");
14421442
if (fp == NULL)
14431443
die_errno(_("could not open '%s' for reading"),
@@ -1463,7 +1463,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14631463
reflog_msg = (whence == FROM_CHERRY_PICK)
14641464
? "commit (cherry-pick)"
14651465
: "commit";
1466-
pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
1466+
pptr = &commit_list_insert(current_head, pptr)->next;
14671467
}
14681468

14691469
/* Finally, get the commit message */
@@ -1489,15 +1489,17 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
14891489
exit(1);
14901490
}
14911491

1492-
if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1,
1492+
if (commit_tree(sb.buf, active_cache_tree->sha1, parents, sha1,
14931493
author_ident.buf)) {
14941494
rollback_index_files();
14951495
die(_("failed to write commit object"));
14961496
}
14971497
strbuf_release(&author_ident);
14981498

14991499
ref_lock = lock_any_ref_for_update("HEAD",
1500-
initial_commit ? NULL : head_sha1,
1500+
!current_head
1501+
? NULL
1502+
: current_head->object.sha1,
15011503
0);
15021504

15031505
nl = strchr(sb.buf, '\n');
@@ -1512,7 +1514,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15121514
rollback_index_files();
15131515
die(_("cannot lock HEAD ref"));
15141516
}
1515-
if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
1517+
if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
15161518
rollback_index_files();
15171519
die(_("cannot update HEAD ref"));
15181520
}
@@ -1534,13 +1536,14 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
15341536
struct notes_rewrite_cfg *cfg;
15351537
cfg = init_copy_notes_for_rewrite("amend");
15361538
if (cfg) {
1537-
copy_note_for_rewrite(cfg, head_sha1, commit_sha1);
1539+
/* we are amending, so current_head is not NULL */
1540+
copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
15381541
finish_copy_notes_for_rewrite(cfg);
15391542
}
1540-
run_rewrite_hook(head_sha1, commit_sha1);
1543+
run_rewrite_hook(current_head->object.sha1, sha1);
15411544
}
15421545
if (!quiet)
1543-
print_summary(prefix, commit_sha1);
1546+
print_summary(prefix, sha1, !current_head);
15441547

15451548
return 0;
15461549
}

0 commit comments

Comments
 (0)