diff-lib.con commit Merge branch 'jc/rebase' (3ee5538)
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 */
   4#include "cache.h"
   5#include "quote.h"
   6#include "commit.h"
   7#include "diff.h"
   8#include "diffcore.h"
   9#include "revision.h"
  10#include "cache-tree.h"
  11#include "path-list.h"
  12#include "unpack-trees.h"
  13#include "refs.h"
  14
  15/*
  16 * diff-files
  17 */
  18
  19static int read_directory(const char *path, struct path_list *list)
  20{
  21        DIR *dir;
  22        struct dirent *e;
  23
  24        if (!(dir = opendir(path)))
  25                return error("Could not open directory %s", path);
  26
  27        while ((e = readdir(dir)))
  28                if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
  29                        path_list_insert(e->d_name, list);
  30
  31        closedir(dir);
  32        return 0;
  33}
  34
  35static int get_mode(const char *path, int *mode)
  36{
  37        struct stat st;
  38
  39        if (!path || !strcmp(path, "/dev/null"))
  40                *mode = 0;
  41        else if (!strcmp(path, "-"))
  42                *mode = create_ce_mode(0666);
  43        else if (stat(path, &st))
  44                return error("Could not access '%s'", path);
  45        else
  46                *mode = st.st_mode;
  47        return 0;
  48}
  49
  50static int queue_diff(struct diff_options *o,
  51                const char *name1, const char *name2)
  52{
  53        int mode1 = 0, mode2 = 0;
  54
  55        if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
  56                return -1;
  57
  58        if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
  59                return error("file/directory conflict: %s, %s", name1, name2);
  60
  61        if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
  62                char buffer1[PATH_MAX], buffer2[PATH_MAX];
  63                struct path_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1};
  64                int len1 = 0, len2 = 0, i1, i2, ret = 0;
  65
  66                if (name1 && read_directory(name1, &p1))
  67                        return -1;
  68                if (name2 && read_directory(name2, &p2)) {
  69                        path_list_clear(&p1, 0);
  70                        return -1;
  71                }
  72
  73                if (name1) {
  74                        len1 = strlen(name1);
  75                        if (len1 > 0 && name1[len1 - 1] == '/')
  76                                len1--;
  77                        memcpy(buffer1, name1, len1);
  78                        buffer1[len1++] = '/';
  79                }
  80
  81                if (name2) {
  82                        len2 = strlen(name2);
  83                        if (len2 > 0 && name2[len2 - 1] == '/')
  84                                len2--;
  85                        memcpy(buffer2, name2, len2);
  86                        buffer2[len2++] = '/';
  87                }
  88
  89                for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
  90                        const char *n1, *n2;
  91                        int comp;
  92
  93                        if (i1 == p1.nr)
  94                                comp = 1;
  95                        else if (i2 == p2.nr)
  96                                comp = -1;
  97                        else
  98                                comp = strcmp(p1.items[i1].path,
  99                                        p2.items[i2].path);
 100
 101                        if (comp > 0)
 102                                n1 = NULL;
 103                        else {
 104                                n1 = buffer1;
 105                                strncpy(buffer1 + len1, p1.items[i1++].path,
 106                                                PATH_MAX - len1);
 107                        }
 108
 109                        if (comp < 0)
 110                                n2 = NULL;
 111                        else {
 112                                n2 = buffer2;
 113                                strncpy(buffer2 + len2, p2.items[i2++].path,
 114                                                PATH_MAX - len2);
 115                        }
 116
 117                        ret = queue_diff(o, n1, n2);
 118                }
 119                path_list_clear(&p1, 0);
 120                path_list_clear(&p2, 0);
 121
 122                return ret;
 123        } else {
 124                struct diff_filespec *d1, *d2;
 125
 126                if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
 127                        unsigned tmp;
 128                        const char *tmp_c;
 129                        tmp = mode1; mode1 = mode2; mode2 = tmp;
 130                        tmp_c = name1; name1 = name2; name2 = tmp_c;
 131                }
 132
 133                if (!name1)
 134                        name1 = "/dev/null";
 135                if (!name2)
 136                        name2 = "/dev/null";
 137                d1 = alloc_filespec(name1);
 138                d2 = alloc_filespec(name2);
 139                fill_filespec(d1, null_sha1, mode1);
 140                fill_filespec(d2, null_sha1, mode2);
 141
 142                diff_queue(&diff_queued_diff, d1, d2);
 143                return 0;
 144        }
 145}
 146
 147/*
 148 * Does the path name a blob in the working tree, or a directory
 149 * in the working tree?
 150 */
 151static int is_in_index(const char *path)
 152{
 153        int len, pos;
 154        struct cache_entry *ce;
 155
 156        len = strlen(path);
 157        while (path[len-1] == '/')
 158                len--;
 159        if (!len)
 160                return 1; /* "." */
 161        pos = cache_name_pos(path, len);
 162        if (0 <= pos)
 163                return 1;
 164        pos = -1 - pos;
 165        while (pos < active_nr) {
 166                ce = active_cache[pos++];
 167                if (ce_namelen(ce) <= len ||
 168                    strncmp(ce->name, path, len) ||
 169                    (ce->name[len] > '/'))
 170                        break; /* path cannot be a prefix */
 171                if (ce->name[len] == '/')
 172                        return 1;
 173        }
 174        return 0;
 175}
 176
 177static int handle_diff_files_args(struct rev_info *revs,
 178                                  int argc, const char **argv,
 179                                  unsigned int *options)
 180{
 181        *options = 0;
 182
 183        /* revs->max_count == -2 means --no-index */
 184        while (1 < argc && argv[1][0] == '-') {
 185                if (!strcmp(argv[1], "--base"))
 186                        revs->max_count = 1;
 187                else if (!strcmp(argv[1], "--ours"))
 188                        revs->max_count = 2;
 189                else if (!strcmp(argv[1], "--theirs"))
 190                        revs->max_count = 3;
 191                else if (!strcmp(argv[1], "-n") ||
 192                                !strcmp(argv[1], "--no-index")) {
 193                        revs->max_count = -2;
 194                        DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
 195                        DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
 196                }
 197                else if (!strcmp(argv[1], "-q"))
 198                        *options |= DIFF_SILENT_ON_REMOVED;
 199                else
 200                        return error("invalid option: %s", argv[1]);
 201                argv++; argc--;
 202        }
 203
 204        if (revs->max_count == -1 && revs->diffopt.nr_paths == 2) {
 205                /*
 206                 * If two files are specified, and at least one is untracked,
 207                 * default to no-index.
 208                 */
 209                read_cache();
 210                if (!is_in_index(revs->diffopt.paths[0]) ||
 211                                        !is_in_index(revs->diffopt.paths[1])) {
 212                        revs->max_count = -2;
 213                        DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
 214                }
 215        }
 216
 217        /*
 218         * Make sure there are NO revision (i.e. pending object) parameter,
 219         * rev.max_count is reasonable (0 <= n <= 3),
 220         * there is no other revision filtering parameters.
 221         */
 222        if (revs->pending.nr || revs->max_count > 3 ||
 223            revs->min_age != -1 || revs->max_age != -1)
 224                return error("no revision allowed with diff-files");
 225
 226        if (revs->max_count == -1 &&
 227            (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
 228                revs->combine_merges = revs->dense_combined_merges = 1;
 229
 230        return 0;
 231}
 232
 233static int is_outside_repo(const char *path, int nongit, const char *prefix)
 234{
 235        int i;
 236        if (nongit || !strcmp(path, "-") || is_absolute_path(path))
 237                return 1;
 238        if (prefixcmp(path, "../"))
 239                return 0;
 240        if (!prefix)
 241                return 1;
 242        for (i = strlen(prefix); !prefixcmp(path, "../"); ) {
 243                while (i > 0 && prefix[i - 1] != '/')
 244                        i--;
 245                if (--i < 0)
 246                        return 1;
 247                path += 3;
 248        }
 249        return 0;
 250}
 251
 252int setup_diff_no_index(struct rev_info *revs,
 253                int argc, const char ** argv, int nongit, const char *prefix)
 254{
 255        int i;
 256        for (i = 1; i < argc; i++)
 257                if (argv[i][0] != '-' || argv[i][1] == '\0')
 258                        break;
 259                else if (!strcmp(argv[i], "--")) {
 260                        i++;
 261                        break;
 262                } else if (i < argc - 3 && !strcmp(argv[i], "--no-index")) {
 263                        i = argc - 3;
 264                        DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
 265                        break;
 266                }
 267        if (argc != i + 2 || (!is_outside_repo(argv[i + 1], nongit, prefix) &&
 268                                !is_outside_repo(argv[i], nongit, prefix)))
 269                return -1;
 270
 271        diff_setup(&revs->diffopt);
 272        for (i = 1; i < argc - 2; )
 273                if (!strcmp(argv[i], "--no-index"))
 274                        i++;
 275                else {
 276                        int j = diff_opt_parse(&revs->diffopt,
 277                                        argv + i, argc - i);
 278                        if (!j)
 279                                die("invalid diff option/value: %s", argv[i]);
 280                        i += j;
 281                }
 282
 283        if (prefix) {
 284                int len = strlen(prefix);
 285
 286                revs->diffopt.paths = xcalloc(2, sizeof(char*));
 287                for (i = 0; i < 2; i++) {
 288                        const char *p = argv[argc - 2 + i];
 289                        /*
 290                         * stdin should be spelled as '-'; if you have
 291                         * path that is '-', spell it as ./-.
 292                         */
 293                        p = (strcmp(p, "-")
 294                             ? xstrdup(prefix_filename(prefix, len, p))
 295                             : p);
 296                        revs->diffopt.paths[i] = p;
 297                }
 298        }
 299        else
 300                revs->diffopt.paths = argv + argc - 2;
 301        revs->diffopt.nr_paths = 2;
 302        DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
 303        revs->max_count = -2;
 304        if (diff_setup_done(&revs->diffopt) < 0)
 305                die("diff_setup_done failed");
 306        return 0;
 307}
 308
 309int run_diff_files_cmd(struct rev_info *revs, int argc, const char **argv)
 310{
 311        unsigned int options;
 312
 313        if (handle_diff_files_args(revs, argc, argv, &options))
 314                return -1;
 315
 316        if (DIFF_OPT_TST(&revs->diffopt, NO_INDEX)) {
 317                if (revs->diffopt.nr_paths != 2)
 318                        return error("need two files/directories with --no-index");
 319                if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
 320                                revs->diffopt.paths[1]))
 321                        return -1;
 322                diffcore_std(&revs->diffopt);
 323                diff_flush(&revs->diffopt);
 324                /*
 325                 * The return code for --no-index imitates diff(1):
 326                 * 0 = no changes, 1 = changes, else error
 327                 */
 328                return revs->diffopt.found_changes;
 329        }
 330
 331        if (read_cache() < 0) {
 332                perror("read_cache");
 333                return -1;
 334        }
 335        return run_diff_files(revs, options);
 336}
 337/*
 338 * See if work tree has an entity that can be staged.  Return 0 if so,
 339 * return 1 if not and return -1 if error.
 340 */
 341static int check_work_tree_entity(const struct cache_entry *ce, struct stat *st, char *symcache)
 342{
 343        if (lstat(ce->name, st) < 0) {
 344                if (errno != ENOENT && errno != ENOTDIR)
 345                        return -1;
 346                return 1;
 347        }
 348        if (has_symlink_leading_path(ce->name, symcache))
 349                return 1;
 350        if (S_ISDIR(st->st_mode)) {
 351                unsigned char sub[20];
 352                if (resolve_gitlink_ref(ce->name, "HEAD", sub))
 353                        return 1;
 354        }
 355        return 0;
 356}
 357
 358int run_diff_files(struct rev_info *revs, unsigned int option)
 359{
 360        int entries, i;
 361        int diff_unmerged_stage = revs->max_count;
 362        int silent_on_removed = option & DIFF_SILENT_ON_REMOVED;
 363        unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
 364                              ? CE_MATCH_RACY_IS_DIRTY : 0);
 365        char symcache[PATH_MAX];
 366
 367        if (diff_unmerged_stage < 0)
 368                diff_unmerged_stage = 2;
 369        entries = active_nr;
 370        symcache[0] = '\0';
 371        for (i = 0; i < entries; i++) {
 372                struct stat st;
 373                unsigned int oldmode, newmode;
 374                struct cache_entry *ce = active_cache[i];
 375                int changed;
 376
 377                if (DIFF_OPT_TST(&revs->diffopt, QUIET) &&
 378                        DIFF_OPT_TST(&revs->diffopt, HAS_CHANGES))
 379                        break;
 380
 381                if (!ce_path_match(ce, revs->prune_data))
 382                        continue;
 383
 384                if (ce_stage(ce)) {
 385                        struct combine_diff_path *dpath;
 386                        int num_compare_stages = 0;
 387                        size_t path_len;
 388
 389                        path_len = ce_namelen(ce);
 390
 391                        dpath = xmalloc(combine_diff_path_size(5, path_len));
 392                        dpath->path = (char *) &(dpath->parent[5]);
 393
 394                        dpath->next = NULL;
 395                        dpath->len = path_len;
 396                        memcpy(dpath->path, ce->name, path_len);
 397                        dpath->path[path_len] = '\0';
 398                        hashclr(dpath->sha1);
 399                        memset(&(dpath->parent[0]), 0,
 400                               sizeof(struct combine_diff_parent)*5);
 401
 402                        changed = check_work_tree_entity(ce, &st, symcache);
 403                        if (!changed)
 404                                dpath->mode = ce_mode_from_stat(ce, st.st_mode);
 405                        else {
 406                                if (changed < 0) {
 407                                        perror(ce->name);
 408                                        continue;
 409                                }
 410                                if (silent_on_removed)
 411                                        continue;
 412                        }
 413
 414                        while (i < entries) {
 415                                struct cache_entry *nce = active_cache[i];
 416                                int stage;
 417
 418                                if (strcmp(ce->name, nce->name))
 419                                        break;
 420
 421                                /* Stage #2 (ours) is the first parent,
 422                                 * stage #3 (theirs) is the second.
 423                                 */
 424                                stage = ce_stage(nce);
 425                                if (2 <= stage) {
 426                                        int mode = nce->ce_mode;
 427                                        num_compare_stages++;
 428                                        hashcpy(dpath->parent[stage-2].sha1, nce->sha1);
 429                                        dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
 430                                        dpath->parent[stage-2].status =
 431                                                DIFF_STATUS_MODIFIED;
 432                                }
 433
 434                                /* diff against the proper unmerged stage */
 435                                if (stage == diff_unmerged_stage)
 436                                        ce = nce;
 437                                i++;
 438                        }
 439                        /*
 440                         * Compensate for loop update
 441                         */
 442                        i--;
 443
 444                        if (revs->combine_merges && num_compare_stages == 2) {
 445                                show_combined_diff(dpath, 2,
 446                                                   revs->dense_combined_merges,
 447                                                   revs);
 448                                free(dpath);
 449                                continue;
 450                        }
 451                        free(dpath);
 452                        dpath = NULL;
 453
 454                        /*
 455                         * Show the diff for the 'ce' if we found the one
 456                         * from the desired stage.
 457                         */
 458                        diff_unmerge(&revs->diffopt, ce->name, 0, null_sha1);
 459                        if (ce_stage(ce) != diff_unmerged_stage)
 460                                continue;
 461                }
 462
 463                if (ce_uptodate(ce))
 464                        continue;
 465
 466                changed = check_work_tree_entity(ce, &st, symcache);
 467                if (changed) {
 468                        if (changed < 0) {
 469                                perror(ce->name);
 470                                continue;
 471                        }
 472                        if (silent_on_removed)
 473                                continue;
 474                        diff_addremove(&revs->diffopt, '-', ce->ce_mode,
 475                                       ce->sha1, ce->name, NULL);
 476                        continue;
 477                }
 478                changed = ce_match_stat(ce, &st, ce_option);
 479                if (!changed && !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
 480                        continue;
 481                oldmode = ce->ce_mode;
 482                newmode = ce_mode_from_stat(ce, st.st_mode);
 483                diff_change(&revs->diffopt, oldmode, newmode,
 484                            ce->sha1, (changed ? null_sha1 : ce->sha1),
 485                            ce->name, NULL);
 486
 487        }
 488        diffcore_std(&revs->diffopt);
 489        diff_flush(&revs->diffopt);
 490        return 0;
 491}
 492
 493/*
 494 * diff-index
 495 */
 496
 497struct oneway_unpack_data {
 498        struct rev_info *revs;
 499        char symcache[PATH_MAX];
 500};
 501
 502/* A file entry went away or appeared */
 503static void diff_index_show_file(struct rev_info *revs,
 504                                 const char *prefix,
 505                                 struct cache_entry *ce,
 506                                 const unsigned char *sha1, unsigned int mode)
 507{
 508        diff_addremove(&revs->diffopt, prefix[0], mode,
 509                       sha1, ce->name, NULL);
 510}
 511
 512static int get_stat_data(struct cache_entry *ce,
 513                         const unsigned char **sha1p,
 514                         unsigned int *modep,
 515                         int cached, int match_missing,
 516                         struct oneway_unpack_data *cbdata)
 517{
 518        const unsigned char *sha1 = ce->sha1;
 519        unsigned int mode = ce->ce_mode;
 520
 521        if (!cached) {
 522                int changed;
 523                struct stat st;
 524                changed = check_work_tree_entity(ce, &st, cbdata->symcache);
 525                if (changed < 0)
 526                        return -1;
 527                else if (changed) {
 528                        if (match_missing) {
 529                                *sha1p = sha1;
 530                                *modep = mode;
 531                                return 0;
 532                        }
 533                        return -1;
 534                }
 535                changed = ce_match_stat(ce, &st, 0);
 536                if (changed) {
 537                        mode = ce_mode_from_stat(ce, st.st_mode);
 538                        sha1 = null_sha1;
 539                }
 540        }
 541
 542        *sha1p = sha1;
 543        *modep = mode;
 544        return 0;
 545}
 546
 547static void show_new_file(struct oneway_unpack_data *cbdata,
 548                          struct cache_entry *new,
 549                          int cached, int match_missing)
 550{
 551        const unsigned char *sha1;
 552        unsigned int mode;
 553        struct rev_info *revs = cbdata->revs;
 554
 555        /*
 556         * New file in the index: it might actually be different in
 557         * the working copy.
 558         */
 559        if (get_stat_data(new, &sha1, &mode, cached, match_missing, cbdata) < 0)
 560                return;
 561
 562        diff_index_show_file(revs, "+", new, sha1, mode);
 563}
 564
 565static int show_modified(struct oneway_unpack_data *cbdata,
 566                         struct cache_entry *old,
 567                         struct cache_entry *new,
 568                         int report_missing,
 569                         int cached, int match_missing)
 570{
 571        unsigned int mode, oldmode;
 572        const unsigned char *sha1;
 573        struct rev_info *revs = cbdata->revs;
 574
 575        if (get_stat_data(new, &sha1, &mode, cached, match_missing, cbdata) < 0) {
 576                if (report_missing)
 577                        diff_index_show_file(revs, "-", old,
 578                                             old->sha1, old->ce_mode);
 579                return -1;
 580        }
 581
 582        if (revs->combine_merges && !cached &&
 583            (hashcmp(sha1, old->sha1) || hashcmp(old->sha1, new->sha1))) {
 584                struct combine_diff_path *p;
 585                int pathlen = ce_namelen(new);
 586
 587                p = xmalloc(combine_diff_path_size(2, pathlen));
 588                p->path = (char *) &p->parent[2];
 589                p->next = NULL;
 590                p->len = pathlen;
 591                memcpy(p->path, new->name, pathlen);
 592                p->path[pathlen] = 0;
 593                p->mode = mode;
 594                hashclr(p->sha1);
 595                memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
 596                p->parent[0].status = DIFF_STATUS_MODIFIED;
 597                p->parent[0].mode = new->ce_mode;
 598                hashcpy(p->parent[0].sha1, new->sha1);
 599                p->parent[1].status = DIFF_STATUS_MODIFIED;
 600                p->parent[1].mode = old->ce_mode;
 601                hashcpy(p->parent[1].sha1, old->sha1);
 602                show_combined_diff(p, 2, revs->dense_combined_merges, revs);
 603                free(p);
 604                return 0;
 605        }
 606
 607        oldmode = old->ce_mode;
 608        if (mode == oldmode && !hashcmp(sha1, old->sha1) &&
 609            !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
 610                return 0;
 611
 612        diff_change(&revs->diffopt, oldmode, mode,
 613                    old->sha1, sha1, old->name, NULL);
 614        return 0;
 615}
 616
 617/*
 618 * This turns all merge entries into "stage 3". That guarantees that
 619 * when we read in the new tree (into "stage 1"), we won't lose sight
 620 * of the fact that we had unmerged entries.
 621 */
 622static void mark_merge_entries(void)
 623{
 624        int i;
 625        for (i = 0; i < active_nr; i++) {
 626                struct cache_entry *ce = active_cache[i];
 627                if (!ce_stage(ce))
 628                        continue;
 629                ce->ce_flags |= CE_STAGEMASK;
 630        }
 631}
 632
 633/*
 634 * This gets a mix of an existing index and a tree, one pathname entry
 635 * at a time. The index entry may be a single stage-0 one, but it could
 636 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
 637 * give you the position and number of entries in the index).
 638 */
 639static void do_oneway_diff(struct unpack_trees_options *o,
 640        struct cache_entry *idx,
 641        struct cache_entry *tree)
 642{
 643        struct oneway_unpack_data *cbdata = o->unpack_data;
 644        struct rev_info *revs = cbdata->revs;
 645        int match_missing, cached;
 646
 647        /*
 648         * Backward compatibility wart - "diff-index -m" does
 649         * not mean "do not ignore merges", but "match_missing".
 650         *
 651         * But with the revision flag parsing, that's found in
 652         * "!revs->ignore_merges".
 653         */
 654        cached = o->index_only;
 655        match_missing = !revs->ignore_merges;
 656
 657        if (cached && idx && ce_stage(idx)) {
 658                if (tree)
 659                        diff_unmerge(&revs->diffopt, idx->name, idx->ce_mode, idx->sha1);
 660                return;
 661        }
 662
 663        /*
 664         * Something added to the tree?
 665         */
 666        if (!tree) {
 667                show_new_file(cbdata, idx, cached, match_missing);
 668                return;
 669        }
 670
 671        /*
 672         * Something removed from the tree?
 673         */
 674        if (!idx) {
 675                diff_index_show_file(revs, "-", tree, tree->sha1, tree->ce_mode);
 676                return;
 677        }
 678
 679        /* Show difference between old and new */
 680        show_modified(cbdata, tree, idx, 1, cached, match_missing);
 681}
 682
 683static inline void skip_same_name(struct cache_entry *ce, struct unpack_trees_options *o)
 684{
 685        int len = ce_namelen(ce);
 686        const struct index_state *index = o->src_index;
 687
 688        while (o->pos < index->cache_nr) {
 689                struct cache_entry *next = index->cache[o->pos];
 690                if (len != ce_namelen(next))
 691                        break;
 692                if (memcmp(ce->name, next->name, len))
 693                        break;
 694                o->pos++;
 695        }
 696}
 697
 698/*
 699 * The unpack_trees() interface is designed for merging, so
 700 * the different source entries are designed primarily for
 701 * the source trees, with the old index being really mainly
 702 * used for being replaced by the result.
 703 *
 704 * For diffing, the index is more important, and we only have a
 705 * single tree.
 706 *
 707 * We're supposed to return how many index entries we want to skip.
 708 *
 709 * This wrapper makes it all more readable, and takes care of all
 710 * the fairly complex unpack_trees() semantic requirements, including
 711 * the skipping, the path matching, the type conflict cases etc.
 712 */
 713static int oneway_diff(struct cache_entry **src, struct unpack_trees_options *o)
 714{
 715        struct cache_entry *idx = src[0];
 716        struct cache_entry *tree = src[1];
 717        struct oneway_unpack_data *cbdata = o->unpack_data;
 718        struct rev_info *revs = cbdata->revs;
 719
 720        if (idx && ce_stage(idx))
 721                skip_same_name(idx, o);
 722
 723        /*
 724         * Unpack-trees generates a DF/conflict entry if
 725         * there was a directory in the index and a tree
 726         * in the tree. From a diff standpoint, that's a
 727         * delete of the tree and a create of the file.
 728         */
 729        if (tree == o->df_conflict_entry)
 730                tree = NULL;
 731
 732        if (ce_path_match(idx ? idx : tree, revs->prune_data))
 733                do_oneway_diff(o, idx, tree);
 734
 735        return 0;
 736}
 737
 738int run_diff_index(struct rev_info *revs, int cached)
 739{
 740        struct object *ent;
 741        struct tree *tree;
 742        const char *tree_name;
 743        struct unpack_trees_options opts;
 744        struct tree_desc t;
 745        struct oneway_unpack_data unpack_cb;
 746
 747        mark_merge_entries();
 748
 749        ent = revs->pending.objects[0].item;
 750        tree_name = revs->pending.objects[0].name;
 751        tree = parse_tree_indirect(ent->sha1);
 752        if (!tree)
 753                return error("bad tree object %s", tree_name);
 754
 755        unpack_cb.revs = revs;
 756        unpack_cb.symcache[0] = '\0';
 757        memset(&opts, 0, sizeof(opts));
 758        opts.head_idx = 1;
 759        opts.index_only = cached;
 760        opts.merge = 1;
 761        opts.fn = oneway_diff;
 762        opts.unpack_data = &unpack_cb;
 763        opts.src_index = &the_index;
 764        opts.dst_index = NULL;
 765
 766        init_tree_desc(&t, tree->buffer, tree->size);
 767        if (unpack_trees(1, &t, &opts))
 768                exit(128);
 769
 770        diffcore_std(&revs->diffopt);
 771        diff_flush(&revs->diffopt);
 772        return 0;
 773}
 774
 775int do_diff_cache(const unsigned char *tree_sha1, struct diff_options *opt)
 776{
 777        struct tree *tree;
 778        struct rev_info revs;
 779        int i;
 780        struct cache_entry **dst;
 781        struct cache_entry *last = NULL;
 782        struct unpack_trees_options opts;
 783        struct tree_desc t;
 784        struct oneway_unpack_data unpack_cb;
 785
 786        /*
 787         * This is used by git-blame to run diff-cache internally;
 788         * it potentially needs to repeatedly run this, so we will
 789         * start by removing the higher order entries the last round
 790         * left behind.
 791         */
 792        dst = active_cache;
 793        for (i = 0; i < active_nr; i++) {
 794                struct cache_entry *ce = active_cache[i];
 795                if (ce_stage(ce)) {
 796                        if (last && !strcmp(ce->name, last->name))
 797                                continue;
 798                        cache_tree_invalidate_path(active_cache_tree,
 799                                                   ce->name);
 800                        last = ce;
 801                        ce->ce_flags |= CE_REMOVE;
 802                }
 803                *dst++ = ce;
 804        }
 805        active_nr = dst - active_cache;
 806
 807        init_revisions(&revs, NULL);
 808        revs.prune_data = opt->paths;
 809        tree = parse_tree_indirect(tree_sha1);
 810        if (!tree)
 811                die("bad tree object %s", sha1_to_hex(tree_sha1));
 812
 813        unpack_cb.revs = &revs;
 814        unpack_cb.symcache[0] = '\0';
 815        memset(&opts, 0, sizeof(opts));
 816        opts.head_idx = 1;
 817        opts.index_only = 1;
 818        opts.merge = 1;
 819        opts.fn = oneway_diff;
 820        opts.unpack_data = &unpack_cb;
 821        opts.src_index = &the_index;
 822        opts.dst_index = &the_index;
 823
 824        init_tree_desc(&t, tree->buffer, tree->size);
 825        if (unpack_trees(1, &t, &opts))
 826                exit(128);
 827        return 0;
 828}