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