t3301: abstract away SHA-1-specific constants
[gitweb.git] / builtin / merge-tree.c
index 4984b7e12e90aab356606e6e7d3e6526f31c8c73..97b54caeb90085e0fb4a88898ac87c7a5cb45eed 100644 (file)
@@ -1,3 +1,4 @@
+#define USE_THE_INDEX_COMPATIBILITY_MACROS
 #include "builtin.h"
 #include "tree-walk.h"
 #include "xdiff-interface.h"
@@ -76,7 +77,8 @@ static void *result(struct merge_list *entry, unsigned long *size)
        their = NULL;
        if (entry)
                their = entry->blob;
-       return merge_blobs(&the_index, path, base, our, their, size);
+       return merge_blobs(the_repository->index, path,
+                          base, our, their, size);
 }
 
 static void *origin(struct merge_list *entry, unsigned long *size)
@@ -154,15 +156,15 @@ static void show_result(void)
 /* An empty entry never compares same, not even to another empty entry */
 static int same_entry(struct name_entry *a, struct name_entry *b)
 {
-       return  a->oid &&
-               b->oid &&
-               oideq(a->oid, b->oid) &&
+       return  !is_null_oid(&a->oid) &&
+               !is_null_oid(&b->oid) &&
+               oideq(&a->oid, &b->oid) &&
                a->mode == b->mode;
 }
 
 static int both_empty(struct name_entry *a, struct name_entry *b)
 {
-       return !(a->oid || b->oid);
+       return is_null_oid(&a->oid) && is_null_oid(&b->oid);
 }
 
 static struct merge_list *create_entry(unsigned stage, unsigned mode, const struct object_id *oid, const char *path)
@@ -178,7 +180,7 @@ static struct merge_list *create_entry(unsigned stage, unsigned mode, const stru
 
 static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
 {
-       char *path = xmallocz(traverse_path_len(info, n));
+       char *path = xmallocz(traverse_path_len(info, n) + the_hash_algo->rawsz);
        return make_traverse_path(path, info, n);
 }
 
@@ -192,8 +194,8 @@ static void resolve(const struct traverse_info *info, struct name_entry *ours, s
                return;
 
        path = traverse_path(info, result);
-       orig = create_entry(2, ours->mode, ours->oid, path);
-       final = create_entry(0, result->mode, result->oid, path);
+       orig = create_entry(2, ours->mode, &ours->oid, path);
+       final = create_entry(0, result->mode, &result->oid, path);
 
        final->link = orig;
 
@@ -203,6 +205,7 @@ static void resolve(const struct traverse_info *info, struct name_entry *ours, s
 static void unresolved_directory(const struct traverse_info *info,
                                 struct name_entry n[3])
 {
+       struct repository *r = the_repository;
        char *newbase;
        struct name_entry *p;
        struct tree_desc t[3];
@@ -217,10 +220,10 @@ static void unresolved_directory(const struct traverse_info *info,
 
        newbase = traverse_path(info, p);
 
-#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? (e)->oid : NULL)
-       buf0 = fill_tree_descriptor(t + 0, ENTRY_OID(n + 0));
-       buf1 = fill_tree_descriptor(t + 1, ENTRY_OID(n + 1));
-       buf2 = fill_tree_descriptor(t + 2, ENTRY_OID(n + 2));
+#define ENTRY_OID(e) (((e)->mode && S_ISDIR((e)->mode)) ? &(e)->oid : NULL)
+       buf0 = fill_tree_descriptor(r, t + 0, ENTRY_OID(n + 0));
+       buf1 = fill_tree_descriptor(r, t + 1, ENTRY_OID(n + 1));
+       buf2 = fill_tree_descriptor(r, t + 2, ENTRY_OID(n + 2));
 #undef ENTRY_OID
 
        merge_trees(t, newbase);
@@ -243,7 +246,7 @@ static struct merge_list *link_entry(unsigned stage, const struct traverse_info
                path = entry->path;
        else
                path = traverse_path(info, n);
-       link = create_entry(stage, n->mode, n->oid, path);
+       link = create_entry(stage, n->mode, &n->oid, path);
        link->link = entry;
        return link;
 }
@@ -318,7 +321,7 @@ static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, s
        }
 
        if (same_entry(entry+0, entry+1)) {
-               if (entry[2].oid && !S_ISDIR(entry[2].mode)) {
+               if (!is_null_oid(&entry[2].oid) && !S_ISDIR(entry[2].mode)) {
                        /* We did not touch, they modified -- take theirs */
                        resolve(info, entry+1, entry+2);
                        return mask;
@@ -349,14 +352,16 @@ static void merge_trees(struct tree_desc t[3], const char *base)
        traverse_trees(&the_index, 3, t, &info);
 }
 
-static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
+static void *get_tree_descriptor(struct repository *r,
+                                struct tree_desc *desc,
+                                const char *rev)
 {
        struct object_id oid;
        void *buf;
 
-       if (get_oid(rev, &oid))
+       if (repo_get_oid(r, rev, &oid))
                die("unknown rev %s", rev);
-       buf = fill_tree_descriptor(desc, &oid);
+       buf = fill_tree_descriptor(r, desc, &oid);
        if (!buf)
                die("%s is not a tree", rev);
        return buf;
@@ -364,15 +369,16 @@ static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
 
 int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 {
+       struct repository *r = the_repository;
        struct tree_desc t[3];
        void *buf1, *buf2, *buf3;
 
        if (argc != 4)
                usage(merge_tree_usage);
 
-       buf1 = get_tree_descriptor(t+0, argv[1]);
-       buf2 = get_tree_descriptor(t+1, argv[2]);
-       buf3 = get_tree_descriptor(t+2, argv[3]);
+       buf1 = get_tree_descriptor(r, t+0, argv[1]);
+       buf2 = get_tree_descriptor(r, t+1, argv[2]);
+       buf3 = get_tree_descriptor(r, t+2, argv[3]);
        merge_trees(t, "");
        free(buf1);
        free(buf2);