eb8424e22cb10502e9854a962293732677c25970
   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 <limits.h>
   8#include "cache.h"
   9#include "diff.h"
  10#include "diffcore.h"
  11
  12static const char *diff_opts = "-pu";
  13static unsigned char null_sha1[20] = { 0, };
  14
  15static int reverse_diff;
  16static int generate_patch;
  17static int line_termination = '\n';
  18static int inter_name_termination = '\t';
  19
  20static const char *external_diff(void)
  21{
  22        static const char *external_diff_cmd = NULL;
  23        static int done_preparing = 0;
  24
  25        if (done_preparing)
  26                return external_diff_cmd;
  27
  28        /*
  29         * Default values above are meant to match the
  30         * Linux kernel development style.  Examples of
  31         * alternative styles you can specify via environment
  32         * variables are:
  33         *
  34         * GIT_DIFF_OPTS="-c";
  35         */
  36        if (gitenv("GIT_EXTERNAL_DIFF"))
  37                external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
  38
  39        /* In case external diff fails... */
  40        diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
  41
  42        done_preparing = 1;
  43        return external_diff_cmd;
  44}
  45
  46/* Help to copy the thing properly quoted for the shell safety.
  47 * any single quote is replaced with '\'', and the caller is
  48 * expected to enclose the result within a single quote pair.
  49 *
  50 * E.g.
  51 *  original     sq_expand     result
  52 *  name     ==> name      ==> 'name'
  53 *  a b      ==> a b       ==> 'a b'
  54 *  a'b      ==> a'\''b    ==> 'a'\''b'
  55 */
  56static char *sq_expand(const char *src)
  57{
  58        static char *buf = NULL;
  59        int cnt, c;
  60        const char *cp;
  61        char *bp;
  62
  63        /* count bytes needed to store the quoted string. */
  64        for (cnt = 1, cp = src; *cp; cnt++, cp++)
  65                if (*cp == '\'')
  66                        cnt += 3;
  67
  68        buf = xmalloc(cnt);
  69        bp = buf;
  70        while ((c = *src++)) {
  71                if (c != '\'')
  72                        *bp++ = c;
  73                else {
  74                        bp = strcpy(bp, "'\\''");
  75                        bp += 4;
  76                }
  77        }
  78        *bp = 0;
  79        return buf;
  80}
  81
  82static struct diff_tempfile {
  83        const char *name; /* filename external diff should read from */
  84        char hex[41];
  85        char mode[10];
  86        char tmp_path[50];
  87} diff_temp[2];
  88
  89static void builtin_diff(const char *name_a,
  90                         const char *name_b,
  91                         struct diff_tempfile *temp,
  92                         const char *xfrm_msg)
  93{
  94        int i, next_at, cmd_size;
  95        const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
  96        const char *diff_arg  = "'%s' '%s'||:"; /* "||:" is to return 0 */
  97        const char *input_name_sq[2];
  98        const char *path0[2];
  99        const char *path1[2];
 100        const char *name_sq[2];
 101        char *cmd;
 102
 103        name_sq[0] = sq_expand(name_a);
 104        name_sq[1] = sq_expand(name_b);
 105
 106        /* diff_cmd and diff_arg have 6 %s in total which makes
 107         * the sum of these strings 12 bytes larger than required.
 108         * we use 2 spaces around diff-opts, and we need to count
 109         * terminating NUL, so we subtract 9 here.
 110         */
 111        cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
 112                        strlen(diff_arg) - 9);
 113        for (i = 0; i < 2; i++) {
 114                input_name_sq[i] = sq_expand(temp[i].name);
 115                if (!strcmp(temp[i].name, "/dev/null")) {
 116                        path0[i] = "/dev/null";
 117                        path1[i] = "";
 118                } else {
 119                        path0[i] = i ? "b/" : "a/";
 120                        path1[i] = name_sq[i];
 121                }
 122                cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
 123                             strlen(input_name_sq[i]));
 124        }
 125
 126        cmd = xmalloc(cmd_size);
 127
 128        next_at = 0;
 129        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 130                            diff_cmd,
 131                            path0[0], path1[0], path0[1], path1[1]);
 132        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 133                            " %s ", diff_opts);
 134        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 135                            diff_arg, input_name_sq[0], input_name_sq[1]);
 136
 137        printf("diff --git a/%s b/%s\n", name_a, name_b);
 138        if (!path1[0][0])
 139                printf("new file mode %s\n", temp[1].mode);
 140        else if (!path1[1][0])
 141                printf("deleted file mode %s\n", temp[0].mode);
 142        else {
 143                if (strcmp(temp[0].mode, temp[1].mode)) {
 144                        printf("old mode %s\n", temp[0].mode);
 145                        printf("new mode %s\n", temp[1].mode);
 146                }
 147                if (xfrm_msg && xfrm_msg[0])
 148                        fputs(xfrm_msg, stdout);
 149
 150                if (strncmp(temp[0].mode, temp[1].mode, 3))
 151                        /* we do not run diff between different kind
 152                         * of objects.
 153                         */
 154                        exit(0);
 155        }
 156        fflush(NULL);
 157        execlp("/bin/sh","sh", "-c", cmd, NULL);
 158}
 159
 160struct diff_filespec *alloc_filespec(const char *path)
 161{
 162        int namelen = strlen(path);
 163        struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
 164        spec->path = (char *)(spec + 1);
 165        strcpy(spec->path, path);
 166        spec->should_free = spec->should_munmap = 0;
 167        spec->xfrm_flags = 0;
 168        spec->size = 0;
 169        spec->data = 0;
 170        spec->mode = 0;
 171        memset(spec->sha1, 0, 20);
 172        return spec;
 173}
 174
 175void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
 176                   unsigned short mode)
 177{
 178        if (mode) { /* just playing defensive */
 179                spec->mode = mode;
 180                memcpy(spec->sha1, sha1, 20);
 181                spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
 182        }
 183}
 184
 185/*
 186 * Given a name and sha1 pair, if the dircache tells us the file in
 187 * the work tree has that object contents, return true, so that
 188 * prepare_temp_file() does not have to inflate and extract.
 189 */
 190static int work_tree_matches(const char *name, const unsigned char *sha1)
 191{
 192        struct cache_entry *ce;
 193        struct stat st;
 194        int pos, len;
 195
 196        /* We do not read the cache ourselves here, because the
 197         * benchmark with my previous version that always reads cache
 198         * shows that it makes things worse for diff-tree comparing
 199         * two linux-2.6 kernel trees in an already checked out work
 200         * tree.  This is because most diff-tree comparisons deal with
 201         * only a small number of files, while reading the cache is
 202         * expensive for a large project, and its cost outweighs the
 203         * savings we get by not inflating the object to a temporary
 204         * file.  Practically, this code only helps when we are used
 205         * by diff-cache --cached, which does read the cache before
 206         * calling us.
 207         */
 208        if (!active_cache)
 209                return 0;
 210
 211        len = strlen(name);
 212        pos = cache_name_pos(name, len);
 213        if (pos < 0)
 214                return 0;
 215        ce = active_cache[pos];
 216        if ((lstat(name, &st) < 0) ||
 217            !S_ISREG(st.st_mode) || /* careful! */
 218            ce_match_stat(ce, &st) ||
 219            memcmp(sha1, ce->sha1, 20))
 220                return 0;
 221        /* we return 1 only when we can stat, it is a regular file,
 222         * stat information matches, and sha1 recorded in the cache
 223         * matches.  I.e. we know the file in the work tree really is
 224         * the same as the <name, sha1> pair.
 225         */
 226        return 1;
 227}
 228
 229/*
 230 * While doing rename detection and pickaxe operation, we may need to
 231 * grab the data for the blob (or file) for our own in-core comparison.
 232 * diff_filespec has data and size fields for this purpose.
 233 */
 234int diff_populate_filespec(struct diff_filespec *s)
 235{
 236        int err = 0;
 237        if (!DIFF_FILE_VALID(s))
 238                die("internal error: asking to populate invalid file.");
 239        if (S_ISDIR(s->mode))
 240                return -1;
 241
 242        if (s->data)
 243                return err;
 244        if (!s->sha1_valid ||
 245            work_tree_matches(s->path, s->sha1)) {
 246                struct stat st;
 247                int fd;
 248                if (lstat(s->path, &st) < 0) {
 249                        if (errno == ENOENT) {
 250                        err_empty:
 251                                err = -1;
 252                        empty:
 253                                s->data = "";
 254                                s->size = 0;
 255                                return err;
 256                        }
 257                }
 258                s->size = st.st_size;
 259                if (!s->size)
 260                        goto empty;
 261                if (S_ISLNK(st.st_mode)) {
 262                        int ret;
 263                        s->data = xmalloc(s->size);
 264                        s->should_free = 1;
 265                        ret = readlink(s->path, s->data, s->size);
 266                        if (ret < 0) {
 267                                free(s->data);
 268                                goto err_empty;
 269                        }
 270                        return 0;
 271                }
 272                fd = open(s->path, O_RDONLY);
 273                if (fd < 0)
 274                        goto err_empty;
 275                s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
 276                s->should_munmap = 1;
 277                close(fd);
 278        }
 279        else {
 280                char type[20];
 281                s->data = read_sha1_file(s->sha1, type, &s->size);
 282                s->should_free = 1;
 283        }
 284        return 0;
 285}
 286
 287void diff_free_filepair(struct diff_filepair *p)
 288{
 289        free(p->xfrm_msg);
 290        free(p);
 291}
 292
 293void diff_free_filespec_data(struct diff_filespec *s)
 294{
 295        if (s->should_free)
 296                free(s->data);
 297        else if (s->should_munmap)
 298                munmap(s->data, s->size);
 299        s->should_free = s->should_munmap = 0;
 300        s->data = 0;
 301}
 302
 303static void prep_temp_blob(struct diff_tempfile *temp,
 304                           void *blob,
 305                           unsigned long size,
 306                           unsigned char *sha1,
 307                           int mode)
 308{
 309        int fd;
 310
 311        strcpy(temp->tmp_path, ".diff_XXXXXX");
 312        fd = mkstemp(temp->tmp_path);
 313        if (fd < 0)
 314                die("unable to create temp-file");
 315        if (write(fd, blob, size) != size)
 316                die("unable to write temp-file");
 317        close(fd);
 318        temp->name = temp->tmp_path;
 319        strcpy(temp->hex, sha1_to_hex(sha1));
 320        temp->hex[40] = 0;
 321        sprintf(temp->mode, "%06o", mode);
 322}
 323
 324static void prepare_temp_file(const char *name,
 325                              struct diff_tempfile *temp,
 326                              struct diff_filespec *one)
 327{
 328        if (!DIFF_FILE_VALID(one)) {
 329        not_a_valid_file:
 330                /* A '-' entry produces this for file-2, and
 331                 * a '+' entry produces this for file-1.
 332                 */
 333                temp->name = "/dev/null";
 334                strcpy(temp->hex, ".");
 335                strcpy(temp->mode, ".");
 336                return;
 337        }
 338
 339        if (!one->sha1_valid ||
 340            work_tree_matches(name, one->sha1)) {
 341                struct stat st;
 342                if (lstat(name, &st) < 0) {
 343                        if (errno == ENOENT)
 344                                goto not_a_valid_file;
 345                        die("stat(%s): %s", name, strerror(errno));
 346                }
 347                if (S_ISLNK(st.st_mode)) {
 348                        int ret;
 349                        char *buf, buf_[1024];
 350                        buf = ((sizeof(buf_) < st.st_size) ?
 351                               xmalloc(st.st_size) : buf_);
 352                        ret = readlink(name, buf, st.st_size);
 353                        if (ret < 0)
 354                                die("readlink(%s)", name);
 355                        prep_temp_blob(temp, buf, st.st_size,
 356                                       (one->sha1_valid ?
 357                                        one->sha1 : null_sha1),
 358                                       (one->sha1_valid ?
 359                                        one->mode : S_IFLNK));
 360                }
 361                else {
 362                        /* we can borrow from the file in the work tree */
 363                        temp->name = name;
 364                        if (!one->sha1_valid)
 365                                strcpy(temp->hex, sha1_to_hex(null_sha1));
 366                        else
 367                                strcpy(temp->hex, sha1_to_hex(one->sha1));
 368                        sprintf(temp->mode, "%06o",
 369                                S_IFREG |ce_permissions(st.st_mode));
 370                }
 371                return;
 372        }
 373        else {
 374                if (diff_populate_filespec(one))
 375                        die("cannot read data blob for %s", one->path);
 376                prep_temp_blob(temp, one->data, one->size,
 377                               one->sha1, one->mode);
 378        }
 379}
 380
 381static void remove_tempfile(void)
 382{
 383        int i;
 384
 385        for (i = 0; i < 2; i++)
 386                if (diff_temp[i].name == diff_temp[i].tmp_path) {
 387                        unlink(diff_temp[i].name);
 388                        diff_temp[i].name = NULL;
 389                }
 390}
 391
 392static void remove_tempfile_on_signal(int signo)
 393{
 394        remove_tempfile();
 395}
 396
 397/* An external diff command takes:
 398 *
 399 * diff-cmd name infile1 infile1-sha1 infile1-mode \
 400 *               infile2 infile2-sha1 infile2-mode [ rename-to ]
 401 *
 402 */
 403static void run_external_diff(const char *name,
 404                              const char *other,
 405                              struct diff_filespec *one,
 406                              struct diff_filespec *two,
 407                              const char *xfrm_msg)
 408{
 409        struct diff_tempfile *temp = diff_temp;
 410        pid_t pid;
 411        int status;
 412        static int atexit_asked = 0;
 413
 414        if (one && two) {
 415                prepare_temp_file(name, &temp[0], one);
 416                prepare_temp_file(other ? : name, &temp[1], two);
 417                if (! atexit_asked &&
 418                    (temp[0].name == temp[0].tmp_path ||
 419                     temp[1].name == temp[1].tmp_path)) {
 420                        atexit_asked = 1;
 421                        atexit(remove_tempfile);
 422                }
 423                signal(SIGINT, remove_tempfile_on_signal);
 424        }
 425
 426        fflush(NULL);
 427        pid = fork();
 428        if (pid < 0)
 429                die("unable to fork");
 430        if (!pid) {
 431                const char *pgm = external_diff();
 432                if (pgm) {
 433                        if (one && two) {
 434                                const char *exec_arg[10];
 435                                const char **arg = &exec_arg[0];
 436                                *arg++ = pgm;
 437                                *arg++ = name;
 438                                *arg++ = temp[0].name;
 439                                *arg++ = temp[0].hex;
 440                                *arg++ = temp[0].mode;
 441                                *arg++ = temp[1].name;
 442                                *arg++ = temp[1].hex;
 443                                *arg++ = temp[1].mode;
 444                                if (other) {
 445                                        *arg++ = other;
 446                                        *arg++ = xfrm_msg;
 447                                }
 448                                *arg = 0;
 449                                execvp(pgm, (char *const*) exec_arg);
 450                        }
 451                        else
 452                                execlp(pgm, pgm, name, NULL);
 453                }
 454                /*
 455                 * otherwise we use the built-in one.
 456                 */
 457                if (one && two)
 458                        builtin_diff(name, other ? : name, temp, xfrm_msg);
 459                else
 460                        printf("* Unmerged path %s\n", name);
 461                exit(0);
 462        }
 463        if (waitpid(pid, &status, 0) < 0 ||
 464            !WIFEXITED(status) || WEXITSTATUS(status)) {
 465                /* Earlier we did not check the exit status because
 466                 * diff exits non-zero if files are different, and
 467                 * we are not interested in knowing that.  It was a
 468                 * mistake which made it harder to quit a diff-*
 469                 * session that uses the git-apply-patch-script as
 470                 * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
 471                 * should also exit non-zero only when it wants to
 472                 * abort the entire diff-* session.
 473                 */
 474                remove_tempfile();
 475                fprintf(stderr, "external diff died, stopping at %s.\n", name);
 476                exit(1);
 477        }
 478        remove_tempfile();
 479}
 480
 481void diff_setup(int reverse_diff_)
 482{
 483        reverse_diff = reverse_diff_;
 484}
 485
 486struct diff_queue_struct diff_queued_diff;
 487
 488void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
 489{
 490        if (queue->alloc <= queue->nr) {
 491                queue->alloc = alloc_nr(queue->alloc);
 492                queue->queue = xrealloc(queue->queue,
 493                                        sizeof(dp) * queue->alloc);
 494        }
 495        queue->queue[queue->nr++] = dp;
 496}
 497
 498struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
 499                                 struct diff_filespec *one,
 500                                 struct diff_filespec *two)
 501{
 502        struct diff_filepair *dp = xmalloc(sizeof(*dp));
 503        dp->one = one;
 504        dp->two = two;
 505        dp->xfrm_msg = 0;
 506        dp->orig_order = queue->nr;
 507        dp->xfrm_work = 0;
 508        diff_q(queue, dp);
 509        return dp;
 510}
 511
 512static void diff_flush_raw(struct diff_filepair *p)
 513{
 514        if (DIFF_PAIR_UNMERGED(p)) {
 515                printf("U %s%c", p->one->path, line_termination);
 516                return;
 517        }
 518        printf(":%06o %06o %s ",
 519               p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1));
 520        printf("%s%c%s%c%s%c",
 521               sha1_to_hex(p->two->sha1), inter_name_termination,
 522               p->one->path, inter_name_termination,
 523               p->two->path, line_termination);
 524}
 525
 526static void diff_flush_patch(struct diff_filepair *p)
 527{
 528        const char *name, *other;
 529
 530        name = p->one->path;
 531        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
 532        if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
 533            (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
 534                return; /* no tree diffs in patch format */ 
 535
 536        if (DIFF_PAIR_UNMERGED(p))
 537                run_external_diff(name, NULL, NULL, NULL, NULL);
 538        else
 539                run_external_diff(name, other, p->one, p->two, p->xfrm_msg);
 540}
 541
 542static int uninteresting(struct diff_filepair *p)
 543{
 544        /* This function is written stricter than necessary to support
 545         * the currently implemented transformers, but the idea is to
 546         * let transformers to produce diff_filepairs any way they want,
 547         * and filter and clean them up here before producing the output.
 548         */
 549        struct diff_filespec *one, *two;
 550
 551        if (DIFF_PAIR_UNMERGED(p))
 552                return 0; /* unmerged is interesting */
 553
 554        one = p->one;
 555        two = p->two;
 556
 557        /* deletion, addition, mode change and renames are all interesting. */
 558        if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
 559            (one->mode != two->mode) ||
 560            strcmp(one->path, two->path))
 561                return 0;
 562
 563        /* both are valid and point at the same path.  that is, we are
 564         * dealing with a change.
 565         */
 566        if (one->sha1_valid && two->sha1_valid &&
 567            !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
 568                return 1; /* no change */
 569        if (!one->sha1_valid && !two->sha1_valid)
 570                return 1; /* both look at the same file on the filesystem. */
 571        return 0;
 572}
 573
 574void diffcore_prune(void)
 575{
 576        /*
 577         * Although rename/copy detection wants to have "no-change"
 578         * entries fed into them, the downstream do not need to see
 579         * them.  This function removes such entries.
 580         *
 581         * The applications that use rename/copy should:
 582         *
 583         * (1) feed change and "no-change" entries via diff_queue().
 584         * (2) call diffcore_rename, and any other future diffcore_xxx
 585         *     that would benefit by still having "no-change" entries.
 586         * (3) call diffcore_prune
 587         * (4) call other diffcore_xxx that do not need to see
 588         *     "no-change" entries.
 589         */
 590        struct diff_queue_struct *q = &diff_queued_diff;
 591        struct diff_queue_struct outq;
 592        int i;
 593
 594        outq.queue = NULL;
 595        outq.nr = outq.alloc = 0;
 596
 597        for (i = 0; i < q->nr; i++) {
 598                struct diff_filepair *p = q->queue[i];
 599                if (!uninteresting(p))
 600                        diff_q(&outq, p);
 601                else
 602                        diff_free_filepair(p);
 603        }
 604        free(q->queue);
 605        *q = outq;
 606        return;
 607}
 608
 609static void diff_flush_one(struct diff_filepair *p)
 610{
 611        if (uninteresting(p))
 612                return;
 613        if (generate_patch)
 614                diff_flush_patch(p);
 615        else
 616                diff_flush_raw(p);
 617}
 618
 619int diff_queue_is_empty(void)
 620{
 621        struct diff_queue_struct *q = &diff_queued_diff;
 622        int i;
 623
 624        for (i = 0; i < q->nr; i++) {
 625                struct diff_filepair *p = q->queue[i];
 626                if (!uninteresting(p))
 627                        return 0;
 628        }
 629        return 1;
 630}
 631
 632void diff_flush(int diff_output_style)
 633{
 634        struct diff_queue_struct *q = &diff_queued_diff;
 635        int i;
 636
 637        generate_patch = 0;
 638        switch (diff_output_style) {
 639        case DIFF_FORMAT_HUMAN:
 640                line_termination = '\n';
 641                inter_name_termination = '\t';
 642                break;
 643        case DIFF_FORMAT_MACHINE:
 644                line_termination = inter_name_termination = 0;
 645                break;
 646        case DIFF_FORMAT_PATCH:
 647                generate_patch = 1;
 648                break;
 649        }
 650        for (i = 0; i < q->nr; i++)
 651                diff_flush_one(q->queue[i]);
 652        for (i = 0; i < q->nr; i++) {
 653                struct diff_filepair *p = q->queue[i];
 654                diff_free_filespec_data(p->one);
 655                diff_free_filespec_data(p->two);
 656                free(p->xfrm_msg);
 657                free(p);
 658        }
 659        free(q->queue);
 660        q->queue = NULL;
 661        q->nr = q->alloc = 0;
 662}
 663
 664void diff_addremove(int addremove, unsigned mode,
 665                    const unsigned char *sha1,
 666                    const char *base, const char *path)
 667{
 668        char concatpath[PATH_MAX];
 669        struct diff_filespec *one, *two;
 670
 671        /* This may look odd, but it is a preparation for
 672         * feeding "there are unchanged files which should
 673         * not produce diffs, but when you are doing copy
 674         * detection you would need them, so here they are"
 675         * entries to the diff-core.  They will be prefixed
 676         * with something like '=' or '*' (I haven't decided
 677         * which but should not make any difference).
 678         * Feeding the same new and old to diff_change() should
 679         * also have the same effect.  diff_flush() should
 680         * filter uninteresting ones out at the final output
 681         * stage.
 682         */
 683        if (reverse_diff)
 684                addremove = (addremove == '+' ? '-' :
 685                             addremove == '-' ? '+' : addremove);
 686
 687        if (!path) path = "";
 688        sprintf(concatpath, "%s%s", base, path);
 689        one = alloc_filespec(concatpath);
 690        two = alloc_filespec(concatpath);
 691
 692        if (addremove != '+')
 693                fill_filespec(one, sha1, mode);
 694        if (addremove != '-')
 695                fill_filespec(two, sha1, mode);
 696
 697        diff_queue(&diff_queued_diff, one, two);
 698}
 699
 700void diff_guif(unsigned old_mode,
 701               unsigned new_mode,
 702               const unsigned char *old_sha1,
 703               const unsigned char *new_sha1,
 704               const char *old_path,
 705               const char *new_path)
 706{
 707        struct diff_filespec *one, *two;
 708
 709        if (reverse_diff) {
 710                unsigned tmp;
 711                const unsigned char *tmp_c;
 712                tmp = old_mode; old_mode = new_mode; new_mode = tmp;
 713                tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
 714        }
 715        one = alloc_filespec(old_path);
 716        two = alloc_filespec(new_path);
 717        if (old_mode)
 718                fill_filespec(one, old_sha1, old_mode);
 719        if (new_mode)
 720                fill_filespec(two, new_sha1, new_mode);
 721        diff_queue(&diff_queued_diff, one, two);
 722}
 723
 724void diff_change(unsigned old_mode, unsigned new_mode,
 725                 const unsigned char *old_sha1,
 726                 const unsigned char *new_sha1,
 727                 const char *base, const char *path) 
 728{
 729        char concatpath[PATH_MAX];
 730        struct diff_filespec *one, *two;
 731
 732        if (reverse_diff) {
 733                unsigned tmp;
 734                const unsigned char *tmp_c;
 735                tmp = old_mode; old_mode = new_mode; new_mode = tmp;
 736                tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
 737        }
 738        if (!path) path = "";
 739        sprintf(concatpath, "%s%s", base, path);
 740        one = alloc_filespec(concatpath);
 741        two = alloc_filespec(concatpath);
 742        fill_filespec(one, old_sha1, old_mode);
 743        fill_filespec(two, new_sha1, new_mode);
 744
 745        diff_queue(&diff_queued_diff, one, two);
 746}
 747
 748void diff_unmerge(const char *path)
 749{
 750        struct diff_filespec *one, *two;
 751        one = alloc_filespec(path);
 752        two = alloc_filespec(path);
 753        diff_queue(&diff_queued_diff, one, two);
 754}