merge-recursive: Avoid unnecessary file rewrites
[gitweb.git] / merge-recursive.c
index c7d5a4591bdf16cb2d2b639e37192a1532fb26f1..71febe94a3f6c6c35e1a1665f31b61d6c9fe3203 100644 (file)
@@ -526,6 +526,15 @@ static int update_stages(const char *path, const struct diff_filespec *o,
                         const struct diff_filespec *a,
                         const struct diff_filespec *b)
 {
+
+       /*
+        * NOTE: It is usually a bad idea to call update_stages on a path
+        * before calling update_file on that same path, since it can
+        * sometimes lead to spurious "refusing to lose untracked file..."
+        * messages from update_file (via make_room_for path via
+        * would_lose_untracked).  Instead, reverse the order of the calls
+        * (executing update_file first and then update_stages).
+        */
        int clear = 1;
        int options = ADD_CACHE_OK_TO_ADD | ADD_CACHE_SKIP_DFCHECK;
        if (clear)
@@ -1027,7 +1036,14 @@ static void handle_change_delete(struct merge_options *o,
                       change_past, o->branch1, o->branch1, path,
                       NULL == renamed ? "" : " at ",
                       NULL == renamed ? "" : renamed);
-               update_file(o, 0, a_sha, a_mode, renamed ? renamed : path);
+               if (renamed)
+                       update_file(o, 0, a_sha, a_mode, renamed);
+               /*
+                * No need to call update_file() on path when !renamed, since
+                * that would needlessly touch path.  We could call
+                * update_file_flags() with update_cache=0 and update_wd=0,
+                * but that's a no-op.
+                */
        }
        free(renamed);
 }
@@ -1039,7 +1055,6 @@ static void conflict_rename_delete(struct merge_options *o,
 {
        const struct diff_filespec *orig = pair->one;
        const struct diff_filespec *dest = pair->two;
-       const char *path;
        const unsigned char *a_sha = NULL;
        const unsigned char *b_sha = NULL;
        int a_mode = 0;
@@ -1053,22 +1068,21 @@ static void conflict_rename_delete(struct merge_options *o,
                b_mode = dest->mode;
        }
 
+       handle_change_delete(o,
+                            o->call_depth ? orig->path : dest->path,
+                            orig->sha1, orig->mode,
+                            a_sha, a_mode,
+                            b_sha, b_mode,
+                            "rename", "renamed");
+
        if (o->call_depth) {
                remove_file_from_cache(dest->path);
-               path = orig->path;
        } else {
-               path = dest->path;
                update_stages(dest->path, NULL,
                              rename_branch == o->branch1 ? dest : NULL,
                              rename_branch == o->branch1 ? NULL : dest);
        }
 
-       handle_change_delete(o,
-                            path,
-                            orig->sha1, orig->mode,
-                            a_sha, a_mode,
-                            b_sha, b_mode,
-                            "rename", "renamed");
 }
 
 static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
@@ -1106,11 +1120,6 @@ static void handle_file(struct merge_options *o,
        }
 
        add = filespec_from_entry(&other, dst_entry, stage ^ 1);
-       if (stage == 2)
-               update_stages(rename->path, NULL, rename, add);
-       else
-               update_stages(rename->path, NULL, add, rename);
-
        if (add) {
                char *add_name = unique_path(o, rename->path, other_branch);
                update_file(o, 0, add->sha1, add->mode, add_name);
@@ -1125,6 +1134,10 @@ static void handle_file(struct merge_options *o,
                }
        }
        update_file(o, 0, rename->sha1, rename->mode, dst_name);
+       if (stage == 2)
+               update_stages(rename->path, NULL, rename, add);
+       else
+               update_stages(rename->path, NULL, add, rename);
 
        if (dst_name != rename->path)
                free(dst_name);
@@ -1390,10 +1403,20 @@ static int process_renames(struct merge_options *o,
                                                           NULL);
                        } else if ((dst_other.mode == ren1->pair->two->mode) &&
                                   sha_eq(dst_other.sha1, ren1->pair->two->sha1)) {
-                               /* Added file on the other side
-                                  identical to the file being
-                                  renamed: clean merge */
-                               update_file(o, 1, ren1->pair->two->sha1, ren1->pair->two->mode, ren1_dst);
+                               /*
+                                * Added file on the other side identical to
+                                * the file being renamed: clean merge.
+                                * Also, there is no need to overwrite the
+                                * file already in the working copy, so call
+                                * update_file_flags() instead of
+                                * update_file().
+                                */
+                               update_file_flags(o,
+                                                 ren1->pair->two->sha1,
+                                                 ren1->pair->two->mode,
+                                                 ren1_dst,
+                                                 1, /* update_cache */
+                                                 0  /* update_wd    */);
                        } else if (!sha_eq(dst_other.sha1, null_sha1)) {
                                clean_merge = 0;
                                try_merge = 1;
@@ -1721,7 +1744,8 @@ static int process_entry(struct merge_options *o,
                        free(new_path);
                } else {
                        output(o, 2, "Adding %s", path);
-                       update_file(o, 1, sha, mode, path);
+                       /* do not overwrite file if already present */
+                       update_file_flags(o, sha, mode, path, 1, !a_sha);
                }
        } else if (a_sha && b_sha) {
                /* Case C: Added in both (check for same permissions) and */