Skip to content

Commit 894642f

Browse files
pcloudsgitster
authored andcommitted
merge: remove global variable head[]
Also kill head_invalid in favor of "head_commit == NULL". Local variable "head" in cmd_merge() is renamed to "head_sha1" to make sure I don't miss any access because this variable should not be used after head_commit is set (use head_commit->object.sha1 instead). Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 10b98fa commit 894642f

File tree

1 file changed

+53
-44
lines changed

1 file changed

+53
-44
lines changed

builtin/merge.c

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ static int fast_forward_only;
5050
static int allow_trivial = 1, have_message;
5151
static struct strbuf merge_msg;
5252
static struct commit_list *remoteheads;
53-
static unsigned char head[20];
5453
static struct strategy **use_strategies;
5554
static size_t use_strategies_nr, use_strategies_alloc;
5655
static const char **xopts;
@@ -279,7 +278,8 @@ static void reset_hard(unsigned const char *sha1, int verbose)
279278
die(_("read-tree failed"));
280279
}
281280

282-
static void restore_state(const unsigned char *stash)
281+
static void restore_state(const unsigned char *head,
282+
const unsigned char *stash)
283283
{
284284
struct strbuf sb = STRBUF_INIT;
285285
const char *args[] = { "stash", "apply", NULL, NULL };
@@ -309,10 +309,9 @@ static void finish_up_to_date(const char *msg)
309309
drop_save();
310310
}
311311

312-
static void squash_message(void)
312+
static void squash_message(struct commit *commit)
313313
{
314314
struct rev_info rev;
315-
struct commit *commit;
316315
struct strbuf out = STRBUF_INIT;
317316
struct commit_list *j;
318317
int fd;
@@ -327,7 +326,6 @@ static void squash_message(void)
327326
rev.ignore_merges = 1;
328327
rev.commit_format = CMIT_FMT_MEDIUM;
329328

330-
commit = lookup_commit(head);
331329
commit->object.flags |= UNINTERESTING;
332330
add_pending_object(&rev, &commit->object, NULL);
333331

@@ -356,9 +354,11 @@ static void squash_message(void)
356354
strbuf_release(&out);
357355
}
358356

359-
static void finish(const unsigned char *new_head, const char *msg)
357+
static void finish(struct commit *head_commit,
358+
const unsigned char *new_head, const char *msg)
360359
{
361360
struct strbuf reflog_message = STRBUF_INIT;
361+
const unsigned char *head = head_commit->object.sha1;
362362

363363
if (!msg)
364364
strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
@@ -369,7 +369,7 @@ static void finish(const unsigned char *new_head, const char *msg)
369369
getenv("GIT_REFLOG_ACTION"), msg);
370370
}
371371
if (squash) {
372-
squash_message();
372+
squash_message(head_commit);
373373
} else {
374374
if (verbosity >= 0 && !merge_msg.len)
375375
printf(_("No merge message -- not updating HEAD\n"));
@@ -664,7 +664,7 @@ int try_merge_command(const char *strategy, size_t xopts_nr,
664664
}
665665

666666
static int try_merge_strategy(const char *strategy, struct commit_list *common,
667-
const char *head_arg)
667+
struct commit *head, const char *head_arg)
668668
{
669669
int index_fd;
670670
struct lock_file *lock = xcalloc(1, sizeof(struct lock_file));
@@ -710,7 +710,7 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,
710710
commit_list_insert(j->item, &reversed);
711711

712712
index_fd = hold_locked_index(lock, 1);
713-
clean = merge_recursive(&o, lookup_commit(head),
713+
clean = merge_recursive(&o, head,
714714
remoteheads->item, reversed, &result);
715715
if (active_cache_changed &&
716716
(write_cache(index_fd, active_cache, active_nr) ||
@@ -861,25 +861,26 @@ static void run_prepare_commit_msg(void)
861861
read_merge_msg();
862862
}
863863

864-
static int merge_trivial(void)
864+
static int merge_trivial(struct commit *head)
865865
{
866866
unsigned char result_tree[20], result_commit[20];
867867
struct commit_list *parent = xmalloc(sizeof(*parent));
868868

869869
write_tree_trivial(result_tree);
870870
printf(_("Wonderful.\n"));
871-
parent->item = lookup_commit(head);
871+
parent->item = head;
872872
parent->next = xmalloc(sizeof(*parent->next));
873873
parent->next->item = remoteheads->item;
874874
parent->next->next = NULL;
875875
run_prepare_commit_msg();
876876
commit_tree(merge_msg.buf, result_tree, parent, result_commit, NULL);
877-
finish(result_commit, "In-index merge");
877+
finish(head, result_commit, "In-index merge");
878878
drop_save();
879879
return 0;
880880
}
881881

882-
static int finish_automerge(struct commit_list *common,
882+
static int finish_automerge(struct commit *head,
883+
struct commit_list *common,
883884
unsigned char *result_tree,
884885
const char *wt_strategy)
885886
{
@@ -890,12 +891,12 @@ static int finish_automerge(struct commit_list *common,
890891
free_commit_list(common);
891892
if (allow_fast_forward) {
892893
parents = remoteheads;
893-
commit_list_insert(lookup_commit(head), &parents);
894+
commit_list_insert(head, &parents);
894895
parents = reduce_heads(parents);
895896
} else {
896897
struct commit_list **pptr = &parents;
897898

898-
pptr = &commit_list_insert(lookup_commit(head),
899+
pptr = &commit_list_insert(head,
899900
pptr)->next;
900901
for (j = remoteheads; j; j = j->next)
901902
pptr = &commit_list_insert(j->item, pptr)->next;
@@ -905,7 +906,7 @@ static int finish_automerge(struct commit_list *common,
905906
run_prepare_commit_msg();
906907
commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
907908
strbuf_addf(&buf, "Merge made by %s.", wt_strategy);
908-
finish(result_commit, buf.buf);
909+
finish(head, result_commit, buf.buf);
909910
strbuf_release(&buf);
910911
drop_save();
911912
return 0;
@@ -939,7 +940,8 @@ static int suggest_conflicts(int renormalizing)
939940
return 1;
940941
}
941942

942-
static struct commit *is_old_style_invocation(int argc, const char **argv)
943+
static struct commit *is_old_style_invocation(int argc, const char **argv,
944+
const unsigned char *head)
943945
{
944946
struct commit *second_token = NULL;
945947
if (argc > 2) {
@@ -1012,9 +1014,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
10121014
{
10131015
unsigned char result_tree[20];
10141016
unsigned char stash[20];
1017+
unsigned char head_sha1[20];
1018+
struct commit *head_commit;
10151019
struct strbuf buf = STRBUF_INIT;
10161020
const char *head_arg;
1017-
int flag, head_invalid = 0, i;
1021+
int flag, i;
10181022
int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
10191023
struct commit_list *common = NULL;
10201024
const char *best_strategy = NULL, *wt_strategy = NULL;
@@ -1027,11 +1031,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
10271031
* Check if we are _not_ on a detached HEAD, i.e. if there is a
10281032
* current branch.
10291033
*/
1030-
branch = resolve_ref("HEAD", head, 0, &flag);
1034+
branch = resolve_ref("HEAD", head_sha1, 0, &flag);
10311035
if (branch && !prefixcmp(branch, "refs/heads/"))
10321036
branch += 11;
1033-
if (!branch || is_null_sha1(head))
1034-
head_invalid = 1;
1037+
if (!branch || is_null_sha1(head_sha1))
1038+
head_commit = NULL;
1039+
else {
1040+
head_commit = lookup_commit(head_sha1);
1041+
if (!head_commit)
1042+
die(_("could not parse HEAD"));
1043+
}
10351044

10361045
git_config(git_merge_config, NULL);
10371046

@@ -1112,12 +1121,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11121121
* additional safety measure to check for it.
11131122
*/
11141123

1115-
if (!have_message && is_old_style_invocation(argc, argv)) {
1124+
if (!have_message && head_commit &&
1125+
is_old_style_invocation(argc, argv, head_commit->object.sha1)) {
11161126
strbuf_addstr(&merge_msg, argv[0]);
11171127
head_arg = argv[1];
11181128
argv += 2;
11191129
argc -= 2;
1120-
} else if (head_invalid) {
1130+
} else if (!head_commit) {
11211131
struct object *remote_head;
11221132
/*
11231133
* If the merged head is a valid one there is no reason
@@ -1164,7 +1174,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
11641174
}
11651175
}
11661176

1167-
if (head_invalid || !argc)
1177+
if (!head_commit || !argc)
11681178
usage_with_options(builtin_merge_usage,
11691179
builtin_merge_options);
11701180

@@ -1205,17 +1215,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12051215
}
12061216

12071217
if (!remoteheads->next)
1208-
common = get_merge_bases(lookup_commit(head),
1209-
remoteheads->item, 1);
1218+
common = get_merge_bases(head_commit, remoteheads->item, 1);
12101219
else {
12111220
struct commit_list *list = remoteheads;
1212-
commit_list_insert(lookup_commit(head), &list);
1221+
commit_list_insert(head_commit, &list);
12131222
common = get_octopus_merge_bases(list);
12141223
free(list);
12151224
}
12161225

1217-
update_ref("updating ORIG_HEAD", "ORIG_HEAD", head, NULL, 0,
1218-
DIE_ON_ERR);
1226+
update_ref("updating ORIG_HEAD", "ORIG_HEAD", head_commit->object.sha1,
1227+
NULL, 0, DIE_ON_ERR);
12191228

12201229
if (!common)
12211230
; /* No common ancestors found. We need a real merge. */
@@ -1229,13 +1238,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12291238
return 0;
12301239
} else if (allow_fast_forward && !remoteheads->next &&
12311240
!common->next &&
1232-
!hashcmp(common->item->object.sha1, head)) {
1241+
!hashcmp(common->item->object.sha1, head_commit->object.sha1)) {
12331242
/* Again the most common case of merging one remote. */
12341243
struct strbuf msg = STRBUF_INIT;
12351244
struct object *o;
12361245
char hex[41];
12371246

1238-
strcpy(hex, find_unique_abbrev(head, DEFAULT_ABBREV));
1247+
strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV));
12391248

12401249
if (verbosity >= 0)
12411250
printf(_("Updating %s..%s\n"),
@@ -1251,10 +1260,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12511260
if (!o)
12521261
return 1;
12531262

1254-
if (checkout_fast_forward(head, remoteheads->item->object.sha1))
1263+
if (checkout_fast_forward(head_commit->object.sha1, remoteheads->item->object.sha1))
12551264
return 1;
12561265

1257-
finish(o->sha1, msg.buf);
1266+
finish(head_commit, o->sha1, msg.buf);
12581267
drop_save();
12591268
return 0;
12601269
} else if (!remoteheads->next && common->next)
@@ -1274,8 +1283,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12741283
git_committer_info(IDENT_ERROR_ON_NO_NAME);
12751284
printf(_("Trying really trivial in-index merge...\n"));
12761285
if (!read_tree_trivial(common->item->object.sha1,
1277-
head, remoteheads->item->object.sha1))
1278-
return merge_trivial();
1286+
head_commit->object.sha1, remoteheads->item->object.sha1))
1287+
return merge_trivial(head_commit);
12791288
printf(_("Nope.\n"));
12801289
}
12811290
} else {
@@ -1294,8 +1303,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
12941303
* merge_bases again, otherwise "git merge HEAD^
12951304
* HEAD^^" would be missed.
12961305
*/
1297-
common_one = get_merge_bases(lookup_commit(head),
1298-
j->item, 1);
1306+
common_one = get_merge_bases(head_commit, j->item, 1);
12991307
if (hashcmp(common_one->item->object.sha1,
13001308
j->item->object.sha1)) {
13011309
up_to_date = 0;
@@ -1333,7 +1341,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13331341
int ret;
13341342
if (i) {
13351343
printf(_("Rewinding the tree to pristine...\n"));
1336-
restore_state(stash);
1344+
restore_state(head_commit->object.sha1, stash);
13371345
}
13381346
if (use_strategies_nr != 1)
13391347
printf(_("Trying merge strategy %s...\n"),
@@ -1345,7 +1353,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13451353
wt_strategy = use_strategies[i]->name;
13461354

13471355
ret = try_merge_strategy(use_strategies[i]->name,
1348-
common, head_arg);
1356+
common, head_commit, head_arg);
13491357
if (!option_commit && !ret) {
13501358
merge_was_ok = 1;
13511359
/*
@@ -1387,14 +1395,15 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
13871395
* auto resolved the merge cleanly.
13881396
*/
13891397
if (automerge_was_ok)
1390-
return finish_automerge(common, result_tree, wt_strategy);
1398+
return finish_automerge(head_commit, common, result_tree,
1399+
wt_strategy);
13911400

13921401
/*
13931402
* Pick the result from the best strategy and have the user fix
13941403
* it up.
13951404
*/
13961405
if (!best_strategy) {
1397-
restore_state(stash);
1406+
restore_state(head_commit->object.sha1, stash);
13981407
if (use_strategies_nr > 1)
13991408
fprintf(stderr,
14001409
_("No merge strategy handled the merge.\n"));
@@ -1406,14 +1415,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
14061415
; /* We already have its result in the working tree. */
14071416
else {
14081417
printf(_("Rewinding the tree to pristine...\n"));
1409-
restore_state(stash);
1418+
restore_state(head_commit->object.sha1, stash);
14101419
printf(_("Using the %s to prepare resolving by hand.\n"),
14111420
best_strategy);
1412-
try_merge_strategy(best_strategy, common, head_arg);
1421+
try_merge_strategy(best_strategy, common, head_commit, head_arg);
14131422
}
14141423

14151424
if (squash)
1416-
finish(NULL, NULL);
1425+
finish(head_commit, NULL, NULL);
14171426
else {
14181427
int fd;
14191428
struct commit_list *j;

0 commit comments

Comments
 (0)