merge-recursive: Make modify/delete handling code reusable

modify/delete and rename/delete share a lot of similarities; we'd like all the criss-cross and D/F conflict handling specializations to be shared between the two. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Aug 11, 2011 at 23:20 UTC b70332520d2ba0f32b5006feff86d71242061011
2 files changed +50 -36
merge-recursive.c
+48 -34
@@ -992,6 +992,46 @@ static struct merge_file_info merge_file(struct merge_options *o,
992 return merge_file_1(o, &one, &a, &b, branch1, branch2);
993 }
994
995 +static void handle_change_delete(struct merge_options *o,
996 + const char *path,
997 + const unsigned char *o_sha, int o_mode,
998 + const unsigned char *a_sha, int a_mode,
999 + const unsigned char *b_sha, int b_mode,
1000 + const char *change, const char *change_past)
1001 +{
1002 + char *renamed = NULL;
1003 + if (dir_in_way(path, !o->call_depth)) {
1004 + renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
1005 + }
1006 +
1007 + if (o->call_depth) {
1008 + /*
1009 + * We cannot arbitrarily accept either a_sha or b_sha as
1010 + * correct; since there is no true "middle point" between
1011 + * them, simply reuse the base version for virtual merge base.
1012 + */
1013 + remove_file_from_cache(path);
1014 + update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
1015 + } else if (!a_sha) {
1016 + output(o, 1, "CONFLICT (%s/delete): %s deleted in %s "
1017 + "and %s in %s. Version %s of %s left in tree%s%s.",
1018 + change, path, o->branch1,
1019 + change_past, o->branch2, o->branch2, path,
1020 + NULL == renamed ? "" : " at ",
1021 + NULL == renamed ? "" : renamed);
1022 + update_file(o, 0, b_sha, b_mode, renamed ? renamed : path);
1023 + } else {
1024 + output(o, 1, "CONFLICT (%s/delete): %s deleted in %s "
1025 + "and %s in %s. Version %s of %s left in tree%s%s.",
1026 + change, path, o->branch2,
1027 + change_past, o->branch1, o->branch1, path,
1028 + NULL == renamed ? "" : " at ",
1029 + NULL == renamed ? "" : renamed);
1030 + update_file(o, 0, a_sha, a_mode, renamed ? renamed : path);
1031 + }
1032 + free(renamed);
1033 +}
1034 +
1035 static void conflict_rename_delete(struct merge_options *o,
1036 struct diff_filepair *pair,
1037 const char *rename_branch,
@@ -1409,44 +1449,18 @@ error_return:
1449 return ret;
1450 }
1451
1412 -static void handle_delete_modify(struct merge_options *o,
1452 +static void handle_modify_delete(struct merge_options *o,
1453 const char *path,
1454 unsigned char *o_sha, int o_mode,
1455 unsigned char *a_sha, int a_mode,
1456 unsigned char *b_sha, int b_mode)
1457 {
1418 - char *renamed = NULL;
1419 - if (dir_in_way(path, !o->call_depth)) {
1420 - renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
1421 - }
1422 -
1423 - if (o->call_depth) {
1424 - /*
1425 - * We cannot arbitrarily accept either a_sha or b_sha as
1426 - * correct; since there is no true "middle point" between
1427 - * them, simply reuse the base version for virtual merge base.
1428 - */
1429 - remove_file_from_cache(path);
1430 - update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
1431 - } else if (!a_sha) {
1432 - output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1433 - "and modified in %s. Version %s of %s left in tree%s%s.",
1434 - path, o->branch1,
1435 - o->branch2, o->branch2, path,
1436 - NULL == renamed ? "" : " at ",
1437 - NULL == renamed ? "" : renamed);
1438 - update_file(o, 0, b_sha, b_mode, renamed ? renamed : path);
1439 - } else {
1440 - output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
1441 - "and modified in %s. Version %s of %s left in tree%s%s.",
1442 - path, o->branch2,
1443 - o->branch1, o->branch1, path,
1444 - NULL == renamed ? "" : " at ",
1445 - NULL == renamed ? "" : renamed);
1446 - update_file(o, 0, a_sha, a_mode, renamed ? renamed : path);
1447 - }
1448 - free(renamed);
1449 -
1458 + handle_change_delete(o,
1459 + path,
1460 + o_sha, o_mode,
1461 + a_sha, a_mode,
1462 + b_sha, b_mode,
1463 + "modify", "modified");
1464 }
1465
1466 static int merge_content(struct merge_options *o,
@@ -1612,7 +1626,7 @@ static int process_entry(struct merge_options *o,
1626 } else {
1627 /* Modify/delete; deleted side may have put a directory in the way */
1628 clean_merge = 0;
1615 - handle_delete_modify(o, path, o_sha, o_mode,
1629 + handle_modify_delete(o, path, o_sha, o_mode,
1630 a_sha, a_mode, b_sha, b_mode);
1631 }
1632 } else if ((!o_sha && a_sha && !b_sha) ||
t/t6022-merge-rename.sh
+2 -2
@@ -303,7 +303,7 @@ test_expect_success 'Rename+D/F conflict; renamed file merges but dir in way' '
303 git checkout -q renamed-file-has-no-conflicts^0 &&
304 test_must_fail git merge --strategy=recursive dir-in-way >output &&
305
306 - grep "CONFLICT (delete/modify): dir/file-in-the-way" output &&
306 + grep "CONFLICT (modify/delete): dir/file-in-the-way" output &&
307 grep "Auto-merging dir" output &&
308 grep "Adding as dir~HEAD instead" output &&
309
@@ -325,7 +325,7 @@ test_expect_success 'Same as previous, but merged other way' '
325 test_must_fail git merge --strategy=recursive renamed-file-has-no-conflicts >output 2>errors &&
326
327 ! grep "error: refusing to lose untracked file at" errors &&
328 - grep "CONFLICT (delete/modify): dir/file-in-the-way" output &&
328 + grep "CONFLICT (modify/delete): dir/file-in-the-way" output &&
329 grep "Auto-merging dir" output &&
330 grep "Adding as dir~renamed-file-has-no-conflicts instead" output &&
331