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