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