]> git.ipfire.org Git - thirdparty/git.git/blobdiff - merge-recursive.c
merge-recursive: provide a better label for diff3 common ancestor
[thirdparty/git.git] / merge-recursive.c
index d2e380b7ed845e6fb2cd5616754a3f12a11c7b18..e6b84db2ef8eb5b9bdc1de66136c54b2848f23fa 100644 (file)
@@ -153,9 +153,9 @@ static struct tree *shift_tree_object(struct repository *repo,
        struct object_id shifted;
 
        if (!*subtree_shift) {
-               shift_tree(&one->object.oid, &two->object.oid, &shifted, 0);
+               shift_tree(repo, &one->object.oid, &two->object.oid, &shifted, 0);
        } else {
-               shift_tree_by(&one->object.oid, &two->object.oid, &shifted,
+               shift_tree_by(repo, &one->object.oid, &two->object.oid, &shifted,
                              subtree_shift);
        }
        if (oideq(&two->object.oid, &shifted))
@@ -465,17 +465,18 @@ static void get_files_dirs(struct merge_options *opt, struct tree *tree)
 {
        struct pathspec match_all;
        memset(&match_all, 0, sizeof(match_all));
-       read_tree_recursive(the_repository, tree, "", 0, 0,
+       read_tree_recursive(opt->repo, tree, "", 0, 0,
                            &match_all, save_files_dirs, opt);
 }
 
-static int get_tree_entry_if_blob(const struct object_id *tree,
+static int get_tree_entry_if_blob(struct repository *r,
+                                 const struct object_id *tree,
                                  const char *path,
                                  struct diff_filespec *dfs)
 {
        int ret;
 
-       ret = get_tree_entry(tree, path, &dfs->oid, &dfs->mode);
+       ret = get_tree_entry(r, tree, path, &dfs->oid, &dfs->mode);
        if (S_ISDIR(dfs->mode)) {
                oidcpy(&dfs->oid, &null_oid);
                dfs->mode = 0;
@@ -487,15 +488,16 @@ static int get_tree_entry_if_blob(const struct object_id *tree,
  * Returns an index_entry instance which doesn't have to correspond to
  * a real cache entry in Git's index.
  */
-static struct stage_data *insert_stage_data(const char *path,
+static struct stage_data *insert_stage_data(struct repository *r,
+               const char *path,
                struct tree *o, struct tree *a, struct tree *b,
                struct string_list *entries)
 {
        struct string_list_item *item;
        struct stage_data *e = xcalloc(1, sizeof(struct stage_data));
-       get_tree_entry_if_blob(&o->object.oid, path, &e->stages[1]);
-       get_tree_entry_if_blob(&a->object.oid, path, &e->stages[2]);
-       get_tree_entry_if_blob(&b->object.oid, path, &e->stages[3]);
+       get_tree_entry_if_blob(r, &o->object.oid, path, &e->stages[1]);
+       get_tree_entry_if_blob(r, &a->object.oid, path, &e->stages[2]);
+       get_tree_entry_if_blob(r, &b->object.oid, path, &e->stages[3]);
        item = string_list_insert(entries, path);
        item->util = e;
        return e;
@@ -1017,7 +1019,7 @@ static int merge_3way(struct merge_options *opt,
 {
        mmfile_t orig, src1, src2;
        struct ll_merge_options ll_opts = {0};
-       char *base_name, *name1, *name2;
+       char *base, *name1, *name2;
        int merge_status;
 
        ll_opts.renormalize = opt->renormalize;
@@ -1041,16 +1043,13 @@ static int merge_3way(struct merge_options *opt,
                }
        }
 
-       assert(a->path && b->path);
-       if (strcmp(a->path, b->path) ||
-           (opt->ancestor != NULL && strcmp(a->path, o->path) != 0)) {
-               base_name = opt->ancestor == NULL ? NULL :
-                       mkpathdup("%s:%s", opt->ancestor, o->path);
+       assert(a->path && b->path && o->path && opt->ancestor);
+       if (strcmp(a->path, b->path) || strcmp(a->path, o->path) != 0) {
+               base  = mkpathdup("%s:%s", opt->ancestor, o->path);
                name1 = mkpathdup("%s:%s", branch1, a->path);
                name2 = mkpathdup("%s:%s", branch2, b->path);
        } else {
-               base_name = opt->ancestor == NULL ? NULL :
-                       mkpathdup("%s", opt->ancestor);
+               base  = mkpathdup("%s", opt->ancestor);
                name1 = mkpathdup("%s", branch1);
                name2 = mkpathdup("%s", branch2);
        }
@@ -1059,11 +1058,11 @@ static int merge_3way(struct merge_options *opt,
        read_mmblob(&src1, &a->oid);
        read_mmblob(&src2, &b->oid);
 
-       merge_status = ll_merge(result_buf, a->path, &orig, base_name,
+       merge_status = ll_merge(result_buf, a->path, &orig, base,
                                &src1, name1, &src2, name2,
                                opt->repo->index, &ll_opts);
 
-       free(base_name);
+       free(base);
        free(name1);
        free(name2);
        free(orig.ptr);
@@ -1900,12 +1899,14 @@ static struct diff_queue_struct *get_diffpairs(struct merge_options *opt,
        return ret;
 }
 
-static int tree_has_path(struct tree *tree, const char *path)
+static int tree_has_path(struct repository *r, struct tree *tree,
+                        const char *path)
 {
        struct object_id hashy;
        unsigned short mode_o;
 
-       return !get_tree_entry(&tree->object.oid, path,
+       return !get_tree_entry(r,
+                              &tree->object.oid, path,
                               &hashy, &mode_o);
 }
 
@@ -2056,7 +2057,7 @@ static char *handle_path_level_conflicts(struct merge_options *opt,
         */
        if (collision_ent->reported_already) {
                clean = 0;
-       } else if (tree_has_path(tree, new_path)) {
+       } else if (tree_has_path(opt->repo, tree, new_path)) {
                collision_ent->reported_already = 1;
                strbuf_add_separated_string_list(&collision_paths, ", ",
                                                 &collision_ent->source_files);
@@ -2134,7 +2135,7 @@ static void handle_directory_level_conflicts(struct merge_options *opt,
                        string_list_append(&remove_from_merge,
                                           merge_ent->dir)->util = merge_ent;
                        strbuf_release(&merge_ent->new_dir);
-               } else if (tree_has_path(head, head_ent->dir)) {
+               } else if (tree_has_path(opt->repo, head, head_ent->dir)) {
                        /* 2. This wasn't a directory rename after all */
                        string_list_append(&remove_from_head,
                                           head_ent->dir)->util = head_ent;
@@ -2148,7 +2149,7 @@ static void handle_directory_level_conflicts(struct merge_options *opt,
        hashmap_iter_init(dir_re_merge, &iter);
        while ((merge_ent = hashmap_iter_next(&iter))) {
                head_ent = dir_rename_find_entry(dir_re_head, merge_ent->dir);
-               if (tree_has_path(merge, merge_ent->dir)) {
+               if (tree_has_path(opt->repo, merge, merge_ent->dir)) {
                        /* 2. This wasn't a directory rename after all */
                        string_list_append(&remove_from_merge,
                                           merge_ent->dir)->util = merge_ent;
@@ -2477,7 +2478,7 @@ static void apply_directory_rename_modifications(struct merge_options *opt,
                if (pair->status == 'R')
                        re->dst_entry->processed = 1;
 
-               re->dst_entry = insert_stage_data(new_path,
+               re->dst_entry = insert_stage_data(opt->repo, new_path,
                                                  o_tree, a_tree, b_tree,
                                                  entries);
                item = string_list_insert(entries, new_path);
@@ -2500,7 +2501,8 @@ static void apply_directory_rename_modifications(struct merge_options *opt,
         * the various handle_rename_*() functions update the index
         * explicitly rather than relying on unpack_trees() to have done it.
         */
-       get_tree_entry(&tree->object.oid,
+       get_tree_entry(opt->repo,
+                      &tree->object.oid,
                       pair->two->path,
                       &re->dst_entry->stages[stage].oid,
                       &re->dst_entry->stages[stage].mode);
@@ -2585,14 +2587,16 @@ static struct string_list *get_renames(struct merge_options *opt,
                re->dir_rename_original_dest = NULL;
                item = string_list_lookup(entries, re->pair->one->path);
                if (!item)
-                       re->src_entry = insert_stage_data(re->pair->one->path,
+                       re->src_entry = insert_stage_data(opt->repo,
+                                       re->pair->one->path,
                                        o_tree, a_tree, b_tree, entries);
                else
                        re->src_entry = item->util;
 
                item = string_list_lookup(entries, re->pair->two->path);
                if (!item)
-                       re->dst_entry = insert_stage_data(re->pair->two->path,
+                       re->dst_entry = insert_stage_data(opt->repo,
+                                       re->pair->two->path,
                                        o_tree, a_tree, b_tree, entries);
                else
                        re->dst_entry = item->util;
@@ -2856,7 +2860,8 @@ static int detect_and_process_renames(struct merge_options *opt,
        head_pairs = get_diffpairs(opt, common, head);
        merge_pairs = get_diffpairs(opt, common, merge);
 
-       if (opt->detect_directory_renames) {
+       if ((opt->detect_directory_renames == 2) ||
+           (opt->detect_directory_renames == 1 && !opt->call_depth)) {
                dir_re_head = get_directory_renames(head_pairs);
                dir_re_merge = get_directory_renames(merge_pairs);
 
@@ -3382,6 +3387,8 @@ int merge_trees(struct merge_options *opt,
        int code, clean;
        struct strbuf sb = STRBUF_INIT;
 
+       assert(opt->ancestor != NULL);
+
        if (!opt->call_depth && repo_index_has_changes(opt->repo, head, &sb)) {
                err(opt, _("Your local changes to the following files would be overwritten by merge:\n  %s"),
                    sb.buf);
@@ -3500,6 +3507,11 @@ int merge_recursive(struct merge_options *opt,
        struct commit *merged_common_ancestors;
        struct tree *mrtree;
        int clean;
+       const char *ancestor_name;
+       struct strbuf merge_base_abbrev = STRBUF_INIT;
+
+       if (!opt->call_depth)
+               assert(opt->ancestor == NULL);
 
        if (show(opt, 4)) {
                output(opt, 4, _("Merging:"));
@@ -3528,6 +3540,14 @@ int merge_recursive(struct merge_options *opt,
 
                tree = lookup_tree(opt->repo, opt->repo->hash_algo->empty_tree);
                merged_common_ancestors = make_virtual_commit(opt->repo, tree, "ancestor");
+               ancestor_name = "empty tree";
+       } else if (ca) {
+               ancestor_name = "merged common ancestors";
+       } else {
+               strbuf_add_unique_abbrev(&merge_base_abbrev,
+                                        &merged_common_ancestors->object.oid,
+                                        DEFAULT_ABBREV);
+               ancestor_name = merge_base_abbrev.buf;
        }
 
        for (iter = ca; iter; iter = iter->next) {
@@ -3561,10 +3581,11 @@ int merge_recursive(struct merge_options *opt,
        if (!opt->call_depth)
                repo_read_index(opt->repo);
 
-       opt->ancestor = "merged common ancestors";
+       opt->ancestor = ancestor_name;
        clean = merge_trees(opt, get_commit_tree(h1), get_commit_tree(h2),
                            get_commit_tree(merged_common_ancestors),
                            &mrtree);
+       strbuf_release(&merge_base_abbrev);
        if (clean < 0) {
                flush_output(opt);
                return clean;