builtin / merge-tree.con commit t6022: Add testcase for spurious "refusing to lose untracked" messages (3f680ff)
   1#include "builtin.h"
   2#include "tree-walk.h"
   3#include "xdiff-interface.h"
   4#include "blob.h"
   5#include "exec_cmd.h"
   6
   7static const char merge_tree_usage[] = "git merge-tree <base-tree> <branch1> <branch2>";
   8static int resolve_directories = 1;
   9
  10struct merge_list {
  11        struct merge_list *next;
  12        struct merge_list *link;        /* other stages for this object */
  13
  14        unsigned int stage : 2,
  15                     flags : 30;
  16        unsigned int mode;
  17        const char *path;
  18        struct blob *blob;
  19};
  20
  21static struct merge_list *merge_result, **merge_result_end = &merge_result;
  22
  23static void add_merge_entry(struct merge_list *entry)
  24{
  25        *merge_result_end = entry;
  26        merge_result_end = &entry->next;
  27}
  28
  29static void merge_trees(struct tree_desc t[3], const char *base);
  30
  31static const char *explanation(struct merge_list *entry)
  32{
  33        switch (entry->stage) {
  34        case 0:
  35                return "merged";
  36        case 3:
  37                return "added in remote";
  38        case 2:
  39                if (entry->link)
  40                        return "added in both";
  41                return "added in local";
  42        }
  43
  44        /* Existed in base */
  45        entry = entry->link;
  46        if (!entry)
  47                return "removed in both";
  48
  49        if (entry->link)
  50                return "changed in both";
  51
  52        if (entry->stage == 3)
  53                return "removed in local";
  54        return "removed in remote";
  55}
  56
  57extern void *merge_file(const char *, struct blob *, struct blob *, struct blob *, unsigned long *);
  58
  59static void *result(struct merge_list *entry, unsigned long *size)
  60{
  61        enum object_type type;
  62        struct blob *base, *our, *their;
  63        const char *path = entry->path;
  64
  65        if (!entry->stage)
  66                return read_sha1_file(entry->blob->object.sha1, &type, size);
  67        base = NULL;
  68        if (entry->stage == 1) {
  69                base = entry->blob;
  70                entry = entry->link;
  71        }
  72        our = NULL;
  73        if (entry && entry->stage == 2) {
  74                our = entry->blob;
  75                entry = entry->link;
  76        }
  77        their = NULL;
  78        if (entry)
  79                their = entry->blob;
  80        return merge_file(path, base, our, their, size);
  81}
  82
  83static void *origin(struct merge_list *entry, unsigned long *size)
  84{
  85        enum object_type type;
  86        while (entry) {
  87                if (entry->stage == 2)
  88                        return read_sha1_file(entry->blob->object.sha1, &type, size);
  89                entry = entry->link;
  90        }
  91        return NULL;
  92}
  93
  94static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)
  95{
  96        int i;
  97        for (i = 0; i < nbuf; i++)
  98                printf("%.*s", (int) mb[i].size, mb[i].ptr);
  99        return 0;
 100}
 101
 102static void show_diff(struct merge_list *entry)
 103{
 104        unsigned long size;
 105        mmfile_t src, dst;
 106        xpparam_t xpp;
 107        xdemitconf_t xecfg;
 108        xdemitcb_t ecb;
 109
 110        xpp.flags = 0;
 111        memset(&xecfg, 0, sizeof(xecfg));
 112        xecfg.ctxlen = 3;
 113        ecb.outf = show_outf;
 114        ecb.priv = NULL;
 115
 116        src.ptr = origin(entry, &size);
 117        if (!src.ptr)
 118                size = 0;
 119        src.size = size;
 120        dst.ptr = result(entry, &size);
 121        if (!dst.ptr)
 122                size = 0;
 123        dst.size = size;
 124        xdi_diff(&src, &dst, &xpp, &xecfg, &ecb);
 125        free(src.ptr);
 126        free(dst.ptr);
 127}
 128
 129static void show_result_list(struct merge_list *entry)
 130{
 131        printf("%s\n", explanation(entry));
 132        do {
 133                struct merge_list *link = entry->link;
 134                static const char *desc[4] = { "result", "base", "our", "their" };
 135                printf("  %-6s %o %s %s\n", desc[entry->stage], entry->mode, sha1_to_hex(entry->blob->object.sha1), entry->path);
 136                entry = link;
 137        } while (entry);
 138}
 139
 140static void show_result(void)
 141{
 142        struct merge_list *walk;
 143
 144        walk = merge_result;
 145        while (walk) {
 146                show_result_list(walk);
 147                show_diff(walk);
 148                walk = walk->next;
 149        }
 150}
 151
 152/* An empty entry never compares same, not even to another empty entry */
 153static int same_entry(struct name_entry *a, struct name_entry *b)
 154{
 155        return  a->sha1 &&
 156                b->sha1 &&
 157                !hashcmp(a->sha1, b->sha1) &&
 158                a->mode == b->mode;
 159}
 160
 161static struct merge_list *create_entry(unsigned stage, unsigned mode, const unsigned char *sha1, const char *path)
 162{
 163        struct merge_list *res = xcalloc(1, sizeof(*res));
 164
 165        res->stage = stage;
 166        res->path = path;
 167        res->mode = mode;
 168        res->blob = lookup_blob(sha1);
 169        return res;
 170}
 171
 172static char *traverse_path(const struct traverse_info *info, const struct name_entry *n)
 173{
 174        char *path = xmalloc(traverse_path_len(info, n) + 1);
 175        return make_traverse_path(path, info, n);
 176}
 177
 178static void resolve(const struct traverse_info *info, struct name_entry *branch1, struct name_entry *result)
 179{
 180        struct merge_list *orig, *final;
 181        const char *path;
 182
 183        /* If it's already branch1, don't bother showing it */
 184        if (!branch1)
 185                return;
 186
 187        path = traverse_path(info, result);
 188        orig = create_entry(2, branch1->mode, branch1->sha1, path);
 189        final = create_entry(0, result->mode, result->sha1, path);
 190
 191        final->link = orig;
 192
 193        add_merge_entry(final);
 194}
 195
 196static int unresolved_directory(const struct traverse_info *info, struct name_entry n[3])
 197{
 198        char *newbase;
 199        struct name_entry *p;
 200        struct tree_desc t[3];
 201        void *buf0, *buf1, *buf2;
 202
 203        if (!resolve_directories)
 204                return 0;
 205        p = n;
 206        if (!p->mode) {
 207                p++;
 208                if (!p->mode)
 209                        p++;
 210        }
 211        if (!S_ISDIR(p->mode))
 212                return 0;
 213        newbase = traverse_path(info, p);
 214        buf0 = fill_tree_descriptor(t+0, n[0].sha1);
 215        buf1 = fill_tree_descriptor(t+1, n[1].sha1);
 216        buf2 = fill_tree_descriptor(t+2, n[2].sha1);
 217        merge_trees(t, newbase);
 218
 219        free(buf0);
 220        free(buf1);
 221        free(buf2);
 222        free(newbase);
 223        return 1;
 224}
 225
 226
 227static struct merge_list *link_entry(unsigned stage, const struct traverse_info *info, struct name_entry *n, struct merge_list *entry)
 228{
 229        const char *path;
 230        struct merge_list *link;
 231
 232        if (!n->mode)
 233                return entry;
 234        if (entry)
 235                path = entry->path;
 236        else
 237                path = traverse_path(info, n);
 238        link = create_entry(stage, n->mode, n->sha1, path);
 239        link->link = entry;
 240        return link;
 241}
 242
 243static void unresolved(const struct traverse_info *info, struct name_entry n[3])
 244{
 245        struct merge_list *entry = NULL;
 246
 247        if (unresolved_directory(info, n))
 248                return;
 249
 250        /*
 251         * Do them in reverse order so that the resulting link
 252         * list has the stages in order - link_entry adds new
 253         * links at the front.
 254         */
 255        entry = link_entry(3, info, n + 2, entry);
 256        entry = link_entry(2, info, n + 1, entry);
 257        entry = link_entry(1, info, n + 0, entry);
 258
 259        add_merge_entry(entry);
 260}
 261
 262/*
 263 * Merge two trees together (t[1] and t[2]), using a common base (t[0])
 264 * as the origin.
 265 *
 266 * This walks the (sorted) trees in lock-step, checking every possible
 267 * name. Note that directories automatically sort differently from other
 268 * files (see "base_name_compare"), so you'll never see file/directory
 269 * conflicts, because they won't ever compare the same.
 270 *
 271 * IOW, if a directory changes to a filename, it will automatically be
 272 * seen as the directory going away, and the filename being created.
 273 *
 274 * Think of this as a three-way diff.
 275 *
 276 * The output will be either:
 277 *  - successful merge
 278 *       "0 mode sha1 filename"
 279 *    NOTE NOTE NOTE! FIXME! We really really need to walk the index
 280 *    in parallel with this too!
 281 *
 282 *  - conflict:
 283 *      "1 mode sha1 filename"
 284 *      "2 mode sha1 filename"
 285 *      "3 mode sha1 filename"
 286 *    where not all of the 1/2/3 lines may exist, of course.
 287 *
 288 * The successful merge rules are the same as for the three-way merge
 289 * in git-read-tree.
 290 */
 291static int threeway_callback(int n, unsigned long mask, unsigned long dirmask, struct name_entry *entry, struct traverse_info *info)
 292{
 293        /* Same in both? */
 294        if (same_entry(entry+1, entry+2)) {
 295                if (entry[0].sha1) {
 296                        resolve(info, NULL, entry+1);
 297                        return mask;
 298                }
 299        }
 300
 301        if (same_entry(entry+0, entry+1)) {
 302                if (entry[2].sha1 && !S_ISDIR(entry[2].mode)) {
 303                        resolve(info, entry+1, entry+2);
 304                        return mask;
 305                }
 306        }
 307
 308        if (same_entry(entry+0, entry+2)) {
 309                if (entry[1].sha1 && !S_ISDIR(entry[1].mode)) {
 310                        resolve(info, NULL, entry+1);
 311                        return mask;
 312                }
 313        }
 314
 315        unresolved(info, entry);
 316        return mask;
 317}
 318
 319static void merge_trees(struct tree_desc t[3], const char *base)
 320{
 321        struct traverse_info info;
 322
 323        setup_traverse_info(&info, base);
 324        info.fn = threeway_callback;
 325        traverse_trees(3, t, &info);
 326}
 327
 328static void *get_tree_descriptor(struct tree_desc *desc, const char *rev)
 329{
 330        unsigned char sha1[20];
 331        void *buf;
 332
 333        if (get_sha1(rev, sha1))
 334                die("unknown rev %s", rev);
 335        buf = fill_tree_descriptor(desc, sha1);
 336        if (!buf)
 337                die("%s is not a tree", rev);
 338        return buf;
 339}
 340
 341int cmd_merge_tree(int argc, const char **argv, const char *prefix)
 342{
 343        struct tree_desc t[3];
 344        void *buf1, *buf2, *buf3;
 345
 346        if (argc != 4)
 347                usage(merge_tree_usage);
 348
 349        buf1 = get_tree_descriptor(t+0, argv[1]);
 350        buf2 = get_tree_descriptor(t+1, argv[2]);
 351        buf3 = get_tree_descriptor(t+2, argv[3]);
 352        merge_trees(t, "");
 353        free(buf1);
 354        free(buf2);
 355        free(buf3);
 356
 357        show_result();
 358        return 0;
 359}