diff.con commit make --find-copies-harder imply -C (03b9d56)
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 */
   4#include <sys/types.h>
   5#include <sys/wait.h>
   6#include <signal.h>
   7#include "cache.h"
   8#include "quote.h"
   9#include "diff.h"
  10#include "diffcore.h"
  11#include "delta.h"
  12#include "xdiff-interface.h"
  13
  14static int use_size_cache;
  15
  16static int diff_detect_rename_default = 0;
  17static int diff_rename_limit_default = -1;
  18static int diff_use_color_default = 0;
  19
  20/* "\033[1;38;5;2xx;48;5;2xxm\0" is 23 bytes */
  21static char diff_colors[][24] = {
  22        "\033[m",       /* reset */
  23        "",             /* normal */
  24        "\033[1m",      /* bold */
  25        "\033[36m",     /* cyan */
  26        "\033[31m",     /* red */
  27        "\033[32m",     /* green */
  28        "\033[33m"      /* yellow */
  29};
  30
  31static int parse_diff_color_slot(const char *var, int ofs)
  32{
  33        if (!strcasecmp(var+ofs, "plain"))
  34                return DIFF_PLAIN;
  35        if (!strcasecmp(var+ofs, "meta"))
  36                return DIFF_METAINFO;
  37        if (!strcasecmp(var+ofs, "frag"))
  38                return DIFF_FRAGINFO;
  39        if (!strcasecmp(var+ofs, "old"))
  40                return DIFF_FILE_OLD;
  41        if (!strcasecmp(var+ofs, "new"))
  42                return DIFF_FILE_NEW;
  43        if (!strcasecmp(var+ofs, "commit"))
  44                return DIFF_COMMIT;
  45        die("bad config variable '%s'", var);
  46}
  47
  48static int parse_color(const char *name, int len)
  49{
  50        static const char * const color_names[] = {
  51                "normal", "black", "red", "green", "yellow",
  52                "blue", "magenta", "cyan", "white"
  53        };
  54        char *end;
  55        int i;
  56        for (i = 0; i < ARRAY_SIZE(color_names); i++) {
  57                const char *str = color_names[i];
  58                if (!strncasecmp(name, str, len) && !str[len])
  59                        return i - 1;
  60        }
  61        i = strtol(name, &end, 10);
  62        if (*name && !*end && i >= -1 && i <= 255)
  63                return i;
  64        return -2;
  65}
  66
  67static int parse_attr(const char *name, int len)
  68{
  69        static const int attr_values[] = { 1, 2, 4, 5, 7 };
  70        static const char * const attr_names[] = {
  71                "bold", "dim", "ul", "blink", "reverse"
  72        };
  73        int i;
  74        for (i = 0; i < ARRAY_SIZE(attr_names); i++) {
  75                const char *str = attr_names[i];
  76                if (!strncasecmp(name, str, len) && !str[len])
  77                        return attr_values[i];
  78        }
  79        return -1;
  80}
  81
  82static void parse_diff_color_value(const char *value, const char *var, char *dst)
  83{
  84        const char *ptr = value;
  85        int attr = -1;
  86        int fg = -2;
  87        int bg = -2;
  88
  89        if (!strcasecmp(value, "reset")) {
  90                strcpy(dst, "\033[m");
  91                return;
  92        }
  93
  94        /* [fg [bg]] [attr] */
  95        while (*ptr) {
  96                const char *word = ptr;
  97                int val, len = 0;
  98
  99                while (word[len] && !isspace(word[len]))
 100                        len++;
 101
 102                ptr = word + len;
 103                while (*ptr && isspace(*ptr))
 104                        ptr++;
 105
 106                val = parse_color(word, len);
 107                if (val >= -1) {
 108                        if (fg == -2) {
 109                                fg = val;
 110                                continue;
 111                        }
 112                        if (bg == -2) {
 113                                bg = val;
 114                                continue;
 115                        }
 116                        goto bad;
 117                }
 118                val = parse_attr(word, len);
 119                if (val < 0 || attr != -1)
 120                        goto bad;
 121                attr = val;
 122        }
 123
 124        if (attr >= 0 || fg >= 0 || bg >= 0) {
 125                int sep = 0;
 126
 127                *dst++ = '\033';
 128                *dst++ = '[';
 129                if (attr >= 0) {
 130                        *dst++ = '0' + attr;
 131                        sep++;
 132                }
 133                if (fg >= 0) {
 134                        if (sep++)
 135                                *dst++ = ';';
 136                        if (fg < 8) {
 137                                *dst++ = '3';
 138                                *dst++ = '0' + fg;
 139                        } else {
 140                                dst += sprintf(dst, "38;5;%d", fg);
 141                        }
 142                }
 143                if (bg >= 0) {
 144                        if (sep++)
 145                                *dst++ = ';';
 146                        if (bg < 8) {
 147                                *dst++ = '4';
 148                                *dst++ = '0' + bg;
 149                        } else {
 150                                dst += sprintf(dst, "48;5;%d", bg);
 151                        }
 152                }
 153                *dst++ = 'm';
 154        }
 155        *dst = 0;
 156        return;
 157bad:
 158        die("bad config value '%s' for variable '%s'", value, var);
 159}
 160
 161/*
 162 * These are to give UI layer defaults.
 163 * The core-level commands such as git-diff-files should
 164 * never be affected by the setting of diff.renames
 165 * the user happens to have in the configuration file.
 166 */
 167int git_diff_ui_config(const char *var, const char *value)
 168{
 169        if (!strcmp(var, "diff.renamelimit")) {
 170                diff_rename_limit_default = git_config_int(var, value);
 171                return 0;
 172        }
 173        if (!strcmp(var, "diff.color")) {
 174                if (!value)
 175                        diff_use_color_default = 1; /* bool */
 176                else if (!strcasecmp(value, "auto")) {
 177                        diff_use_color_default = 0;
 178                        if (isatty(1) || (pager_in_use && pager_use_color)) {
 179                                char *term = getenv("TERM");
 180                                if (term && strcmp(term, "dumb"))
 181                                        diff_use_color_default = 1;
 182                        }
 183                }
 184                else if (!strcasecmp(value, "never"))
 185                        diff_use_color_default = 0;
 186                else if (!strcasecmp(value, "always"))
 187                        diff_use_color_default = 1;
 188                else
 189                        diff_use_color_default = git_config_bool(var, value);
 190                return 0;
 191        }
 192        if (!strcmp(var, "diff.renames")) {
 193                if (!value)
 194                        diff_detect_rename_default = DIFF_DETECT_RENAME;
 195                else if (!strcasecmp(value, "copies") ||
 196                         !strcasecmp(value, "copy"))
 197                        diff_detect_rename_default = DIFF_DETECT_COPY;
 198                else if (git_config_bool(var,value))
 199                        diff_detect_rename_default = DIFF_DETECT_RENAME;
 200                return 0;
 201        }
 202        if (!strncmp(var, "diff.color.", 11)) {
 203                int slot = parse_diff_color_slot(var, 11);
 204                parse_diff_color_value(value, var, diff_colors[slot]);
 205                return 0;
 206        }
 207        return git_default_config(var, value);
 208}
 209
 210static char *quote_one(const char *str)
 211{
 212        int needlen;
 213        char *xp;
 214
 215        if (!str)
 216                return NULL;
 217        needlen = quote_c_style(str, NULL, NULL, 0);
 218        if (!needlen)
 219                return strdup(str);
 220        xp = xmalloc(needlen + 1);
 221        quote_c_style(str, xp, NULL, 0);
 222        return xp;
 223}
 224
 225static char *quote_two(const char *one, const char *two)
 226{
 227        int need_one = quote_c_style(one, NULL, NULL, 1);
 228        int need_two = quote_c_style(two, NULL, NULL, 1);
 229        char *xp;
 230
 231        if (need_one + need_two) {
 232                if (!need_one) need_one = strlen(one);
 233                if (!need_two) need_one = strlen(two);
 234
 235                xp = xmalloc(need_one + need_two + 3);
 236                xp[0] = '"';
 237                quote_c_style(one, xp + 1, NULL, 1);
 238                quote_c_style(two, xp + need_one + 1, NULL, 1);
 239                strcpy(xp + need_one + need_two + 1, "\"");
 240                return xp;
 241        }
 242        need_one = strlen(one);
 243        need_two = strlen(two);
 244        xp = xmalloc(need_one + need_two + 1);
 245        strcpy(xp, one);
 246        strcpy(xp + need_one, two);
 247        return xp;
 248}
 249
 250static const char *external_diff(void)
 251{
 252        static const char *external_diff_cmd = NULL;
 253        static int done_preparing = 0;
 254
 255        if (done_preparing)
 256                return external_diff_cmd;
 257        external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
 258        done_preparing = 1;
 259        return external_diff_cmd;
 260}
 261
 262#define TEMPFILE_PATH_LEN               50
 263
 264static struct diff_tempfile {
 265        const char *name; /* filename external diff should read from */
 266        char hex[41];
 267        char mode[10];
 268        char tmp_path[TEMPFILE_PATH_LEN];
 269} diff_temp[2];
 270
 271static int count_lines(const char *data, int size)
 272{
 273        int count, ch, completely_empty = 1, nl_just_seen = 0;
 274        count = 0;
 275        while (0 < size--) {
 276                ch = *data++;
 277                if (ch == '\n') {
 278                        count++;
 279                        nl_just_seen = 1;
 280                        completely_empty = 0;
 281                }
 282                else {
 283                        nl_just_seen = 0;
 284                        completely_empty = 0;
 285                }
 286        }
 287        if (completely_empty)
 288                return 0;
 289        if (!nl_just_seen)
 290                count++; /* no trailing newline */
 291        return count;
 292}
 293
 294static void print_line_count(int count)
 295{
 296        switch (count) {
 297        case 0:
 298                printf("0,0");
 299                break;
 300        case 1:
 301                printf("1");
 302                break;
 303        default:
 304                printf("1,%d", count);
 305                break;
 306        }
 307}
 308
 309static void copy_file(int prefix, const char *data, int size)
 310{
 311        int ch, nl_just_seen = 1;
 312        while (0 < size--) {
 313                ch = *data++;
 314                if (nl_just_seen)
 315                        putchar(prefix);
 316                putchar(ch);
 317                if (ch == '\n')
 318                        nl_just_seen = 1;
 319                else
 320                        nl_just_seen = 0;
 321        }
 322        if (!nl_just_seen)
 323                printf("\n\\ No newline at end of file\n");
 324}
 325
 326static void emit_rewrite_diff(const char *name_a,
 327                              const char *name_b,
 328                              struct diff_filespec *one,
 329                              struct diff_filespec *two)
 330{
 331        int lc_a, lc_b;
 332        diff_populate_filespec(one, 0);
 333        diff_populate_filespec(two, 0);
 334        lc_a = count_lines(one->data, one->size);
 335        lc_b = count_lines(two->data, two->size);
 336        printf("--- %s\n+++ %s\n@@ -", name_a, name_b);
 337        print_line_count(lc_a);
 338        printf(" +");
 339        print_line_count(lc_b);
 340        printf(" @@\n");
 341        if (lc_a)
 342                copy_file('-', one->data, one->size);
 343        if (lc_b)
 344                copy_file('+', two->data, two->size);
 345}
 346
 347static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
 348{
 349        if (!DIFF_FILE_VALID(one)) {
 350                mf->ptr = (char *)""; /* does not matter */
 351                mf->size = 0;
 352                return 0;
 353        }
 354        else if (diff_populate_filespec(one, 0))
 355                return -1;
 356        mf->ptr = one->data;
 357        mf->size = one->size;
 358        return 0;
 359}
 360
 361struct emit_callback {
 362        struct xdiff_emit_state xm;
 363        int nparents, color_diff;
 364        const char **label_path;
 365};
 366
 367const char *diff_get_color(int diff_use_color, enum color_diff ix)
 368{
 369        if (diff_use_color)
 370                return diff_colors[ix];
 371        return "";
 372}
 373
 374static void fn_out_consume(void *priv, char *line, unsigned long len)
 375{
 376        int i;
 377        struct emit_callback *ecbdata = priv;
 378        const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
 379        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
 380
 381        if (ecbdata->label_path[0]) {
 382                printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset);
 383                printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset);
 384                ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
 385        }
 386
 387        /* This is not really necessary for now because
 388         * this codepath only deals with two-way diffs.
 389         */
 390        for (i = 0; i < len && line[i] == '@'; i++)
 391                ;
 392        if (2 <= i && i < len && line[i] == ' ') {
 393                ecbdata->nparents = i - 1;
 394                set = diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO);
 395        }
 396        else if (len < ecbdata->nparents)
 397                set = reset;
 398        else {
 399                int nparents = ecbdata->nparents;
 400                int color = DIFF_PLAIN;
 401                for (i = 0; i < nparents && len; i++) {
 402                        if (line[i] == '-')
 403                                color = DIFF_FILE_OLD;
 404                        else if (line[i] == '+')
 405                                color = DIFF_FILE_NEW;
 406                }
 407                set = diff_get_color(ecbdata->color_diff, color);
 408        }
 409        if (len > 0 && line[len-1] == '\n')
 410                len--;
 411        fputs (set, stdout);
 412        fwrite (line, len, 1, stdout);
 413        puts (reset);
 414}
 415
 416static char *pprint_rename(const char *a, const char *b)
 417{
 418        const char *old = a;
 419        const char *new = b;
 420        char *name = NULL;
 421        int pfx_length, sfx_length;
 422        int len_a = strlen(a);
 423        int len_b = strlen(b);
 424
 425        /* Find common prefix */
 426        pfx_length = 0;
 427        while (*old && *new && *old == *new) {
 428                if (*old == '/')
 429                        pfx_length = old - a + 1;
 430                old++;
 431                new++;
 432        }
 433
 434        /* Find common suffix */
 435        old = a + len_a;
 436        new = b + len_b;
 437        sfx_length = 0;
 438        while (a <= old && b <= new && *old == *new) {
 439                if (*old == '/')
 440                        sfx_length = len_a - (old - a);
 441                old--;
 442                new--;
 443        }
 444
 445        /*
 446         * pfx{mid-a => mid-b}sfx
 447         * {pfx-a => pfx-b}sfx
 448         * pfx{sfx-a => sfx-b}
 449         * name-a => name-b
 450         */
 451        if (pfx_length + sfx_length) {
 452                int a_midlen = len_a - pfx_length - sfx_length;
 453                int b_midlen = len_b - pfx_length - sfx_length;
 454                if (a_midlen < 0) a_midlen = 0;
 455                if (b_midlen < 0) b_midlen = 0;
 456
 457                name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7);
 458                sprintf(name, "%.*s{%.*s => %.*s}%s",
 459                        pfx_length, a,
 460                        a_midlen, a + pfx_length,
 461                        b_midlen, b + pfx_length,
 462                        a + len_a - sfx_length);
 463        }
 464        else {
 465                name = xmalloc(len_a + len_b + 5);
 466                sprintf(name, "%s => %s", a, b);
 467        }
 468        return name;
 469}
 470
 471struct diffstat_t {
 472        struct xdiff_emit_state xm;
 473
 474        int nr;
 475        int alloc;
 476        struct diffstat_file {
 477                char *name;
 478                unsigned is_unmerged:1;
 479                unsigned is_binary:1;
 480                unsigned is_renamed:1;
 481                unsigned int added, deleted;
 482        } **files;
 483};
 484
 485static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 486                                          const char *name_a,
 487                                          const char *name_b)
 488{
 489        struct diffstat_file *x;
 490        x = xcalloc(sizeof (*x), 1);
 491        if (diffstat->nr == diffstat->alloc) {
 492                diffstat->alloc = alloc_nr(diffstat->alloc);
 493                diffstat->files = xrealloc(diffstat->files,
 494                                diffstat->alloc * sizeof(x));
 495        }
 496        diffstat->files[diffstat->nr++] = x;
 497        if (name_b) {
 498                x->name = pprint_rename(name_a, name_b);
 499                x->is_renamed = 1;
 500        }
 501        else
 502                x->name = strdup(name_a);
 503        return x;
 504}
 505
 506static void diffstat_consume(void *priv, char *line, unsigned long len)
 507{
 508        struct diffstat_t *diffstat = priv;
 509        struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
 510
 511        if (line[0] == '+')
 512                x->added++;
 513        else if (line[0] == '-')
 514                x->deleted++;
 515}
 516
 517static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++";
 518static const char minuses[]= "----------------------------------------------------------------------";
 519const char mime_boundary_leader[] = "------------";
 520
 521static void show_stats(struct diffstat_t* data)
 522{
 523        int i, len, add, del, total, adds = 0, dels = 0;
 524        int max, max_change = 0, max_len = 0;
 525        int total_files = data->nr;
 526
 527        if (data->nr == 0)
 528                return;
 529
 530        for (i = 0; i < data->nr; i++) {
 531                struct diffstat_file *file = data->files[i];
 532
 533                len = strlen(file->name);
 534                if (max_len < len)
 535                        max_len = len;
 536
 537                if (file->is_binary || file->is_unmerged)
 538                        continue;
 539                if (max_change < file->added + file->deleted)
 540                        max_change = file->added + file->deleted;
 541        }
 542
 543        for (i = 0; i < data->nr; i++) {
 544                const char *prefix = "";
 545                char *name = data->files[i]->name;
 546                int added = data->files[i]->added;
 547                int deleted = data->files[i]->deleted;
 548
 549                if (0 < (len = quote_c_style(name, NULL, NULL, 0))) {
 550                        char *qname = xmalloc(len + 1);
 551                        quote_c_style(name, qname, NULL, 0);
 552                        free(name);
 553                        data->files[i]->name = name = qname;
 554                }
 555
 556                /*
 557                 * "scale" the filename
 558                 */
 559                len = strlen(name);
 560                max = max_len;
 561                if (max > 50)
 562                        max = 50;
 563                if (len > max) {
 564                        char *slash;
 565                        prefix = "...";
 566                        max -= 3;
 567                        name += len - max;
 568                        slash = strchr(name, '/');
 569                        if (slash)
 570                                name = slash;
 571                }
 572                len = max;
 573
 574                /*
 575                 * scale the add/delete
 576                 */
 577                max = max_change;
 578                if (max + len > 70)
 579                        max = 70 - len;
 580
 581                if (data->files[i]->is_binary) {
 582                        printf(" %s%-*s |  Bin\n", prefix, len, name);
 583                        goto free_diffstat_file;
 584                }
 585                else if (data->files[i]->is_unmerged) {
 586                        printf(" %s%-*s |  Unmerged\n", prefix, len, name);
 587                        goto free_diffstat_file;
 588                }
 589                else if (!data->files[i]->is_renamed &&
 590                         (added + deleted == 0)) {
 591                        total_files--;
 592                        goto free_diffstat_file;
 593                }
 594
 595                add = added;
 596                del = deleted;
 597                total = add + del;
 598                adds += add;
 599                dels += del;
 600
 601                if (max_change > 0) {
 602                        total = (total * max + max_change / 2) / max_change;
 603                        add = (add * max + max_change / 2) / max_change;
 604                        del = total - add;
 605                }
 606                printf(" %s%-*s |%5d %.*s%.*s\n", prefix,
 607                                len, name, added + deleted,
 608                                add, pluses, del, minuses);
 609        free_diffstat_file:
 610                free(data->files[i]->name);
 611                free(data->files[i]);
 612        }
 613        free(data->files);
 614        printf(" %d files changed, %d insertions(+), %d deletions(-)\n",
 615                        total_files, adds, dels);
 616}
 617
 618struct checkdiff_t {
 619        struct xdiff_emit_state xm;
 620        const char *filename;
 621        int lineno;
 622};
 623
 624static void checkdiff_consume(void *priv, char *line, unsigned long len)
 625{
 626        struct checkdiff_t *data = priv;
 627
 628        if (line[0] == '+') {
 629                int i, spaces = 0;
 630
 631                data->lineno++;
 632
 633                /* check space before tab */
 634                for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++)
 635                        if (line[i] == ' ')
 636                                spaces++;
 637                if (line[i - 1] == '\t' && spaces)
 638                        printf("%s:%d: space before tab:%.*s\n",
 639                                data->filename, data->lineno, (int)len, line);
 640
 641                /* check white space at line end */
 642                if (line[len - 1] == '\n')
 643                        len--;
 644                if (isspace(line[len - 1]))
 645                        printf("%s:%d: white space at end: %.*s\n",
 646                                data->filename, data->lineno, (int)len, line);
 647        } else if (line[0] == ' ')
 648                data->lineno++;
 649        else if (line[0] == '@') {
 650                char *plus = strchr(line, '+');
 651                if (plus)
 652                        data->lineno = strtol(plus, NULL, 10);
 653                else
 654                        die("invalid diff");
 655        }
 656}
 657
 658static unsigned char *deflate_it(char *data,
 659                                 unsigned long size,
 660                                 unsigned long *result_size)
 661{
 662        int bound;
 663        unsigned char *deflated;
 664        z_stream stream;
 665
 666        memset(&stream, 0, sizeof(stream));
 667        deflateInit(&stream, zlib_compression_level);
 668        bound = deflateBound(&stream, size);
 669        deflated = xmalloc(bound);
 670        stream.next_out = deflated;
 671        stream.avail_out = bound;
 672
 673        stream.next_in = (unsigned char *)data;
 674        stream.avail_in = size;
 675        while (deflate(&stream, Z_FINISH) == Z_OK)
 676                ; /* nothing */
 677        deflateEnd(&stream);
 678        *result_size = stream.total_out;
 679        return deflated;
 680}
 681
 682static void emit_binary_diff(mmfile_t *one, mmfile_t *two)
 683{
 684        void *cp;
 685        void *delta;
 686        void *deflated;
 687        void *data;
 688        unsigned long orig_size;
 689        unsigned long delta_size;
 690        unsigned long deflate_size;
 691        unsigned long data_size;
 692
 693        printf("GIT binary patch\n");
 694        /* We could do deflated delta, or we could do just deflated two,
 695         * whichever is smaller.
 696         */
 697        delta = NULL;
 698        deflated = deflate_it(two->ptr, two->size, &deflate_size);
 699        if (one->size && two->size) {
 700                delta = diff_delta(one->ptr, one->size,
 701                                   two->ptr, two->size,
 702                                   &delta_size, deflate_size);
 703                if (delta) {
 704                        void *to_free = delta;
 705                        orig_size = delta_size;
 706                        delta = deflate_it(delta, delta_size, &delta_size);
 707                        free(to_free);
 708                }
 709        }
 710
 711        if (delta && delta_size < deflate_size) {
 712                printf("delta %lu\n", orig_size);
 713                free(deflated);
 714                data = delta;
 715                data_size = delta_size;
 716        }
 717        else {
 718                printf("literal %lu\n", two->size);
 719                free(delta);
 720                data = deflated;
 721                data_size = deflate_size;
 722        }
 723
 724        /* emit data encoded in base85 */
 725        cp = data;
 726        while (data_size) {
 727                int bytes = (52 < data_size) ? 52 : data_size;
 728                char line[70];
 729                data_size -= bytes;
 730                if (bytes <= 26)
 731                        line[0] = bytes + 'A' - 1;
 732                else
 733                        line[0] = bytes - 26 + 'a' - 1;
 734                encode_85(line + 1, cp, bytes);
 735                cp = (char *) cp + bytes;
 736                puts(line);
 737        }
 738        printf("\n");
 739        free(data);
 740}
 741
 742#define FIRST_FEW_BYTES 8000
 743static int mmfile_is_binary(mmfile_t *mf)
 744{
 745        long sz = mf->size;
 746        if (FIRST_FEW_BYTES < sz)
 747                sz = FIRST_FEW_BYTES;
 748        if (memchr(mf->ptr, 0, sz))
 749                return 1;
 750        return 0;
 751}
 752
 753static void builtin_diff(const char *name_a,
 754                         const char *name_b,
 755                         struct diff_filespec *one,
 756                         struct diff_filespec *two,
 757                         const char *xfrm_msg,
 758                         struct diff_options *o,
 759                         int complete_rewrite)
 760{
 761        mmfile_t mf1, mf2;
 762        const char *lbl[2];
 763        char *a_one, *b_two;
 764        const char *set = diff_get_color(o->color_diff, DIFF_METAINFO);
 765        const char *reset = diff_get_color(o->color_diff, DIFF_RESET);
 766
 767        a_one = quote_two("a/", name_a);
 768        b_two = quote_two("b/", name_b);
 769        lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
 770        lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
 771        printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
 772        if (lbl[0][0] == '/') {
 773                /* /dev/null */
 774                printf("%snew file mode %06o%s\n", set, two->mode, reset);
 775                if (xfrm_msg && xfrm_msg[0])
 776                        printf("%s%s%s\n", set, xfrm_msg, reset);
 777        }
 778        else if (lbl[1][0] == '/') {
 779                printf("%sdeleted file mode %06o%s\n", set, one->mode, reset);
 780                if (xfrm_msg && xfrm_msg[0])
 781                        printf("%s%s%s\n", set, xfrm_msg, reset);
 782        }
 783        else {
 784                if (one->mode != two->mode) {
 785                        printf("%sold mode %06o%s\n", set, one->mode, reset);
 786                        printf("%snew mode %06o%s\n", set, two->mode, reset);
 787                }
 788                if (xfrm_msg && xfrm_msg[0])
 789                        printf("%s%s%s\n", set, xfrm_msg, reset);
 790                /*
 791                 * we do not run diff between different kind
 792                 * of objects.
 793                 */
 794                if ((one->mode ^ two->mode) & S_IFMT)
 795                        goto free_ab_and_return;
 796                if (complete_rewrite) {
 797                        emit_rewrite_diff(name_a, name_b, one, two);
 798                        goto free_ab_and_return;
 799                }
 800        }
 801
 802        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
 803                die("unable to read files to diff");
 804
 805        if (!o->text && (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))) {
 806                /* Quite common confusing case */
 807                if (mf1.size == mf2.size &&
 808                    !memcmp(mf1.ptr, mf2.ptr, mf1.size))
 809                        goto free_ab_and_return;
 810                if (o->binary)
 811                        emit_binary_diff(&mf1, &mf2);
 812                else
 813                        printf("Binary files %s and %s differ\n",
 814                               lbl[0], lbl[1]);
 815        }
 816        else {
 817                /* Crazy xdl interfaces.. */
 818                const char *diffopts = getenv("GIT_DIFF_OPTS");
 819                xpparam_t xpp;
 820                xdemitconf_t xecfg;
 821                xdemitcb_t ecb;
 822                struct emit_callback ecbdata;
 823
 824                memset(&ecbdata, 0, sizeof(ecbdata));
 825                ecbdata.label_path = lbl;
 826                ecbdata.color_diff = o->color_diff;
 827                xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
 828                xecfg.ctxlen = o->context;
 829                xecfg.flags = XDL_EMIT_FUNCNAMES;
 830                if (!diffopts)
 831                        ;
 832                else if (!strncmp(diffopts, "--unified=", 10))
 833                        xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
 834                else if (!strncmp(diffopts, "-u", 2))
 835                        xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
 836                ecb.outf = xdiff_outf;
 837                ecb.priv = &ecbdata;
 838                ecbdata.xm.consume = fn_out_consume;
 839                xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
 840        }
 841
 842 free_ab_and_return:
 843        free(a_one);
 844        free(b_two);
 845        return;
 846}
 847
 848static void builtin_diffstat(const char *name_a, const char *name_b,
 849                             struct diff_filespec *one,
 850                             struct diff_filespec *two,
 851                             struct diffstat_t *diffstat,
 852                             struct diff_options *o,
 853                             int complete_rewrite)
 854{
 855        mmfile_t mf1, mf2;
 856        struct diffstat_file *data;
 857
 858        data = diffstat_add(diffstat, name_a, name_b);
 859
 860        if (!one || !two) {
 861                data->is_unmerged = 1;
 862                return;
 863        }
 864        if (complete_rewrite) {
 865                diff_populate_filespec(one, 0);
 866                diff_populate_filespec(two, 0);
 867                data->deleted = count_lines(one->data, one->size);
 868                data->added = count_lines(two->data, two->size);
 869                return;
 870        }
 871        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
 872                die("unable to read files to diff");
 873
 874        if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2))
 875                data->is_binary = 1;
 876        else {
 877                /* Crazy xdl interfaces.. */
 878                xpparam_t xpp;
 879                xdemitconf_t xecfg;
 880                xdemitcb_t ecb;
 881
 882                xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
 883                xecfg.ctxlen = 0;
 884                xecfg.flags = 0;
 885                ecb.outf = xdiff_outf;
 886                ecb.priv = diffstat;
 887                xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
 888        }
 889}
 890
 891static void builtin_checkdiff(const char *name_a, const char *name_b,
 892                             struct diff_filespec *one,
 893                             struct diff_filespec *two)
 894{
 895        mmfile_t mf1, mf2;
 896        struct checkdiff_t data;
 897
 898        if (!two)
 899                return;
 900
 901        memset(&data, 0, sizeof(data));
 902        data.xm.consume = checkdiff_consume;
 903        data.filename = name_b ? name_b : name_a;
 904        data.lineno = 0;
 905
 906        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
 907                die("unable to read files to diff");
 908
 909        if (mmfile_is_binary(&mf2))
 910                return;
 911        else {
 912                /* Crazy xdl interfaces.. */
 913                xpparam_t xpp;
 914                xdemitconf_t xecfg;
 915                xdemitcb_t ecb;
 916
 917                xpp.flags = XDF_NEED_MINIMAL;
 918                xecfg.ctxlen = 0;
 919                xecfg.flags = 0;
 920                ecb.outf = xdiff_outf;
 921                ecb.priv = &data;
 922                xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
 923        }
 924}
 925
 926struct diff_filespec *alloc_filespec(const char *path)
 927{
 928        int namelen = strlen(path);
 929        struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
 930
 931        memset(spec, 0, sizeof(*spec));
 932        spec->path = (char *)(spec + 1);
 933        memcpy(spec->path, path, namelen+1);
 934        return spec;
 935}
 936
 937void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
 938                   unsigned short mode)
 939{
 940        if (mode) {
 941                spec->mode = canon_mode(mode);
 942                memcpy(spec->sha1, sha1, 20);
 943                spec->sha1_valid = !!memcmp(sha1, null_sha1, 20);
 944        }
 945}
 946
 947/*
 948 * Given a name and sha1 pair, if the dircache tells us the file in
 949 * the work tree has that object contents, return true, so that
 950 * prepare_temp_file() does not have to inflate and extract.
 951 */
 952static int work_tree_matches(const char *name, const unsigned char *sha1)
 953{
 954        struct cache_entry *ce;
 955        struct stat st;
 956        int pos, len;
 957
 958        /* We do not read the cache ourselves here, because the
 959         * benchmark with my previous version that always reads cache
 960         * shows that it makes things worse for diff-tree comparing
 961         * two linux-2.6 kernel trees in an already checked out work
 962         * tree.  This is because most diff-tree comparisons deal with
 963         * only a small number of files, while reading the cache is
 964         * expensive for a large project, and its cost outweighs the
 965         * savings we get by not inflating the object to a temporary
 966         * file.  Practically, this code only helps when we are used
 967         * by diff-cache --cached, which does read the cache before
 968         * calling us.
 969         */
 970        if (!active_cache)
 971                return 0;
 972
 973        len = strlen(name);
 974        pos = cache_name_pos(name, len);
 975        if (pos < 0)
 976                return 0;
 977        ce = active_cache[pos];
 978        if ((lstat(name, &st) < 0) ||
 979            !S_ISREG(st.st_mode) || /* careful! */
 980            ce_match_stat(ce, &st, 0) ||
 981            memcmp(sha1, ce->sha1, 20))
 982                return 0;
 983        /* we return 1 only when we can stat, it is a regular file,
 984         * stat information matches, and sha1 recorded in the cache
 985         * matches.  I.e. we know the file in the work tree really is
 986         * the same as the <name, sha1> pair.
 987         */
 988        return 1;
 989}
 990
 991static struct sha1_size_cache {
 992        unsigned char sha1[20];
 993        unsigned long size;
 994} **sha1_size_cache;
 995static int sha1_size_cache_nr, sha1_size_cache_alloc;
 996
 997static struct sha1_size_cache *locate_size_cache(unsigned char *sha1,
 998                                                 int find_only,
 999                                                 unsigned long size)
1000{
1001        int first, last;
1002        struct sha1_size_cache *e;
1003
1004        first = 0;
1005        last = sha1_size_cache_nr;
1006        while (last > first) {
1007                int cmp, next = (last + first) >> 1;
1008                e = sha1_size_cache[next];
1009                cmp = memcmp(e->sha1, sha1, 20);
1010                if (!cmp)
1011                        return e;
1012                if (cmp < 0) {
1013                        last = next;
1014                        continue;
1015                }
1016                first = next+1;
1017        }
1018        /* not found */
1019        if (find_only)
1020                return NULL;
1021        /* insert to make it at "first" */
1022        if (sha1_size_cache_alloc <= sha1_size_cache_nr) {
1023                sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc);
1024                sha1_size_cache = xrealloc(sha1_size_cache,
1025                                           sha1_size_cache_alloc *
1026                                           sizeof(*sha1_size_cache));
1027        }
1028        sha1_size_cache_nr++;
1029        if (first < sha1_size_cache_nr)
1030                memmove(sha1_size_cache + first + 1, sha1_size_cache + first,
1031                        (sha1_size_cache_nr - first - 1) *
1032                        sizeof(*sha1_size_cache));
1033        e = xmalloc(sizeof(struct sha1_size_cache));
1034        sha1_size_cache[first] = e;
1035        memcpy(e->sha1, sha1, 20);
1036        e->size = size;
1037        return e;
1038}
1039
1040/*
1041 * While doing rename detection and pickaxe operation, we may need to
1042 * grab the data for the blob (or file) for our own in-core comparison.
1043 * diff_filespec has data and size fields for this purpose.
1044 */
1045int diff_populate_filespec(struct diff_filespec *s, int size_only)
1046{
1047        int err = 0;
1048        if (!DIFF_FILE_VALID(s))
1049                die("internal error: asking to populate invalid file.");
1050        if (S_ISDIR(s->mode))
1051                return -1;
1052
1053        if (!use_size_cache)
1054                size_only = 0;
1055
1056        if (s->data)
1057                return err;
1058        if (!s->sha1_valid ||
1059            work_tree_matches(s->path, s->sha1)) {
1060                struct stat st;
1061                int fd;
1062                if (lstat(s->path, &st) < 0) {
1063                        if (errno == ENOENT) {
1064                        err_empty:
1065                                err = -1;
1066                        empty:
1067                                s->data = (char *)"";
1068                                s->size = 0;
1069                                return err;
1070                        }
1071                }
1072                s->size = st.st_size;
1073                if (!s->size)
1074                        goto empty;
1075                if (size_only)
1076                        return 0;
1077                if (S_ISLNK(st.st_mode)) {
1078                        int ret;
1079                        s->data = xmalloc(s->size);
1080                        s->should_free = 1;
1081                        ret = readlink(s->path, s->data, s->size);
1082                        if (ret < 0) {
1083                                free(s->data);
1084                                goto err_empty;
1085                        }
1086                        return 0;
1087                }
1088                fd = open(s->path, O_RDONLY);
1089                if (fd < 0)
1090                        goto err_empty;
1091                s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1092                close(fd);
1093                if (s->data == MAP_FAILED)
1094                        goto err_empty;
1095                s->should_munmap = 1;
1096        }
1097        else {
1098                char type[20];
1099                struct sha1_size_cache *e;
1100
1101                if (size_only) {
1102                        e = locate_size_cache(s->sha1, 1, 0);
1103                        if (e) {
1104                                s->size = e->size;
1105                                return 0;
1106                        }
1107                        if (!sha1_object_info(s->sha1, type, &s->size))
1108                                locate_size_cache(s->sha1, 0, s->size);
1109                }
1110                else {
1111                        s->data = read_sha1_file(s->sha1, type, &s->size);
1112                        s->should_free = 1;
1113                }
1114        }
1115        return 0;
1116}
1117
1118void diff_free_filespec_data(struct diff_filespec *s)
1119{
1120        if (s->should_free)
1121                free(s->data);
1122        else if (s->should_munmap)
1123                munmap(s->data, s->size);
1124        s->should_free = s->should_munmap = 0;
1125        s->data = NULL;
1126        free(s->cnt_data);
1127        s->cnt_data = NULL;
1128}
1129
1130static void prep_temp_blob(struct diff_tempfile *temp,
1131                           void *blob,
1132                           unsigned long size,
1133                           const unsigned char *sha1,
1134                           int mode)
1135{
1136        int fd;
1137
1138        fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX");
1139        if (fd < 0)
1140                die("unable to create temp-file");
1141        if (write(fd, blob, size) != size)
1142                die("unable to write temp-file");
1143        close(fd);
1144        temp->name = temp->tmp_path;
1145        strcpy(temp->hex, sha1_to_hex(sha1));
1146        temp->hex[40] = 0;
1147        sprintf(temp->mode, "%06o", mode);
1148}
1149
1150static void prepare_temp_file(const char *name,
1151                              struct diff_tempfile *temp,
1152                              struct diff_filespec *one)
1153{
1154        if (!DIFF_FILE_VALID(one)) {
1155        not_a_valid_file:
1156                /* A '-' entry produces this for file-2, and
1157                 * a '+' entry produces this for file-1.
1158                 */
1159                temp->name = "/dev/null";
1160                strcpy(temp->hex, ".");
1161                strcpy(temp->mode, ".");
1162                return;
1163        }
1164
1165        if (!one->sha1_valid ||
1166            work_tree_matches(name, one->sha1)) {
1167                struct stat st;
1168                if (lstat(name, &st) < 0) {
1169                        if (errno == ENOENT)
1170                                goto not_a_valid_file;
1171                        die("stat(%s): %s", name, strerror(errno));
1172                }
1173                if (S_ISLNK(st.st_mode)) {
1174                        int ret;
1175                        char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1176                        if (sizeof(buf) <= st.st_size)
1177                                die("symlink too long: %s", name);
1178                        ret = readlink(name, buf, st.st_size);
1179                        if (ret < 0)
1180                                die("readlink(%s)", name);
1181                        prep_temp_blob(temp, buf, st.st_size,
1182                                       (one->sha1_valid ?
1183                                        one->sha1 : null_sha1),
1184                                       (one->sha1_valid ?
1185                                        one->mode : S_IFLNK));
1186                }
1187                else {
1188                        /* we can borrow from the file in the work tree */
1189                        temp->name = name;
1190                        if (!one->sha1_valid)
1191                                strcpy(temp->hex, sha1_to_hex(null_sha1));
1192                        else
1193                                strcpy(temp->hex, sha1_to_hex(one->sha1));
1194                        /* Even though we may sometimes borrow the
1195                         * contents from the work tree, we always want
1196                         * one->mode.  mode is trustworthy even when
1197                         * !(one->sha1_valid), as long as
1198                         * DIFF_FILE_VALID(one).
1199                         */
1200                        sprintf(temp->mode, "%06o", one->mode);
1201                }
1202                return;
1203        }
1204        else {
1205                if (diff_populate_filespec(one, 0))
1206                        die("cannot read data blob for %s", one->path);
1207                prep_temp_blob(temp, one->data, one->size,
1208                               one->sha1, one->mode);
1209        }
1210}
1211
1212static void remove_tempfile(void)
1213{
1214        int i;
1215
1216        for (i = 0; i < 2; i++)
1217                if (diff_temp[i].name == diff_temp[i].tmp_path) {
1218                        unlink(diff_temp[i].name);
1219                        diff_temp[i].name = NULL;
1220                }
1221}
1222
1223static void remove_tempfile_on_signal(int signo)
1224{
1225        remove_tempfile();
1226        signal(SIGINT, SIG_DFL);
1227        raise(signo);
1228}
1229
1230static int spawn_prog(const char *pgm, const char **arg)
1231{
1232        pid_t pid;
1233        int status;
1234
1235        fflush(NULL);
1236        pid = fork();
1237        if (pid < 0)
1238                die("unable to fork");
1239        if (!pid) {
1240                execvp(pgm, (char *const*) arg);
1241                exit(255);
1242        }
1243
1244        while (waitpid(pid, &status, 0) < 0) {
1245                if (errno == EINTR)
1246                        continue;
1247                return -1;
1248        }
1249
1250        /* Earlier we did not check the exit status because
1251         * diff exits non-zero if files are different, and
1252         * we are not interested in knowing that.  It was a
1253         * mistake which made it harder to quit a diff-*
1254         * session that uses the git-apply-patch-script as
1255         * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
1256         * should also exit non-zero only when it wants to
1257         * abort the entire diff-* session.
1258         */
1259        if (WIFEXITED(status) && !WEXITSTATUS(status))
1260                return 0;
1261        return -1;
1262}
1263
1264/* An external diff command takes:
1265 *
1266 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1267 *               infile2 infile2-sha1 infile2-mode [ rename-to ]
1268 *
1269 */
1270static void run_external_diff(const char *pgm,
1271                              const char *name,
1272                              const char *other,
1273                              struct diff_filespec *one,
1274                              struct diff_filespec *two,
1275                              const char *xfrm_msg,
1276                              int complete_rewrite)
1277{
1278        const char *spawn_arg[10];
1279        struct diff_tempfile *temp = diff_temp;
1280        int retval;
1281        static int atexit_asked = 0;
1282        const char *othername;
1283        const char **arg = &spawn_arg[0];
1284
1285        othername = (other? other : name);
1286        if (one && two) {
1287                prepare_temp_file(name, &temp[0], one);
1288                prepare_temp_file(othername, &temp[1], two);
1289                if (! atexit_asked &&
1290                    (temp[0].name == temp[0].tmp_path ||
1291                     temp[1].name == temp[1].tmp_path)) {
1292                        atexit_asked = 1;
1293                        atexit(remove_tempfile);
1294                }
1295                signal(SIGINT, remove_tempfile_on_signal);
1296        }
1297
1298        if (one && two) {
1299                *arg++ = pgm;
1300                *arg++ = name;
1301                *arg++ = temp[0].name;
1302                *arg++ = temp[0].hex;
1303                *arg++ = temp[0].mode;
1304                *arg++ = temp[1].name;
1305                *arg++ = temp[1].hex;
1306                *arg++ = temp[1].mode;
1307                if (other) {
1308                        *arg++ = other;
1309                        *arg++ = xfrm_msg;
1310                }
1311        } else {
1312                *arg++ = pgm;
1313                *arg++ = name;
1314        }
1315        *arg = NULL;
1316        retval = spawn_prog(pgm, spawn_arg);
1317        remove_tempfile();
1318        if (retval) {
1319                fprintf(stderr, "external diff died, stopping at %s.\n", name);
1320                exit(1);
1321        }
1322}
1323
1324static void run_diff_cmd(const char *pgm,
1325                         const char *name,
1326                         const char *other,
1327                         struct diff_filespec *one,
1328                         struct diff_filespec *two,
1329                         const char *xfrm_msg,
1330                         struct diff_options *o,
1331                         int complete_rewrite)
1332{
1333        if (pgm) {
1334                run_external_diff(pgm, name, other, one, two, xfrm_msg,
1335                                  complete_rewrite);
1336                return;
1337        }
1338        if (one && two)
1339                builtin_diff(name, other ? other : name,
1340                             one, two, xfrm_msg, o, complete_rewrite);
1341        else
1342                printf("* Unmerged path %s\n", name);
1343}
1344
1345static void diff_fill_sha1_info(struct diff_filespec *one)
1346{
1347        if (DIFF_FILE_VALID(one)) {
1348                if (!one->sha1_valid) {
1349                        struct stat st;
1350                        if (lstat(one->path, &st) < 0)
1351                                die("stat %s", one->path);
1352                        if (index_path(one->sha1, one->path, &st, 0))
1353                                die("cannot hash %s\n", one->path);
1354                }
1355        }
1356        else
1357                memset(one->sha1, 0, 20);
1358}
1359
1360static void run_diff(struct diff_filepair *p, struct diff_options *o)
1361{
1362        const char *pgm = external_diff();
1363        char msg[PATH_MAX*2+300], *xfrm_msg;
1364        struct diff_filespec *one;
1365        struct diff_filespec *two;
1366        const char *name;
1367        const char *other;
1368        char *name_munged, *other_munged;
1369        int complete_rewrite = 0;
1370        int len;
1371
1372        if (DIFF_PAIR_UNMERGED(p)) {
1373                /* unmerged */
1374                run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0);
1375                return;
1376        }
1377
1378        name = p->one->path;
1379        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1380        name_munged = quote_one(name);
1381        other_munged = quote_one(other);
1382        one = p->one; two = p->two;
1383
1384        diff_fill_sha1_info(one);
1385        diff_fill_sha1_info(two);
1386
1387        len = 0;
1388        switch (p->status) {
1389        case DIFF_STATUS_COPIED:
1390                len += snprintf(msg + len, sizeof(msg) - len,
1391                                "similarity index %d%%\n"
1392                                "copy from %s\n"
1393                                "copy to %s\n",
1394                                (int)(0.5 + p->score * 100.0/MAX_SCORE),
1395                                name_munged, other_munged);
1396                break;
1397        case DIFF_STATUS_RENAMED:
1398                len += snprintf(msg + len, sizeof(msg) - len,
1399                                "similarity index %d%%\n"
1400                                "rename from %s\n"
1401                                "rename to %s\n",
1402                                (int)(0.5 + p->score * 100.0/MAX_SCORE),
1403                                name_munged, other_munged);
1404                break;
1405        case DIFF_STATUS_MODIFIED:
1406                if (p->score) {
1407                        len += snprintf(msg + len, sizeof(msg) - len,
1408                                        "dissimilarity index %d%%\n",
1409                                        (int)(0.5 + p->score *
1410                                              100.0/MAX_SCORE));
1411                        complete_rewrite = 1;
1412                        break;
1413                }
1414                /* fallthru */
1415        default:
1416                /* nothing */
1417                ;
1418        }
1419
1420        if (memcmp(one->sha1, two->sha1, 20)) {
1421                int abbrev = o->full_index ? 40 : DEFAULT_ABBREV;
1422
1423                len += snprintf(msg + len, sizeof(msg) - len,
1424                                "index %.*s..%.*s",
1425                                abbrev, sha1_to_hex(one->sha1),
1426                                abbrev, sha1_to_hex(two->sha1));
1427                if (one->mode == two->mode)
1428                        len += snprintf(msg + len, sizeof(msg) - len,
1429                                        " %06o", one->mode);
1430                len += snprintf(msg + len, sizeof(msg) - len, "\n");
1431        }
1432
1433        if (len)
1434                msg[--len] = 0;
1435        xfrm_msg = len ? msg : NULL;
1436
1437        if (!pgm &&
1438            DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
1439            (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
1440                /* a filepair that changes between file and symlink
1441                 * needs to be split into deletion and creation.
1442                 */
1443                struct diff_filespec *null = alloc_filespec(two->path);
1444                run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0);
1445                free(null);
1446                null = alloc_filespec(one->path);
1447                run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0);
1448                free(null);
1449        }
1450        else
1451                run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o,
1452                             complete_rewrite);
1453
1454        free(name_munged);
1455        free(other_munged);
1456}
1457
1458static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
1459                         struct diffstat_t *diffstat)
1460{
1461        const char *name;
1462        const char *other;
1463        int complete_rewrite = 0;
1464
1465        if (DIFF_PAIR_UNMERGED(p)) {
1466                /* unmerged */
1467                builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
1468                return;
1469        }
1470
1471        name = p->one->path;
1472        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1473
1474        diff_fill_sha1_info(p->one);
1475        diff_fill_sha1_info(p->two);
1476
1477        if (p->status == DIFF_STATUS_MODIFIED && p->score)
1478                complete_rewrite = 1;
1479        builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
1480}
1481
1482static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
1483{
1484        const char *name;
1485        const char *other;
1486
1487        if (DIFF_PAIR_UNMERGED(p)) {
1488                /* unmerged */
1489                return;
1490        }
1491
1492        name = p->one->path;
1493        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
1494
1495        diff_fill_sha1_info(p->one);
1496        diff_fill_sha1_info(p->two);
1497
1498        builtin_checkdiff(name, other, p->one, p->two);
1499}
1500
1501void diff_setup(struct diff_options *options)
1502{
1503        memset(options, 0, sizeof(*options));
1504        options->line_termination = '\n';
1505        options->break_opt = -1;
1506        options->rename_limit = -1;
1507        options->context = 3;
1508        options->msg_sep = "";
1509
1510        options->change = diff_change;
1511        options->add_remove = diff_addremove;
1512        options->color_diff = diff_use_color_default;
1513        options->detect_rename = diff_detect_rename_default;
1514}
1515
1516int diff_setup_done(struct diff_options *options)
1517{
1518        if (options->find_copies_harder)
1519                options->detect_rename = DIFF_DETECT_COPY;
1520
1521        if ((0 <= options->rename_limit && !options->detect_rename)
1522                return -1;
1523
1524        if (options->output_format & (DIFF_FORMAT_NAME |
1525                                      DIFF_FORMAT_NAME_STATUS |
1526                                      DIFF_FORMAT_CHECKDIFF |
1527                                      DIFF_FORMAT_NO_OUTPUT))
1528                options->output_format &= ~(DIFF_FORMAT_RAW |
1529                                            DIFF_FORMAT_DIFFSTAT |
1530                                            DIFF_FORMAT_SUMMARY |
1531                                            DIFF_FORMAT_PATCH);
1532
1533        /*
1534         * These cases always need recursive; we do not drop caller-supplied
1535         * recursive bits for other formats here.
1536         */
1537        if (options->output_format & (DIFF_FORMAT_PATCH |
1538                                      DIFF_FORMAT_DIFFSTAT |
1539                                      DIFF_FORMAT_CHECKDIFF))
1540                options->recursive = 1;
1541        /*
1542         * Also pickaxe would not work very well if you do not say recursive
1543         */
1544        if (options->pickaxe)
1545                options->recursive = 1;
1546
1547        if (options->detect_rename && options->rename_limit < 0)
1548                options->rename_limit = diff_rename_limit_default;
1549        if (options->setup & DIFF_SETUP_USE_CACHE) {
1550                if (!active_cache)
1551                        /* read-cache does not die even when it fails
1552                         * so it is safe for us to do this here.  Also
1553                         * it does not smudge active_cache or active_nr
1554                         * when it fails, so we do not have to worry about
1555                         * cleaning it up ourselves either.
1556                         */
1557                        read_cache();
1558        }
1559        if (options->setup & DIFF_SETUP_USE_SIZE_CACHE)
1560                use_size_cache = 1;
1561        if (options->abbrev <= 0 || 40 < options->abbrev)
1562                options->abbrev = 40; /* full */
1563
1564        return 0;
1565}
1566
1567static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
1568{
1569        char c, *eq;
1570        int len;
1571
1572        if (*arg != '-')
1573                return 0;
1574        c = *++arg;
1575        if (!c)
1576                return 0;
1577        if (c == arg_short) {
1578                c = *++arg;
1579                if (!c)
1580                        return 1;
1581                if (val && isdigit(c)) {
1582                        char *end;
1583                        int n = strtoul(arg, &end, 10);
1584                        if (*end)
1585                                return 0;
1586                        *val = n;
1587                        return 1;
1588                }
1589                return 0;
1590        }
1591        if (c != '-')
1592                return 0;
1593        arg++;
1594        eq = strchr(arg, '=');
1595        if (eq)
1596                len = eq - arg;
1597        else
1598                len = strlen(arg);
1599        if (!len || strncmp(arg, arg_long, len))
1600                return 0;
1601        if (eq) {
1602                int n;
1603                char *end;
1604                if (!isdigit(*++eq))
1605                        return 0;
1606                n = strtoul(eq, &end, 10);
1607                if (*end)
1608                        return 0;
1609                *val = n;
1610        }
1611        return 1;
1612}
1613
1614int diff_opt_parse(struct diff_options *options, const char **av, int ac)
1615{
1616        const char *arg = av[0];
1617        if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
1618                options->output_format |= DIFF_FORMAT_PATCH;
1619        else if (opt_arg(arg, 'U', "unified", &options->context))
1620                options->output_format |= DIFF_FORMAT_PATCH;
1621        else if (!strcmp(arg, "--raw"))
1622                options->output_format |= DIFF_FORMAT_RAW;
1623        else if (!strcmp(arg, "--patch-with-raw")) {
1624                options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
1625        }
1626        else if (!strcmp(arg, "--stat"))
1627                options->output_format |= DIFF_FORMAT_DIFFSTAT;
1628        else if (!strcmp(arg, "--check"))
1629                options->output_format |= DIFF_FORMAT_CHECKDIFF;
1630        else if (!strcmp(arg, "--summary"))
1631                options->output_format |= DIFF_FORMAT_SUMMARY;
1632        else if (!strcmp(arg, "--patch-with-stat")) {
1633                options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
1634        }
1635        else if (!strcmp(arg, "-z"))
1636                options->line_termination = 0;
1637        else if (!strncmp(arg, "-l", 2))
1638                options->rename_limit = strtoul(arg+2, NULL, 10);
1639        else if (!strcmp(arg, "--full-index"))
1640                options->full_index = 1;
1641        else if (!strcmp(arg, "--binary")) {
1642                options->output_format |= DIFF_FORMAT_PATCH;
1643                options->full_index = options->binary = 1;
1644        }
1645        else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) {
1646                options->text = 1;
1647        }
1648        else if (!strcmp(arg, "--name-only"))
1649                options->output_format |= DIFF_FORMAT_NAME;
1650        else if (!strcmp(arg, "--name-status"))
1651                options->output_format |= DIFF_FORMAT_NAME_STATUS;
1652        else if (!strcmp(arg, "-R"))
1653                options->reverse_diff = 1;
1654        else if (!strncmp(arg, "-S", 2))
1655                options->pickaxe = arg + 2;
1656        else if (!strcmp(arg, "-s")) {
1657                options->output_format |= DIFF_FORMAT_NO_OUTPUT;
1658        }
1659        else if (!strncmp(arg, "-O", 2))
1660                options->orderfile = arg + 2;
1661        else if (!strncmp(arg, "--diff-filter=", 14))
1662                options->filter = arg + 14;
1663        else if (!strcmp(arg, "--pickaxe-all"))
1664                options->pickaxe_opts = DIFF_PICKAXE_ALL;
1665        else if (!strcmp(arg, "--pickaxe-regex"))
1666                options->pickaxe_opts = DIFF_PICKAXE_REGEX;
1667        else if (!strncmp(arg, "-B", 2)) {
1668                if ((options->break_opt =
1669                     diff_scoreopt_parse(arg)) == -1)
1670                        return -1;
1671        }
1672        else if (!strncmp(arg, "-M", 2)) {
1673                if ((options->rename_score =
1674                     diff_scoreopt_parse(arg)) == -1)
1675                        return -1;
1676                options->detect_rename = DIFF_DETECT_RENAME;
1677        }
1678        else if (!strncmp(arg, "-C", 2)) {
1679                if ((options->rename_score =
1680                     diff_scoreopt_parse(arg)) == -1)
1681                        return -1;
1682                options->detect_rename = DIFF_DETECT_COPY;
1683        }
1684        else if (!strcmp(arg, "--find-copies-harder"))
1685                options->find_copies_harder = 1;
1686        else if (!strcmp(arg, "--abbrev"))
1687                options->abbrev = DEFAULT_ABBREV;
1688        else if (!strncmp(arg, "--abbrev=", 9)) {
1689                options->abbrev = strtoul(arg + 9, NULL, 10);
1690                if (options->abbrev < MINIMUM_ABBREV)
1691                        options->abbrev = MINIMUM_ABBREV;
1692                else if (40 < options->abbrev)
1693                        options->abbrev = 40;
1694        }
1695        else if (!strcmp(arg, "--color"))
1696                options->color_diff = 1;
1697        else if (!strcmp(arg, "--no-color"))
1698                options->color_diff = 0;
1699        else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
1700                options->xdl_opts |= XDF_IGNORE_WHITESPACE;
1701        else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
1702                options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
1703        else if (!strcmp(arg, "--no-renames"))
1704                options->detect_rename = 0;
1705        else
1706                return 0;
1707        return 1;
1708}
1709
1710static int parse_num(const char **cp_p)
1711{
1712        unsigned long num, scale;
1713        int ch, dot;
1714        const char *cp = *cp_p;
1715
1716        num = 0;
1717        scale = 1;
1718        dot = 0;
1719        for(;;) {
1720                ch = *cp;
1721                if ( !dot && ch == '.' ) {
1722                        scale = 1;
1723                        dot = 1;
1724                } else if ( ch == '%' ) {
1725                        scale = dot ? scale*100 : 100;
1726                        cp++;   /* % is always at the end */
1727                        break;
1728                } else if ( ch >= '0' && ch <= '9' ) {
1729                        if ( scale < 100000 ) {
1730                                scale *= 10;
1731                                num = (num*10) + (ch-'0');
1732                        }
1733                } else {
1734                        break;
1735                }
1736                cp++;
1737        }
1738        *cp_p = cp;
1739
1740        /* user says num divided by scale and we say internally that
1741         * is MAX_SCORE * num / scale.
1742         */
1743        return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale);
1744}
1745
1746int diff_scoreopt_parse(const char *opt)
1747{
1748        int opt1, opt2, cmd;
1749
1750        if (*opt++ != '-')
1751                return -1;
1752        cmd = *opt++;
1753        if (cmd != 'M' && cmd != 'C' && cmd != 'B')
1754                return -1; /* that is not a -M, -C nor -B option */
1755
1756        opt1 = parse_num(&opt);
1757        if (cmd != 'B')
1758                opt2 = 0;
1759        else {
1760                if (*opt == 0)
1761                        opt2 = 0;
1762                else if (*opt != '/')
1763                        return -1; /* we expect -B80/99 or -B80 */
1764                else {
1765                        opt++;
1766                        opt2 = parse_num(&opt);
1767                }
1768        }
1769        if (*opt != 0)
1770                return -1;
1771        return opt1 | (opt2 << 16);
1772}
1773
1774struct diff_queue_struct diff_queued_diff;
1775
1776void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
1777{
1778        if (queue->alloc <= queue->nr) {
1779                queue->alloc = alloc_nr(queue->alloc);
1780                queue->queue = xrealloc(queue->queue,
1781                                        sizeof(dp) * queue->alloc);
1782        }
1783        queue->queue[queue->nr++] = dp;
1784}
1785
1786struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
1787                                 struct diff_filespec *one,
1788                                 struct diff_filespec *two)
1789{
1790        struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
1791        dp->one = one;
1792        dp->two = two;
1793        if (queue)
1794                diff_q(queue, dp);
1795        return dp;
1796}
1797
1798void diff_free_filepair(struct diff_filepair *p)
1799{
1800        diff_free_filespec_data(p->one);
1801        diff_free_filespec_data(p->two);
1802        free(p->one);
1803        free(p->two);
1804        free(p);
1805}
1806
1807/* This is different from find_unique_abbrev() in that
1808 * it stuffs the result with dots for alignment.
1809 */
1810const char *diff_unique_abbrev(const unsigned char *sha1, int len)
1811{
1812        int abblen;
1813        const char *abbrev;
1814        if (len == 40)
1815                return sha1_to_hex(sha1);
1816
1817        abbrev = find_unique_abbrev(sha1, len);
1818        if (!abbrev)
1819                return sha1_to_hex(sha1);
1820        abblen = strlen(abbrev);
1821        if (abblen < 37) {
1822                static char hex[41];
1823                if (len < abblen && abblen <= len + 2)
1824                        sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
1825                else
1826                        sprintf(hex, "%s...", abbrev);
1827                return hex;
1828        }
1829        return sha1_to_hex(sha1);
1830}
1831
1832static void diff_flush_raw(struct diff_filepair *p,
1833                           struct diff_options *options)
1834{
1835        int two_paths;
1836        char status[10];
1837        int abbrev = options->abbrev;
1838        const char *path_one, *path_two;
1839        int inter_name_termination = '\t';
1840        int line_termination = options->line_termination;
1841
1842        if (!line_termination)
1843                inter_name_termination = 0;
1844
1845        path_one = p->one->path;
1846        path_two = p->two->path;
1847        if (line_termination) {
1848                path_one = quote_one(path_one);
1849                path_two = quote_one(path_two);
1850        }
1851
1852        if (p->score)
1853                sprintf(status, "%c%03d", p->status,
1854                        (int)(0.5 + p->score * 100.0/MAX_SCORE));
1855        else {
1856                status[0] = p->status;
1857                status[1] = 0;
1858        }
1859        switch (p->status) {
1860        case DIFF_STATUS_COPIED:
1861        case DIFF_STATUS_RENAMED:
1862                two_paths = 1;
1863                break;
1864        case DIFF_STATUS_ADDED:
1865        case DIFF_STATUS_DELETED:
1866                two_paths = 0;
1867                break;
1868        default:
1869                two_paths = 0;
1870                break;
1871        }
1872        if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) {
1873                printf(":%06o %06o %s ",
1874                       p->one->mode, p->two->mode,
1875                       diff_unique_abbrev(p->one->sha1, abbrev));
1876                printf("%s ",
1877                       diff_unique_abbrev(p->two->sha1, abbrev));
1878        }
1879        printf("%s%c%s", status, inter_name_termination, path_one);
1880        if (two_paths)
1881                printf("%c%s", inter_name_termination, path_two);
1882        putchar(line_termination);
1883        if (path_one != p->one->path)
1884                free((void*)path_one);
1885        if (path_two != p->two->path)
1886                free((void*)path_two);
1887}
1888
1889static void diff_flush_name(struct diff_filepair *p, int line_termination)
1890{
1891        char *path = p->two->path;
1892
1893        if (line_termination)
1894                path = quote_one(p->two->path);
1895        printf("%s%c", path, line_termination);
1896        if (p->two->path != path)
1897                free(path);
1898}
1899
1900int diff_unmodified_pair(struct diff_filepair *p)
1901{
1902        /* This function is written stricter than necessary to support
1903         * the currently implemented transformers, but the idea is to
1904         * let transformers to produce diff_filepairs any way they want,
1905         * and filter and clean them up here before producing the output.
1906         */
1907        struct diff_filespec *one, *two;
1908
1909        if (DIFF_PAIR_UNMERGED(p))
1910                return 0; /* unmerged is interesting */
1911
1912        one = p->one;
1913        two = p->two;
1914
1915        /* deletion, addition, mode or type change
1916         * and rename are all interesting.
1917         */
1918        if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
1919            DIFF_PAIR_MODE_CHANGED(p) ||
1920            strcmp(one->path, two->path))
1921                return 0;
1922
1923        /* both are valid and point at the same path.  that is, we are
1924         * dealing with a change.
1925         */
1926        if (one->sha1_valid && two->sha1_valid &&
1927            !memcmp(one->sha1, two->sha1, sizeof(one->sha1)))
1928                return 1; /* no change */
1929        if (!one->sha1_valid && !two->sha1_valid)
1930                return 1; /* both look at the same file on the filesystem. */
1931        return 0;
1932}
1933
1934static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
1935{
1936        if (diff_unmodified_pair(p))
1937                return;
1938
1939        if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1940            (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1941                return; /* no tree diffs in patch format */
1942
1943        run_diff(p, o);
1944}
1945
1946static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
1947                            struct diffstat_t *diffstat)
1948{
1949        if (diff_unmodified_pair(p))
1950                return;
1951
1952        if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1953            (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1954                return; /* no tree diffs in patch format */
1955
1956        run_diffstat(p, o, diffstat);
1957}
1958
1959static void diff_flush_checkdiff(struct diff_filepair *p,
1960                struct diff_options *o)
1961{
1962        if (diff_unmodified_pair(p))
1963                return;
1964
1965        if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
1966            (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
1967                return; /* no tree diffs in patch format */
1968
1969        run_checkdiff(p, o);
1970}
1971
1972int diff_queue_is_empty(void)
1973{
1974        struct diff_queue_struct *q = &diff_queued_diff;
1975        int i;
1976        for (i = 0; i < q->nr; i++)
1977                if (!diff_unmodified_pair(q->queue[i]))
1978                        return 0;
1979        return 1;
1980}
1981
1982#if DIFF_DEBUG
1983void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
1984{
1985        fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
1986                x, one ? one : "",
1987                s->path,
1988                DIFF_FILE_VALID(s) ? "valid" : "invalid",
1989                s->mode,
1990                s->sha1_valid ? sha1_to_hex(s->sha1) : "");
1991        fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
1992                x, one ? one : "",
1993                s->size, s->xfrm_flags);
1994}
1995
1996void diff_debug_filepair(const struct diff_filepair *p, int i)
1997{
1998        diff_debug_filespec(p->one, i, "one");
1999        diff_debug_filespec(p->two, i, "two");
2000        fprintf(stderr, "score %d, status %c stays %d broken %d\n",
2001                p->score, p->status ? p->status : '?',
2002                p->source_stays, p->broken_pair);
2003}
2004
2005void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2006{
2007        int i;
2008        if (msg)
2009                fprintf(stderr, "%s\n", msg);
2010        fprintf(stderr, "q->nr = %d\n", q->nr);
2011        for (i = 0; i < q->nr; i++) {
2012                struct diff_filepair *p = q->queue[i];
2013                diff_debug_filepair(p, i);
2014        }
2015}
2016#endif
2017
2018static void diff_resolve_rename_copy(void)
2019{
2020        int i, j;
2021        struct diff_filepair *p, *pp;
2022        struct diff_queue_struct *q = &diff_queued_diff;
2023
2024        diff_debug_queue("resolve-rename-copy", q);
2025
2026        for (i = 0; i < q->nr; i++) {
2027                p = q->queue[i];
2028                p->status = 0; /* undecided */
2029                if (DIFF_PAIR_UNMERGED(p))
2030                        p->status = DIFF_STATUS_UNMERGED;
2031                else if (!DIFF_FILE_VALID(p->one))
2032                        p->status = DIFF_STATUS_ADDED;
2033                else if (!DIFF_FILE_VALID(p->two))
2034                        p->status = DIFF_STATUS_DELETED;
2035                else if (DIFF_PAIR_TYPE_CHANGED(p))
2036                        p->status = DIFF_STATUS_TYPE_CHANGED;
2037
2038                /* from this point on, we are dealing with a pair
2039                 * whose both sides are valid and of the same type, i.e.
2040                 * either in-place edit or rename/copy edit.
2041                 */
2042                else if (DIFF_PAIR_RENAME(p)) {
2043                        if (p->source_stays) {
2044                                p->status = DIFF_STATUS_COPIED;
2045                                continue;
2046                        }
2047                        /* See if there is some other filepair that
2048                         * copies from the same source as us.  If so
2049                         * we are a copy.  Otherwise we are either a
2050                         * copy if the path stays, or a rename if it
2051                         * does not, but we already handled "stays" case.
2052                         */
2053                        for (j = i + 1; j < q->nr; j++) {
2054                                pp = q->queue[j];
2055                                if (strcmp(pp->one->path, p->one->path))
2056                                        continue; /* not us */
2057                                if (!DIFF_PAIR_RENAME(pp))
2058                                        continue; /* not a rename/copy */
2059                                /* pp is a rename/copy from the same source */
2060                                p->status = DIFF_STATUS_COPIED;
2061                                break;
2062                        }
2063                        if (!p->status)
2064                                p->status = DIFF_STATUS_RENAMED;
2065                }
2066                else if (memcmp(p->one->sha1, p->two->sha1, 20) ||
2067                         p->one->mode != p->two->mode)
2068                        p->status = DIFF_STATUS_MODIFIED;
2069                else {
2070                        /* This is a "no-change" entry and should not
2071                         * happen anymore, but prepare for broken callers.
2072                         */
2073                        error("feeding unmodified %s to diffcore",
2074                              p->one->path);
2075                        p->status = DIFF_STATUS_UNKNOWN;
2076                }
2077        }
2078        diff_debug_queue("resolve-rename-copy done", q);
2079}
2080
2081static int check_pair_status(struct diff_filepair *p)
2082{
2083        switch (p->status) {
2084        case DIFF_STATUS_UNKNOWN:
2085                return 0;
2086        case 0:
2087                die("internal error in diff-resolve-rename-copy");
2088        default:
2089                return 1;
2090        }
2091}
2092
2093static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2094{
2095        int fmt = opt->output_format;
2096
2097        if (fmt & DIFF_FORMAT_CHECKDIFF)
2098                diff_flush_checkdiff(p, opt);
2099        else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2100                diff_flush_raw(p, opt);
2101        else if (fmt & DIFF_FORMAT_NAME)
2102                diff_flush_name(p, opt->line_termination);
2103}
2104
2105static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs)
2106{
2107        if (fs->mode)
2108                printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path);
2109        else
2110                printf(" %s %s\n", newdelete, fs->path);
2111}
2112
2113
2114static void show_mode_change(struct diff_filepair *p, int show_name)
2115{
2116        if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2117                if (show_name)
2118                        printf(" mode change %06o => %06o %s\n",
2119                               p->one->mode, p->two->mode, p->two->path);
2120                else
2121                        printf(" mode change %06o => %06o\n",
2122                               p->one->mode, p->two->mode);
2123        }
2124}
2125
2126static void show_rename_copy(const char *renamecopy, struct diff_filepair *p)
2127{
2128        const char *old, *new;
2129
2130        /* Find common prefix */
2131        old = p->one->path;
2132        new = p->two->path;
2133        while (1) {
2134                const char *slash_old, *slash_new;
2135                slash_old = strchr(old, '/');
2136                slash_new = strchr(new, '/');
2137                if (!slash_old ||
2138                    !slash_new ||
2139                    slash_old - old != slash_new - new ||
2140                    memcmp(old, new, slash_new - new))
2141                        break;
2142                old = slash_old + 1;
2143                new = slash_new + 1;
2144        }
2145        /* p->one->path thru old is the common prefix, and old and new
2146         * through the end of names are renames
2147         */
2148        if (old != p->one->path)
2149                printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy,
2150                       (int)(old - p->one->path), p->one->path,
2151                       old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE));
2152        else
2153                printf(" %s %s => %s (%d%%)\n", renamecopy,
2154                       p->one->path, p->two->path,
2155                       (int)(0.5 + p->score * 100.0/MAX_SCORE));
2156        show_mode_change(p, 0);
2157}
2158
2159static void diff_summary(struct diff_filepair *p)
2160{
2161        switch(p->status) {
2162        case DIFF_STATUS_DELETED:
2163                show_file_mode_name("delete", p->one);
2164                break;
2165        case DIFF_STATUS_ADDED:
2166                show_file_mode_name("create", p->two);
2167                break;
2168        case DIFF_STATUS_COPIED:
2169                show_rename_copy("copy", p);
2170                break;
2171        case DIFF_STATUS_RENAMED:
2172                show_rename_copy("rename", p);
2173                break;
2174        default:
2175                if (p->score) {
2176                        printf(" rewrite %s (%d%%)\n", p->two->path,
2177                                (int)(0.5 + p->score * 100.0/MAX_SCORE));
2178                        show_mode_change(p, 0);
2179                } else  show_mode_change(p, 1);
2180                break;
2181        }
2182}
2183
2184struct patch_id_t {
2185        struct xdiff_emit_state xm;
2186        SHA_CTX *ctx;
2187        int patchlen;
2188};
2189
2190static int remove_space(char *line, int len)
2191{
2192        int i;
2193        char *dst = line;
2194        unsigned char c;
2195
2196        for (i = 0; i < len; i++)
2197                if (!isspace((c = line[i])))
2198                        *dst++ = c;
2199
2200        return dst - line;
2201}
2202
2203static void patch_id_consume(void *priv, char *line, unsigned long len)
2204{
2205        struct patch_id_t *data = priv;
2206        int new_len;
2207
2208        /* Ignore line numbers when computing the SHA1 of the patch */
2209        if (!strncmp(line, "@@ -", 4))
2210                return;
2211
2212        new_len = remove_space(line, len);
2213
2214        SHA1_Update(data->ctx, line, new_len);
2215        data->patchlen += new_len;
2216}
2217
2218/* returns 0 upon success, and writes result into sha1 */
2219static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2220{
2221        struct diff_queue_struct *q = &diff_queued_diff;
2222        int i;
2223        SHA_CTX ctx;
2224        struct patch_id_t data;
2225        char buffer[PATH_MAX * 4 + 20];
2226
2227        SHA1_Init(&ctx);
2228        memset(&data, 0, sizeof(struct patch_id_t));
2229        data.ctx = &ctx;
2230        data.xm.consume = patch_id_consume;
2231
2232        for (i = 0; i < q->nr; i++) {
2233                xpparam_t xpp;
2234                xdemitconf_t xecfg;
2235                xdemitcb_t ecb;
2236                mmfile_t mf1, mf2;
2237                struct diff_filepair *p = q->queue[i];
2238                int len1, len2;
2239
2240                if (p->status == 0)
2241                        return error("internal diff status error");
2242                if (p->status == DIFF_STATUS_UNKNOWN)
2243                        continue;
2244                if (diff_unmodified_pair(p))
2245                        continue;
2246                if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2247                    (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2248                        continue;
2249                if (DIFF_PAIR_UNMERGED(p))
2250                        continue;
2251
2252                diff_fill_sha1_info(p->one);
2253                diff_fill_sha1_info(p->two);
2254                if (fill_mmfile(&mf1, p->one) < 0 ||
2255                                fill_mmfile(&mf2, p->two) < 0)
2256                        return error("unable to read files to diff");
2257
2258                /* Maybe hash p->two? into the patch id? */
2259                if (mmfile_is_binary(&mf2))
2260                        continue;
2261
2262                len1 = remove_space(p->one->path, strlen(p->one->path));
2263                len2 = remove_space(p->two->path, strlen(p->two->path));
2264                if (p->one->mode == 0)
2265                        len1 = snprintf(buffer, sizeof(buffer),
2266                                        "diff--gita/%.*sb/%.*s"
2267                                        "newfilemode%06o"
2268                                        "---/dev/null"
2269                                        "+++b/%.*s",
2270                                        len1, p->one->path,
2271                                        len2, p->two->path,
2272                                        p->two->mode,
2273                                        len2, p->two->path);
2274                else if (p->two->mode == 0)
2275                        len1 = snprintf(buffer, sizeof(buffer),
2276                                        "diff--gita/%.*sb/%.*s"
2277                                        "deletedfilemode%06o"
2278                                        "---a/%.*s"
2279                                        "+++/dev/null",
2280                                        len1, p->one->path,
2281                                        len2, p->two->path,
2282                                        p->one->mode,
2283                                        len1, p->one->path);
2284                else
2285                        len1 = snprintf(buffer, sizeof(buffer),
2286                                        "diff--gita/%.*sb/%.*s"
2287                                        "---a/%.*s"
2288                                        "+++b/%.*s",
2289                                        len1, p->one->path,
2290                                        len2, p->two->path,
2291                                        len1, p->one->path,
2292                                        len2, p->two->path);
2293                SHA1_Update(&ctx, buffer, len1);
2294
2295                xpp.flags = XDF_NEED_MINIMAL;
2296                xecfg.ctxlen = 3;
2297                xecfg.flags = XDL_EMIT_FUNCNAMES;
2298                ecb.outf = xdiff_outf;
2299                ecb.priv = &data;
2300                xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb);
2301        }
2302
2303        SHA1_Final(sha1, &ctx);
2304        return 0;
2305}
2306
2307int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
2308{
2309        struct diff_queue_struct *q = &diff_queued_diff;
2310        int i;
2311        int result = diff_get_patch_id(options, sha1);
2312
2313        for (i = 0; i < q->nr; i++)
2314                diff_free_filepair(q->queue[i]);
2315
2316        free(q->queue);
2317        q->queue = NULL;
2318        q->nr = q->alloc = 0;
2319
2320        return result;
2321}
2322
2323static int is_summary_empty(const struct diff_queue_struct *q)
2324{
2325        int i;
2326
2327        for (i = 0; i < q->nr; i++) {
2328                const struct diff_filepair *p = q->queue[i];
2329
2330                switch (p->status) {
2331                case DIFF_STATUS_DELETED:
2332                case DIFF_STATUS_ADDED:
2333                case DIFF_STATUS_COPIED:
2334                case DIFF_STATUS_RENAMED:
2335                        return 0;
2336                default:
2337                        if (p->score)
2338                                return 0;
2339                        if (p->one->mode && p->two->mode &&
2340                            p->one->mode != p->two->mode)
2341                                return 0;
2342                        break;
2343                }
2344        }
2345        return 1;
2346}
2347
2348void diff_flush(struct diff_options *options)
2349{
2350        struct diff_queue_struct *q = &diff_queued_diff;
2351        int i, output_format = options->output_format;
2352        int separator = 0;
2353
2354        /*
2355         * Order: raw, stat, summary, patch
2356         * or:    name/name-status/checkdiff (other bits clear)
2357         */
2358        if (!q->nr)
2359                goto free_queue;
2360
2361        if (output_format & (DIFF_FORMAT_RAW |
2362                             DIFF_FORMAT_NAME |
2363                             DIFF_FORMAT_NAME_STATUS |
2364                             DIFF_FORMAT_CHECKDIFF)) {
2365                for (i = 0; i < q->nr; i++) {
2366                        struct diff_filepair *p = q->queue[i];
2367                        if (check_pair_status(p))
2368                                flush_one_pair(p, options);
2369                }
2370                separator++;
2371        }
2372
2373        if (output_format & DIFF_FORMAT_DIFFSTAT) {
2374                struct diffstat_t diffstat;
2375
2376                memset(&diffstat, 0, sizeof(struct diffstat_t));
2377                diffstat.xm.consume = diffstat_consume;
2378                for (i = 0; i < q->nr; i++) {
2379                        struct diff_filepair *p = q->queue[i];
2380                        if (check_pair_status(p))
2381                                diff_flush_stat(p, options, &diffstat);
2382                }
2383                show_stats(&diffstat);
2384                separator++;
2385        }
2386
2387        if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
2388                for (i = 0; i < q->nr; i++)
2389                        diff_summary(q->queue[i]);
2390                separator++;
2391        }
2392
2393        if (output_format & DIFF_FORMAT_PATCH) {
2394                if (separator) {
2395                        if (options->stat_sep) {
2396                                /* attach patch instead of inline */
2397                                fputs(options->stat_sep, stdout);
2398                        } else {
2399                                putchar(options->line_termination);
2400                        }
2401                }
2402
2403                for (i = 0; i < q->nr; i++) {
2404                        struct diff_filepair *p = q->queue[i];
2405                        if (check_pair_status(p))
2406                                diff_flush_patch(p, options);
2407                }
2408        }
2409
2410        for (i = 0; i < q->nr; i++)
2411                diff_free_filepair(q->queue[i]);
2412free_queue:
2413        free(q->queue);
2414        q->queue = NULL;
2415        q->nr = q->alloc = 0;
2416}
2417
2418static void diffcore_apply_filter(const char *filter)
2419{
2420        int i;
2421        struct diff_queue_struct *q = &diff_queued_diff;
2422        struct diff_queue_struct outq;
2423        outq.queue = NULL;
2424        outq.nr = outq.alloc = 0;
2425
2426        if (!filter)
2427                return;
2428
2429        if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
2430                int found;
2431                for (i = found = 0; !found && i < q->nr; i++) {
2432                        struct diff_filepair *p = q->queue[i];
2433                        if (((p->status == DIFF_STATUS_MODIFIED) &&
2434                             ((p->score &&
2435                               strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2436                              (!p->score &&
2437                               strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2438                            ((p->status != DIFF_STATUS_MODIFIED) &&
2439                             strchr(filter, p->status)))
2440                                found++;
2441                }
2442                if (found)
2443                        return;
2444
2445                /* otherwise we will clear the whole queue
2446                 * by copying the empty outq at the end of this
2447                 * function, but first clear the current entries
2448                 * in the queue.
2449                 */
2450                for (i = 0; i < q->nr; i++)
2451                        diff_free_filepair(q->queue[i]);
2452        }
2453        else {
2454                /* Only the matching ones */
2455                for (i = 0; i < q->nr; i++) {
2456                        struct diff_filepair *p = q->queue[i];
2457
2458                        if (((p->status == DIFF_STATUS_MODIFIED) &&
2459                             ((p->score &&
2460                               strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
2461                              (!p->score &&
2462                               strchr(filter, DIFF_STATUS_MODIFIED)))) ||
2463                            ((p->status != DIFF_STATUS_MODIFIED) &&
2464                             strchr(filter, p->status)))
2465                                diff_q(&outq, p);
2466                        else
2467                                diff_free_filepair(p);
2468                }
2469        }
2470        free(q->queue);
2471        *q = outq;
2472}
2473
2474void diffcore_std(struct diff_options *options)
2475{
2476        if (options->break_opt != -1)
2477                diffcore_break(options->break_opt);
2478        if (options->detect_rename)
2479                diffcore_rename(options);
2480        if (options->break_opt != -1)
2481                diffcore_merge_broken();
2482        if (options->pickaxe)
2483                diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2484        if (options->orderfile)
2485                diffcore_order(options->orderfile);
2486        diff_resolve_rename_copy();
2487        diffcore_apply_filter(options->filter);
2488}
2489
2490
2491void diffcore_std_no_resolve(struct diff_options *options)
2492{
2493        if (options->pickaxe)
2494                diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
2495        if (options->orderfile)
2496                diffcore_order(options->orderfile);
2497        diffcore_apply_filter(options->filter);
2498}
2499
2500void diff_addremove(struct diff_options *options,
2501                    int addremove, unsigned mode,
2502                    const unsigned char *sha1,
2503                    const char *base, const char *path)
2504{
2505        char concatpath[PATH_MAX];
2506        struct diff_filespec *one, *two;
2507
2508        /* This may look odd, but it is a preparation for
2509         * feeding "there are unchanged files which should
2510         * not produce diffs, but when you are doing copy
2511         * detection you would need them, so here they are"
2512         * entries to the diff-core.  They will be prefixed
2513         * with something like '=' or '*' (I haven't decided
2514         * which but should not make any difference).
2515         * Feeding the same new and old to diff_change() 
2516         * also has the same effect.
2517         * Before the final output happens, they are pruned after
2518         * merged into rename/copy pairs as appropriate.
2519         */
2520        if (options->reverse_diff)
2521                addremove = (addremove == '+' ? '-' :
2522                             addremove == '-' ? '+' : addremove);
2523
2524        if (!path) path = "";
2525        sprintf(concatpath, "%s%s", base, path);
2526        one = alloc_filespec(concatpath);
2527        two = alloc_filespec(concatpath);
2528
2529        if (addremove != '+')
2530                fill_filespec(one, sha1, mode);
2531        if (addremove != '-')
2532                fill_filespec(two, sha1, mode);
2533
2534        diff_queue(&diff_queued_diff, one, two);
2535}
2536
2537void diff_change(struct diff_options *options,
2538                 unsigned old_mode, unsigned new_mode,
2539                 const unsigned char *old_sha1,
2540                 const unsigned char *new_sha1,
2541                 const char *base, const char *path) 
2542{
2543        char concatpath[PATH_MAX];
2544        struct diff_filespec *one, *two;
2545
2546        if (options->reverse_diff) {
2547                unsigned tmp;
2548                const unsigned char *tmp_c;
2549                tmp = old_mode; old_mode = new_mode; new_mode = tmp;
2550                tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
2551        }
2552        if (!path) path = "";
2553        sprintf(concatpath, "%s%s", base, path);
2554        one = alloc_filespec(concatpath);
2555        two = alloc_filespec(concatpath);
2556        fill_filespec(one, old_sha1, old_mode);
2557        fill_filespec(two, new_sha1, new_mode);
2558
2559        diff_queue(&diff_queued_diff, one, two);
2560}
2561
2562void diff_unmerge(struct diff_options *options,
2563                  const char *path)
2564{
2565        struct diff_filespec *one, *two;
2566        one = alloc_filespec(path);
2567        two = alloc_filespec(path);
2568        diff_queue(&diff_queued_diff, one, two);
2569}