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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Sep 17, 2011 at 21:57 UTC 894642f68d3160db9116ca350da83c4a78cff8f2
1 file changed +53 -44
builtin/merge.c
+53 -44
@@ -50,7 +50,6 @@ static int fast_forward_only;
50 static int allow_trivial = 1, have_message;
51 static struct strbuf merge_msg;
52 static struct commit_list *remoteheads;
53 -static unsigned char head[20];
53 static struct strategy **use_strategies;
54 static size_t use_strategies_nr, use_strategies_alloc;
55 static const char **xopts;
@@ -279,7 +278,8 @@ static void reset_hard(unsigned const char *sha1, int verbose)
278 die(_("read-tree failed"));
279 }
280
282 -static void restore_state(const unsigned char *stash)
281 +static void restore_state(const unsigned char *head,
282 + const unsigned char *stash)
283 {
284 struct strbuf sb = STRBUF_INIT;
285 const char *args[] = { "stash", "apply", NULL, NULL };
@@ -309,10 +309,9 @@ static void finish_up_to_date(const char *msg)
309 drop_save();
310 }
311
312 -static void squash_message(void)
312 +static void squash_message(struct commit *commit)
313 {
314 struct rev_info rev;
315 - struct commit *commit;
315 struct strbuf out = STRBUF_INIT;
316 struct commit_list *j;
317 int fd;
@@ -327,7 +326,6 @@ static void squash_message(void)
326 rev.ignore_merges = 1;
327 rev.commit_format = CMIT_FMT_MEDIUM;
328
330 - commit = lookup_commit(head);
329 commit->object.flags |= UNINTERESTING;
330 add_pending_object(&rev, &commit->object, NULL);
331
@@ -356,9 +354,11 @@ static void squash_message(void)
354 strbuf_release(&out);
355 }
356
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)
359 {
360 struct strbuf reflog_message = STRBUF_INIT;
361 + const unsigned char *head = head_commit->object.sha1;
362
363 if (!msg)
364 strbuf_addstr(&reflog_message, getenv("GIT_REFLOG_ACTION"));
@@ -369,7 +369,7 @@ static void finish(const unsigned char *new_head, const char *msg)
369 getenv("GIT_REFLOG_ACTION"), msg);
370 }
371 if (squash) {
372 - squash_message();
372 + squash_message(head_commit);
373 } else {
374 if (verbosity >= 0 && !merge_msg.len)
375 printf(_("No merge message -- not updating HEAD\n"));
@@ -664,7 +664,7 @@ int try_merge_command(const char *strategy, size_t xopts_nr,
664 }
665
666 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)
668 {
669 int index_fd;
670 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,
710 commit_list_insert(j->item, &reversed);
711
712 index_fd = hold_locked_index(lock, 1);
713 - clean = merge_recursive(&o, lookup_commit(head),
713 + clean = merge_recursive(&o, head,
714 remoteheads->item, reversed, &result);
715 if (active_cache_changed &&
716 (write_cache(index_fd, active_cache, active_nr) ||
@@ -861,25 +861,26 @@ static void run_prepare_commit_msg(void)
861 read_merge_msg();
862 }
863
864 -static int merge_trivial(void)
864 +static int merge_trivial(struct commit *head)
865 {
866 unsigned char result_tree[20], result_commit[20];
867 struct commit_list *parent = xmalloc(sizeof(*parent));
868
869 write_tree_trivial(result_tree);
870 printf(_("Wonderful.\n"));
871 - parent->item = lookup_commit(head);
871 + parent->item = head;
872 parent->next = xmalloc(sizeof(*parent->next));
873 parent->next->item = remoteheads->item;
874 parent->next->next = NULL;
875 run_prepare_commit_msg();
876 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");
878 drop_save();
879 return 0;
880 }
881
882 -static int finish_automerge(struct commit_list *common,
882 +static int finish_automerge(struct commit *head,
883 + struct commit_list *common,
884 unsigned char *result_tree,
885 const char *wt_strategy)
886 {
@@ -890,12 +891,12 @@ static int finish_automerge(struct commit_list *common,
891 free_commit_list(common);
892 if (allow_fast_forward) {
893 parents = remoteheads;
893 - commit_list_insert(lookup_commit(head), &parents);
894 + commit_list_insert(head, &parents);
895 parents = reduce_heads(parents);
896 } else {
897 struct commit_list **pptr = &parents;
898
898 - pptr = &commit_list_insert(lookup_commit(head),
899 + pptr = &commit_list_insert(head,
900 pptr)->next;
901 for (j = remoteheads; j; j = j->next)
902 pptr = &commit_list_insert(j->item, pptr)->next;
@@ -905,7 +906,7 @@ static int finish_automerge(struct commit_list *common,
906 run_prepare_commit_msg();
907 commit_tree(merge_msg.buf, result_tree, parents, result_commit, NULL);
908 strbuf_addf(&buf, "Merge made by %s.", wt_strategy);
908 - finish(result_commit, buf.buf);
909 + finish(head, result_commit, buf.buf);
910 strbuf_release(&buf);
911 drop_save();
912 return 0;
@@ -939,7 +940,8 @@ static int suggest_conflicts(int renormalizing)
940 return 1;
941 }
942
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)
945 {
946 struct commit *second_token = NULL;
947 if (argc > 2) {
@@ -1012,9 +1014,11 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1014 {
1015 unsigned char result_tree[20];
1016 unsigned char stash[20];
1017 + unsigned char head_sha1[20];
1018 + struct commit *head_commit;
1019 struct strbuf buf = STRBUF_INIT;
1020 const char *head_arg;
1017 - int flag, head_invalid = 0, i;
1021 + int flag, i;
1022 int best_cnt = -1, merge_was_ok = 0, automerge_was_ok = 0;
1023 struct commit_list *common = NULL;
1024 const char *best_strategy = NULL, *wt_strategy = NULL;
@@ -1027,11 +1031,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1031 * Check if we are _not_ on a detached HEAD, i.e. if there is a
1032 * current branch.
1033 */
1030 - branch = resolve_ref("HEAD", head, 0, &flag);
1034 + branch = resolve_ref("HEAD", head_sha1, 0, &flag);
1035 if (branch && !prefixcmp(branch, "refs/heads/"))
1036 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 + }
1044
1045 git_config(git_merge_config, NULL);
1046
@@ -1112,12 +1121,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1121 * additional safety measure to check for it.
1122 */
1123
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)) {
1126 strbuf_addstr(&merge_msg, argv[0]);
1127 head_arg = argv[1];
1128 argv += 2;
1129 argc -= 2;
1120 - } else if (head_invalid) {
1130 + } else if (!head_commit) {
1131 struct object *remote_head;
1132 /*
1133 * 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)
1174 }
1175 }
1176
1167 - if (head_invalid || !argc)
1177 + if (!head_commit || !argc)
1178 usage_with_options(builtin_merge_usage,
1179 builtin_merge_options);
1180
@@ -1205,17 +1215,16 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1215 }
1216
1217 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);
1219 else {
1220 struct commit_list *list = remoteheads;
1212 - commit_list_insert(lookup_commit(head), &list);
1221 + commit_list_insert(head_commit, &list);
1222 common = get_octopus_merge_bases(list);
1223 free(list);
1224 }
1225
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);
1228
1229 if (!common)
1230 ; /* No common ancestors found. We need a real merge. */
@@ -1229,13 +1238,13 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1238 return 0;
1239 } else if (allow_fast_forward && !remoteheads->next &&
1240 !common->next &&
1232 - !hashcmp(common->item->object.sha1, head)) {
1241 + !hashcmp(common->item->object.sha1, head_commit->object.sha1)) {
1242 /* Again the most common case of merging one remote. */
1243 struct strbuf msg = STRBUF_INIT;
1244 struct object *o;
1245 char hex[41];
1246
1238 - strcpy(hex, find_unique_abbrev(head, DEFAULT_ABBREV));
1247 + strcpy(hex, find_unique_abbrev(head_commit->object.sha1, DEFAULT_ABBREV));
1248
1249 if (verbosity >= 0)
1250 printf(_("Updating %s..%s\n"),
@@ -1251,10 +1260,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1260 if (!o)
1261 return 1;
1262
1254 - if (checkout_fast_forward(head, remoteheads->item->object.sha1))
1263 + if (checkout_fast_forward(head_commit->object.sha1, remoteheads->item->object.sha1))
1264 return 1;
1265
1257 - finish(o->sha1, msg.buf);
1266 + finish(head_commit, o->sha1, msg.buf);
1267 drop_save();
1268 return 0;
1269 } else if (!remoteheads->next && common->next)
@@ -1274,8 +1283,8 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1283 git_committer_info(IDENT_ERROR_ON_NO_NAME);
1284 printf(_("Trying really trivial in-index merge...\n"));
1285 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);
1288 printf(_("Nope.\n"));
1289 }
1290 } else {
@@ -1294,8 +1303,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1303 * merge_bases again, otherwise "git merge HEAD^
1304 * HEAD^^" would be missed.
1305 */
1297 - common_one = get_merge_bases(lookup_commit(head),
1298 - j->item, 1);
1306 + common_one = get_merge_bases(head_commit, j->item, 1);
1307 if (hashcmp(common_one->item->object.sha1,
1308 j->item->object.sha1)) {
1309 up_to_date = 0;
@@ -1333,7 +1341,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1341 int ret;
1342 if (i) {
1343 printf(_("Rewinding the tree to pristine...\n"));
1336 - restore_state(stash);
1344 + restore_state(head_commit->object.sha1, stash);
1345 }
1346 if (use_strategies_nr != 1)
1347 printf(_("Trying merge strategy %s...\n"),
@@ -1345,7 +1353,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1353 wt_strategy = use_strategies[i]->name;
1354
1355 ret = try_merge_strategy(use_strategies[i]->name,
1348 - common, head_arg);
1356 + common, head_commit, head_arg);
1357 if (!option_commit && !ret) {
1358 merge_was_ok = 1;
1359 /*
@@ -1387,14 +1395,15 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1395 * auto resolved the merge cleanly.
1396 */
1397 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);
1400
1401 /*
1402 * Pick the result from the best strategy and have the user fix
1403 * it up.
1404 */
1405 if (!best_strategy) {
1397 - restore_state(stash);
1406 + restore_state(head_commit->object.sha1, stash);
1407 if (use_strategies_nr > 1)
1408 fprintf(stderr,
1409 _("No merge strategy handled the merge.\n"));
@@ -1406,14 +1415,14 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
1415 ; /* We already have its result in the working tree. */
1416 else {
1417 printf(_("Rewinding the tree to pristine...\n"));
1409 - restore_state(stash);
1418 + restore_state(head_commit->object.sha1, stash);
1419 printf(_("Using the %s to prepare resolving by hand.\n"),
1420 best_strategy);
1412 - try_merge_strategy(best_strategy, common, head_arg);
1421 + try_merge_strategy(best_strategy, common, head_commit, head_arg);
1422 }
1423
1424 if (squash)
1416 - finish(NULL, NULL);
1425 + finish(head_commit, NULL, NULL);
1426 else {
1427 int fd;
1428 struct commit_list *j;