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