commit: reduce use of redundant global variables
The file-scope global variable head_sha1[] was used to hold the object name of the current HEAD commit (unless we are about to make an initial commit). Also there is an independent "static int initial_commit". Fix all the functions on the call-chain that use these two variables to take a new "(const) struct commit *current_head" argument instead, and replace their uses, e.g. "if (initial_commit)" becomes "if (!current_head)" and a reference to "head_sha1" becomes "current_head->object.sha1". Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Aug 19, 2011 at 11:58 UTC
06bb643b72cc6c67d7c0ac9b8d923463bae68822
1 file changed
+45
-42
builtin/commit.c
+45
-42
@@ -62,8 +62,6 @@ N_("The previous cherry-pick is now empty, possibly due to conflict resolution.\
62
"\n"
63
"Otherwise, please use 'git reset'\n");
64
65
-static unsigned char head_sha1[20];
66
-
65
static const char *use_message_buffer;
66
static const char commit_editmsg[] = "COMMIT_EDITMSG";
67
static struct lock_file index_lock; /* real index */
@@ -102,7 +100,7 @@ static enum {
100
static char *cleanup_arg;
101
102
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;
104
static int show_ignored_in_status;
105
static const char *only_include_assumed;
106
static struct strbuf message;
@@ -294,13 +292,13 @@ static void add_remove_files(struct string_list *list)
292
}
293
}
294
297
-static void create_base_index(void)
295
+static void create_base_index(const struct commit *current_head)
296
{
297
struct tree *tree;
298
struct unpack_trees_options opts;
299
struct tree_desc t;
300
303
- if (initial_commit) {
301
+ if (!current_head) {
302
discard_cache();
303
return;
304
}
@@ -313,7 +311,7 @@ static void create_base_index(void)
311
opts.dst_index = &the_index;
312
313
opts.fn = oneway_merge;
316
- tree = parse_tree_indirect(head_sha1);
314
+ tree = parse_tree_indirect(current_head->object.sha1);
315
if (!tree)
316
die(_("failed to unpack HEAD tree object"));
317
parse_tree(tree);
@@ -332,7 +330,8 @@ static void refresh_cache_or_die(int refresh_flags)
330
die_resolve_conflict("commit");
331
}
332
335
-static char *prepare_index(int argc, const char **argv, const char *prefix, int is_status)
333
+static char *prepare_index(int argc, const char **argv, const char *prefix,
334
+ const struct commit *current_head, int is_status)
335
{
336
int fd;
337
struct string_list partial;
@@ -448,7 +447,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
447
448
memset(&partial, 0, sizeof(partial));
449
partial.strdup_strings = 1;
451
- if (list_paths(&partial, initial_commit ? NULL : "HEAD", prefix, pathspec))
450
+ if (list_paths(&partial, !current_head ? NULL : "HEAD", prefix, pathspec))
451
exit(1);
452
453
discard_cache();
@@ -467,7 +466,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
466
(uintmax_t) getpid()),
467
LOCK_DIE_ON_ERROR);
468
470
- create_base_index();
469
+ create_base_index(current_head);
470
add_remove_files(&partial);
471
refresh_cache(REFRESH_QUIET);
472
@@ -516,12 +515,9 @@ static int run_status(FILE *fp, const char *index_file, const char *prefix, int
515
return s->commitable;
516
}
517
519
-static int is_a_merge(const unsigned char *sha1)
518
+static int is_a_merge(const struct commit *current_head)
519
{
521
- struct commit *commit = lookup_commit(sha1);
522
- if (!commit || parse_commit(commit))
523
- die(_("could not parse HEAD commit"));
524
- return !!(commit->parents && commit->parents->next);
520
+ return !!(current_head->parents && current_head->parents->next);
521
}
522
523
static const char sign_off_header[] = "Signed-off-by: ";
@@ -625,6 +621,7 @@ static char *cut_ident_timestamp_part(char *string)
621
}
622
623
static int prepare_to_commit(const char *index_file, const char *prefix,
624
+ struct commit *current_head,
625
struct wt_status *s,
626
struct strbuf *author_ident)
627
{
@@ -846,7 +843,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
843
* empty due to conflict resolution, which the user should okay.
844
*/
845
if (!commitable && whence != FROM_MERGE && !allow_empty &&
849
- !(amend && is_a_merge(head_sha1))) {
846
+ !(amend && is_a_merge(current_head))) {
847
run_status(stdout, index_file, prefix, 0, s);
848
if (amend)
849
fputs(_(empty_amend_advice), stderr);
@@ -1004,6 +1001,7 @@ static const char *read_commit_message(const char *name)
1001
static int parse_and_validate_options(int argc, const char *argv[],
1002
const char * const usage[],
1003
const char *prefix,
1004
+ struct commit *current_head,
1005
struct wt_status *s)
1006
{
1007
int f = 0;
@@ -1024,11 +1022,8 @@ static int parse_and_validate_options(int argc, const char *argv[],
1022
if (!use_editor)
1023
setenv("GIT_EDITOR", ":", 1);
1024
1027
- if (get_sha1("HEAD", head_sha1))
1028
- initial_commit = 1;
1029
-
1025
/* Sanity check options */
1031
- if (amend && initial_commit)
1026
+ if (amend && !current_head)
1027
die(_("You have nothing to amend."));
1028
if (amend && whence != FROM_COMMIT)
1029
die(_("You are in the middle of a %s -- cannot amend."), whence_s());
@@ -1100,12 +1095,12 @@ static int parse_and_validate_options(int argc, const char *argv[],
1095
}
1096
1097
static int dry_run_commit(int argc, const char **argv, const char *prefix,
1103
- struct wt_status *s)
1098
+ const struct commit *current_head, struct wt_status *s)
1099
{
1100
int commitable;
1101
const char *index_file;
1102
1108
- index_file = prepare_index(argc, argv, prefix, 1);
1103
+ index_file = prepare_index(argc, argv, prefix, current_head, 1);
1104
commitable = run_status(stdout, index_file, prefix, 0, s);
1105
rollback_index_files();
1106
@@ -1258,7 +1253,8 @@ int cmd_status(int argc, const char **argv, const char *prefix)
1253
return 0;
1254
}
1255
1261
-static void print_summary(const char *prefix, const unsigned char *sha1)
1256
+static void print_summary(const char *prefix, const unsigned char *sha1,
1257
+ int initial_commit)
1258
{
1259
struct rev_info rev;
1260
struct commit *commit;
@@ -1380,12 +1376,13 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1376
struct strbuf author_ident = STRBUF_INIT;
1377
const char *index_file, *reflog_msg;
1378
char *nl, *p;
1383
- unsigned char commit_sha1[20];
1379
+ unsigned char sha1[20];
1380
struct ref_lock *ref_lock;
1381
struct commit_list *parents = NULL, **pptr = &parents;
1382
struct stat statbuf;
1383
int allow_fast_forward = 1;
1384
struct wt_status s;
1385
+ struct commit *current_head = NULL;
1386
1387
if (argc == 2 && !strcmp(argv[1], "-h"))
1388
usage_with_options(builtin_commit_usage, builtin_commit_options);
@@ -1396,38 +1393,41 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1393
1394
if (s.use_color == -1)
1395
s.use_color = git_use_color_default;
1396
+ if (get_sha1("HEAD", sha1))
1397
+ current_head = NULL;
1398
+ else {
1399
+ current_head = lookup_commit(sha1);
1400
+ if (!current_head || parse_commit(current_head))
1401
+ die(_("could not parse HEAD commit"));
1402
+ }
1403
argc = parse_and_validate_options(argc, argv, builtin_commit_usage,
1400
- prefix, &s);
1404
+ prefix, current_head, &s);
1405
if (dry_run) {
1406
if (diff_use_color_default == -1)
1407
diff_use_color_default = git_use_color_default;
1404
- return dry_run_commit(argc, argv, prefix, &s);
1408
+ return dry_run_commit(argc, argv, prefix, current_head, &s);
1409
}
1406
- index_file = prepare_index(argc, argv, prefix, 0);
1410
+ index_file = prepare_index(argc, argv, prefix, current_head, 0);
1411
1412
/* Set up everything for writing the commit object. This includes
1413
running hooks, writing the trees, and interacting with the user. */
1410
- if (!prepare_to_commit(index_file, prefix, &s, &author_ident)) {
1414
+ if (!prepare_to_commit(index_file, prefix,
1415
+ current_head, &s, &author_ident)) {
1416
rollback_index_files();
1417
return 1;
1418
}
1419
1420
/* Determine parents */
1421
reflog_msg = getenv("GIT_REFLOG_ACTION");
1417
- if (initial_commit) {
1422
+ if (!current_head) {
1423
if (!reflog_msg)
1424
reflog_msg = "commit (initial)";
1425
} else if (amend) {
1426
struct commit_list *c;
1422
- struct commit *commit;
1427
1428
if (!reflog_msg)
1429
reflog_msg = "commit (amend)";
1426
- commit = lookup_commit(head_sha1);
1427
- if (!commit || parse_commit(commit))
1428
- die(_("could not parse HEAD commit"));
1429
-
1430
- for (c = commit->parents; c; c = c->next)
1430
+ for (c = current_head->parents; c; c = c->next)
1431
pptr = &commit_list_insert(c->item, pptr)->next;
1432
} else if (whence == FROM_MERGE) {
1433
struct strbuf m = STRBUF_INIT;
@@ -1435,7 +1435,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1435
1436
if (!reflog_msg)
1437
reflog_msg = "commit (merge)";
1438
- pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
1438
+ pptr = &commit_list_insert(current_head, pptr)->next;
1439
fp = fopen(git_path("MERGE_HEAD"), "r");
1440
if (fp == NULL)
1441
die_errno(_("could not open '%s' for reading"),
@@ -1461,7 +1461,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1461
reflog_msg = (whence == FROM_CHERRY_PICK)
1462
? "commit (cherry-pick)"
1463
: "commit";
1464
- pptr = &commit_list_insert(lookup_commit(head_sha1), pptr)->next;
1464
+ pptr = &commit_list_insert(current_head, pptr)->next;
1465
}
1466
1467
/* Finally, get the commit message */
@@ -1487,7 +1487,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1487
exit(1);
1488
}
1489
1490
- if (commit_tree(sb.buf, active_cache_tree->sha1, parents, commit_sha1,
1490
+ if (commit_tree(sb.buf, active_cache_tree->sha1, parents, sha1,
1491
author_ident.buf)) {
1492
rollback_index_files();
1493
die(_("failed to write commit object"));
@@ -1495,7 +1495,9 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1495
strbuf_release(&author_ident);
1496
1497
ref_lock = lock_any_ref_for_update("HEAD",
1498
- initial_commit ? NULL : head_sha1,
1498
+ !current_head
1499
+ ? NULL
1500
+ : current_head->object.sha1,
1501
0);
1502
1503
nl = strchr(sb.buf, '\n');
@@ -1510,7 +1512,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1512
rollback_index_files();
1513
die(_("cannot lock HEAD ref"));
1514
}
1513
- if (write_ref_sha1(ref_lock, commit_sha1, sb.buf) < 0) {
1515
+ if (write_ref_sha1(ref_lock, sha1, sb.buf) < 0) {
1516
rollback_index_files();
1517
die(_("cannot update HEAD ref"));
1518
}
@@ -1532,13 +1534,14 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
1534
struct notes_rewrite_cfg *cfg;
1535
cfg = init_copy_notes_for_rewrite("amend");
1536
if (cfg) {
1535
- copy_note_for_rewrite(cfg, head_sha1, commit_sha1);
1537
+ /* we are amending, so current_head is not NULL */
1538
+ copy_note_for_rewrite(cfg, current_head->object.sha1, sha1);
1539
finish_copy_notes_for_rewrite(cfg);
1540
}
1538
- run_rewrite_hook(head_sha1, commit_sha1);
1541
+ run_rewrite_hook(current_head->object.sha1, sha1);
1542
}
1543
if (!quiet)
1541
- print_summary(prefix, commit_sha1);
1544
+ print_summary(prefix, sha1, !current_head);
1545
1546
return 0;
1547
}