merge-recursive: Fix code checking for D/F conflicts still being present

Previously, we were using lstat() to determine if a directory was still present after a merge (and thus in the way of adding a file). We should have been using lstat() only to determine if untracked directories were in the way (and then only when necessary to check for untracked directories); we should instead using the index to determine if there is a tracked directory in the way. Create a new function to do this and use it to replace the existing checks for directories being in the way. 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:19 UTC f2507b4e0ef0b7fc8c1e75004e8a86a0430dc512
1 file changed +34 -15
merge-recursive.c
+34 -15
@@ -591,6 +591,30 @@ static void flush_buffer(int fd, const char *buf, unsigned long size)
591 }
592 }
593
594 +static int dir_in_way(const char *path, int check_working_copy)
595 +{
596 + int pos, pathlen = strlen(path);
597 + char *dirpath = xmalloc(pathlen + 2);
598 + struct stat st;
599 +
600 + strcpy(dirpath, path);
601 + dirpath[pathlen] = '/';
602 + dirpath[pathlen+1] = '\0';
603 +
604 + pos = cache_name_pos(dirpath, pathlen+1);
605 +
606 + if (pos < 0)
607 + pos = -1 - pos;
608 + if (pos < active_nr &&
609 + !strncmp(dirpath, active_cache[pos]->name, pathlen+1)) {
610 + free(dirpath);
611 + return 1;
612 + }
613 +
614 + free(dirpath);
615 + return check_working_copy && !lstat(path, &st) && S_ISDIR(st.st_mode);
616 +}
617 +
618 static int would_lose_untracked(const char *path)
619 {
620 int pos = cache_name_pos(path, strlen(path));
@@ -880,7 +904,6 @@ static void conflict_rename_delete(struct merge_options *o,
904 {
905 char *dest_name = pair->two->path;
906 int df_conflict = 0;
883 - struct stat st;
907
908 output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
909 "and deleted in %s",
@@ -890,7 +913,7 @@ static void conflict_rename_delete(struct merge_options *o,
913 update_stages(dest_name, NULL,
914 rename_branch == o->branch1 ? pair->two : NULL,
915 rename_branch == o->branch1 ? NULL : pair->two);
893 - if (lstat(dest_name, &st) == 0 && S_ISDIR(st.st_mode)) {
916 + if (dir_in_way(dest_name, !o->call_depth)) {
917 dest_name = unique_path(o, dest_name, rename_branch);
918 df_conflict = 1;
919 }
@@ -912,13 +935,12 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
935 const char *ren2_dst = pair2->two->path;
936 const char *dst_name1 = ren1_dst;
937 const char *dst_name2 = ren2_dst;
915 - struct stat st;
916 - if (lstat(ren1_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
938 + if (dir_in_way(ren1_dst, !o->call_depth)) {
939 dst_name1 = del[delp++] = unique_path(o, ren1_dst, branch1);
940 output(o, 1, "%s is a directory in %s adding as %s instead",
941 ren1_dst, branch2, dst_name1);
942 }
921 - if (lstat(ren2_dst, &st) == 0 && S_ISDIR(st.st_mode)) {
943 + if (dir_in_way(ren2_dst, !o->call_depth)) {
944 dst_name2 = del[delp++] = unique_path(o, ren2_dst, branch2);
945 output(o, 1, "%s is a directory in %s adding as %s instead",
946 ren2_dst, branch1, dst_name2);
@@ -1078,7 +1100,7 @@ static int process_renames(struct merge_options *o,
1100 try_merge = 0;
1101
1102 if (sha_eq(src_other.sha1, null_sha1)) {
1081 - if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
1103 + if (dir_in_way(ren1_dst, 0 /*check_wc*/)) {
1104 ren1->dst_entry->processed = 0;
1105 setup_rename_df_conflict_info(RENAME_DELETE,
1106 ren1->pair,
@@ -1157,7 +1179,7 @@ static int process_renames(struct merge_options *o,
1179 a = &src_other;
1180 }
1181 update_stages_and_entry(ren1_dst, ren1->dst_entry, one, a, b, 1);
1160 - if (string_list_has_string(&o->current_directory_set, ren1_dst)) {
1182 + if (dir_in_way(ren1_dst, 0 /*check_wc*/)) {
1183 setup_rename_df_conflict_info(RENAME_NORMAL,
1184 ren1->pair,
1185 NULL,
@@ -1262,7 +1284,6 @@ static int merge_content(struct merge_options *o,
1284 const char *reason = "content";
1285 struct merge_file_info mfi;
1286 struct diff_filespec one, a, b;
1265 - struct stat st;
1287 unsigned df_conflict_remains = 0;
1288
1289 if (!o_sha) {
@@ -1279,7 +1300,7 @@ static int merge_content(struct merge_options *o,
1300
1301 mfi = merge_file(o, &one, &a, &b, o->branch1, o->branch2);
1302 if (df_rename_conflict_branch &&
1282 - lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
1303 + dir_in_way(path, !o->call_depth)) {
1304 df_conflict_remains = 1;
1305 }
1306
@@ -1344,8 +1365,7 @@ static int process_entry(struct merge_options *o,
1365 output(o, 2, "Removing %s", path);
1366 /* do not touch working file if it did not exist */
1367 remove_file(o, 1, path, !a_sha);
1347 - } else if (string_list_has_string(&o->current_directory_set,
1348 - path)) {
1368 + } else if (dir_in_way(path, 0 /*check_wc*/)) {
1369 entry->processed = 0;
1370 return 1; /* Assume clean until processed */
1371 } else {
@@ -1368,7 +1388,7 @@ static int process_entry(struct merge_options *o,
1388 mode = b_mode;
1389 sha = b_sha;
1390 }
1371 - if (string_list_has_string(&o->current_directory_set, path)) {
1391 + if (dir_in_way(path, 0 /*check_wc*/)) {
1392 /* Handle D->F conflicts after all subfiles */
1393 entry->processed = 0;
1394 return 1; /* Assume clean until processed */
@@ -1416,7 +1436,6 @@ static int process_df_entry(struct merge_options *o,
1436 unsigned char *o_sha = stage_sha(entry->stages[1].sha, o_mode);
1437 unsigned char *a_sha = stage_sha(entry->stages[2].sha, a_mode);
1438 unsigned char *b_sha = stage_sha(entry->stages[3].sha, b_mode);
1419 - struct stat st;
1439
1440 entry->processed = 1;
1441 if (entry->rename_df_conflict_info) {
@@ -1461,7 +1480,7 @@ static int process_df_entry(struct merge_options *o,
1480 } else if (o_sha && (!a_sha || !b_sha)) {
1481 /* Modify/delete; deleted side may have put a directory in the way */
1482 char *renamed = NULL;
1464 - if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
1483 + if (dir_in_way(path, !o->call_depth)) {
1484 renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
1485 }
1486 clean_merge = 0;
@@ -1489,7 +1508,7 @@ static int process_df_entry(struct merge_options *o,
1508 sha = b_sha;
1509 conf = "directory/file";
1510 }
1492 - if (lstat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
1511 + if (dir_in_way(path, !o->call_depth)) {
1512 char *new_path = unique_path(o, path, add_branch);
1513 clean_merge = 0;
1514 output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "