tree-diff.con commit tree-diff: no need to pass match to skip_uninteresting() (e906612)
   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(&t1->entry);
  25        pathlen2 = tree_entry_len(&t2->entry);
  26
  27        /*
  28         * NOTE files and directories *always* compare differently,
  29         * even when having the same name.
  30         */
  31        cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
  32        if (cmp < 0) {
  33                show_entry(opt, "-", t1, base);
  34                return -1;
  35        }
  36        if (cmp > 0) {
  37                show_entry(opt, "+", t2, base);
  38                return 1;
  39        }
  40        if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
  41                return 0;
  42
  43        strbuf_add(base, path1, pathlen1);
  44        if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
  45                if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
  46                        opt->change(opt, mode1, mode2,
  47                                    sha1, sha2, 1, 1, base->buf, 0, 0);
  48                }
  49                strbuf_addch(base, '/');
  50                diff_tree_sha1(sha1, sha2, base->buf, opt);
  51        } else {
  52                opt->change(opt, mode1, mode2, sha1, sha2, 1, 1, base->buf, 0, 0);
  53        }
  54        strbuf_setlen(base, old_baselen);
  55        return 0;
  56}
  57
  58/* A whole sub-tree went away or appeared */
  59static void show_tree(struct diff_options *opt, const char *prefix,
  60                      struct tree_desc *desc, struct strbuf *base)
  61{
  62        enum interesting match = entry_not_interesting;
  63        for (; desc->size; update_tree_entry(desc)) {
  64                if (match != all_entries_interesting) {
  65                        match = tree_entry_interesting(&desc->entry, base, 0,
  66                                                       &opt->pathspec);
  67                        if (match == all_entries_not_interesting)
  68                                break;
  69                        if (match == entry_not_interesting)
  70                                continue;
  71                }
  72                show_entry(opt, prefix, desc, base);
  73        }
  74}
  75
  76/* A file entry went away or appeared */
  77static void show_entry(struct diff_options *opt, const char *prefix,
  78                       struct tree_desc *desc, struct strbuf *base)
  79{
  80        unsigned mode;
  81        const char *path;
  82        const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
  83        int pathlen = tree_entry_len(&desc->entry);
  84        int old_baselen = base->len;
  85
  86        strbuf_add(base, path, pathlen);
  87        if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
  88                enum object_type type;
  89                struct tree_desc inner;
  90                void *tree;
  91                unsigned long size;
  92
  93                tree = read_sha1_file(sha1, &type, &size);
  94                if (!tree || type != OBJ_TREE)
  95                        die("corrupt tree sha %s", sha1_to_hex(sha1));
  96
  97                if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE))
  98                        opt->add_remove(opt, *prefix, mode, sha1, 1, base->buf, 0);
  99
 100                strbuf_addch(base, '/');
 101
 102                init_tree_desc(&inner, tree, size);
 103                show_tree(opt, prefix, &inner, base);
 104                free(tree);
 105        } else
 106                opt->add_remove(opt, prefix[0], mode, sha1, 1, base->buf, 0);
 107
 108        strbuf_setlen(base, old_baselen);
 109}
 110
 111static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
 112                               struct diff_options *opt)
 113{
 114        enum interesting match;
 115
 116        while (t->size) {
 117                match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
 118                if (match) {
 119                        if (match == all_entries_not_interesting)
 120                                t->size = 0;
 121                        break;
 122                }
 123                update_tree_entry(t);
 124        }
 125}
 126
 127int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
 128              const char *base_str, struct diff_options *opt)
 129{
 130        struct strbuf base;
 131        int baselen = strlen(base_str);
 132
 133        /* Enable recursion indefinitely */
 134        opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
 135
 136        strbuf_init(&base, PATH_MAX);
 137        strbuf_add(&base, base_str, baselen);
 138
 139        for (;;) {
 140                if (diff_can_quit_early(opt))
 141                        break;
 142                if (opt->pathspec.nr) {
 143                        skip_uninteresting(t1, &base, opt);
 144                        skip_uninteresting(t2, &base, opt);
 145                }
 146                if (!t1->size) {
 147                        if (!t2->size)
 148                                break;
 149                        show_entry(opt, "+", t2, &base);
 150                        update_tree_entry(t2);
 151                        continue;
 152                }
 153                if (!t2->size) {
 154                        show_entry(opt, "-", t1, &base);
 155                        update_tree_entry(t1);
 156                        continue;
 157                }
 158                switch (compare_tree_entry(t1, t2, &base, opt)) {
 159                case -1:
 160                        update_tree_entry(t1);
 161                        continue;
 162                case 0:
 163                        update_tree_entry(t1);
 164                        /* Fallthrough */
 165                case 1:
 166                        update_tree_entry(t2);
 167                        continue;
 168                }
 169                die("git diff-tree: internal error");
 170        }
 171
 172        strbuf_release(&base);
 173        return 0;
 174}
 175
 176/*
 177 * Does it look like the resulting diff might be due to a rename?
 178 *  - single entry
 179 *  - not a valid previous file
 180 */
 181static inline int diff_might_be_rename(void)
 182{
 183        return diff_queued_diff.nr == 1 &&
 184                !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
 185}
 186
 187static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
 188{
 189        struct diff_options diff_opts;
 190        struct diff_queue_struct *q = &diff_queued_diff;
 191        struct diff_filepair *choice;
 192        int i;
 193
 194        /*
 195         * follow-rename code is very specific, we need exactly one
 196         * path. Magic that matches more than one path is not
 197         * supported.
 198         */
 199        GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
 200#if 0
 201        /*
 202         * We should reject wildcards as well. Unfortunately we
 203         * haven't got a reliable way to detect that 'foo\*bar' in
 204         * fact has no wildcards. nowildcard_len is merely a hint for
 205         * optimization. Let it slip for now until wildmatch is taught
 206         * about dry-run mode and returns wildcard info.
 207         */
 208        if (opt->pathspec.has_wildcard)
 209                die("BUG:%s:%d: wildcards are not supported",
 210                    __FILE__, __LINE__);
 211#endif
 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.items[0].match;
 222        diff_opts.break_opt = opt->break_opt;
 223        diff_opts.rename_score = opt->rename_score;
 224        diff_setup_done(&diff_opts);
 225        diff_tree(t1, t2, base, &diff_opts);
 226        diffcore_std(&diff_opts);
 227        free_pathspec(&diff_opts.pathspec);
 228
 229        /* Go through the new set of filepairing, and see if we find a more interesting one */
 230        opt->found_follow = 0;
 231        for (i = 0; i < q->nr; i++) {
 232                struct diff_filepair *p = q->queue[i];
 233
 234                /*
 235                 * Found a source? Not only do we use that for the new
 236                 * diff_queued_diff, we will also use that as the path in
 237                 * the future!
 238                 */
 239                if ((p->status == 'R' || p->status == 'C') &&
 240                    !strcmp(p->two->path, opt->pathspec.items[0].match)) {
 241                        const char *path[2];
 242
 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                        path[0] = p->one->path;
 249                        path[1] = NULL;
 250                        free_pathspec(&opt->pathspec);
 251                        parse_pathspec(&opt->pathspec,
 252                                       PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
 253                                       PATHSPEC_LITERAL_PATH, "", path);
 254
 255                        /*
 256                         * The caller expects us to return a set of vanilla
 257                         * filepairs to let a later call to diffcore_std()
 258                         * it makes to sort the renames out (among other
 259                         * things), but we already have found renames
 260                         * ourselves; signal diffcore_std() not to muck with
 261                         * rename information.
 262                         */
 263                        opt->found_follow = 1;
 264                        break;
 265                }
 266        }
 267
 268        /*
 269         * Then, discard all the non-relevant file pairs...
 270         */
 271        for (i = 0; i < q->nr; i++) {
 272                struct diff_filepair *p = q->queue[i];
 273                diff_free_filepair(p);
 274        }
 275
 276        /*
 277         * .. and re-instate the one we want (which might be either the
 278         * original one, or the rename/copy we found)
 279         */
 280        q->queue[0] = choice;
 281        q->nr = 1;
 282}
 283
 284int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
 285{
 286        void *tree1, *tree2;
 287        struct tree_desc t1, t2;
 288        unsigned long size1, size2;
 289        int retval;
 290
 291        tree1 = fill_tree_descriptor(&t1, old);
 292        tree2 = fill_tree_descriptor(&t2, new);
 293        size1 = t1.size;
 294        size2 = t2.size;
 295        retval = diff_tree(&t1, &t2, base, opt);
 296        if (!*base && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
 297                init_tree_desc(&t1, tree1, size1);
 298                init_tree_desc(&t2, tree2, size2);
 299                try_to_follow_renames(&t1, &t2, base, opt);
 300        }
 301        free(tree1);
 302        free(tree2);
 303        return retval;
 304}
 305
 306int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
 307{
 308        return diff_tree_sha1(NULL, new, base, opt);
 309}