@@ -1017,14 +1017,22 @@ static int process_renames(struct merge_options *o,
10171017
10181018 if (mfi .clean &&
10191019 sha_eq (mfi .sha , ren1 -> pair -> two -> sha1 ) &&
1020- mfi .mode == ren1 -> pair -> two -> mode )
1020+ mfi .mode == ren1 -> pair -> two -> mode ) {
10211021 /*
1022- * This messaged is part of
1022+ * This message is part of
10231023 * t6022 test. If you change
10241024 * it update the test too.
10251025 */
10261026 output (o , 3 , "Skipped %s (merged same as existing)" , ren1_dst );
1027- else {
1027+
1028+ /* There may be higher stage entries left
1029+ * in the index (e.g. due to a D/F
1030+ * conflict) that need to be resolved.
1031+ */
1032+ if (!ren1 -> dst_entry -> stages [2 ].mode !=
1033+ !ren1 -> dst_entry -> stages [3 ].mode )
1034+ ren1 -> dst_entry -> processed = 0 ;
1035+ } else {
10281036 if (mfi .merge || !mfi .clean )
10291037 output (o , 1 , "Renaming %s => %s" , ren1_src , ren1_dst );
10301038 if (mfi .merge )
@@ -1070,6 +1078,7 @@ static int process_entry(struct merge_options *o,
10701078 unsigned char * a_sha = stage_sha (entry -> stages [2 ].sha , a_mode );
10711079 unsigned char * b_sha = stage_sha (entry -> stages [3 ].sha , b_mode );
10721080
1081+ entry -> processed = 1 ;
10731082 if (o_sha && (!a_sha || !b_sha )) {
10741083 /* Case A: Deleted in one */
10751084 if ((!a_sha && !b_sha ) ||
@@ -1102,33 +1111,28 @@ static int process_entry(struct merge_options *o,
11021111 } else if ((!o_sha && a_sha && !b_sha ) ||
11031112 (!o_sha && !a_sha && b_sha )) {
11041113 /* Case B: Added in one. */
1105- const char * add_branch ;
1106- const char * other_branch ;
11071114 unsigned mode ;
11081115 const unsigned char * sha ;
1109- const char * conf ;
11101116
11111117 if (a_sha ) {
1112- add_branch = o -> branch1 ;
1113- other_branch = o -> branch2 ;
11141118 mode = a_mode ;
11151119 sha = a_sha ;
1116- conf = "file/directory" ;
11171120 } else {
1118- add_branch = o -> branch2 ;
1119- other_branch = o -> branch1 ;
11201121 mode = b_mode ;
11211122 sha = b_sha ;
1122- conf = "directory/file" ;
11231123 }
11241124 if (string_list_has_string (& o -> current_directory_set , path )) {
1125- const char * new_path = unique_path (o , path , add_branch );
1126- clean_merge = 0 ;
1127- output (o , 1 , "CONFLICT (%s): There is a directory with name %s in %s. "
1128- "Adding %s as %s" ,
1129- conf , path , other_branch , path , new_path );
1130- remove_file (o , 0 , path , 0 );
1131- update_file (o , 0 , sha , mode , new_path );
1125+ /* Handle D->F conflicts after all subfiles */
1126+ entry -> processed = 0 ;
1127+ /* But get any file out of the way now, so conflicted
1128+ * entries below the directory of the same name can
1129+ * be put in the working directory.
1130+ */
1131+ if (a_sha )
1132+ output (o , 2 , "Removing %s" , path );
1133+ /* do not touch working file if it did not exist */
1134+ remove_file (o , 0 , path , !a_sha );
1135+ return 1 ; /* Assume clean till processed */
11321136 } else {
11331137 output (o , 2 , "Adding %s" , path );
11341138 update_file (o , 1 , sha , mode , path );
@@ -1176,6 +1180,64 @@ static int process_entry(struct merge_options *o,
11761180 return clean_merge ;
11771181}
11781182
1183+ /*
1184+ * Per entry merge function for D/F conflicts, to be called only after
1185+ * all files below dir have been processed. We do this because in the
1186+ * cases we can cleanly resolve D/F conflicts, process_entry() can clean
1187+ * out all the files below the directory for us.
1188+ */
1189+ static int process_df_entry (struct merge_options * o ,
1190+ const char * path , struct stage_data * entry )
1191+ {
1192+ int clean_merge = 1 ;
1193+ unsigned o_mode = entry -> stages [1 ].mode ;
1194+ unsigned a_mode = entry -> stages [2 ].mode ;
1195+ unsigned b_mode = entry -> stages [3 ].mode ;
1196+ unsigned char * o_sha = stage_sha (entry -> stages [1 ].sha , o_mode );
1197+ unsigned char * a_sha = stage_sha (entry -> stages [2 ].sha , a_mode );
1198+ unsigned char * b_sha = stage_sha (entry -> stages [3 ].sha , b_mode );
1199+ const char * add_branch ;
1200+ const char * other_branch ;
1201+ unsigned mode ;
1202+ const unsigned char * sha ;
1203+ const char * conf ;
1204+ struct stat st ;
1205+
1206+ /* We currently only handle D->F cases */
1207+ assert ((!o_sha && a_sha && !b_sha ) ||
1208+ (!o_sha && !a_sha && b_sha ));
1209+
1210+ entry -> processed = 1 ;
1211+
1212+ if (a_sha ) {
1213+ add_branch = o -> branch1 ;
1214+ other_branch = o -> branch2 ;
1215+ mode = a_mode ;
1216+ sha = a_sha ;
1217+ conf = "file/directory" ;
1218+ } else {
1219+ add_branch = o -> branch2 ;
1220+ other_branch = o -> branch1 ;
1221+ mode = b_mode ;
1222+ sha = b_sha ;
1223+ conf = "directory/file" ;
1224+ }
1225+ if (lstat (path , & st ) == 0 && S_ISDIR (st .st_mode )) {
1226+ const char * new_path = unique_path (o , path , add_branch );
1227+ clean_merge = 0 ;
1228+ output (o , 1 , "CONFLICT (%s): There is a directory with name %s in %s. "
1229+ "Adding %s as %s" ,
1230+ conf , path , other_branch , path , new_path );
1231+ remove_file (o , 0 , path , 0 );
1232+ update_file (o , 0 , sha , mode , new_path );
1233+ } else {
1234+ output (o , 2 , "Adding %s" , path );
1235+ update_file (o , 1 , sha , mode , path );
1236+ }
1237+
1238+ return clean_merge ;
1239+ }
1240+
11791241void set_porcelain_error_msgs (const char * * msgs , const char * cmd )
11801242{
11811243 const char * msg ;
@@ -1269,6 +1331,13 @@ int merge_trees(struct merge_options *o,
12691331 && !process_entry (o , path , e ))
12701332 clean = 0 ;
12711333 }
1334+ for (i = 0 ; i < entries -> nr ; i ++ ) {
1335+ const char * path = entries -> items [i ].string ;
1336+ struct stage_data * e = entries -> items [i ].util ;
1337+ if (!e -> processed
1338+ && !process_df_entry (o , path , e ))
1339+ clean = 0 ;
1340+ }
12721341
12731342 string_list_clear (re_merge , 0 );
12741343 string_list_clear (re_head , 0 );
0 commit comments