tree-diff.con commit coccinelle: make use of the "type" FREE_AND_NULL() rule (6a83d90)
   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
   9/*
  10 * internal mode marker, saying a tree entry != entry of tp[imin]
  11 * (see ll_diff_tree_paths for what it means there)
  12 *
  13 * we will update/use/emit entry for diff only with it unset.
  14 */
  15#define S_IFXMIN_NEQ    S_DIFFTREE_IFXMIN_NEQ
  16
  17#define FAST_ARRAY_ALLOC(x, nr) do { \
  18        if ((nr) <= 2) \
  19                (x) = xalloca((nr) * sizeof(*(x))); \
  20        else \
  21                ALLOC_ARRAY((x), nr); \
  22} while(0)
  23#define FAST_ARRAY_FREE(x, nr) do { \
  24        if ((nr) > 2) \
  25                free((x)); \
  26} while(0)
  27
  28static struct combine_diff_path *ll_diff_tree_paths(
  29        struct combine_diff_path *p, const unsigned char *sha1,
  30        const unsigned char **parents_sha1, int nparent,
  31        struct strbuf *base, struct diff_options *opt);
  32static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new,
  33                             struct strbuf *base, struct diff_options *opt);
  34
  35/*
  36 * Compare two tree entries, taking into account only path/S_ISDIR(mode),
  37 * but not their sha1's.
  38 *
  39 * NOTE files and directories *always* compare differently, even when having
  40 *      the same name - thanks to base_name_compare().
  41 *
  42 * NOTE empty (=invalid) descriptor(s) take part in comparison as +infty,
  43 *      so that they sort *after* valid tree entries.
  44 *
  45 *      Due to this convention, if trees are scanned in sorted order, all
  46 *      non-empty descriptors will be processed first.
  47 */
  48static int tree_entry_pathcmp(struct tree_desc *t1, struct tree_desc *t2)
  49{
  50        struct name_entry *e1, *e2;
  51        int cmp;
  52
  53        /* empty descriptors sort after valid tree entries */
  54        if (!t1->size)
  55                return t2->size ? 1 : 0;
  56        else if (!t2->size)
  57                return -1;
  58
  59        e1 = &t1->entry;
  60        e2 = &t2->entry;
  61        cmp = base_name_compare(e1->path, tree_entry_len(e1), e1->mode,
  62                                e2->path, tree_entry_len(e2), e2->mode);
  63        return cmp;
  64}
  65
  66
  67/*
  68 * convert path -> opt->diff_*() callbacks
  69 *
  70 * emits diff to first parent only, and tells diff tree-walker that we are done
  71 * with p and it can be freed.
  72 */
  73static int emit_diff_first_parent_only(struct diff_options *opt, struct combine_diff_path *p)
  74{
  75        struct combine_diff_parent *p0 = &p->parent[0];
  76        if (p->mode && p0->mode) {
  77                opt->change(opt, p0->mode, p->mode, p0->oid.hash, p->oid.hash,
  78                        1, 1, p->path, 0, 0);
  79        }
  80        else {
  81                const unsigned char *sha1;
  82                unsigned int mode;
  83                int addremove;
  84
  85                if (p->mode) {
  86                        addremove = '+';
  87                        sha1 = p->oid.hash;
  88                        mode = p->mode;
  89                } else {
  90                        addremove = '-';
  91                        sha1 = p0->oid.hash;
  92                        mode = p0->mode;
  93                }
  94
  95                opt->add_remove(opt, addremove, mode, sha1, 1, p->path, 0);
  96        }
  97
  98        return 0;       /* we are done with p */
  99}
 100
 101
 102/*
 103 * Make a new combine_diff_path from path/mode/sha1
 104 * and append it to paths list tail.
 105 *
 106 * Memory for created elements could be reused:
 107 *
 108 *      - if last->next == NULL, the memory is allocated;
 109 *
 110 *      - if last->next != NULL, it is assumed that p=last->next was returned
 111 *        earlier by this function, and p->next was *not* modified.
 112 *        The memory is then reused from p.
 113 *
 114 * so for clients,
 115 *
 116 * - if you do need to keep the element
 117 *
 118 *      p = path_appendnew(p, ...);
 119 *      process(p);
 120 *      p->next = NULL;
 121 *
 122 * - if you don't need to keep the element after processing
 123 *
 124 *      pprev = p;
 125 *      p = path_appendnew(p, ...);
 126 *      process(p);
 127 *      p = pprev;
 128 *      ; don't forget to free tail->next in the end
 129 *
 130 * p->parent[] remains uninitialized.
 131 */
 132static struct combine_diff_path *path_appendnew(struct combine_diff_path *last,
 133        int nparent, const struct strbuf *base, const char *path, int pathlen,
 134        unsigned mode, const unsigned char *sha1)
 135{
 136        struct combine_diff_path *p;
 137        size_t len = st_add(base->len, pathlen);
 138        size_t alloclen = combine_diff_path_size(nparent, len);
 139
 140        /* if last->next is !NULL - it is a pre-allocated memory, we can reuse */
 141        p = last->next;
 142        if (p && (alloclen > (intptr_t)p->next)) {
 143                FREE_AND_NULL(p);
 144        }
 145
 146        if (!p) {
 147                p = xmalloc(alloclen);
 148
 149                /*
 150                 * until we go to it next round, .next holds how many bytes we
 151                 * allocated (for faster realloc - we don't need copying old data).
 152                 */
 153                p->next = (struct combine_diff_path *)(intptr_t)alloclen;
 154        }
 155
 156        last->next = p;
 157
 158        p->path = (char *)&(p->parent[nparent]);
 159        memcpy(p->path, base->buf, base->len);
 160        memcpy(p->path + base->len, path, pathlen);
 161        p->path[len] = 0;
 162        p->mode = mode;
 163        hashcpy(p->oid.hash, sha1 ? sha1 : null_sha1);
 164
 165        return p;
 166}
 167
 168/*
 169 * new path should be added to combine diff
 170 *
 171 * 3 cases on how/when it should be called and behaves:
 172 *
 173 *       t, !tp         -> path added, all parents lack it
 174 *      !t,  tp         -> path removed from all parents
 175 *       t,  tp         -> path modified/added
 176 *                         (M for tp[i]=tp[imin], A otherwise)
 177 */
 178static struct combine_diff_path *emit_path(struct combine_diff_path *p,
 179        struct strbuf *base, struct diff_options *opt, int nparent,
 180        struct tree_desc *t, struct tree_desc *tp,
 181        int imin)
 182{
 183        unsigned mode;
 184        const char *path;
 185        const unsigned char *sha1;
 186        int pathlen;
 187        int old_baselen = base->len;
 188        int i, isdir, recurse = 0, emitthis = 1;
 189
 190        /* at least something has to be valid */
 191        assert(t || tp);
 192
 193        if (t) {
 194                /* path present in resulting tree */
 195                sha1 = tree_entry_extract(t, &path, &mode)->hash;
 196                pathlen = tree_entry_len(&t->entry);
 197                isdir = S_ISDIR(mode);
 198        } else {
 199                /*
 200                 * a path was removed - take path from imin parent. Also take
 201                 * mode from that parent, to decide on recursion(1).
 202                 *
 203                 * 1) all modes for tp[i]=tp[imin] should be the same wrt
 204                 *    S_ISDIR, thanks to base_name_compare().
 205                 */
 206                tree_entry_extract(&tp[imin], &path, &mode);
 207                pathlen = tree_entry_len(&tp[imin].entry);
 208
 209                isdir = S_ISDIR(mode);
 210                sha1 = NULL;
 211                mode = 0;
 212        }
 213
 214        if (DIFF_OPT_TST(opt, RECURSIVE) && isdir) {
 215                recurse = 1;
 216                emitthis = DIFF_OPT_TST(opt, TREE_IN_RECURSIVE);
 217        }
 218
 219        if (emitthis) {
 220                int keep;
 221                struct combine_diff_path *pprev = p;
 222                p = path_appendnew(p, nparent, base, path, pathlen, mode, sha1);
 223
 224                for (i = 0; i < nparent; ++i) {
 225                        /*
 226                         * tp[i] is valid, if present and if tp[i]==tp[imin] -
 227                         * otherwise, we should ignore it.
 228                         */
 229                        int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);
 230
 231                        const unsigned char *sha1_i;
 232                        unsigned mode_i;
 233
 234                        p->parent[i].status =
 235                                !t ? DIFF_STATUS_DELETED :
 236                                        tpi_valid ?
 237                                                DIFF_STATUS_MODIFIED :
 238                                                DIFF_STATUS_ADDED;
 239
 240                        if (tpi_valid) {
 241                                sha1_i = tp[i].entry.oid->hash;
 242                                mode_i = tp[i].entry.mode;
 243                        }
 244                        else {
 245                                sha1_i = NULL;
 246                                mode_i = 0;
 247                        }
 248
 249                        p->parent[i].mode = mode_i;
 250                        hashcpy(p->parent[i].oid.hash, sha1_i ? sha1_i : null_sha1);
 251                }
 252
 253                keep = 1;
 254                if (opt->pathchange)
 255                        keep = opt->pathchange(opt, p);
 256
 257                /*
 258                 * If a path was filtered or consumed - we don't need to add it
 259                 * to the list and can reuse its memory, leaving it as
 260                 * pre-allocated element on the tail.
 261                 *
 262                 * On the other hand, if path needs to be kept, we need to
 263                 * correct its .next to NULL, as it was pre-initialized to how
 264                 * much memory was allocated.
 265                 *
 266                 * see path_appendnew() for details.
 267                 */
 268                if (!keep)
 269                        p = pprev;
 270                else
 271                        p->next = NULL;
 272        }
 273
 274        if (recurse) {
 275                const unsigned char **parents_sha1;
 276
 277                FAST_ARRAY_ALLOC(parents_sha1, nparent);
 278                for (i = 0; i < nparent; ++i) {
 279                        /* same rule as in emitthis */
 280                        int tpi_valid = tp && !(tp[i].entry.mode & S_IFXMIN_NEQ);
 281
 282                        parents_sha1[i] = tpi_valid ? tp[i].entry.oid->hash
 283                                                    : NULL;
 284                }
 285
 286                strbuf_add(base, path, pathlen);
 287                strbuf_addch(base, '/');
 288                p = ll_diff_tree_paths(p, sha1, parents_sha1, nparent, base, opt);
 289                FAST_ARRAY_FREE(parents_sha1, nparent);
 290        }
 291
 292        strbuf_setlen(base, old_baselen);
 293        return p;
 294}
 295
 296static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
 297                               struct diff_options *opt)
 298{
 299        enum interesting match;
 300
 301        while (t->size) {
 302                match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
 303                if (match) {
 304                        if (match == all_entries_not_interesting)
 305                                t->size = 0;
 306                        break;
 307                }
 308                update_tree_entry(t);
 309        }
 310}
 311
 312
 313/*
 314 * generate paths for combined diff D(sha1,parents_sha1[])
 315 *
 316 * Resulting paths are appended to combine_diff_path linked list, and also, are
 317 * emitted on the go via opt->pathchange() callback, so it is possible to
 318 * process the result as batch or incrementally.
 319 *
 320 * The paths are generated scanning new tree and all parents trees
 321 * simultaneously, similarly to what diff_tree() was doing for 2 trees.
 322 * The theory behind such scan is as follows:
 323 *
 324 *
 325 * D(T,P1...Pn) calculation scheme
 326 * -------------------------------
 327 *
 328 * D(T,P1...Pn) = D(T,P1) ^ ... ^ D(T,Pn)       (regarding resulting paths set)
 329 *
 330 *      D(T,Pj)         - diff between T..Pj
 331 *      D(T,P1...Pn)    - combined diff from T to parents P1,...,Pn
 332 *
 333 *
 334 * We start from all trees, which are sorted, and compare their entries in
 335 * lock-step:
 336 *
 337 *       T     P1       Pn
 338 *       -     -        -
 339 *      |t|   |p1|     |pn|
 340 *      |-|   |--| ... |--|      imin = argmin(p1...pn)
 341 *      | |   |  |     |  |
 342 *      |-|   |--|     |--|
 343 *      |.|   |. |     |. |
 344 *       .     .        .
 345 *       .     .        .
 346 *
 347 * at any time there could be 3 cases:
 348 *
 349 *      1)  t < p[imin];
 350 *      2)  t > p[imin];
 351 *      3)  t = p[imin].
 352 *
 353 * Schematic deduction of what every case means, and what to do, follows:
 354 *
 355 * 1)  t < p[imin]  ->  ∀j t ∉ Pj  ->  "+t" ∈ D(T,Pj)  ->  D += "+t";  t↓
 356 *
 357 * 2)  t > p[imin]
 358 *
 359 *     2.1) ∃j: pj > p[imin]  ->  "-p[imin]" ∉ D(T,Pj)  ->  D += ø;  ∀ pi=p[imin]  pi↓
 360 *     2.2) ∀i  pi = p[imin]  ->  pi ∉ T  ->  "-pi" ∈ D(T,Pi)  ->  D += "-p[imin]";  ∀i pi↓
 361 *
 362 * 3)  t = p[imin]
 363 *
 364 *     3.1) ∃j: pj > p[imin]  ->  "+t" ∈ D(T,Pj)  ->  only pi=p[imin] remains to investigate
 365 *     3.2) pi = p[imin]  ->  investigate δ(t,pi)
 366 *      |
 367 *      |
 368 *      v
 369 *
 370 *     3.1+3.2) looking at δ(t,pi) ∀i: pi=p[imin] - if all != ø  ->
 371 *
 372 *                       ⎧δ(t,pi)  - if pi=p[imin]
 373 *              ->  D += ⎨
 374 *                       ⎩"+t"     - if pi>p[imin]
 375 *
 376 *
 377 *     in any case t↓  ∀ pi=p[imin]  pi↓
 378 *
 379 *
 380 * ~~~~~~~~
 381 *
 382 * NOTE
 383 *
 384 *      Usual diff D(A,B) is by definition the same as combined diff D(A,[B]),
 385 *      so this diff paths generator can, and is used, for plain diffs
 386 *      generation too.
 387 *
 388 *      Please keep attention to the common D(A,[B]) case when working on the
 389 *      code, in order not to slow it down.
 390 *
 391 * NOTE
 392 *      nparent must be > 0.
 393 */
 394
 395
 396/* ∀ pi=p[imin]  pi↓ */
 397static inline void update_tp_entries(struct tree_desc *tp, int nparent)
 398{
 399        int i;
 400        for (i = 0; i < nparent; ++i)
 401                if (!(tp[i].entry.mode & S_IFXMIN_NEQ))
 402                        update_tree_entry(&tp[i]);
 403}
 404
 405static struct combine_diff_path *ll_diff_tree_paths(
 406        struct combine_diff_path *p, const unsigned char *sha1,
 407        const unsigned char **parents_sha1, int nparent,
 408        struct strbuf *base, struct diff_options *opt)
 409{
 410        struct tree_desc t, *tp;
 411        void *ttree, **tptree;
 412        int i;
 413
 414        FAST_ARRAY_ALLOC(tp, nparent);
 415        FAST_ARRAY_ALLOC(tptree, nparent);
 416
 417        /*
 418         * load parents first, as they are probably already cached.
 419         *
 420         * ( log_tree_diff() parses commit->parent before calling here via
 421         *   diff_tree_sha1(parent, commit) )
 422         */
 423        for (i = 0; i < nparent; ++i)
 424                tptree[i] = fill_tree_descriptor(&tp[i], parents_sha1[i]);
 425        ttree = fill_tree_descriptor(&t, sha1);
 426
 427        /* Enable recursion indefinitely */
 428        opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);
 429
 430        for (;;) {
 431                int imin, cmp;
 432
 433                if (diff_can_quit_early(opt))
 434                        break;
 435
 436                if (opt->pathspec.nr) {
 437                        skip_uninteresting(&t, base, opt);
 438                        for (i = 0; i < nparent; i++)
 439                                skip_uninteresting(&tp[i], base, opt);
 440                }
 441
 442                /* comparing is finished when all trees are done */
 443                if (!t.size) {
 444                        int done = 1;
 445                        for (i = 0; i < nparent; ++i)
 446                                if (tp[i].size) {
 447                                        done = 0;
 448                                        break;
 449                                }
 450                        if (done)
 451                                break;
 452                }
 453
 454                /*
 455                 * lookup imin = argmin(p1...pn),
 456                 * mark entries whether they =p[imin] along the way
 457                 */
 458                imin = 0;
 459                tp[0].entry.mode &= ~S_IFXMIN_NEQ;
 460
 461                for (i = 1; i < nparent; ++i) {
 462                        cmp = tree_entry_pathcmp(&tp[i], &tp[imin]);
 463                        if (cmp < 0) {
 464                                imin = i;
 465                                tp[i].entry.mode &= ~S_IFXMIN_NEQ;
 466                        }
 467                        else if (cmp == 0) {
 468                                tp[i].entry.mode &= ~S_IFXMIN_NEQ;
 469                        }
 470                        else {
 471                                tp[i].entry.mode |= S_IFXMIN_NEQ;
 472                        }
 473                }
 474
 475                /* fixup markings for entries before imin */
 476                for (i = 0; i < imin; ++i)
 477                        tp[i].entry.mode |= S_IFXMIN_NEQ;       /* pi > p[imin] */
 478
 479
 480
 481                /* compare t vs p[imin] */
 482                cmp = tree_entry_pathcmp(&t, &tp[imin]);
 483
 484                /* t = p[imin] */
 485                if (cmp == 0) {
 486                        /* are either pi > p[imin] or diff(t,pi) != ø ? */
 487                        if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER)) {
 488                                for (i = 0; i < nparent; ++i) {
 489                                        /* p[i] > p[imin] */
 490                                        if (tp[i].entry.mode & S_IFXMIN_NEQ)
 491                                                continue;
 492
 493                                        /* diff(t,pi) != ø */
 494                                        if (oidcmp(t.entry.oid, tp[i].entry.oid) ||
 495                                            (t.entry.mode != tp[i].entry.mode))
 496                                                continue;
 497
 498                                        goto skip_emit_t_tp;
 499                                }
 500                        }
 501
 502                        /* D += {δ(t,pi) if pi=p[imin];  "+a" if pi > p[imin]} */
 503                        p = emit_path(p, base, opt, nparent,
 504                                        &t, tp, imin);
 505
 506                skip_emit_t_tp:
 507                        /* t↓,  ∀ pi=p[imin]  pi↓ */
 508                        update_tree_entry(&t);
 509                        update_tp_entries(tp, nparent);
 510                }
 511
 512                /* t < p[imin] */
 513                else if (cmp < 0) {
 514                        /* D += "+t" */
 515                        p = emit_path(p, base, opt, nparent,
 516                                        &t, /*tp=*/NULL, -1);
 517
 518                        /* t↓ */
 519                        update_tree_entry(&t);
 520                }
 521
 522                /* t > p[imin] */
 523                else {
 524                        /* ∀i pi=p[imin] -> D += "-p[imin]" */
 525                        if (!DIFF_OPT_TST(opt, FIND_COPIES_HARDER)) {
 526                                for (i = 0; i < nparent; ++i)
 527                                        if (tp[i].entry.mode & S_IFXMIN_NEQ)
 528                                                goto skip_emit_tp;
 529                        }
 530
 531                        p = emit_path(p, base, opt, nparent,
 532                                        /*t=*/NULL, tp, imin);
 533
 534                skip_emit_tp:
 535                        /* ∀ pi=p[imin]  pi↓ */
 536                        update_tp_entries(tp, nparent);
 537                }
 538        }
 539
 540        free(ttree);
 541        for (i = nparent-1; i >= 0; i--)
 542                free(tptree[i]);
 543        FAST_ARRAY_FREE(tptree, nparent);
 544        FAST_ARRAY_FREE(tp, nparent);
 545
 546        return p;
 547}
 548
 549struct combine_diff_path *diff_tree_paths(
 550        struct combine_diff_path *p, const unsigned char *sha1,
 551        const unsigned char **parents_sha1, int nparent,
 552        struct strbuf *base, struct diff_options *opt)
 553{
 554        p = ll_diff_tree_paths(p, sha1, parents_sha1, nparent, base, opt);
 555
 556        /*
 557         * free pre-allocated last element, if any
 558         * (see path_appendnew() for details about why)
 559         */
 560        if (p->next) {
 561                FREE_AND_NULL(p->next);
 562        }
 563
 564        return p;
 565}
 566
 567/*
 568 * Does it look like the resulting diff might be due to a rename?
 569 *  - single entry
 570 *  - not a valid previous file
 571 */
 572static inline int diff_might_be_rename(void)
 573{
 574        return diff_queued_diff.nr == 1 &&
 575                !DIFF_FILE_VALID(diff_queued_diff.queue[0]->one);
 576}
 577
 578static void try_to_follow_renames(const unsigned char *old, const unsigned char *new, struct strbuf *base, struct diff_options *opt)
 579{
 580        struct diff_options diff_opts;
 581        struct diff_queue_struct *q = &diff_queued_diff;
 582        struct diff_filepair *choice;
 583        int i;
 584
 585        /*
 586         * follow-rename code is very specific, we need exactly one
 587         * path. Magic that matches more than one path is not
 588         * supported.
 589         */
 590        GUARD_PATHSPEC(&opt->pathspec, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
 591#if 0
 592        /*
 593         * We should reject wildcards as well. Unfortunately we
 594         * haven't got a reliable way to detect that 'foo\*bar' in
 595         * fact has no wildcards. nowildcard_len is merely a hint for
 596         * optimization. Let it slip for now until wildmatch is taught
 597         * about dry-run mode and returns wildcard info.
 598         */
 599        if (opt->pathspec.has_wildcard)
 600                die("BUG:%s:%d: wildcards are not supported",
 601                    __FILE__, __LINE__);
 602#endif
 603
 604        /* Remove the file creation entry from the diff queue, and remember it */
 605        choice = q->queue[0];
 606        q->nr = 0;
 607
 608        diff_setup(&diff_opts);
 609        DIFF_OPT_SET(&diff_opts, RECURSIVE);
 610        DIFF_OPT_SET(&diff_opts, FIND_COPIES_HARDER);
 611        diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
 612        diff_opts.single_follow = opt->pathspec.items[0].match;
 613        diff_opts.break_opt = opt->break_opt;
 614        diff_opts.rename_score = opt->rename_score;
 615        diff_setup_done(&diff_opts);
 616        ll_diff_tree_sha1(old, new, base, &diff_opts);
 617        diffcore_std(&diff_opts);
 618        clear_pathspec(&diff_opts.pathspec);
 619
 620        /* Go through the new set of filepairing, and see if we find a more interesting one */
 621        opt->found_follow = 0;
 622        for (i = 0; i < q->nr; i++) {
 623                struct diff_filepair *p = q->queue[i];
 624
 625                /*
 626                 * Found a source? Not only do we use that for the new
 627                 * diff_queued_diff, we will also use that as the path in
 628                 * the future!
 629                 */
 630                if ((p->status == 'R' || p->status == 'C') &&
 631                    !strcmp(p->two->path, opt->pathspec.items[0].match)) {
 632                        const char *path[2];
 633
 634                        /* Switch the file-pairs around */
 635                        q->queue[i] = choice;
 636                        choice = p;
 637
 638                        /* Update the path we use from now on.. */
 639                        path[0] = p->one->path;
 640                        path[1] = NULL;
 641                        clear_pathspec(&opt->pathspec);
 642                        parse_pathspec(&opt->pathspec,
 643                                       PATHSPEC_ALL_MAGIC & ~PATHSPEC_LITERAL,
 644                                       PATHSPEC_LITERAL_PATH, "", path);
 645
 646                        /*
 647                         * The caller expects us to return a set of vanilla
 648                         * filepairs to let a later call to diffcore_std()
 649                         * it makes to sort the renames out (among other
 650                         * things), but we already have found renames
 651                         * ourselves; signal diffcore_std() not to muck with
 652                         * rename information.
 653                         */
 654                        opt->found_follow = 1;
 655                        break;
 656                }
 657        }
 658
 659        /*
 660         * Then, discard all the non-relevant file pairs...
 661         */
 662        for (i = 0; i < q->nr; i++) {
 663                struct diff_filepair *p = q->queue[i];
 664                diff_free_filepair(p);
 665        }
 666
 667        /*
 668         * .. and re-instate the one we want (which might be either the
 669         * original one, or the rename/copy we found)
 670         */
 671        q->queue[0] = choice;
 672        q->nr = 1;
 673}
 674
 675static int ll_diff_tree_sha1(const unsigned char *old, const unsigned char *new,
 676                             struct strbuf *base, struct diff_options *opt)
 677{
 678        struct combine_diff_path phead, *p;
 679        pathchange_fn_t pathchange_old = opt->pathchange;
 680
 681        phead.next = NULL;
 682        opt->pathchange = emit_diff_first_parent_only;
 683        diff_tree_paths(&phead, new, &old, 1, base, opt);
 684
 685        for (p = phead.next; p;) {
 686                struct combine_diff_path *pprev = p;
 687                p = p->next;
 688                free(pprev);
 689        }
 690
 691        opt->pathchange = pathchange_old;
 692        return 0;
 693}
 694
 695int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base_str, struct diff_options *opt)
 696{
 697        struct strbuf base;
 698        int retval;
 699
 700        strbuf_init(&base, PATH_MAX);
 701        strbuf_addstr(&base, base_str);
 702
 703        retval = ll_diff_tree_sha1(old, new, &base, opt);
 704        if (!*base_str && DIFF_OPT_TST(opt, FOLLOW_RENAMES) && diff_might_be_rename())
 705                try_to_follow_renames(old, new, &base, opt);
 706
 707        strbuf_release(&base);
 708
 709        return retval;
 710}
 711
 712int diff_root_tree_sha1(const unsigned char *new, const char *base, struct diff_options *opt)
 713{
 714        return diff_tree_sha1(NULL, new, base, opt);
 715}