4d85fef5d0aebbf5867cd95009721f0e28d71523
   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 char *malloc_base(const char *base, int baselen, const char *path, int pathlen)
  10{
  11        char *newbase = xmalloc(baselen + pathlen + 2);
  12        memcpy(newbase, base, baselen);
  13        memcpy(newbase + baselen, path, pathlen);
  14        memcpy(newbase + baselen + pathlen, "/", 2);
  15        return newbase;
  16}
  17
  18static char *malloc_fullname(const char *base, int baselen, const char *path, int pathlen)
  19{
  20        char *fullname = xmalloc(baselen + pathlen + 1);
  21        memcpy(fullname, base, baselen);
  22        memcpy(fullname + baselen, path, pathlen);
  23        fullname[baselen + pathlen] = 0;
  24        return fullname;
  25}
  26
  27static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
  28                       const char *base, int baselen);
  29
  30static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2, const char *base, int baselen, struct diff_options *opt)
  31{
  32        unsigned mode1, mode2;
  33        const char *path1, *path2;
  34        const unsigned char *sha1, *sha2;
  35        int cmp, pathlen1, pathlen2;
  36        char *fullname;
  37
  38        sha1 = tree_entry_extract(t1, &path1, &mode1);
  39        sha2 = tree_entry_extract(t2, &path2, &mode2);
  40
  41        pathlen1 = tree_entry_len(path1, sha1);
  42        pathlen2 = tree_entry_len(path2, sha2);
  43        cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
  44        if (cmp < 0) {
  45                show_entry(opt, "-", t1, base, baselen);
  46                return -1;
  47        }
  48        if (cmp > 0) {
  49                show_entry(opt, "+", t2, base, baselen);
  50                return 1;
  51        }
  52        if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER) && !hashcmp(sha1, sha2) && mode1 == mode2)
  53                return 0;
  54
  55        /*
  56         * If the filemode has changed to/from a directory from/to a regular
  57         * file, we need to consider it a remove and an add.
  58         */
  59        if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
  60                show_entry(opt, "-", t1, base, baselen);
  61                show_entry(opt, "+", t2, base, baselen);
  62                return 0;
  63        }
  64
  65        if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode1)) {
  66                int retval;
  67                char *newbase = malloc_base(base, baselen, path1, pathlen1);
  68                if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
  69                        newbase[baselen + pathlen1] = 0;
  70                        opt->change(opt, mode1, mode2,
  71                                    sha1, sha2, newbase, 0, 0);
  72                        newbase[baselen + pathlen1] = '/';
  73                }
  74                retval = diff_tree_sha1(sha1, sha2, newbase, opt);
  75                free(newbase);
  76                return retval;
  77        }
  78
  79        fullname = malloc_fullname(base, baselen, path1, pathlen1);
  80        opt->change(opt, mode1, mode2, sha1, sha2, fullname, 0, 0);
  81        free(fullname);
  82        return 0;
  83}
  84
  85/*
  86 * Is a tree entry interesting given the pathspec we have?
  87 *
  88 * Pre-condition: baselen == 0 || base[baselen-1] == '/'
  89 *
  90 * Return:
  91 *  - 2 for "yes, and all subsequent entries will be"
  92 *  - 1 for yes
  93 *  - zero for no
  94 *  - negative for "no, and no subsequent entries will be either"
  95 */
  96static int tree_entry_interesting(struct tree_desc *desc, const char *base, int baselen, struct diff_options *opt)
  97{
  98        const char *path;
  99        const unsigned char *sha1;
 100        unsigned mode;
 101        int i;
 102        int pathlen;
 103        int never_interesting = -1;
 104
 105        if (!opt->nr_paths)
 106                return 1;
 107
 108        sha1 = tree_entry_extract(desc, &path, &mode);
 109
 110        pathlen = tree_entry_len(path, sha1);
 111
 112        for (i = 0; i < opt->nr_paths; i++) {
 113                const char *match = opt->paths[i];
 114                int matchlen = opt->pathlens[i];
 115                int m = -1; /* signals that we haven't called strncmp() */
 116
 117                if (baselen >= matchlen) {
 118                        /* If it doesn't match, move along... */
 119                        if (strncmp(base, match, matchlen))
 120                                continue;
 121
 122                        /*
 123                         * If the base is a subdirectory of a path which
 124                         * was specified, all of them are interesting.
 125                         */
 126                        if (!matchlen ||
 127                            base[matchlen] == '/' ||
 128                            match[matchlen - 1] == '/')
 129                                return 2;
 130
 131                        /* Just a random prefix match */
 132                        continue;
 133                }
 134
 135                /* Does the base match? */
 136                if (strncmp(base, match, baselen))
 137                        continue;
 138
 139                match += baselen;
 140                matchlen -= baselen;
 141
 142                if (never_interesting) {
 143                        /*
 144                         * We have not seen any match that sorts later
 145                         * than the current path.
 146                         */
 147
 148                        /*
 149                         * Does match sort strictly earlier than path
 150                         * with their common parts?
 151                         */
 152                        m = strncmp(match, path,
 153                                    (matchlen < pathlen) ? matchlen : pathlen);
 154                        if (m < 0)
 155                                continue;
 156
 157                        /*
 158                         * If we come here even once, that means there is at
 159                         * least one pathspec that would sort equal to or
 160                         * later than the path we are currently looking at.
 161                         * In other words, if we have never reached this point
 162                         * after iterating all pathspecs, it means all
 163                         * pathspecs are either outside of base, or inside the
 164                         * base but sorts strictly earlier than the current
 165                         * one.  In either case, they will never match the
 166                         * subsequent entries.  In such a case, we initialized
 167                         * the variable to -1 and that is what will be
 168                         * returned, allowing the caller to terminate early.
 169                         */
 170                        never_interesting = 0;
 171                }
 172
 173                if (pathlen > matchlen)
 174                        continue;
 175
 176                if (matchlen > pathlen) {
 177                        if (match[pathlen] != '/')
 178                                continue;
 179                        if (!S_ISDIR(mode))
 180                                continue;
 181                }
 182
 183                if (m == -1)
 184                        /*
 185                         * we cheated and did not do strncmp(), so we do
 186                         * that here.
 187                         */
 188                        m = strncmp(match, path, pathlen);
 189
 190                /*
 191                 * If common part matched earlier then it is a hit,
 192                 * because we rejected the case where path is not a
 193                 * leading directory and is shorter than match.
 194                 */
 195                if (!m)
 196                        return 1;
 197        }
 198        return never_interesting; /* No matches */
 199}
 200
 201/* A whole sub-tree went away or appeared */
 202static void show_tree(struct diff_options *opt, const char *prefix, struct tree_desc *desc, const char *base, int baselen)
 203{
 204        int all_interesting = 0;
 205        while (desc->size) {
 206                int show;
 207
 208                if (all_interesting)
 209                        show = 1;
 210                else {
 211                        show = tree_entry_interesting(desc, base, baselen,
 212                                                      opt);
 213                        if (show == 2)
 214                                all_interesting = 1;
 215                }
 216                if (show < 0)
 217                        break;
 218                if (show)
 219                        show_entry(opt, prefix, desc, base, baselen);
 220                update_tree_entry(desc);
 221        }
 222}
 223
 224/* A file entry went away or appeared */
 225static void show_entry(struct diff_options *opt, const char *prefix, struct tree_desc *desc,
 226                       const char *base, int baselen)
 227{
 228        unsigned mode;
 229        const char *path;
 230        const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
 231        int pathlen = tree_entry_len(path, sha1);
 232
 233        if (DIFF_OPT_TST(opt, RECURSIVE) && S_ISDIR(mode)) {
 234                enum object_type type;
 235                char *newbase = malloc_base(base, baselen, path, pathlen);
 236                struct tree_desc inner;
 237                void *tree;
 238                unsigned long size;
 239
 240                tree = read_sha1_file(sha1, &type, &size);
 241                if (!tree || type != OBJ_TREE)
 242                        die("corrupt tree sha %s", sha1_to_hex(sha1));
 243
 244                if (DIFF_OPT_TST(opt, TREE_IN_RECURSIVE)) {
 245                        newbase[baselen + pathlen] = 0;
 246                        opt->add_remove(opt, *prefix, mode, sha1, newbase, 0);
 247                        newbase[baselen + pathlen] = '/';
 248                }
 249
 250                init_tree_desc(&inner, tree, size);
 251                show_tree(opt, prefix, &inner, newbase, baselen + 1 + pathlen);
 252
 253                free(tree);
 254                free(newbase);
 255        } else {
 256                char *fullname = malloc_fullname(base, baselen, path, pathlen);
 257                opt->add_remove(opt, prefix[0], mode, sha1, fullname, 0);
 258                free(fullname);
 259        }
 260}
 261
 262static void skip_uninteresting(struct tree_desc *t, const char *base, int baselen, struct diff_options *opt)
 263{
 264        int all_interesting = 0;
 265        while (t->size) {
 266                int show;
 267
 268                if (all_interesting)
 269                        show = 1;
 270                else {
 271                        show = tree_entry_interesting(t, base, baselen, opt);
 272                        if (show == 2)
 273                                all_interesting = 1;
 274                }
 275                if (!show) {
 276                        update_tree_entry(t);
 277                        continue;
 278                }
 279                /* Skip it all? */
 280                if (show < 0)
 281                        t->size = 0;
 282                return;
 283        }
 284}
 285
 286int diff_tree(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
 287{
 288        int baselen = strlen(base);
 289
 290        for (;;) {
 291                if (DIFF_OPT_TST(opt, QUICK) &&
 292                    DIFF_OPT_TST(opt, HAS_CHANGES))
 293                        break;
 294                if (opt->nr_paths) {
 295                        skip_uninteresting(t1, base, baselen, opt);
 296                        skip_uninteresting(t2, base, baselen, opt);
 297                }
 298                if (!t1->size) {
 299                        if (!t2->size)
 300                                break;
 301                        show_entry(opt, "+", t2, base, baselen);
 302                        update_tree_entry(t2);
 303                        continue;
 304                }
 305                if (!t2->size) {
 306                        show_entry(opt, "-", t1, base, baselen);
 307                        update_tree_entry(t1);
 308                        continue;
 309                }
 310                switch (compare_tree_entry(t1, t2, base, baselen, opt)) {
 311                case -1:
 312                        update_tree_entry(t1);
 313                        continue;
 314                case 0:
 315                        update_tree_entry(t1);
 316                        /* Fallthrough */
 317                case 1:
 318                        update_tree_entry(t2);
 319                        continue;
 320                }
 321                die("git diff-tree: internal error");
 322        }
 323        return 0;
 324}
 325
 326/*
 327 * Does it look like the resulting diff might be due to a rename?
 328 *  - single entry
 329 *  - not a valid previous file
 330 */
 331static inline int diff_might_be_rename(void)
 332{
 333        return diff_queued_diff.nr == 1 &&
 334                !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
 335}
 336
 337static void try_to_follow_renames(struct tree_desc *t1, struct tree_desc *t2, const char *base, struct diff_options *opt)
 338{
 339        struct diff_options diff_opts;
 340        struct diff_queue_struct *q = &diff_queued_diff;
 341        struct diff_filepair *choice;
 342        const char *paths[1];
 343        int i;
 344
 345        /* Remove the file creation entry from the diff queue, and remember it */
 346        choice = q->queue[0];
 347        q->nr = 0;
 348
 349        diff_setup(&diff_opts);
 350        DIFF_OPT_SET(&diff_opts, RECURSIVE);
 351        diff_opts.detect_rename = DIFF_DETECT_RENAME;
 352        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 353        diff_opts.single_follow = opt->paths[0];
 354        diff_opts.break_opt = opt->break_opt;
 355        paths[0] = NULL;
 356        diff_tree_setup_paths(paths, &diff_opts);
 357        if (diff_setup_done(&diff_opts) < 0)
 358                die("unable to set up diff options to follow renames");
 359        diff_tree(t1, t2, base, &diff_opts);
 360        diffcore_std(&diff_opts);
 361        diff_tree_release_paths(&diff_opts);
 362
 363        /* Go through the new set of filepairing, and see if we find a more interesting one */
 364        for (i = 0; i < q->nr; i++) {
 365                struct diff_filepair *p = q->queue[i];
 366
 367                /*
 368                 * Found a source? Not only do we use that for the new
 369                 * diff_queued_diff, we will also use that as the path in
 370                 * the future!
 371                 */
 372                if ((p->status == 'R' || p->status == 'C') && !strcmp(p->two->path, opt->paths[0])) {
 373                        /* Switch the file-pairs around */
 374                        q->queue[i] = choice;
 375                        choice = p;
 376
 377                        /* Update the path we use from now on.. */
 378                        diff_tree_release_paths(opt);
 379                        opt->paths[0] = xstrdup(p->one->path);
 380                        diff_tree_setup_paths(opt->paths, opt);
 381                        break;
 382                }
 383        }
 384
 385        /*
 386         * Then, discard all the non-relevant file pairs...
 387         */
 388        for (i = 0; i < q->nr; i++) {
 389                struct diff_filepair *p = q->queue[i];
 390                diff_free_filepair(p);
 391        }
 392
 393        /*
 394         * .. and re-instate the one we want (which might be either the
 395         * original one, or the rename/copy we found)
 396         */
 397        q->queue[0] = choice;
 398        q->nr = 1;
 399}
 400
 401int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base, struct diff_options *opt)
 402{
 403        void *tree1, *tree2;
 404        struct tree_desc t1, t2;
 405        unsigned long size1, size2;
 406        int retval;
 407
 408        tree1 = read_object_with_reference(old, tree_type, &size1, NULL);
 409        if (!tree1)
 410                die("unable to read source tree (%s)", sha1_to_hex(old));
 411        tree2 = read_object_with_reference(new, tree_type, &size2, NULL);
 412        if (!tree2)
 413                die("unable to read destination tree (%s)", sha1_to_hex(new));
 414        init_tree_desc(&t1, tree1, size1);
 415        init_tree_desc(&t2, tree2, size2);
 416        retval = diff_tree(&t1, &t2, base, opt);
 417        if (DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename()) {
 418                init_tree_desc(&t1, tree1, size1);
 419                init_tree_desc(&t2, tree2, size2);
 420                try_to_follow_renames(&t1, &t2, base, opt);
 421        }
 422        free(tree1);
 423        free(tree2);
 424        return retval;
 425}
 426
 427int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
 428{
 429        int retval;
 430        void *tree;
 431        unsigned long size;
 432        struct tree_desc empty, real;
 433
 434        tree = read_object_with_reference(new, tree_type, &size, NULL);
 435        if (!tree)
 436                die("unable to read root tree (%s)", sha1_to_hex(new));
 437        init_tree_desc(&real, tree, size);
 438
 439        init_tree_desc(&empty, "", 0);
 440        retval = diff_tree(&empty, &real, base, opt);
 441        free(tree);
 442        return retval;
 443}
 444
 445static int count_paths(const char **paths)
 446{
 447        int i = 0;
 448        while (*paths++)
 449                i++;
 450        return i;
 451}
 452
 453void diff_tree_release_paths(struct diff_options *opt)
 454{
 455        free(opt->pathlens);
 456}
 457
 458void diff_tree_setup_paths(const char **p, struct diff_options *opt)
 459{
 460        opt->nr_paths = 0;
 461        opt->pathlens = NULL;
 462        opt->paths = NULL;
 463
 464        if (p) {
 465                int i;
 466
 467                opt->paths = p;
 468                opt->nr_paths = count_paths(p);
 469                if (opt->nr_paths == 0) {
 470                        opt->pathlens = NULL;
 471                        return;
 472                }
 473                opt->pathlens = xmalloc(opt->nr_paths * sizeof(int));
 474                for (i=0; i < opt->nr_paths; i++)
 475                        opt->pathlens[i] = strlen(p[i]);
 476        }
 477}