2fa285a8efc69fac1ba922b0b7ada9230de93c79
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 */
   4#include <sys/types.h>
   5#include <sys/wait.h>
   6#include <signal.h>
   7#include "cache.h"
   8#include "quote.h"
   9#include "diff.h"
  10#include "diffcore.h"
  11#include "xdiff/xdiff.h"
  12
  13static int use_size_cache;
  14
  15int diff_rename_limit_default = -1;
  16
  17int git_diff_config(const char *var, const char *value)
  18{
  19        if (!strcmp(var, "diff.renamelimit")) {
  20                diff_rename_limit_default = git_config_int(var, value);
  21                return 0;
  22        }
  23
  24        return git_default_config(var, value);
  25}
  26
  27static char *quote_one(const char *str)
  28{
  29        int needlen;
  30        char *xp;
  31
  32        if (!str)
  33                return NULL;
  34        needlen = quote_c_style(str, NULL, NULL, 0);
  35        if (!needlen)
  36                return strdup(str);
  37        xp = xmalloc(needlen + 1);
  38        quote_c_style(str, xp, NULL, 0);
  39        return xp;
  40}
  41
  42static char *quote_two(const char *one, const char *two)
  43{
  44        int need_one = quote_c_style(one, NULL, NULL, 1);
  45        int need_two = quote_c_style(two, NULL, NULL, 1);
  46        char *xp;
  47
  48        if (need_one + need_two) {
  49                if (!need_one) need_one = strlen(one);
  50                if (!need_two) need_one = strlen(two);
  51
  52                xp = xmalloc(need_one + need_two + 3);
  53                xp[0] = '"';
  54                quote_c_style(one, xp + 1, NULL, 1);
  55                quote_c_style(two, xp + need_one + 1, NULL, 1);
  56                strcpy(xp + need_one + need_two + 1, "\"");
  57                return xp;
  58        }
  59        need_one = strlen(one);
  60        need_two = strlen(two);
  61        xp = xmalloc(need_one + need_two + 1);
  62        strcpy(xp, one);
  63        strcpy(xp + need_one, two);
  64        return xp;
  65}
  66
  67static const char *external_diff(void)
  68{
  69        static const char *external_diff_cmd = NULL;
  70        static int done_preparing = 0;
  71
  72        if (done_preparing)
  73                return external_diff_cmd;
  74        external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
  75        done_preparing = 1;
  76        return external_diff_cmd;
  77}
  78
  79#define TEMPFILE_PATH_LEN               50
  80
  81static struct diff_tempfile {
  82        const char *name; /* filename external diff should read from */
  83        char hex[41];
  84        char mode[10];
  85        char tmp_path[TEMPFILE_PATH_LEN];
  86} diff_temp[2];
  87
  88static int count_lines(const char *data, int size)
  89{
  90        int count, ch, completely_empty = 1, nl_just_seen = 0;
  91        count = 0;
  92        while (0 < size--) {
  93                ch = *data++;
  94                if (ch == '\n') {
  95                        count++;
  96                        nl_just_seen = 1;
  97                        completely_empty = 0;
  98                }
  99                else {
 100                        nl_just_seen = 0;
 101                        completely_empty = 0;
 102                }
 103        }
 104        if (completely_empty)
 105                return 0;
 106        if (!nl_just_seen)
 107                count++; /* no trailing newline */
 108        return count;
 109}
 110
 111static void print_line_count(int count)
 112{
 113        switch (count) {
 114        case 0:
 115                printf("0,0");
 116                break;
 117        case 1:
 118                printf("1");
 119                break;
 120        default:
 121                printf("1,%d", count);
 122                break;
 123        }
 124}
 125
 126static void copy_file(int prefix, const char *data, int size)
 127{
 128        int ch, nl_just_seen = 1;
 129        while (0 < size--) {
 130                ch = *data++;
 131                if (nl_just_seen)
 132                        putchar(prefix);
 133                putchar(ch);
 134                if (ch == '\n')
 135                        nl_just_seen = 1;
 136                else
 137                        nl_just_seen = 0;
 138        }
 139        if (!nl_just_seen)
 140                printf("\n\\ No newline at end of file\n");
 141}
 142
 143static void emit_rewrite_diff(const char *name_a,
 144                              const char *name_b,
 145                              struct diff_filespec *one,
 146                              struct diff_filespec *two)
 147{
 148        int lc_a, lc_b;
 149        diff_populate_filespec(one, 0);
 150        diff_populate_filespec(two, 0);
 151        lc_a = count_lines(one->data, one->size);
 152        lc_b = count_lines(two->data, two->size);
 153        printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
 154        print_line_count(lc_a);
 155        printf(" +");
 156        print_line_count(lc_b);
 157        printf(" @@\n");
 158        if (lc_a)
 159                copy_file('-', one->data, one->size);
 160        if (lc_b)
 161                copy_file('+', two->data, two->size);
 162}
 163
 164static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
 165{
 166        if (!DIFF_FILE_VALID(one)) {
 167                mf->ptr = ""; /* does not matter */
 168                mf->size = 0;
 169                return 0;
 170        }
 171        else if (diff_populate_filespec(one, 0))
 172                return -1;
 173        mf->ptr = one->data;
 174        mf->size = one->size;
 175        return 0;
 176}
 177
 178struct emit_callback {
 179        const char **label_path;
 180};
 181
 182static int fn_out(void *priv, mmbuffer_t *mb, int nbuf)
 183{
 184        int i;
 185        struct emit_callback *ecbdata = priv;
 186
 187        if (ecbdata->label_path[0]) {
 188                printf("--- %s\n", ecbdata->label_path[0]);
 189                printf("+++ %s\n", ecbdata->label_path[1]);
 190                ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
 191        }
 192        for (i = 0; i < nbuf; i++)
 193                if (!fwrite(mb[i].ptr, mb[i].size, 1, stdout))
 194                        return -1;
 195        return 0;
 196}
 197
 198#define FIRST_FEW_BYTES 8000
 199static int mmfile_is_binary(mmfile_t *mf)
 200{
 201        long sz = mf->size;
 202        if (FIRST_FEW_BYTES < sz)
 203                sz = FIRST_FEW_BYTES;
 204        if (memchr(mf->ptr, 0, sz))
 205                return 1;
 206        return 0;
 207}
 208
 209static void builtin_diff(const char *name_a,
 210                         const char *name_b,
 211                         struct diff_filespec *one,
 212                         struct diff_filespec *two,
 213                         const char *xfrm_msg,
 214                         int complete_rewrite)
 215{
 216        mmfile_t mf1, mf2;
 217        const char *lbl[2];
 218        char *a_one, *b_two;
 219
 220        a_one = quote_two("a/", name_a);
 221        b_two = quote_two("b/", name_b);
 222        lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
 223        lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
 224        printf("diff --git %s %s\n", a_one, b_two);
 225        if (lbl[0][0] == '/') {
 226                /* /dev/null */
 227                printf("new file mode %06o\n", two->mode);
 228                if (xfrm_msg && xfrm_msg[0])
 229                        puts(xfrm_msg);
 230        }
 231        else if (lbl[1][0] == '/') {
 232                printf("deleted file mode %06o\n", one->mode);
 233                if (xfrm_msg && xfrm_msg[0])
 234                        puts(xfrm_msg);
 235        }
 236        else {
 237                if (one->mode != two->mode) {
 238                        printf("old mode %06o\n", one->mode);
 239                        printf("new mode %06o\n", two->mode);
 240                }
 241                if (xfrm_msg && xfrm_msg[0])
 242                        puts(xfrm_msg);
 243                /*
 244                 * we do not run diff between different kind
 245                 * of objects.
 246                 */
 247                if ((one->mode ^ two->mode) & S_IFMT)
 248                        goto free_ab_and_return;
 249                if (complete_rewrite) {
 250                        emit_rewrite_diff(name_a, name_b, one, two);
 251                        goto free_ab_and_return;
 252                }
 253        }
 254
 255        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
 256                die("unable to read files to diff");
 257
 258        if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
 259                printf("Binary files %s and %s differ\n", lbl[0], lbl[1]);
 260        else {
 261                /* Crazy xdl interfaces.. */
 262                const char *diffopts = getenv("GIT_DIFF_OPTS");
 263                xpparam_t xpp;
 264                xdemitconf_t xecfg;
 265                xdemitcb_t ecb;
 266                struct emit_callback ecbdata;
 267
 268                ecbdata.label_path = lbl;
 269                xpp.flags = XDF_NEED_MINIMAL;
 270                xecfg.ctxlen = 3;
 271                xecfg.flags = XDL_EMIT_FUNCNAMES;
 272                if (!diffopts)
 273                        ;
 274                else if (!strncmp(diffopts, "--unified=", 10))
 275                        xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
 276                else if (!strncmp(diffopts, "-u", 2))
 277                        xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
 278                ecb.outf = fn_out;
 279                ecb.priv = &ecbdata;
 280                xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
 281        }
 282
 283 free_ab_and_return:
 284        free(a_one);
 285        free(b_two);
 286        return;
 287}
 288
 289struct diff_filespec *alloc_filespec(const char *path)
 290{
 291        int namelen = strlen(path);
 292        struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
 293
 294        memset(spec, 0, sizeof(*spec));
 295        spec->path = (char *)(spec + 1);
 296        memcpy(spec->path, path, namelen+1);
 297        return spec;
 298}
 299
 300void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
 301                   unsigned short mode)
 302{
 303        if (mode) {
 304                spec->mode = canon_mode(mode);
 305                memcpy(spec->sha1, sha1, 20);
 306                spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
 307        }
 308}
 309
 310/*
 311 * Given a name and sha1 pair, if the dircache tells us the file in
 312 * the work tree has that object contents, return true, so that
 313 * prepare_temp_file() does not have to inflate and extract.
 314 */
 315static int work_tree_matches(const char *name, const unsigned char *sha1)
 316{
 317        struct cache_entry *ce;
 318        struct stat st;
 319        int pos, len;
 320
 321        /* We do not read the cache ourselves here, because the
 322         * benchmark with my previous version that always reads cache
 323         * shows that it makes things worse for diff-tree comparing
 324         * two linux-2.6 kernel trees in an already checked out work
 325         * tree.  This is because most diff-tree comparisons deal with
 326         * only a small number of files, while reading the cache is
 327         * expensive for a large project, and its cost outweighs the
 328         * savings we get by not inflating the object to a temporary
 329         * file.  Practically, this code only helps when we are used
 330         * by diff-cache --cached, which does read the cache before
 331         * calling us.
 332         */
 333        if (!active_cache)
 334                return 0;
 335
 336        len = strlen(name);
 337        pos = cache_name_pos(name, len);
 338        if (pos < 0)
 339                return 0;
 340        ce = active_cache[pos];
 341        if ((lstat(name, &st) < 0) ||
 342            !S_ISREG(st.st_mode) || /* careful! */
 343            ce_match_stat(ce, &st, 0) ||
 344            memcmp(sha1, ce->sha1, 20))
 345                return 0;
 346        /* we return 1 only when we can stat, it is a regular file,
 347         * stat information matches, and sha1 recorded in the cache
 348         * matches.  I.e. we know the file in the work tree really is
 349         * the same as the <name, sha1> pair.
 350         */
 351        return 1;
 352}
 353
 354static struct sha1_size_cache {
 355        unsigned char sha1[20];
 356        unsigned long size;
 357} **sha1_size_cache;
 358static int sha1_size_cache_nr, sha1_size_cache_alloc;
 359
 360static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
 361                                                 int find_only,
 362                                                 unsigned long size)
 363{
 364        int first, last;
 365        struct sha1_size_cache *e;
 366
 367        first = 0;
 368        last = sha1_size_cache_nr;
 369        while (last > first) {
 370                int cmp, next = (last + first) >> 1;
 371                e = sha1_size_cache[next];
 372                cmp = memcmp(e->sha1, sha1, 20);
 373                if (!cmp)
 374                        return e;
 375                if (cmp < 0) {
 376                        last = next;
 377                        continue;
 378                }
 379                first = next+1;
 380        }
 381        /* not found */
 382        if (find_only)
 383                return NULL;
 384        /* insert to make it at "first" */
 385        if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
 386                sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
 387                sha1_size_cache = xrealloc(sha1_size_cache,
 388                                           sha1_size_cache_alloc *
 389                                           sizeof(*sha1_size_cache));
 390        }
 391        sha1_size_cache_nr++;
 392        if (first < sha1_size_cache_nr)
 393                memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
 394                        (sha1_size_cache_nr - first - 1) *
 395                        sizeof(*sha1_size_cache));
 396        e = xmalloc(sizeof(struct sha1_size_cache));
 397        sha1_size_cache[first] = e;
 398        memcpy(e->sha1, sha1, 20);
 399        e->size = size;
 400        return e;
 401}
 402
 403/*
 404 * While doing rename detection and pickaxe operation, we may need to
 405 * grab the data for the blob (or file) for our own in-core comparison.
 406 * diff_filespec has data and size fields for this purpose.
 407 */
 408int diff_populate_filespec(struct diff_filespec *s, int size_only)
 409{
 410        int err = 0;
 411        if (!DIFF_FILE_VALID(s))
 412                die("internal error: asking to populate invalid file.");
 413        if (S_ISDIR(s->mode))
 414                return -1;
 415
 416        if (!use_size_cache)
 417                size_only = 0;
 418
 419        if (s->data)
 420                return err;
 421        if (!s->sha1_valid ||
 422            work_tree_matches(s->path, s->sha1)) {
 423                struct stat st;
 424                int fd;
 425                if (lstat(s->path, &st) < 0) {
 426                        if (errno == ENOENT) {
 427                        err_empty:
 428                                err = -1;
 429                        empty:
 430                                s->data = "";
 431                                s->size = 0;
 432                                return err;
 433                        }
 434                }
 435                s->size = st.st_size;
 436                if (!s->size)
 437                        goto empty;
 438                if (size_only)
 439                        return 0;
 440                if (S_ISLNK(st.st_mode)) {
 441                        int ret;
 442                        s->data = xmalloc(s->size);
 443                        s->should_free = 1;
 444                        ret = readlink(s->path, s->data, s->size);
 445                        if (ret < 0) {
 446                                free(s->data);
 447                                goto err_empty;
 448                        }
 449                        return 0;
 450                }
 451                fd = open(s->path, O_RDONLY);
 452                if (fd < 0)
 453                        goto err_empty;
 454                s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
 455                close(fd);
 456                if (s->data == MAP_FAILED)
 457                        goto err_empty;
 458                s->should_munmap = 1;
 459        }
 460        else {
 461                char type[20];
 462                struct sha1_size_cache *e;
 463
 464                if (size_only) {
 465                        e = locate_size_cache(s->sha1, 1, 0);
 466                        if (e) {
 467                                s->size = e->size;
 468                                return 0;
 469                        }
 470                        if (!sha1_object_info(s->sha1, type, &s->size))
 471                                locate_size_cache(s->sha1, 0, s->size);
 472                }
 473                else {
 474                        s->data = read_sha1_file(s->sha1, type, &s->size);
 475                        s->should_free = 1;
 476                }
 477        }
 478        return 0;
 479}
 480
 481void diff_free_filespec_data(struct diff_filespec *s)
 482{
 483        if (s->should_free)
 484                free(s->data);
 485        else if (s->should_munmap)
 486                munmap(s->data, s->size);
 487        s->should_free = s->should_munmap = 0;
 488        s->data = NULL;
 489        free(s->cnt_data);
 490        s->cnt_data = NULL;
 491}
 492
 493static void prep_temp_blob(struct diff_tempfile *temp,
 494                           void *blob,
 495                           unsigned long size,
 496                           const unsigned char *sha1,
 497                           int mode)
 498{
 499        int fd;
 500
 501        fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
 502        if (fd < 0)
 503                die("unable to create temp-file");
 504        if (write(fd, blob, size) != size)
 505                die("unable to write temp-file");
 506        close(fd);
 507        temp->name = temp->tmp_path;
 508        strcpy(temp->hex, sha1_to_hex(sha1));
 509        temp->hex[40] = 0;
 510        sprintf(temp->mode, "%06o", mode);
 511}
 512
 513static void prepare_temp_file(const char *name,
 514                              struct diff_tempfile *temp,
 515                              struct diff_filespec *one)
 516{
 517        if (!DIFF_FILE_VALID(one)) {
 518        not_a_valid_file:
 519                /* A '-' entry produces this for file-2, and
 520                 * a '+' entry produces this for file-1.
 521                 */
 522                temp->name = "/dev/null";
 523                strcpy(temp->hex, ".");
 524                strcpy(temp->mode, ".");
 525                return;
 526        }
 527
 528        if (!one->sha1_valid ||
 529            work_tree_matches(name, one->sha1)) {
 530                struct stat st;
 531                if (lstat(name, &st) < 0) {
 532                        if (errno == ENOENT)
 533                                goto not_a_valid_file;
 534                        die("stat(%s): %s", name, strerror(errno));
 535                }
 536                if (S_ISLNK(st.st_mode)) {
 537                        int ret;
 538                        char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
 539                        if (sizeof(buf) <= st.st_size)
 540                                die("symlink too long: %s", name);
 541                        ret = readlink(name, buf, st.st_size);
 542                        if (ret < 0)
 543                                die("readlink(%s)", name);
 544                        prep_temp_blob(temp, buf, st.st_size,
 545                                       (one->sha1_valid ?
 546                                        one->sha1 : null_sha1),
 547                                       (one->sha1_valid ?
 548                                        one->mode : S_IFLNK));
 549                }
 550                else {
 551                        /* we can borrow from the file in the work tree */
 552                        temp->name = name;
 553                        if (!one->sha1_valid)
 554                                strcpy(temp->hex, sha1_to_hex(null_sha1));
 555                        else
 556                                strcpy(temp->hex, sha1_to_hex(one->sha1));
 557                        /* Even though we may sometimes borrow the
 558                         * contents from the work tree, we always want
 559                         * one->mode.  mode is trustworthy even when
 560                         * !(one->sha1_valid), as long as
 561                         * DIFF_FILE_VALID(one).
 562                         */
 563                        sprintf(temp->mode, "%06o", one->mode);
 564                }
 565                return;
 566        }
 567        else {
 568                if (diff_populate_filespec(one, 0))
 569                        die("cannot read data blob for %s", one->path);
 570                prep_temp_blob(temp, one->data, one->size,
 571                               one->sha1, one->mode);
 572        }
 573}
 574
 575static void remove_tempfile(void)
 576{
 577        int i;
 578
 579        for (i = 0; i < 2; i++)
 580                if (diff_temp[i].name == diff_temp[i].tmp_path) {
 581                        unlink(diff_temp[i].name);
 582                        diff_temp[i].name = NULL;
 583                }
 584}
 585
 586static void remove_tempfile_on_signal(int signo)
 587{
 588        remove_tempfile();
 589        signal(SIGINT, SIG_DFL);
 590        raise(signo);
 591}
 592
 593static int spawn_prog(const char *pgm, const char **arg)
 594{
 595        pid_t pid;
 596        int status;
 597
 598        fflush(NULL);
 599        pid = fork();
 600        if (pid < 0)
 601                die("unable to fork");
 602        if (!pid) {
 603                execvp(pgm, (char *const*) arg);
 604                exit(255);
 605        }
 606
 607        while (waitpid(pid, &status, 0) < 0) {
 608                if (errno == EINTR)
 609                        continue;
 610                return -1;
 611        }
 612
 613        /* Earlier we did not check the exit status because
 614         * diff exits non-zero if files are different, and
 615         * we are not interested in knowing that.  It was a
 616         * mistake which made it harder to quit a diff-*
 617         * session that uses the git-apply-patch-script as
 618         * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
 619         * should also exit non-zero only when it wants to
 620         * abort the entire diff-* session.
 621         */
 622        if (WIFEXITED(status) && !WEXITSTATUS(status))
 623                return 0;
 624        return -1;
 625}
 626
 627/* An external diff command takes:
 628 *
 629 * diff-cmd name infile1 infile1-sha1 infile1-mode \
 630 *               infile2 infile2-sha1 infile2-mode [ rename-to ]
 631 *
 632 */
 633static void run_external_diff(const char *pgm,
 634                              const char *name,
 635                              const char *other,
 636                              struct diff_filespec *one,
 637                              struct diff_filespec *two,
 638                              const char *xfrm_msg,
 639                              int complete_rewrite)
 640{
 641        const char *spawn_arg[10];
 642        struct diff_tempfile *temp = diff_temp;
 643        int retval;
 644        static int atexit_asked = 0;
 645        const char *othername;
 646        const char **arg = &spawn_arg[0];
 647
 648        othername = (other? other : name);
 649        if (one && two) {
 650                prepare_temp_file(name, &temp[0], one);
 651                prepare_temp_file(othername, &temp[1], two);
 652                if (! atexit_asked &&
 653                    (temp[0].name == temp[0].tmp_path ||
 654                     temp[1].name == temp[1].tmp_path)) {
 655                        atexit_asked = 1;
 656                        atexit(remove_tempfile);
 657                }
 658                signal(SIGINT, remove_tempfile_on_signal);
 659        }
 660
 661        if (one && two) {
 662                *arg++ = pgm;
 663                *arg++ = name;
 664                *arg++ = temp[0].name;
 665                *arg++ = temp[0].hex;
 666                *arg++ = temp[0].mode;
 667                *arg++ = temp[1].name;
 668                *arg++ = temp[1].hex;
 669                *arg++ = temp[1].mode;
 670                if (other) {
 671                        *arg++ = other;
 672                        *arg++ = xfrm_msg;
 673                }
 674        } else {
 675                *arg++ = pgm;
 676                *arg++ = name;
 677        }
 678        *arg = NULL;
 679        retval = spawn_prog(pgm, spawn_arg);
 680        remove_tempfile();
 681        if (retval) {
 682                fprintf(stderr, "external diff died, stopping at %s.\n", name);
 683                exit(1);
 684        }
 685}
 686
 687static void run_diff_cmd(const char *pgm,
 688                         const char *name,
 689                         const char *other,
 690                         struct diff_filespec *one,
 691                         struct diff_filespec *two,
 692                         const char *xfrm_msg,
 693                         int complete_rewrite)
 694{
 695        if (pgm) {
 696                run_external_diff(pgm, name, other, one, two, xfrm_msg,
 697                                  complete_rewrite);
 698                return;
 699        }
 700        if (one && two)
 701                builtin_diff(name, other ? other : name,
 702                             one, two, xfrm_msg, complete_rewrite);
 703        else
 704                printf("* Unmerged path %s\n", name);
 705}
 706
 707static void diff_fill_sha1_info(struct diff_filespec *one)
 708{
 709        if (DIFF_FILE_VALID(one)) {
 710                if (!one->sha1_valid) {
 711                        struct stat st;
 712                        if (lstat(one->path, &st) < 0)
 713                                die("stat %s", one->path);
 714                        if (index_path(one->sha1, one->path, &st, 0))
 715                                die("cannot hash %s\n", one->path);
 716                }
 717        }
 718        else
 719                memset(one->sha1, 0, 20);
 720}
 721
 722static void run_diff(struct diff_filepair *p, struct diff_options *o)
 723{
 724        const char *pgm = external_diff();
 725        char msg[PATH_MAX*2+300], *xfrm_msg;
 726        struct diff_filespec *one;
 727        struct diff_filespec *two;
 728        const char *name;
 729        const char *other;
 730        char *name_munged, *other_munged;
 731        int complete_rewrite = 0;
 732        int len;
 733
 734        if (DIFF_PAIR_UNMERGED(p)) {
 735                /* unmerged */
 736                run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, 0);
 737                return;
 738        }
 739
 740        name = p->one->path;
 741        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
 742        name_munged = quote_one(name);
 743        other_munged = quote_one(other);
 744        one = p->one; two = p->two;
 745
 746        diff_fill_sha1_info(one);
 747        diff_fill_sha1_info(two);
 748
 749        len = 0;
 750        switch (p->status) {
 751        case DIFF_STATUS_COPIED:
 752                len += snprintf(msg + len, sizeof(msg) - len,
 753                                "similarity index %d%%\n"
 754                                "copy from %s\n"
 755                                "copy to %s\n",
 756                                (int)(0.5 + p->score * 100.0/MAX_SCORE),
 757                                name_munged, other_munged);
 758                break;
 759        case DIFF_STATUS_RENAMED:
 760                len += snprintf(msg + len, sizeof(msg) - len,
 761                                "similarity index %d%%\n"
 762                                "rename from %s\n"
 763                                "rename to %s\n",
 764                                (int)(0.5 + p->score * 100.0/MAX_SCORE),
 765                                name_munged, other_munged);
 766                break;
 767        case DIFF_STATUS_MODIFIED:
 768                if (p->score) {
 769                        len += snprintf(msg + len, sizeof(msg) - len,
 770                                        "dissimilarity index %d%%\n",
 771                                        (int)(0.5 + p->score *
 772                                              100.0/MAX_SCORE));
 773                        complete_rewrite = 1;
 774                        break;
 775                }
 776                /* fallthru */
 777        default:
 778                /* nothing */
 779                ;
 780        }
 781
 782        if (memcmp(one->sha1, two->sha1, 20)) {
 783                char one_sha1[41];
 784                int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
 785                memcpy(one_sha1, sha1_to_hex(one->sha1), 41);
 786
 787                len += snprintf(msg + len, sizeof(msg) - len,
 788                                "index %.*s..%.*s",
 789                                abbrev, one_sha1, abbrev,
 790                                sha1_to_hex(two->sha1));
 791                if (one->mode == two->mode)
 792                        len += snprintf(msg + len, sizeof(msg) - len,
 793                                        " %06o", one->mode);
 794                len += snprintf(msg + len, sizeof(msg) - len, "\n");
 795        }
 796
 797        if (len)
 798                msg[--len] = 0;
 799        xfrm_msg = len ? msg : NULL;
 800
 801        if (!pgm &&
 802            DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
 803            (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
 804                /* a filepair that changes between file and symlink
 805                 * needs to be split into deletion and creation.
 806                 */
 807                struct diff_filespec *null = alloc_filespec(two->path);
 808                run_diff_cmd(NULL, name, other, one, null, xfrm_msg, 0);
 809                free(null);
 810                null = alloc_filespec(one->path);
 811                run_diff_cmd(NULL, name, other, null, two, xfrm_msg, 0);
 812                free(null);
 813        }
 814        else
 815                run_diff_cmd(pgm, name, other, one, two, xfrm_msg,
 816                             complete_rewrite);
 817
 818        free(name_munged);
 819        free(other_munged);
 820}
 821
 822void diff_setup(struct diff_options *options)
 823{
 824        memset(options, 0, sizeof(*options));
 825        options->output_format = DIFF_FORMAT_RAW;
 826        options->line_termination = '\n';
 827        options->break_opt = -1;
 828        options->rename_limit = -1;
 829
 830        options->change = diff_change;
 831        options->add_remove = diff_addremove;
 832}
 833
 834int diff_setup_done(struct diff_options *options)
 835{
 836        if ((options->find_copies_harder &&
 837             options->detect_rename != DIFF_DETECT_COPY) ||
 838            (0 <= options->rename_limit && !options->detect_rename))
 839                return -1;
 840        if (options->detect_rename && options->rename_limit < 0)
 841                options->rename_limit = diff_rename_limit_default;
 842        if (options->setup & DIFF_SETUP_USE_CACHE) {
 843                if (!active_cache)
 844                        /* read-cache does not die even when it fails
 845                         * so it is safe for us to do this here.  Also
 846                         * it does not smudge active_cache or active_nr
 847                         * when it fails, so we do not have to worry about
 848                         * cleaning it up ourselves either.
 849                         */
 850                        read_cache();
 851        }
 852        if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
 853                use_size_cache = 1;
 854        if (options->abbrev <= 0 || 40 < options->abbrev)
 855                options->abbrev = 40; /* full */
 856
 857        return 0;
 858}
 859
 860int diff_opt_parse(struct diff_options *options, const char **av, int ac)
 861{
 862        const char *arg = av[0];
 863        if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
 864                options->output_format = DIFF_FORMAT_PATCH;
 865        else if (!strcmp(arg, "-z"))
 866                options->line_termination = 0;
 867        else if (!strncmp(arg, "-l", 2))
 868                options->rename_limit = strtoul(arg+2, NULL, 10);
 869        else if (!strcmp(arg, "--full-index"))
 870                options->full_index = 1;
 871        else if (!strcmp(arg, "--name-only"))
 872                options->output_format = DIFF_FORMAT_NAME;
 873        else if (!strcmp(arg, "--name-status"))
 874                options->output_format = DIFF_FORMAT_NAME_STATUS;
 875        else if (!strcmp(arg, "-R"))
 876                options->reverse_diff = 1;
 877        else if (!strncmp(arg, "-S", 2))
 878                options->pickaxe = arg + 2;
 879        else if (!strcmp(arg, "-s"))
 880                options->output_format = DIFF_FORMAT_NO_OUTPUT;
 881        else if (!strncmp(arg, "-O", 2))
 882                options->orderfile = arg + 2;
 883        else if (!strncmp(arg, "--diff-filter=", 14))
 884                options->filter = arg + 14;
 885        else if (!strcmp(arg, "--pickaxe-all"))
 886                options->pickaxe_opts = DIFF_PICKAXE_ALL;
 887        else if (!strcmp(arg, "--pickaxe-regex"))
 888                options->pickaxe_opts = DIFF_PICKAXE_REGEX;
 889        else if (!strncmp(arg, "-B", 2)) {
 890                if ((options->break_opt =
 891                     diff_scoreopt_parse(arg)) == -1)
 892                        return -1;
 893        }
 894        else if (!strncmp(arg, "-M", 2)) {
 895                if ((options->rename_score =
 896                     diff_scoreopt_parse(arg)) == -1)
 897                        return -1;
 898                options->detect_rename = DIFF_DETECT_RENAME;
 899        }
 900        else if (!strncmp(arg, "-C", 2)) {
 901                if ((options->rename_score =
 902                     diff_scoreopt_parse(arg)) == -1)
 903                        return -1;
 904                options->detect_rename = DIFF_DETECT_COPY;
 905        }
 906        else if (!strcmp(arg, "--find-copies-harder"))
 907                options->find_copies_harder = 1;
 908        else if (!strcmp(arg, "--abbrev"))
 909                options->abbrev = DEFAULT_ABBREV;
 910        else if (!strncmp(arg, "--abbrev=", 9)) {
 911                options->abbrev = strtoul(arg + 9, NULL, 10);
 912                if (options->abbrev < MINIMUM_ABBREV)
 913                        options->abbrev = MINIMUM_ABBREV;
 914                else if (40 < options->abbrev)
 915                        options->abbrev = 40;
 916        }
 917        else
 918                return 0;
 919        return 1;
 920}
 921
 922static int parse_num(const char **cp_p)
 923{
 924        unsigned long num, scale;
 925        int ch, dot;
 926        const char *cp = *cp_p;
 927
 928        num = 0;
 929        scale = 1;
 930        dot = 0;
 931        for(;;) {
 932                ch = *cp;
 933                if ( !dot && ch == '.' ) {
 934                        scale = 1;
 935                        dot = 1;
 936                } else if ( ch == '%' ) {
 937                        scale = dot ? scale*100 : 100;
 938                        cp++;   /* % is always at the end */
 939                        break;
 940                } else if ( ch >= '0' && ch <= '9' ) {
 941                        if ( scale < 100000 ) {
 942                                scale *= 10;
 943                                num = (num*10) + (ch-'0');
 944                        }
 945                } else {
 946                        break;
 947                }
 948                cp++;
 949        }
 950        *cp_p = cp;
 951
 952        /* user says num divided by scale and we say internally that
 953         * is MAX_SCORE * num / scale.
 954         */
 955        return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
 956}
 957
 958int diff_scoreopt_parse(const char *opt)
 959{
 960        int opt1, opt2, cmd;
 961
 962        if (*opt++ != '-')
 963                return -1;
 964        cmd = *opt++;
 965        if (cmd != 'M' && cmd != 'C' && cmd != 'B')
 966                return -1; /* that is not a -M, -C nor -B option */
 967
 968        opt1 = parse_num(&opt);
 969        if (cmd != 'B')
 970                opt2 = 0;
 971        else {
 972                if (*opt == 0)
 973                        opt2 = 0;
 974                else if (*opt != '/')
 975                        return -1; /* we expect -B80/99 or -B80 */
 976                else {
 977                        opt++;
 978                        opt2 = parse_num(&opt);
 979                }
 980        }
 981        if (*opt != 0)
 982                return -1;
 983        return opt1 | (opt2 << 16);
 984}
 985
 986struct diff_queue_struct diff_queued_diff;
 987
 988void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
 989{
 990        if (queue->alloc <= queue->nr) {
 991                queue->alloc = alloc_nr(queue->alloc);
 992                queue->queue = xrealloc(queue->queue,
 993                                        sizeof(dp) * queue->alloc);
 994        }
 995        queue->queue[queue->nr++] = dp;
 996}
 997
 998struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
 999                                 struct diff_filespec *one,
1000                                 struct diff_filespec *two)
1001{
1002        struct diff_filepair *dp = xmalloc(sizeof(*dp));
1003        dp->one = one;
1004        dp->two = two;
1005        dp->score = 0;
1006        dp->status = 0;
1007        dp->source_stays = 0;
1008        dp->broken_pair = 0;
1009        if (queue)
1010                diff_q(queue, dp);
1011        return dp;
1012}
1013
1014void diff_free_filepair(struct diff_filepair *p)
1015{
1016        diff_free_filespec_data(p->one);
1017        diff_free_filespec_data(p->two);
1018        free(p->one);
1019        free(p->two);
1020        free(p);
1021}
1022
1023/* This is different from find_unique_abbrev() in that
1024 * it stuffs the result with dots for alignment.
1025 */
1026const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1027{
1028        int abblen;
1029        const char *abbrev;
1030        if (len == 40)
1031                return sha1_to_hex(sha1);
1032
1033        abbrev = find_unique_abbrev(sha1, len);
1034        if (!abbrev)
1035                return sha1_to_hex(sha1);
1036        abblen = strlen(abbrev);
1037        if (abblen < 37) {
1038                static char hex[41];
1039                if (len < abblen && abblen <= len + 2)
1040                        sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1041                else
1042                        sprintf(hex, "%s...", abbrev);
1043                return hex;
1044        }
1045        return sha1_to_hex(sha1);
1046}
1047
1048static void diff_flush_raw(struct diff_filepair *p,
1049                           int line_termination,
1050                           int inter_name_termination,
1051                           struct diff_options *options)
1052{
1053        int two_paths;
1054        char status[10];
1055        int abbrev = options->abbrev;
1056        const char *path_one, *path_two;
1057        int output_format = options->output_format;
1058
1059        path_one = p->one->path;
1060        path_two = p->two->path;
1061        if (line_termination) {
1062                path_one = quote_one(path_one);
1063                path_two = quote_one(path_two);
1064        }
1065
1066        if (p->score)
1067                sprintf(status, "%c%03d", p->status,
1068                        (int)(0.5 + p->score * 100.0/MAX_SCORE));
1069        else {
1070                status[0] = p->status;
1071                status[1] = 0;
1072        }
1073        switch (p->status) {
1074        case DIFF_STATUS_COPIED:
1075        case DIFF_STATUS_RENAMED:
1076                two_paths = 1;
1077                break;
1078        case DIFF_STATUS_ADDED:
1079        case DIFF_STATUS_DELETED:
1080                two_paths = 0;
1081                break;
1082        default:
1083                two_paths = 0;
1084                break;
1085        }
1086        if (output_format != DIFF_FORMAT_NAME_STATUS) {
1087                printf(":%06o %06o %s ",
1088                       p->one->mode, p->two->mode,
1089                       diff_unique_abbrev(p->one->sha1, abbrev));
1090                printf("%s ",
1091                       diff_unique_abbrev(p->two->sha1, abbrev));
1092        }
1093        printf("%s%c%s", status, inter_name_termination, path_one);
1094        if (two_paths)
1095                printf("%c%s", inter_name_termination, path_two);
1096        putchar(line_termination);
1097        if (path_one != p->one->path)
1098                free((void*)path_one);
1099        if (path_two != p->two->path)
1100                free((void*)path_two);
1101}
1102
1103static void diff_flush_name(struct diff_filepair *p,
1104                            int inter_name_termination,
1105                            int line_termination)
1106{
1107        char *path = p->two->path;
1108
1109        if (line_termination)
1110                path = quote_one(p->two->path);
1111        else
1112                path = p->two->path;
1113        printf("%s%c", path, line_termination);
1114        if (p->two->path != path)
1115                free(path);
1116}
1117
1118int diff_unmodified_pair(struct diff_filepair *p)
1119{
1120        /* This function is written stricter than necessary to support
1121         * the currently implemented transformers, but the idea is to
1122         * let transformers to produce diff_filepairs any way they want,
1123         * and filter and clean them up here before producing the output.
1124         */
1125        struct diff_filespec *one, *two;
1126
1127        if (DIFF_PAIR_UNMERGED(p))
1128                return 0; /* unmerged is interesting */
1129
1130        one = p->one;
1131        two = p->two;
1132
1133        /* deletion, addition, mode or type change
1134         * and rename are all interesting.
1135         */
1136        if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1137            DIFF_PAIR_MODE_CHANGED(p) ||
1138            strcmp(one->path, two->path))
1139                return 0;
1140
1141        /* both are valid and point at the same path.  that is, we are
1142         * dealing with a change.
1143         */
1144        if (one->sha1_valid && two->sha1_valid &&
1145            !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1146                return 1; /* no change */
1147        if (!one->sha1_valid && !two->sha1_valid)
1148                return 1; /* both look at the same file on the filesystem. */
1149        return 0;
1150}
1151
1152static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1153{
1154        if (diff_unmodified_pair(p))
1155                return;
1156
1157        if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1158            (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1159                return; /* no tree diffs in patch format */ 
1160
1161        run_diff(p, o);
1162}
1163
1164int diff_queue_is_empty(void)
1165{
1166        struct diff_queue_struct *q = &diff_queued_diff;
1167        int i;
1168        for (i = 0; i < q->nr; i++)
1169                if (!diff_unmodified_pair(q->queue[i]))
1170                        return 0;
1171        return 1;
1172}
1173
1174#if DIFF_DEBUG
1175void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1176{
1177        fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1178                x, one ? one : "",
1179                s->path,
1180                DIFF_FILE_VALID(s) ? "valid" : "invalid",
1181                s->mode,
1182                s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1183        fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1184                x, one ? one : "",
1185                s->size, s->xfrm_flags);
1186}
1187
1188void diff_debug_filepair(const struct diff_filepair *p, int i)
1189{
1190        diff_debug_filespec(p->one, i, "one");
1191        diff_debug_filespec(p->two, i, "two");
1192        fprintf(stderr, "score %d, status %c stays %d broken %d\n",
1193                p->score, p->status ? p->status : '?',
1194                p->source_stays, p->broken_pair);
1195}
1196
1197void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
1198{
1199        int i;
1200        if (msg)
1201                fprintf(stderr, "%s\n", msg);
1202        fprintf(stderr, "q->nr = %d\n", q->nr);
1203        for (i = 0; i < q->nr; i++) {
1204                struct diff_filepair *p = q->queue[i];
1205                diff_debug_filepair(p, i);
1206        }
1207}
1208#endif
1209
1210static void diff_resolve_rename_copy(void)
1211{
1212        int i, j;
1213        struct diff_filepair *p, *pp;
1214        struct diff_queue_struct *q = &diff_queued_diff;
1215
1216        diff_debug_queue("resolve-rename-copy", q);
1217
1218        for (i = 0; i < q->nr; i++) {
1219                p = q->queue[i];
1220                p->status = 0; /* undecided */
1221                if (DIFF_PAIR_UNMERGED(p))
1222                        p->status = DIFF_STATUS_UNMERGED;
1223                else if (!DIFF_FILE_VALID(p->one))
1224                        p->status = DIFF_STATUS_ADDED;
1225                else if (!DIFF_FILE_VALID(p->two))
1226                        p->status = DIFF_STATUS_DELETED;
1227                else if (DIFF_PAIR_TYPE_CHANGED(p))
1228                        p->status = DIFF_STATUS_TYPE_CHANGED;
1229
1230                /* from this point on, we are dealing with a pair
1231                 * whose both sides are valid and of the same type, i.e.
1232                 * either in-place edit or rename/copy edit.
1233                 */
1234                else if (DIFF_PAIR_RENAME(p)) {
1235                        if (p->source_stays) {
1236                                p->status = DIFF_STATUS_COPIED;
1237                                continue;
1238                        }
1239                        /* See if there is some other filepair that
1240                         * copies from the same source as us.  If so
1241                         * we are a copy.  Otherwise we are either a
1242                         * copy if the path stays, or a rename if it
1243                         * does not, but we already handled "stays" case.
1244                         */
1245                        for (j = i + 1; j < q->nr; j++) {
1246                                pp = q->queue[j];
1247                                if (strcmp(pp->one->path, p->one->path))
1248                                        continue; /* not us */
1249                                if (!DIFF_PAIR_RENAME(pp))
1250                                        continue; /* not a rename/copy */
1251                                /* pp is a rename/copy from the same source */
1252                                p->status = DIFF_STATUS_COPIED;
1253                                break;
1254                        }
1255                        if (!p->status)
1256                                p->status = DIFF_STATUS_RENAMED;
1257                }
1258                else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
1259                         p->one->mode != p->two->mode)
1260                        p->status = DIFF_STATUS_MODIFIED;
1261                else {
1262                        /* This is a "no-change" entry and should not
1263                         * happen anymore, but prepare for broken callers.
1264                         */
1265                        error("feeding unmodified %s to diffcore",
1266                              p->one->path);
1267                        p->status = DIFF_STATUS_UNKNOWN;
1268                }
1269        }
1270        diff_debug_queue("resolve-rename-copy done", q);
1271}
1272
1273void diff_flush(struct diff_options *options)
1274{
1275        struct diff_queue_struct *q = &diff_queued_diff;
1276        int i;
1277        int inter_name_termination = '\t';
1278        int diff_output_format = options->output_format;
1279        int line_termination = options->line_termination;
1280
1281        if (!line_termination)
1282                inter_name_termination = 0;
1283
1284        for (i = 0; i < q->nr; i++) {
1285                struct diff_filepair *p = q->queue[i];
1286
1287                switch (p->status) {
1288                case DIFF_STATUS_UNKNOWN:
1289                        break;
1290                case 0:
1291                        die("internal error in diff-resolve-rename-copy");
1292                        break;
1293                default:
1294                        switch (diff_output_format) {
1295                        case DIFF_FORMAT_PATCH:
1296                                diff_flush_patch(p, options);
1297                                break;
1298                        case DIFF_FORMAT_RAW:
1299                        case DIFF_FORMAT_NAME_STATUS:
1300                                diff_flush_raw(p, line_termination,
1301                                               inter_name_termination,
1302                                               options);
1303                                break;
1304                        case DIFF_FORMAT_NAME:
1305                                diff_flush_name(p,
1306                                                inter_name_termination,
1307                                                line_termination);
1308                                break;
1309                        case DIFF_FORMAT_NO_OUTPUT:
1310                                break;
1311                        }
1312                }
1313                diff_free_filepair(p);
1314        }
1315        free(q->queue);
1316        q->queue = NULL;
1317        q->nr = q->alloc = 0;
1318}
1319
1320static void diffcore_apply_filter(const char *filter)
1321{
1322        int i;
1323        struct diff_queue_struct *q = &diff_queued_diff;
1324        struct diff_queue_struct outq;
1325        outq.queue = NULL;
1326        outq.nr = outq.alloc = 0;
1327
1328        if (!filter)
1329                return;
1330
1331        if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
1332                int found;
1333                for (i = found = 0; !found && i < q->nr; i++) {
1334                        struct diff_filepair *p = q->queue[i];
1335                        if (((p->status == DIFF_STATUS_MODIFIED) &&
1336                             ((p->score &&
1337                               strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1338                              (!p->score &&
1339                               strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1340                            ((p->status != DIFF_STATUS_MODIFIED) &&
1341                             strchr(filter, p->status)))
1342                                found++;
1343                }
1344                if (found)
1345                        return;
1346
1347                /* otherwise we will clear the whole queue
1348                 * by copying the empty outq at the end of this
1349                 * function, but first clear the current entries
1350                 * in the queue.
1351                 */
1352                for (i = 0; i < q->nr; i++)
1353                        diff_free_filepair(q->queue[i]);
1354        }
1355        else {
1356                /* Only the matching ones */
1357                for (i = 0; i < q->nr; i++) {
1358                        struct diff_filepair *p = q->queue[i];
1359
1360                        if (((p->status == DIFF_STATUS_MODIFIED) &&
1361                             ((p->score &&
1362                               strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
1363                              (!p->score &&
1364                               strchr(filter, DIFF_STATUS_MODIFIED)))) ||
1365                            ((p->status != DIFF_STATUS_MODIFIED) &&
1366                             strchr(filter, p->status)))
1367                                diff_q(&outq, p);
1368                        else
1369                                diff_free_filepair(p);
1370                }
1371        }
1372        free(q->queue);
1373        *q = outq;
1374}
1375
1376void diffcore_std(struct diff_options *options)
1377{
1378        if (options->break_opt != -1)
1379                diffcore_break(options->break_opt);
1380        if (options->detect_rename)
1381                diffcore_rename(options);
1382        if (options->break_opt != -1)
1383                diffcore_merge_broken();
1384        if (options->pickaxe)
1385                diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1386        if (options->orderfile)
1387                diffcore_order(options->orderfile);
1388        diff_resolve_rename_copy();
1389        diffcore_apply_filter(options->filter);
1390}
1391
1392
1393void diffcore_std_no_resolve(struct diff_options *options)
1394{
1395        if (options->pickaxe)
1396                diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
1397        if (options->orderfile)
1398                diffcore_order(options->orderfile);
1399        diffcore_apply_filter(options->filter);
1400}
1401
1402void diff_addremove(struct diff_options *options,
1403                    int addremove, unsigned mode,
1404                    const unsigned char *sha1,
1405                    const char *base, const char *path)
1406{
1407        char concatpath[PATH_MAX];
1408        struct diff_filespec *one, *two;
1409
1410        /* This may look odd, but it is a preparation for
1411         * feeding "there are unchanged files which should
1412         * not produce diffs, but when you are doing copy
1413         * detection you would need them, so here they are"
1414         * entries to the diff-core.  They will be prefixed
1415         * with something like '=' or '*' (I haven't decided
1416         * which but should not make any difference).
1417         * Feeding the same new and old to diff_change() 
1418         * also has the same effect.
1419         * Before the final output happens, they are pruned after
1420         * merged into rename/copy pairs as appropriate.
1421         */
1422        if (options->reverse_diff)
1423                addremove = (addremove == '+' ? '-' :
1424                             addremove == '-' ? '+' : addremove);
1425
1426        if (!path) path = "";
1427        sprintf(concatpath, "%s%s", base, path);
1428        one = alloc_filespec(concatpath);
1429        two = alloc_filespec(concatpath);
1430
1431        if (addremove != '+')
1432                fill_filespec(one, sha1, mode);
1433        if (addremove != '-')
1434                fill_filespec(two, sha1, mode);
1435
1436        diff_queue(&diff_queued_diff, one, two);
1437}
1438
1439void diff_change(struct diff_options *options,
1440                 unsigned old_mode, unsigned new_mode,
1441                 const unsigned char *old_sha1,
1442                 const unsigned char *new_sha1,
1443                 const char *base, const char *path) 
1444{
1445        char concatpath[PATH_MAX];
1446        struct diff_filespec *one, *two;
1447
1448        if (options->reverse_diff) {
1449                unsigned tmp;
1450                const unsigned char *tmp_c;
1451                tmp = old_mode; old_mode = new_mode; new_mode = tmp;
1452                tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
1453        }
1454        if (!path) path = "";
1455        sprintf(concatpath, "%s%s", base, path);
1456        one = alloc_filespec(concatpath);
1457        two = alloc_filespec(concatpath);
1458        fill_filespec(one, old_sha1, old_mode);
1459        fill_filespec(two, new_sha1, new_mode);
1460
1461        diff_queue(&diff_queued_diff, one, two);
1462}
1463
1464void diff_unmerge(struct diff_options *options,
1465                  const char *path)
1466{
1467        struct diff_filespec *one, *two;
1468        one = alloc_filespec(path);
1469        two = alloc_filespec(path);
1470        diff_queue(&diff_queued_diff, one, two);
1471}