merge-recursive: Fix spurious 'refusing to lose untracked file...' messages

Calling update_stages() before update_file() can sometimes result in git thinking the file being updated is untracked (whenever update_stages moves it to stage 3). Reverse the call order, and add a big comment to update_stages to hopefully prevent others from making the same mistake. 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 f53d39778cf41cc93aa140bb41bb7d3a6590e179
2 files changed +21 -15
merge-recursive.c
+20 -14
@@ -526,6 +526,15 @@ static int update_stages(const char *path, const struct diff_filespec *o,
526 const struct diff_filespec *a,
527 const struct diff_filespec *b)
528 {
529 +
530 + /*
531 + * NOTE: It is usually a bad idea to call update_stages on a path
532 + * before calling update_file on that same path, since it can
533 + * sometimes lead to spurious "refusing to lose untracked file..."
534 + * messages from update_file (via make_room_for path via
535 + * would_lose_untracked). Instead, reverse the order of the calls
536 + * (executing update_file first and then update_stages).
537 + */
538 int clear = 1;
539 int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
540 if (clear)
@@ -1039,7 +1048,6 @@ static void conflict_rename_delete(struct merge_options *o,
1048 {
1049 const struct diff_filespec *orig = pair->one;
1050 const struct diff_filespec *dest = pair->two;
1042 - const char *path;
1051 const unsigned char *a_sha = NULL;
1052 const unsigned char *b_sha = NULL;
1053 int a_mode = 0;
@@ -1053,22 +1061,21 @@ static void conflict_rename_delete(struct merge_options *o,
1061 b_mode = dest->mode;
1062 }
1063
1064 + handle_change_delete(o,
1065 + o->call_depth ? orig->path : dest->path,
1066 + orig->sha1, orig->mode,
1067 + a_sha, a_mode,
1068 + b_sha, b_mode,
1069 + "rename", "renamed");
1070 +
1071 if (o->call_depth) {
1072 remove_file_from_cache(dest->path);
1058 - path = orig->path;
1073 } else {
1060 - path = dest->path;
1074 update_stages(dest->path, NULL,
1075 rename_branch == o->branch1 ? dest : NULL,
1076 rename_branch == o->branch1 ? NULL : dest);
1077 }
1078
1066 - handle_change_delete(o,
1067 - path,
1068 - orig->sha1, orig->mode,
1069 - a_sha, a_mode,
1070 - b_sha, b_mode,
1071 - "rename", "renamed");
1079 }
1080
1081 static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
@@ -1106,11 +1113,6 @@ static void handle_file(struct merge_options *o,
1113 }
1114
1115 add = filespec_from_entry(&other, dst_entry, stage ^ 1);
1109 - if (stage == 2)
1110 - update_stages(rename->path, NULL, rename, add);
1111 - else
1112 - update_stages(rename->path, NULL, add, rename);
1113 -
1116 if (add) {
1117 char *add_name = unique_path(o, rename->path, other_branch);
1118 update_file(o, 0, add->sha1, add->mode, add_name);
@@ -1125,6 +1127,10 @@ static void handle_file(struct merge_options *o,
1127 }
1128 }
1129 update_file(o, 0, rename->sha1, rename->mode, dst_name);
1130 + if (stage == 2)
1131 + update_stages(rename->path, NULL, rename, add);
1132 + else
1133 + update_stages(rename->path, NULL, add, rename);
1134
1135 if (dst_name != rename->path)
1136 free(dst_name);
t/t6022-merge-rename.sh
+1 -1
@@ -787,7 +787,7 @@ test_expect_success 'setup spurious "refusing to lose untracked" message' '
787 git commit -mC
788 '
789
790 -test_expect_failure 'no spurious "refusing to lose untracked" message' '
790 +test_expect_success 'no spurious "refusing to lose untracked" message' '
791 git checkout master^0 &&
792 test_must_fail git merge rename^0 2>errors.txt &&
793 ! grep "refusing to lose untracked file" errors.txt