tree-diff.con commit Merge remote-tracking branch 'ko/maint' into jc/diff-index-quick-exit-early (2d11f21)
   1/*
   2 * Helper functions for tree diff generation
   3 */
   4#include "cache.h"
   5#include "diff.h"
   6#include "diffcore.h"
   7#include "tree.h"
   8
   9static void show_entry(struct diff_options *opt, const char *prefix,
  10                       struct tree_desc *desc, struct strbuf *base);
  11
  12static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
  13                              struct strbuf *base, struct diff_options *opt)
  14{
  15        unsigned mode1, mode2;
  16        const char *path1, *path2;
  17        const unsigned char *sha1, *sha2;
  18        int cmp, pathlen1, pathlen2;
  19        int old_baselen = base->len;
  20
  21        sha1 = tree_entry_extract(t1, &path1, &mode1);
  22        sha2 = tree_entry_extract(t2, &path2, &mode2);
  23
  24        pathlen1 = tree_entry_len(path1, sha1);
  25        pathlen2 = tree_entry_len(path2, sha2);
  26        cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
  27        if (cmp < 0) {
  28                show_entry(opt, "-", t1, base);
  29                return -1;
  30        }
  31        if (cmp > 0) {
  32                show_entry(opt, "+", t2, base);
  33                return 1;
  34        }
  35        if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
  36                return 0;
  37
  38        /*
  39         * If the filemode has changed to/from a directory from/to a regular
  40         * file, we need to consider it a remove and an add.
  41         */
  42        if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
  43                show_entry(opt, "-", t1, base);
  44                show_entry(opt, "+", t2, base);
  45                return 0;
  46        }
  47
  48        strbuf_add(base, path1, pathlen1);
  49        if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
  50                if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
  51                        opt->change(opt, mode1, mode2,
  52                                    sha1, sha2, base->buf, 0, 0);
  53                }
  54                strbuf_addch(base, '/');
  55                diff_tree_sha1(sha1, sha2, base->buf, opt);
  56        } else {
  57                opt->change(opt, mode1, mode2, sha1, sha2, base->buf, 0, 0);
  58        }
  59        strbuf_setlen(base, old_baselen);
  60        return 0;
  61}
  62
  63/* A whole sub-tree went away or appeared */
  64static void show_tree(struct diff_options *opt, const char *prefix,
  65                      struct tree_desc *desc, struct strbuf *base)
  66{
  67        int all_interesting = 0;
  68        while (desc->size) {
  69                int show;
  70
  71                if (all_interesting)
  72                        show = 1;
  73                else {
  74                        show = tree_entry_interesting(&desc->entry, base, 0,
  75                                                      &opt->pathspec);
  76                        if (show == 2)
  77                                all_interesting = 1;
  78                }
  79                if (show < 0)
  80                        break;
  81                if (show)
  82                        show_entry(opt, prefix, desc, base);
  83                update_tree_entry(desc);
  84        }
  85}
  86
  87/* A file entry went away or appeared */
  88static void show_entry(struct diff_options *opt, const char *prefix,
  89                       struct tree_desc *desc, struct strbuf *base)
  90{
  91        unsigned mode;
  92        const char *path;
  93        const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
  94        int pathlen = tree_entry_len(path, sha1);
  95        int old_baselen = base->len;
  96
  97        strbuf_add(base, path, pathlen);
  98        if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
  99                enum object_type type;
 100                struct tree_desc inner;
 101                void *tree;
 102                unsigned long size;
 103
 104                tree = read_sha1_file(sha1, &type, &size);
 105                if (!tree || type != OBJ_TREE)
 106                        die("corrupt tree sha %s", sha1_to_hex(sha1));
 107
 108                if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
 109                        opt->add_remove(opt, *prefix, mode, sha1, base->buf, 0);
 110
 111                strbuf_addch(base, '/');
 112
 113                init_tree_desc(&inner, tree, size);
 114                show_tree(opt, prefix, &inner, base);
 115                free(tree);
 116        } else
 117                opt->add_remove(opt, prefix[0], mode, sha1, base->buf, 0);
 118
 119        strbuf_setlen(base, old_baselen);
 120}
 121
 122static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
 123                               struct diff_options *opt, int *all_interesting)
 124{
 125        while (t->size) {
 126                int show = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
 127                if (show == 2)
 128                        *all_interesting = 1;
 129                if (!show) {
 130                        update_tree_entry(t);
 131                        continue;
 132                }
 133                /* Skip it all? */
 134                if (show < 0)
 135                        t->size = 0;
 136                return;
 137        }
 138}
 139
 140int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
 141              const char *base_str, struct diff_options *opt)
 142{
 143        struct strbuf base;
 144        int baselen = strlen(base_str);
 145        int all_t1_interesting = 0;
 146        int all_t2_interesting = 0;
 147
 148        /* Enable recursion indefinitely */
 149        opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
 150        opt->pathspec.max_depth = -1;
 151
 152        strbuf_init(&base, PATH_MAX);
 153        strbuf_add(&base, base_str, baselen);
 154
 155        for (;;) {
 156                if (diff_can_quit_early(opt))
 157                        break;
 158                if (opt->pathspec.nr) {
 159                        if (!all_t1_interesting)
 160                                skip_uninteresting(t1, &base, opt, &all_t1_interesting);
 161                        if (!all_t2_interesting)
 162                                skip_uninteresting(t2, &base, opt, &all_t2_interesting);
 163                }
 164                if (!t1->size) {
 165                        if (!t2->size)
 166                                break;
 167                        show_entry(opt, "+", t2, &base);
 168                        update_tree_entry(t2);
 169                        continue;
 170                }
 171                if (!t2->size) {
 172                        show_entry(opt, "-", t1, &base);
 173                        update_tree_entry(t1);
 174                        continue;
 175                }
 176                switch (compare_tree_entry(t1, t2, &base, opt)) {
 177                case -1:
 178                        update_tree_entry(t1);
 179                        continue;
 180                case 0:
 181                        update_tree_entry(t1);
 182                        /* Fallthrough */
 183                case 1:
 184                        update_tree_entry(t2);
 185                        continue;
 186                }
 187                die("git diff-tree: internal error");
 188        }
 189
 190        strbuf_release(&base);
 191        return 0;
 192}
 193
 194/*
 195 * Does it look like the resulting diff might be due to a rename?
 196 *  - single entry
 197 *  - not a valid previous file
 198 */
 199static inline int diff_might_be_rename(void)
 200{
 201        return diff_queued_diff.nr == 1 &&
 202                !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
 203}
 204
 205static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
 206{
 207        struct diff_options diff_opts;
 208        struct diff_queue_struct *q = &diff_queued_diff;
 209        struct diff_filepair *choice;
 210        const char *paths[1];
 211        int i;
 212
 213        /* Remove the file creation entry from the diff queue, and remember it */
 214        choice = q->queue[0];
 215        q->nr = 0;
 216
 217        diff_setup(&diff_opts);
 218        DIFF_OPT_SET(&diff_opts, RECURSIVE);
 219        DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
 220        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 221        diff_opts.single_follow = opt->pathspec.raw[0];
 222        diff_opts.break_opt = opt->break_opt;
 223        paths[0] = NULL;
 224        diff_tree_setup_paths(paths, &diff_opts);
 225        if (diff_setup_done(&diff_opts) < 0)
 226                die("unable to set up diff options to follow renames");
 227        diff_tree(t1, t2, base, &diff_opts);
 228        diffcore_std(&diff_opts);
 229        diff_tree_release_paths(&diff_opts);
 230
 231        /* Go through the new set of filepairing, and see if we find a more interesting one */
 232        opt->found_follow = 0;
 233        for (i = 0; i < q->nr; i++) {
 234                struct diff_filepair *p = q->queue[i];
 235
 236                /*
 237                 * Found a source? Not only do we use that for the new
 238                 * diff_queued_diff, we will also use that as the path in
 239                 * the future!
 240                 */
 241                if ((p->status == 'R' || p->status == 'C') &&
 242                    !strcmp(p->two->path, opt->pathspec.raw[0])) {
 243                        /* Switch the file-pairs around */
 244                        q->queue[i] = choice;
 245                        choice = p;
 246
 247                        /* Update the path we use from now on.. */
 248                        diff_tree_release_paths(opt);
 249                        opt->pathspec.raw[0] = xstrdup(p->one->path);
 250                        diff_tree_setup_paths(opt->pathspec.raw, opt);
 251
 252                        /*
 253                         * The caller expects us to return a set of vanilla
 254                         * filepairs to let a later call to diffcore_std()
 255                         * it makes to sort the renames out (among other
 256                         * things), but we already have found renames
 257                         * ourselves; signal diffcore_std() not to muck with
 258                         * rename information.
 259                         */
 260                        opt->found_follow = 1;
 261                        break;
 262                }
 263        }
 264
 265        /*
 266         * Then, discard all the non-relevant file pairs...
 267         */
 268        for (i = 0; i < q->nr; i++) {
 269                struct diff_filepair *p = q->queue[i];
 270                diff_free_filepair(p);
 271        }
 272
 273        /*
 274         * .. and re-instate the one we want (which might be either the
 275         * original one, or the rename/copy we found)
 276         */
 277        q->queue[0] = choice;
 278        q->nr = 1;
 279}
 280
 281int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
 282{
 283        void *tree1, *tree2;
 284        struct tree_desc t1, t2;
 285        unsigned long size1, size2;
 286        int retval;
 287
 288        tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
 289        if (!tree1)
 290                die("unable to read source tree (%s)", sha1_to_hex(old));
 291        tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
 292        if (!tree2)
 293                die("unable to read destination tree (%s)", sha1_to_hex(new));
 294        init_tree_desc(&t1, tree1, size1);
 295        init_tree_desc(&t2, tree2, size2);
 296        retval = diff_tree(&t1, &t2, base, opt);
 297        if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
 298                init_tree_desc(&t1, tree1, size1);
 299                init_tree_desc(&t2, tree2, size2);
 300                try_to_follow_renames(&t1, &t2, base, opt);
 301        }
 302        free(tree1);
 303        free(tree2);
 304        return retval;
 305}
 306
 307int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
 308{
 309        int retval;
 310        void *tree;
 311        unsigned long size;
 312        struct tree_desc empty, real;
 313
 314        tree = read_object_with_reference(new, tree_type, &size, NULL);
 315        if (!tree)
 316                die("unable to read root tree (%s)", sha1_to_hex(new));
 317        init_tree_desc(&real, tree, size);
 318
 319        init_tree_desc(&empty, "", 0);
 320        retval = diff_tree(&empty, &real, base, opt);
 321        free(tree);
 322        return retval;
 323}
 324
 325void diff_tree_release_paths(struct diff_options *opt)
 326{
 327        free_pathspec(&opt->pathspec);
 328}
 329
 330void diff_tree_setup_paths(const char **p, struct diff_options *opt)
 331{
 332        init_pathspec(&opt->pathspec, p);
 333}