Move try_merge_command and checkout_fast_forward to libgit.a
[gitweb.git] / merge-recursive.c
index 3124144011f2c3b716e06d052a98d7f1c2eb8ca2..d8820604ca3535ce25796012412fe69f1cbdd37b 100644 (file)
@@ -38,16 +38,15 @@ static struct tree *shift_tree_object(struct tree *one, struct tree *two,
        return lookup_tree(shifted);
 }
 
-/*
- * A virtual commit has (const char *)commit->util set to the name.
- */
-
 static struct commit *make_virtual_commit(struct tree *tree, const char *comment)
 {
        struct commit *commit = xcalloc(1, sizeof(struct commit));
+       struct merge_remote_desc *desc = xmalloc(sizeof(*desc));
+
+       desc->name = comment;
+       desc->obj = (struct object *)commit;
        commit->tree = tree;
-       commit->util = (void*)comment;
-       /* avoid warnings */
+       commit->util = desc;
        commit->object.parsed = 1;
        return commit;
 }
@@ -184,11 +183,11 @@ static void output_commit_title(struct merge_options *o, struct commit *commit)
        for (i = o->call_depth; i--;)
                fputs("  ", stdout);
        if (commit->util)
-               printf("virtual %s\n", (char *)commit->util);
+               printf("virtual %s\n", merge_remote_util(commit)->name);
        else {
                printf("%s ", find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
                if (parse_commit(commit) != 0)
-                       printf("(bad commit)\n");
+                       printf(_("(bad commit)\n"));
                else {
                        const char *title;
                        int len = find_commit_subject(commit->buffer, &title);
@@ -204,7 +203,7 @@ static int add_cacheinfo(unsigned int mode, const unsigned char *sha1,
        struct cache_entry *ce;
        ce = make_cache_entry(mode, sha1 ? sha1 : null_sha1, path, stage, refresh);
        if (!ce)
-               return error("addinfo_cache failed for path '%s'", path);
+               return error(_("addinfo_cache failed for path '%s'"), path);
        return add_cache_entry(ce, options);
 }
 
@@ -265,8 +264,8 @@ struct tree *write_tree_from_memory(struct merge_options *o)
 
        if (!cache_tree_fully_valid(active_cache_tree) &&
            cache_tree_update(active_cache_tree,
-                             active_cache, active_nr, 0, 0) < 0)
-               die("error building trees");
+                             active_cache, active_nr, 0) < 0)
+               die(_("error building trees"));
 
        result = lookup_tree(active_cache_tree->sha1);
 
@@ -297,7 +296,9 @@ static int save_files_dirs(const unsigned char *sha1,
 static int get_files_dirs(struct merge_options *o, struct tree *tree)
 {
        int n;
-       if (read_tree_recursive(tree, "", 0, 0, NULL, save_files_dirs, o))
+       struct pathspec match_all;
+       init_pathspec(&match_all, NULL);
+       if (read_tree_recursive(tree, "", 0, 0, &match_all, save_files_dirs, o))
                return 0;
        n = o->current_file_set.nr + o->current_directory_set.nr;
        return n;
@@ -388,7 +389,7 @@ static void record_df_conflict_files(struct merge_options *o,
                                     struct string_list *entries)
 {
        /* If there is a D/F conflict and the file for such a conflict
-        * currently exist in the working copy, we want to allow it to be
+        * currently exist in the working tree, we want to allow it to be
         * removed to make room for the corresponding directory if needed.
         * The files underneath the directories of such D/F conflicts will
         * be processed before the corresponding file involved in the D/F
@@ -400,6 +401,7 @@ static void record_df_conflict_files(struct merge_options *o,
         * and the file need to be present, then the D/F file will be
         * reinstated with a new unique name at the time it is processed.
         */
+       struct string_list df_sorted_entries;
        const char *last_file = NULL;
        int last_len = 0;
        int i;
@@ -412,14 +414,20 @@ static void record_df_conflict_files(struct merge_options *o,
                return;
 
        /* Ensure D/F conflicts are adjacent in the entries list. */
-       qsort(entries->items, entries->nr, sizeof(*entries->items),
+       memset(&df_sorted_entries, 0, sizeof(struct string_list));
+       for (i = 0; i < entries->nr; i++) {
+               struct string_list_item *next = &entries->items[i];
+               string_list_append(&df_sorted_entries, next->string)->util =
+                                  next->util;
+       }
+       qsort(df_sorted_entries.items, entries->nr, sizeof(*entries->items),
              string_list_df_name_compare);
 
        string_list_clear(&o->df_conflict_file_set, 1);
-       for (i = 0; i < entries->nr; i++) {
-               const char *path = entries->items[i].string;
+       for (i = 0; i < df_sorted_entries.nr; i++) {
+               const char *path = df_sorted_entries.items[i].string;
                int len = strlen(path);
-               struct stage_data *e = entries->items[i].util;
+               struct stage_data *e = df_sorted_entries.items[i].util;
 
                /*
                 * Check if last_file & path correspond to a D/F conflict;
@@ -447,6 +455,7 @@ static void record_df_conflict_files(struct merge_options *o,
                        last_file = NULL;
                }
        }
+       string_list_clear(&df_sorted_entries, 0);
 }
 
 struct rename {
@@ -476,6 +485,7 @@ static struct string_list *get_renames(struct merge_options *o,
        renames = xcalloc(1, sizeof(struct string_list));
        diff_setup(&opts);
        DIFF_OPT_SET(&opts, RECURSIVE);
+       DIFF_OPT_CLR(&opts, RENAME_EMPTY);
        opts.detect_rename = DIFF_DETECT_RENAME;
        opts.rename_limit = o->merge_rename_limit >= 0 ? o->merge_rename_limit :
                            o->diff_rename_limit >= 0 ? o->diff_rename_limit :
@@ -483,8 +493,7 @@ static struct string_list *get_renames(struct merge_options *o,
        opts.rename_score = o->rename_score;
        opts.show_rename_progress = o->show_rename_progress;
        opts.output_format = DIFF_FORMAT_NO_OUTPUT;
-       if (diff_setup_done(&opts) < 0)
-               die("diff setup failed");
+       diff_setup_done(&opts);
        diff_tree_sha1(o_tree->object.sha1, tree->object.sha1, "", &opts);
        diffcore_std(&opts);
        if (opts.needed_rename_limit > o->needed_rename_limit)
@@ -526,6 +535,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)
@@ -595,23 +613,6 @@ static char *unique_path(struct merge_options *o, const char *path, const char *
        return newpath;
 }
 
-static void flush_buffer(int fd, const char *buf, unsigned long size)
-{
-       while (size > 0) {
-               long ret = write_in_full(fd, buf, size);
-               if (ret < 0) {
-                       /* Ignore epipe */
-                       if (errno == EPIPE)
-                               break;
-                       die_errno("merge-recursive");
-               } else if (!ret) {
-                       die("merge-recursive: disk full?");
-               }
-               size -= ret;
-               buf += ret;
-       }
-}
-
 static int dir_in_way(const char *path, int check_working_copy)
 {
        int pos, pathlen = strlen(path);
@@ -668,7 +669,7 @@ static int would_lose_untracked(const char *path)
 static int make_room_for_path(struct merge_options *o, const char *path)
 {
        int status, i;
-       const char *msg = "failed to create path '%s'%s";
+       const char *msg = _("failed to create path '%s'%s");
 
        /* Unlink any D/F conflict files that are in the way */
        for (i = 0; i < o->df_conflict_file_set.nr; i++) {
@@ -679,7 +680,7 @@ static int make_room_for_path(struct merge_options *o, const char *path)
                    path[df_pathlen] == '/' &&
                    strncmp(path, df_path, df_pathlen) == 0) {
                        output(o, 3,
-                              "Removing %s to make room for subdirectory\n",
+                              _("Removing %s to make room for subdirectory\n"),
                               df_path);
                        unlink(df_path);
                        unsorted_string_list_delete_item(&o->df_conflict_file_set,
@@ -693,7 +694,7 @@ static int make_room_for_path(struct merge_options *o, const char *path)
        if (status) {
                if (status == -3) {
                        /* something else exists */
-                       error(msg, path, ": perhaps a D/F conflict?");
+                       error(msg, path, _(": perhaps a D/F conflict?"));
                        return -1;
                }
                die(msg, path, "");
@@ -704,7 +705,7 @@ static int make_room_for_path(struct merge_options *o, const char *path)
         * tracking it.
         */
        if (would_lose_untracked(path))
-               return error("refusing to lose untracked file at '%s'",
+               return error(_("refusing to lose untracked file at '%s'"),
                             path);
 
        /* Successful unlink is good.. */
@@ -714,7 +715,7 @@ static int make_room_for_path(struct merge_options *o, const char *path)
        if (errno == ENOENT)
                return 0;
        /* .. but not some other error (who really cares what?) */
-       return error(msg, path, ": perhaps a D/F conflict?");
+       return error(msg, path, _(": perhaps a D/F conflict?"));
 }
 
 static void update_file_flags(struct merge_options *o,
@@ -744,9 +745,9 @@ static void update_file_flags(struct merge_options *o,
 
                buf = read_sha1_file(sha, &type, &size);
                if (!buf)
-                       die("cannot read object %s '%s'", sha1_to_hex(sha), path);
+                       die(_("cannot read object %s '%s'"), sha1_to_hex(sha), path);
                if (type != OBJ_BLOB)
-                       die("blob expected for %s '%s'", sha1_to_hex(sha), path);
+                       die(_("blob expected for %s '%s'"), sha1_to_hex(sha), path);
                if (S_ISREG(mode)) {
                        struct strbuf strbuf = STRBUF_INIT;
                        if (convert_to_working_tree(path, buf, size, &strbuf)) {
@@ -769,18 +770,18 @@ static void update_file_flags(struct merge_options *o,
                                mode = 0666;
                        fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, mode);
                        if (fd < 0)
-                               die_errno("failed to open '%s'", path);
-                       flush_buffer(fd, buf, size);
+                               die_errno(_("failed to open '%s'"), path);
+                       write_in_full(fd, buf, size);
                        close(fd);
                } else if (S_ISLNK(mode)) {
                        char *lnk = xmemdupz(buf, size);
                        safe_create_leading_directories_const(path);
                        unlink(path);
                        if (symlink(lnk, path))
-                               die_errno("failed to symlink '%s'", path);
+                               die_errno(_("failed to symlink '%s'"), path);
                        free(lnk);
                } else
-                       die("do not know what to do with %06o %s '%s'",
+                       die(_("do not know what to do with %06o %s '%s'"),
                            mode, sha1_to_hex(sha), path);
                free(buf);
        }
@@ -843,14 +844,14 @@ static int merge_3way(struct merge_options *o,
        if (strcmp(a->path, b->path) ||
            (o->ancestor != NULL && strcmp(a->path, one->path) != 0)) {
                base_name = o->ancestor == NULL ? NULL :
-                       xstrdup(mkpath("%s:%s", o->ancestor, one->path));
-               name1 = xstrdup(mkpath("%s:%s", branch1, a->path));
-               name2 = xstrdup(mkpath("%s:%s", branch2, b->path));
+                       mkpathdup("%s:%s", o->ancestor, one->path);
+               name1 = mkpathdup("%s:%s", branch1, a->path);
+               name2 = mkpathdup("%s:%s", branch2, b->path);
        } else {
                base_name = o->ancestor == NULL ? NULL :
-                       xstrdup(mkpath("%s", o->ancestor));
-               name1 = xstrdup(mkpath("%s", branch1));
-               name2 = xstrdup(mkpath("%s", branch2));
+                       mkpathdup("%s", o->ancestor);
+               name1 = mkpathdup("%s", branch1);
+               name2 = mkpathdup("%s", branch2);
        }
 
        read_mmblob(&orig, one->sha1);
@@ -860,6 +861,7 @@ static int merge_3way(struct merge_options *o,
        merge_status = ll_merge(result_buf, a->path, &orig, base_name,
                                &src1, name1, &src2, name2, &ll_opts);
 
+       free(base_name);
        free(name1);
        free(name2);
        free(orig.ptr);
@@ -917,25 +919,27 @@ static struct merge_file_info merge_file_1(struct merge_options *o,
                                                  branch1, branch2);
 
                        if ((merge_status < 0) || !result_buf.ptr)
-                               die("Failed to execute internal merge");
+                               die(_("Failed to execute internal merge"));
 
                        if (write_sha1_file(result_buf.ptr, result_buf.size,
                                            blob_type, result.sha))
-                               die("Unable to add %s to database",
+                               die(_("Unable to add %s to database"),
                                    a->path);
 
                        free(result_buf.ptr);
                        result.clean = (merge_status == 0);
                } else if (S_ISGITLINK(a->mode)) {
-                       result.clean = merge_submodule(result.sha, one->path, one->sha1,
-                                                      a->sha1, b->sha1);
+                       result.clean = merge_submodule(result.sha,
+                                                      one->path, one->sha1,
+                                                      a->sha1, b->sha1,
+                                                      !o->call_depth);
                } else if (S_ISLNK(a->mode)) {
                        hashcpy(result.sha, a->sha1);
 
                        if (!sha_eq(a->sha1, b->sha1))
                                result.clean = 0;
                } else {
-                       die("unsupported object type in the tree");
+                       die(_("unsupported object type in the tree"));
                }
        }
 
@@ -992,29 +996,156 @@ static struct merge_file_info merge_file(struct merge_options *o,
        return merge_file_1(o, &one, &a, &b, branch1, branch2);
 }
 
+static void handle_change_delete(struct merge_options *o,
+                                const char *path,
+                                const unsigned char *o_sha, int o_mode,
+                                const unsigned char *a_sha, int a_mode,
+                                const unsigned char *b_sha, int b_mode,
+                                const char *change, const char *change_past)
+{
+       char *renamed = NULL;
+       if (dir_in_way(path, !o->call_depth)) {
+               renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
+       }
+
+       if (o->call_depth) {
+               /*
+                * We cannot arbitrarily accept either a_sha or b_sha as
+                * correct; since there is no true "middle point" between
+                * them, simply reuse the base version for virtual merge base.
+                */
+               remove_file_from_cache(path);
+               update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
+       } else if (!a_sha) {
+               if (!renamed) {
+                       output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
+                              "and %s in %s. Version %s of %s left in tree."),
+                              change, path, o->branch1, change_past,
+                              o->branch2, o->branch2, path);
+                       update_file(o, 0, b_sha, b_mode, path);
+               } else {
+                       output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
+                              "and %s in %s. Version %s of %s left in tree at %s."),
+                              change, path, o->branch1, change_past,
+                              o->branch2, o->branch2, path, renamed);
+                       update_file(o, 0, b_sha, b_mode, renamed);
+               }
+       } else {
+               if (!renamed) {
+                       output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
+                              "and %s in %s. Version %s of %s left in tree."),
+                              change, path, o->branch2, change_past,
+                              o->branch1, o->branch1, path);
+               } else {
+                       output(o, 1, _("CONFLICT (%s/delete): %s deleted in %s "
+                              "and %s in %s. Version %s of %s left in tree at %s."),
+                              change, path, o->branch2, change_past,
+                              o->branch1, o->branch1, path, 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);
+}
+
 static void conflict_rename_delete(struct merge_options *o,
                                   struct diff_filepair *pair,
                                   const char *rename_branch,
                                   const char *other_branch)
 {
-       char *dest_name = pair->two->path;
-       int df_conflict = 0;
+       const struct diff_filespec *orig = pair->one;
+       const struct diff_filespec *dest = pair->two;
+       const unsigned char *a_sha = NULL;
+       const unsigned char *b_sha = NULL;
+       int a_mode = 0;
+       int b_mode = 0;
+
+       if (rename_branch == o->branch1) {
+               a_sha = dest->sha1;
+               a_mode = dest->mode;
+       } else {
+               b_sha = dest->sha1;
+               b_mode = dest->mode;
+       }
 
-       output(o, 1, "CONFLICT (rename/delete): Rename %s->%s in %s "
-              "and deleted in %s",
-              pair->one->path, pair->two->path, rename_branch,
-              other_branch);
-       if (!o->call_depth)
-               update_stages(dest_name, NULL,
-                             rename_branch == o->branch1 ? pair->two : NULL,
-                             rename_branch == o->branch1 ? NULL : pair->two);
-       if (dir_in_way(dest_name, !o->call_depth)) {
-               dest_name = unique_path(o, dest_name, rename_branch);
-               df_conflict = 1;
+       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);
+       } else {
+               update_stages(dest->path, NULL,
+                             rename_branch == o->branch1 ? dest : NULL,
+                             rename_branch == o->branch1 ? NULL : dest);
+       }
+
+}
+
+static struct diff_filespec *filespec_from_entry(struct diff_filespec *target,
+                                                struct stage_data *entry,
+                                                int stage)
+{
+       unsigned char *sha = entry->stages[stage].sha;
+       unsigned mode = entry->stages[stage].mode;
+       if (mode == 0 || is_null_sha1(sha))
+               return NULL;
+       hashcpy(target->sha1, sha);
+       target->mode = mode;
+       return target;
+}
+
+static void handle_file(struct merge_options *o,
+                       struct diff_filespec *rename,
+                       int stage,
+                       struct rename_conflict_info *ci)
+{
+       char *dst_name = rename->path;
+       struct stage_data *dst_entry;
+       const char *cur_branch, *other_branch;
+       struct diff_filespec other;
+       struct diff_filespec *add;
+
+       if (stage == 2) {
+               dst_entry = ci->dst_entry1;
+               cur_branch = ci->branch1;
+               other_branch = ci->branch2;
+       } else {
+               dst_entry = ci->dst_entry2;
+               cur_branch = ci->branch2;
+               other_branch = ci->branch1;
        }
-       update_file(o, 0, pair->two->sha1, pair->two->mode, dest_name);
-       if (df_conflict)
-               free(dest_name);
+
+       add = filespec_from_entry(&other, dst_entry, stage ^ 1);
+       if (add) {
+               char *add_name = unique_path(o, rename->path, other_branch);
+               update_file(o, 0, add->sha1, add->mode, add_name);
+
+               remove_file(o, 0, rename->path, 0);
+               dst_name = unique_path(o, rename->path, cur_branch);
+       } else {
+               if (dir_in_way(rename->path, !o->call_depth)) {
+                       dst_name = unique_path(o, rename->path, cur_branch);
+                       output(o, 1, _("%s is a directory in %s adding as %s instead"),
+                              rename->path, other_branch, dst_name);
+               }
+       }
+       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);
 }
 
 static void conflict_rename_rename_1to2(struct merge_options *o,
@@ -1024,29 +1155,17 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
        struct diff_filespec *one = ci->pair1->one;
        struct diff_filespec *a = ci->pair1->two;
        struct diff_filespec *b = ci->pair2->two;
-       const char *dst_name_a = a->path;
-       const char *dst_name_b = b->path;
-       char *del[2];
-       int delp = 0;
 
-       output(o, 1, "CONFLICT (rename/rename): "
+       output(o, 1, _("CONFLICT (rename/rename): "
               "Rename \"%s\"->\"%s\" in branch \"%s\" "
-              "rename \"%s\"->\"%s\" in \"%s\"%s",
+              "rename \"%s\"->\"%s\" in \"%s\"%s"),
               one->path, a->path, ci->branch1,
               one->path, b->path, ci->branch2,
-              o->call_depth ? " (left unresolved)" : "");
-       if (dir_in_way(a->path, !o->call_depth)) {
-               dst_name_a = del[delp++] = unique_path(o, a->path, ci->branch1);
-               output(o, 1, "%s is a directory in %s adding as %s instead",
-                      a->path, ci->branch2, dst_name_a);
-       }
-       if (dir_in_way(b->path, !o->call_depth)) {
-               dst_name_b = del[delp++] = unique_path(o, b->path, ci->branch2);
-               output(o, 1, "%s is a directory in %s adding as %s instead",
-                      b->path, ci->branch1, dst_name_b);
-       }
+              o->call_depth ? _(" (left unresolved)") : "");
        if (o->call_depth) {
                struct merge_file_info mfi;
+               struct diff_filespec other;
+               struct diff_filespec *add;
                mfi = merge_file(o, one->path,
                                 one->sha1, one->mode,
                                 a->sha1, a->mode,
@@ -1059,17 +1178,29 @@ static void conflict_rename_rename_1to2(struct merge_options *o,
                 * unique path, or use that unique path instead of src here.
                 */
                update_file(o, 0, mfi.sha, mfi.mode, one->path);
-               remove_file_from_cache(a->path);
-               remove_file_from_cache(b->path);
-       } else {
-               update_stages(a->path, NULL, a, NULL);
-               update_stages(b->path, NULL, NULL, b);
 
-               update_file(o, 0, a->sha1, a->mode, dst_name_a);
-               update_file(o, 0, b->sha1, b->mode, dst_name_b);
+               /*
+                * Above, we put the merged content at the merge-base's
+                * path.  Now we usually need to delete both a->path and
+                * b->path.  However, the rename on each side of the merge
+                * could also be involved in a rename/add conflict.  In
+                * such cases, we should keep the added file around,
+                * resolving the conflict at that path in its favor.
+                */
+               add = filespec_from_entry(&other, ci->dst_entry1, 2 ^ 1);
+               if (add)
+                       update_file(o, 0, add->sha1, add->mode, a->path);
+               else
+                       remove_file_from_cache(a->path);
+               add = filespec_from_entry(&other, ci->dst_entry2, 3 ^ 1);
+               if (add)
+                       update_file(o, 0, add->sha1, add->mode, b->path);
+               else
+                       remove_file_from_cache(b->path);
+       } else {
+               handle_file(o, a, 2, ci);
+               handle_file(o, b, 3, ci);
        }
-       while (delp--)
-               free(del[delp]);
 }
 
 static void conflict_rename_rename_2to1(struct merge_options *o,
@@ -1084,9 +1215,9 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
        struct merge_file_info mfi_c1;
        struct merge_file_info mfi_c2;
 
-       output(o, 1, "CONFLICT (rename/rename): "
+       output(o, 1, _("CONFLICT (rename/rename): "
               "Rename %s->%s in %s. "
-              "Rename %s->%s in %s",
+              "Rename %s->%s in %s"),
               a->path, c1->path, ci->branch1,
               b->path, c2->path, ci->branch2);
 
@@ -1114,7 +1245,7 @@ static void conflict_rename_rename_2to1(struct merge_options *o,
        } else {
                char *new_path1 = unique_path(o, path, ci->branch1);
                char *new_path2 = unique_path(o, path, ci->branch2);
-               output(o, 1, "Renaming %s to %s and %s to %s instead",
+               output(o, 1, _("Renaming %s to %s and %s to %s instead"),
                       a->path, new_path1, b->path, new_path2);
                remove_file(o, 0, path, 0);
                update_file(o, 0, mfi_c1.sha, mfi_c1.mode, new_path1);
@@ -1296,15 +1427,25 @@ 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;
-                               output(o, 1, "CONFLICT (rename/add): Rename %s->%s in %s. "
-                                      "%s added in %s",
+                               output(o, 1, _("CONFLICT (rename/add): Rename %s->%s in %s. "
+                                      "%s added in %s"),
                                       ren1_src, ren1_dst, branch1,
                                       ren1_dst, branch2);
                                if (o->call_depth) {
@@ -1313,12 +1454,12 @@ static int process_renames(struct merge_options *o,
                                                         ren1->pair->two->sha1, ren1->pair->two->mode,
                                                         dst_other.sha1, dst_other.mode,
                                                         branch1, branch2);
-                                       output(o, 1, "Adding merged %s", ren1_dst);
+                                       output(o, 1, _("Adding merged %s"), ren1_dst);
                                        update_file(o, 0, mfi.sha, mfi.mode, ren1_dst);
                                        try_merge = 0;
                                } else {
                                        char *new_path = unique_path(o, ren1_dst, branch2);
-                                       output(o, 1, "Adding as %s instead", new_path);
+                                       output(o, 1, _("Adding as %s instead"), new_path);
                                        update_file(o, 0, dst_other.sha1, dst_other.mode, new_path);
                                        free(new_path);
                                }
@@ -1369,10 +1510,10 @@ static int read_sha1_strbuf(const unsigned char *sha1, struct strbuf *dst)
        unsigned long size;
        buf = read_sha1_file(sha1, &type, &size);
        if (!buf)
-               return error("cannot read object %s", sha1_to_hex(sha1));
+               return error(_("cannot read object %s"), sha1_to_hex(sha1));
        if (type != OBJ_BLOB) {
                free(buf);
-               return error("object %s is not a blob", sha1_to_hex(sha1));
+               return error(_("object %s is not a blob"), sha1_to_hex(sha1));
        }
        strbuf_attach(dst, buf, size, size + 1);
        return 0;
@@ -1409,44 +1550,18 @@ static int blob_unchanged(const unsigned char *o_sha,
        return ret;
 }
 
-static void handle_delete_modify(struct merge_options *o,
+static void handle_modify_delete(struct merge_options *o,
                                 const char *path,
                                 unsigned char *o_sha, int o_mode,
                                 unsigned char *a_sha, int a_mode,
                                 unsigned char *b_sha, int b_mode)
 {
-       char *renamed = NULL;
-       if (dir_in_way(path, !o->call_depth)) {
-               renamed = unique_path(o, path, a_sha ? o->branch1 : o->branch2);
-       }
-
-       if (o->call_depth) {
-               /*
-                * We cannot arbitrarily accept either a_sha or b_sha as
-                * correct; since there is no true "middle point" between
-                * them, simply reuse the base version for virtual merge base.
-                */
-               remove_file_from_cache(path);
-               update_file(o, 0, o_sha, o_mode, renamed ? renamed : path);
-       } else if (!a_sha) {
-               output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-                      "and modified in %s. Version %s of %s left in tree%s%s.",
-                      path, o->branch1,
-                      o->branch2, o->branch2, path,
-                      NULL == renamed ? "" : " at ",
-                      NULL == renamed ? "" : renamed);
-               update_file(o, 0, b_sha, b_mode, renamed ? renamed : path);
-       } else {
-               output(o, 1, "CONFLICT (delete/modify): %s deleted in %s "
-                      "and modified in %s. Version %s of %s left in tree%s%s.",
-                      path, o->branch2,
-                      o->branch1, o->branch1, path,
-                      NULL == renamed ? "" : " at ",
-                      NULL == renamed ? "" : renamed);
-               update_file(o, 0, a_sha, a_mode, renamed ? renamed : path);
-       }
-       free(renamed);
-
+       handle_change_delete(o,
+                            path,
+                            o_sha, o_mode,
+                            a_sha, a_mode,
+                            b_sha, b_mode,
+                            _("modify"), _("modified"));
 }
 
 static int merge_content(struct merge_options *o,
@@ -1456,14 +1571,14 @@ static int merge_content(struct merge_options *o,
                         unsigned char *b_sha, int b_mode,
                         struct rename_conflict_info *rename_conflict_info)
 {
-       const char *reason = "content";
+       const char *reason = _("content");
        const char *path1 = NULL, *path2 = NULL;
        struct merge_file_info mfi;
        struct diff_filespec one, a, b;
        unsigned df_conflict_remains = 0;
 
        if (!o_sha) {
-               reason = "add/add";
+               reason = _("add/add");
                o_sha = (unsigned char *)null_sha1;
        }
        one.path = a.path = b.path = (char *)path;
@@ -1497,7 +1612,7 @@ static int merge_content(struct merge_options *o,
        if (mfi.clean && !df_conflict_remains &&
            sha_eq(mfi.sha, a_sha) && mfi.mode == a_mode) {
                int path_renamed_outside_HEAD;
-               output(o, 3, "Skipped %s (merged same as existing)", path);
+               output(o, 3, _("Skipped %s (merged same as existing)"), path);
                /*
                 * The content merge resulted in the same file contents we
                 * already had.  We can return early if those file contents
@@ -1507,16 +1622,16 @@ static int merge_content(struct merge_options *o,
                path_renamed_outside_HEAD = !path2 || !strcmp(path, path2);
                if (!path_renamed_outside_HEAD) {
                        add_cacheinfo(mfi.mode, mfi.sha, path,
-                                     0 /*stage*/, 1 /*refresh*/, 0 /*options*/);
+                                     0, (!o->call_depth), 0);
                        return mfi.clean;
                }
        } else
-               output(o, 2, "Auto-merging %s", path);
+               output(o, 2, _("Auto-merging %s"), path);
 
        if (!mfi.clean) {
                if (S_ISGITLINK(mfi.mode))
-                       reason = "submodule";
-               output(o, 1, "CONFLICT (%s): Merge conflict in %s",
+                       reason = _("submodule");
+               output(o, 1, _("CONFLICT (%s): Merge conflict in %s"),
                                reason, path);
                if (rename_conflict_info && !df_conflict_remains)
                        update_stages(path, &one, &a, &b);
@@ -1542,7 +1657,7 @@ static int merge_content(struct merge_options *o,
 
                }
                new_path = unique_path(o, path, rename_conflict_info->branch1);
-               output(o, 1, "Adding as %s instead", new_path);
+               output(o, 1, _("Adding as %s instead"), new_path);
                update_file(o, 0, mfi.sha, mfi.mode, new_path);
                free(new_path);
                mfi.clean = 0;
@@ -1606,13 +1721,13 @@ static int process_entry(struct merge_options *o,
                        /* Deleted in both or deleted in one and
                         * unchanged in the other */
                        if (a_sha)
-                               output(o, 2, "Removing %s", path);
+                               output(o, 2, _("Removing %s"), path);
                        /* do not touch working file if it did not exist */
                        remove_file(o, 1, path, !a_sha);
                } else {
                        /* Modify/delete; deleted side may have put a directory in the way */
                        clean_merge = 0;
-                       handle_delete_modify(o, path, o_sha, o_mode,
+                       handle_modify_delete(o, path, o_sha, o_mode,
                                             a_sha, a_mode, b_sha, b_mode);
                }
        } else if ((!o_sha && a_sha && !b_sha) ||
@@ -1631,19 +1746,19 @@ static int process_entry(struct merge_options *o,
                        other_branch = o->branch2;
                        mode = a_mode;
                        sha = a_sha;
-                       conf = "file/directory";
+                       conf = _("file/directory");
                } else {
                        add_branch = o->branch2;
                        other_branch = o->branch1;
                        mode = b_mode;
                        sha = b_sha;
-                       conf = "directory/file";
+                       conf = _("directory/file");
                }
                if (dir_in_way(path, !o->call_depth)) {
                        char *new_path = unique_path(o, path, add_branch);
                        clean_merge = 0;
-                       output(o, 1, "CONFLICT (%s): There is a directory with name %s in %s. "
-                              "Adding %s as %s",
+                       output(o, 1, _("CONFLICT (%s): There is a directory with name %s in %s. "
+                              "Adding %s as %s"),
                               conf, path, other_branch, path, new_path);
                        if (o->call_depth)
                                remove_file_from_cache(path);
@@ -1652,8 +1767,9 @@ static int process_entry(struct merge_options *o,
                                remove_file_from_cache(path);
                        free(new_path);
                } else {
-                       output(o, 2, "Adding %s", path);
-                       update_file(o, 1, sha, mode, path);
+                       output(o, 2, _("Adding %s"), 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 */
@@ -1668,7 +1784,7 @@ static int process_entry(struct merge_options *o,
                 */
                remove_file(o, 1, path, !a_mode);
        } else
-               die("Fatal merge failure, shouldn't happen.");
+               die(_("Fatal merge failure, shouldn't happen."));
 
        return clean_merge;
 }
@@ -1687,7 +1803,7 @@ int merge_trees(struct merge_options *o,
        }
 
        if (sha_eq(common->object.sha1, merge->object.sha1)) {
-               output(o, 0, "Already up-to-date!");
+               output(o, 0, _("Already up-to-date!"));
                *result = head;
                return 1;
        }
@@ -1696,7 +1812,7 @@ int merge_trees(struct merge_options *o,
 
        if (code != 0) {
                if (show(o, 4) || o->call_depth)
-                       die("merging of trees %s and %s failed",
+                       die(_("merging of trees %s and %s failed"),
                            sha1_to_hex(head->object.sha1),
                            sha1_to_hex(merge->object.sha1));
                else
@@ -1726,7 +1842,7 @@ int merge_trees(struct merge_options *o,
                for (i = 0; i < entries->nr; i++) {
                        struct stage_data *e = entries->items[i].util;
                        if (!e->processed)
-                               die("Unprocessed path??? %s",
+                               die(_("Unprocessed path??? %s"),
                                    entries->items[i].string);
                }
 
@@ -1771,7 +1887,7 @@ int merge_recursive(struct merge_options *o,
        int clean;
 
        if (show(o, 4)) {
-               output(o, 4, "Merging:");
+               output(o, 4, _("Merging:"));
                output_commit_title(o, h1);
                output_commit_title(o, h2);
        }
@@ -1782,19 +1898,20 @@ int merge_recursive(struct merge_options *o,
        }
 
        if (show(o, 5)) {
-               output(o, 5, "found %u common ancestor(s):", commit_list_count(ca));
+               unsigned cnt = commit_list_count(ca);
+
+               output(o, 5, Q_("found %u common ancestor:",
+                               "found %u common ancestors:", cnt), cnt);
                for (iter = ca; iter; iter = iter->next)
                        output_commit_title(o, iter->item);
        }
 
        merged_common_ancestors = pop_commit(&ca);
        if (merged_common_ancestors == NULL) {
-               /* if there is no common ancestor, make an empty tree */
-               struct tree *tree = xcalloc(1, sizeof(struct tree));
+               /* if there is no common ancestor, use an empty tree */
+               struct tree *tree;
 
-               tree->object.parsed = 1;
-               tree->object.type = OBJ_TREE;
-               pretend_sha1_file(NULL, 0, OBJ_TREE, tree->object.sha1);
+               tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
                merged_common_ancestors = make_virtual_commit(tree, "ancestor");
        }
 
@@ -1820,7 +1937,7 @@ int merge_recursive(struct merge_options *o,
                o->call_depth--;
 
                if (!merged_common_ancestors)
-                       die("merge returned no commit");
+                       die(_("merge returned no commit"));
        }
 
        discard_cache();
@@ -1877,7 +1994,7 @@ int merge_recursive_generic(struct merge_options *o,
                for (i = 0; i < num_base_list; ++i) {
                        struct commit *base;
                        if (!(base = get_ref(base_list[i], sha1_to_hex(base_list[i]))))
-                               return error("Could not parse object '%s'",
+                               return error(_("Could not parse object '%s'"),
                                        sha1_to_hex(base_list[i]));
                        commit_list_insert(base, &ca);
                }
@@ -1889,7 +2006,7 @@ int merge_recursive_generic(struct merge_options *o,
        if (active_cache_changed &&
                        (write_cache(index_fd, active_cache, active_nr) ||
                         commit_locked_index(lock)))
-               return error("Unable to write index.");
+               return error(_("Unable to write index."));
 
        return clean ? 0 : 1;
 }
@@ -1948,7 +2065,9 @@ int parse_merge_opt(struct merge_options *o, const char *s)
        else if (!prefixcmp(s, "subtree="))
                o->subtree_shift = s + strlen("subtree=");
        else if (!strcmp(s, "patience"))
-               o->xdl_opts |= XDF_PATIENCE_DIFF;
+               o->xdl_opts = DIFF_WITH_ALG(o, PATIENCE_DIFF);
+       else if (!strcmp(s, "histogram"))
+               o->xdl_opts = DIFF_WITH_ALG(o, HISTOGRAM_DIFF);
        else if (!strcmp(s, "ignore-space-change"))
                o->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
        else if (!strcmp(s, "ignore-all-space"))