6f01595ece4e9934bc9167bdbb61fad362bb7663
   1/*
   2 * Copyright (C) 2005 Junio C Hamano
   3 */
   4#include "cache.h"
   5#include "quote.h"
   6#include "diff.h"
   7#include "diffcore.h"
   8#include "delta.h"
   9#include "xdiff-interface.h"
  10#include "color.h"
  11#include "attr.h"
  12#include "run-command.h"
  13#include "utf8.h"
  14#include "userdiff.h"
  15
  16#ifdef NO_FAST_WORKING_DIRECTORY
  17#define FAST_WORKING_DIRECTORY 0
  18#else
  19#define FAST_WORKING_DIRECTORY 1
  20#endif
  21
  22static int diff_detect_rename_default;
  23static int diff_rename_limit_default = 200;
  24static int diff_suppress_blank_empty;
  25int diff_use_color_default = -1;
  26static const char *external_diff_cmd_cfg;
  27int diff_auto_refresh_index = 1;
  28static int diff_mnemonic_prefix;
  29
  30static char diff_colors[][COLOR_MAXLEN] = {
  31        "\033[m",       /* reset */
  32        "",             /* PLAIN (normal) */
  33        "\033[1m",      /* METAINFO (bold) */
  34        "\033[36m",     /* FRAGINFO (cyan) */
  35        "\033[31m",     /* OLD (red) */
  36        "\033[32m",     /* NEW (green) */
  37        "\033[33m",     /* COMMIT (yellow) */
  38        "\033[41m",     /* WHITESPACE (red background) */
  39};
  40
  41static void diff_filespec_load_driver(struct diff_filespec *one);
  42static char *run_textconv(const char *, struct diff_filespec *, size_t *);
  43
  44static int parse_diff_color_slot(const char *var, int ofs)
  45{
  46        if (!strcasecmp(var+ofs, "plain"))
  47                return DIFF_PLAIN;
  48        if (!strcasecmp(var+ofs, "meta"))
  49                return DIFF_METAINFO;
  50        if (!strcasecmp(var+ofs, "frag"))
  51                return DIFF_FRAGINFO;
  52        if (!strcasecmp(var+ofs, "old"))
  53                return DIFF_FILE_OLD;
  54        if (!strcasecmp(var+ofs, "new"))
  55                return DIFF_FILE_NEW;
  56        if (!strcasecmp(var+ofs, "commit"))
  57                return DIFF_COMMIT;
  58        if (!strcasecmp(var+ofs, "whitespace"))
  59                return DIFF_WHITESPACE;
  60        die("bad config variable '%s'", var);
  61}
  62
  63/*
  64 * These are to give UI layer defaults.
  65 * The core-level commands such as git-diff-files should
  66 * never be affected by the setting of diff.renames
  67 * the user happens to have in the configuration file.
  68 */
  69int git_diff_ui_config(const char *var, const char *value, void *cb)
  70{
  71        if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) {
  72                diff_use_color_default = git_config_colorbool(var, value, -1);
  73                return 0;
  74        }
  75        if (!strcmp(var, "diff.renames")) {
  76                if (!value)
  77                        diff_detect_rename_default = DIFF_DETECT_RENAME;
  78                else if (!strcasecmp(value, "copies") ||
  79                         !strcasecmp(value, "copy"))
  80                        diff_detect_rename_default = DIFF_DETECT_COPY;
  81                else if (git_config_bool(var,value))
  82                        diff_detect_rename_default = DIFF_DETECT_RENAME;
  83                return 0;
  84        }
  85        if (!strcmp(var, "diff.autorefreshindex")) {
  86                diff_auto_refresh_index = git_config_bool(var, value);
  87                return 0;
  88        }
  89        if (!strcmp(var, "diff.mnemonicprefix")) {
  90                diff_mnemonic_prefix = git_config_bool(var, value);
  91                return 0;
  92        }
  93        if (!strcmp(var, "diff.external"))
  94                return git_config_string(&external_diff_cmd_cfg, var, value);
  95
  96        switch (userdiff_config_porcelain(var, value)) {
  97                case 0: break;
  98                case -1: return -1;
  99                default: return 0;
 100        }
 101
 102        return git_diff_basic_config(var, value, cb);
 103}
 104
 105int git_diff_basic_config(const char *var, const char *value, void *cb)
 106{
 107        if (!strcmp(var, "diff.renamelimit")) {
 108                diff_rename_limit_default = git_config_int(var, value);
 109                return 0;
 110        }
 111
 112        if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) {
 113                int slot = parse_diff_color_slot(var, 11);
 114                if (!value)
 115                        return config_error_nonbool(var);
 116                color_parse(value, var, diff_colors[slot]);
 117                return 0;
 118        }
 119
 120        /* like GNU diff's --suppress-blank-empty option  */
 121        if (!strcmp(var, "diff.suppress-blank-empty")) {
 122                diff_suppress_blank_empty = git_config_bool(var, value);
 123                return 0;
 124        }
 125
 126        switch (userdiff_config_basic(var, value)) {
 127                case 0: break;
 128                case -1: return -1;
 129                default: return 0;
 130        }
 131
 132        return git_color_default_config(var, value, cb);
 133}
 134
 135static char *quote_two(const char *one, const char *two)
 136{
 137        int need_one = quote_c_style(one, NULL, NULL, 1);
 138        int need_two = quote_c_style(two, NULL, NULL, 1);
 139        struct strbuf res = STRBUF_INIT;
 140
 141        if (need_one + need_two) {
 142                strbuf_addch(&res, '"');
 143                quote_c_style(one, &res, NULL, 1);
 144                quote_c_style(two, &res, NULL, 1);
 145                strbuf_addch(&res, '"');
 146        } else {
 147                strbuf_addstr(&res, one);
 148                strbuf_addstr(&res, two);
 149        }
 150        return strbuf_detach(&res, NULL);
 151}
 152
 153static const char *external_diff(void)
 154{
 155        static const char *external_diff_cmd = NULL;
 156        static int done_preparing = 0;
 157
 158        if (done_preparing)
 159                return external_diff_cmd;
 160        external_diff_cmd = getenv("GIT_EXTERNAL_DIFF");
 161        if (!external_diff_cmd)
 162                external_diff_cmd = external_diff_cmd_cfg;
 163        done_preparing = 1;
 164        return external_diff_cmd;
 165}
 166
 167static struct diff_tempfile {
 168        const char *name; /* filename external diff should read from */
 169        char hex[41];
 170        char mode[10];
 171        char tmp_path[PATH_MAX];
 172} diff_temp[2];
 173
 174static int count_lines(const char *data, int size)
 175{
 176        int count, ch, completely_empty = 1, nl_just_seen = 0;
 177        count = 0;
 178        while (0 < size--) {
 179                ch = *data++;
 180                if (ch == '\n') {
 181                        count++;
 182                        nl_just_seen = 1;
 183                        completely_empty = 0;
 184                }
 185                else {
 186                        nl_just_seen = 0;
 187                        completely_empty = 0;
 188                }
 189        }
 190        if (completely_empty)
 191                return 0;
 192        if (!nl_just_seen)
 193                count++; /* no trailing newline */
 194        return count;
 195}
 196
 197static void print_line_count(FILE *file, int count)
 198{
 199        switch (count) {
 200        case 0:
 201                fprintf(file, "0,0");
 202                break;
 203        case 1:
 204                fprintf(file, "1");
 205                break;
 206        default:
 207                fprintf(file, "1,%d", count);
 208                break;
 209        }
 210}
 211
 212static void copy_file_with_prefix(FILE *file,
 213                                  int prefix, const char *data, int size,
 214                                  const char *set, const char *reset)
 215{
 216        int ch, nl_just_seen = 1;
 217        while (0 < size--) {
 218                ch = *data++;
 219                if (nl_just_seen) {
 220                        fputs(set, file);
 221                        putc(prefix, file);
 222                }
 223                if (ch == '\n') {
 224                        nl_just_seen = 1;
 225                        fputs(reset, file);
 226                } else
 227                        nl_just_seen = 0;
 228                putc(ch, file);
 229        }
 230        if (!nl_just_seen)
 231                fprintf(file, "%s\n\\ No newline at end of file\n", reset);
 232}
 233
 234static void emit_rewrite_diff(const char *name_a,
 235                              const char *name_b,
 236                              struct diff_filespec *one,
 237                              struct diff_filespec *two,
 238                              struct diff_options *o)
 239{
 240        int lc_a, lc_b;
 241        int color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
 242        const char *name_a_tab, *name_b_tab;
 243        const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO);
 244        const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO);
 245        const char *old = diff_get_color(color_diff, DIFF_FILE_OLD);
 246        const char *new = diff_get_color(color_diff, DIFF_FILE_NEW);
 247        const char *reset = diff_get_color(color_diff, DIFF_RESET);
 248        static struct strbuf a_name = STRBUF_INIT, b_name = STRBUF_INIT;
 249        const char *a_prefix, *b_prefix;
 250
 251        if (diff_mnemonic_prefix && DIFF_OPT_TST(o, REVERSE_DIFF)) {
 252                a_prefix = o->b_prefix;
 253                b_prefix = o->a_prefix;
 254        } else {
 255                a_prefix = o->a_prefix;
 256                b_prefix = o->b_prefix;
 257        }
 258
 259        name_a += (*name_a == '/');
 260        name_b += (*name_b == '/');
 261        name_a_tab = strchr(name_a, ' ') ? "\t" : "";
 262        name_b_tab = strchr(name_b, ' ') ? "\t" : "";
 263
 264        strbuf_reset(&a_name);
 265        strbuf_reset(&b_name);
 266        quote_two_c_style(&a_name, a_prefix, name_a, 0);
 267        quote_two_c_style(&b_name, b_prefix, name_b, 0);
 268
 269        diff_populate_filespec(one, 0);
 270        diff_populate_filespec(two, 0);
 271        lc_a = count_lines(one->data, one->size);
 272        lc_b = count_lines(two->data, two->size);
 273        fprintf(o->file,
 274                "%s--- %s%s%s\n%s+++ %s%s%s\n%s@@ -",
 275                metainfo, a_name.buf, name_a_tab, reset,
 276                metainfo, b_name.buf, name_b_tab, reset, fraginfo);
 277        print_line_count(o->file, lc_a);
 278        fprintf(o->file, " +");
 279        print_line_count(o->file, lc_b);
 280        fprintf(o->file, " @@%s\n", reset);
 281        if (lc_a)
 282                copy_file_with_prefix(o->file, '-', one->data, one->size, old, reset);
 283        if (lc_b)
 284                copy_file_with_prefix(o->file, '+', two->data, two->size, new, reset);
 285}
 286
 287static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one)
 288{
 289        if (!DIFF_FILE_VALID(one)) {
 290                mf->ptr = (char *)""; /* does not matter */
 291                mf->size = 0;
 292                return 0;
 293        }
 294        else if (diff_populate_filespec(one, 0))
 295                return -1;
 296
 297        mf->ptr = one->data;
 298        mf->size = one->size;
 299        return 0;
 300}
 301
 302struct diff_words_buffer {
 303        mmfile_t text;
 304        long alloc;
 305        long current; /* output pointer */
 306        int suppressed_newline;
 307};
 308
 309static void diff_words_append(char *line, unsigned long len,
 310                struct diff_words_buffer *buffer)
 311{
 312        if (buffer->text.size + len > buffer->alloc) {
 313                buffer->alloc = (buffer->text.size + len) * 3 / 2;
 314                buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc);
 315        }
 316        line++;
 317        len--;
 318        memcpy(buffer->text.ptr + buffer->text.size, line, len);
 319        buffer->text.size += len;
 320}
 321
 322struct diff_words_data {
 323        struct diff_words_buffer minus, plus;
 324        FILE *file;
 325};
 326
 327static void print_word(FILE *file, struct diff_words_buffer *buffer, int len, int color,
 328                int suppress_newline)
 329{
 330        const char *ptr;
 331        int eol = 0;
 332
 333        if (len == 0)
 334                return;
 335
 336        ptr  = buffer->text.ptr + buffer->current;
 337        buffer->current += len;
 338
 339        if (ptr[len - 1] == '\n') {
 340                eol = 1;
 341                len--;
 342        }
 343
 344        fputs(diff_get_color(1, color), file);
 345        fwrite(ptr, len, 1, file);
 346        fputs(diff_get_color(1, DIFF_RESET), file);
 347
 348        if (eol) {
 349                if (suppress_newline)
 350                        buffer->suppressed_newline = 1;
 351                else
 352                        putc('\n', file);
 353        }
 354}
 355
 356static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len)
 357{
 358        struct diff_words_data *diff_words = priv;
 359
 360        if (diff_words->minus.suppressed_newline) {
 361                if (line[0] != '+')
 362                        putc('\n', diff_words->file);
 363                diff_words->minus.suppressed_newline = 0;
 364        }
 365
 366        len--;
 367        switch (line[0]) {
 368                case '-':
 369                        print_word(diff_words->file,
 370                                   &diff_words->minus, len, DIFF_FILE_OLD, 1);
 371                        break;
 372                case '+':
 373                        print_word(diff_words->file,
 374                                   &diff_words->plus, len, DIFF_FILE_NEW, 0);
 375                        break;
 376                case ' ':
 377                        print_word(diff_words->file,
 378                                   &diff_words->plus, len, DIFF_PLAIN, 0);
 379                        diff_words->minus.current += len;
 380                        break;
 381        }
 382}
 383
 384/* this executes the word diff on the accumulated buffers */
 385static void diff_words_show(struct diff_words_data *diff_words)
 386{
 387        xpparam_t xpp;
 388        xdemitconf_t xecfg;
 389        xdemitcb_t ecb;
 390        mmfile_t minus, plus;
 391        int i;
 392
 393        memset(&xecfg, 0, sizeof(xecfg));
 394        minus.size = diff_words->minus.text.size;
 395        minus.ptr = xmalloc(minus.size);
 396        memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size);
 397        for (i = 0; i < minus.size; i++)
 398                if (isspace(minus.ptr[i]))
 399                        minus.ptr[i] = '\n';
 400        diff_words->minus.current = 0;
 401
 402        plus.size = diff_words->plus.text.size;
 403        plus.ptr = xmalloc(plus.size);
 404        memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size);
 405        for (i = 0; i < plus.size; i++)
 406                if (isspace(plus.ptr[i]))
 407                        plus.ptr[i] = '\n';
 408        diff_words->plus.current = 0;
 409
 410        xpp.flags = XDF_NEED_MINIMAL;
 411        xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc;
 412        xdi_diff_outf(&minus, &plus, fn_out_diff_words_aux, diff_words,
 413                      &xpp, &xecfg, &ecb);
 414        free(minus.ptr);
 415        free(plus.ptr);
 416        diff_words->minus.text.size = diff_words->plus.text.size = 0;
 417
 418        if (diff_words->minus.suppressed_newline) {
 419                putc('\n', diff_words->file);
 420                diff_words->minus.suppressed_newline = 0;
 421        }
 422}
 423
 424typedef unsigned long (*sane_truncate_fn)(char *line, unsigned long len);
 425
 426struct emit_callback {
 427        int nparents, color_diff;
 428        unsigned ws_rule;
 429        sane_truncate_fn truncate;
 430        const char **label_path;
 431        struct diff_words_data *diff_words;
 432        int *found_changesp;
 433        FILE *file;
 434};
 435
 436static void free_diff_words_data(struct emit_callback *ecbdata)
 437{
 438        if (ecbdata->diff_words) {
 439                /* flush buffers */
 440                if (ecbdata->diff_words->minus.text.size ||
 441                                ecbdata->diff_words->plus.text.size)
 442                        diff_words_show(ecbdata->diff_words);
 443
 444                free (ecbdata->diff_words->minus.text.ptr);
 445                free (ecbdata->diff_words->plus.text.ptr);
 446                free(ecbdata->diff_words);
 447                ecbdata->diff_words = NULL;
 448        }
 449}
 450
 451const char *diff_get_color(int diff_use_color, enum color_diff ix)
 452{
 453        if (diff_use_color)
 454                return diff_colors[ix];
 455        return "";
 456}
 457
 458static void emit_line(FILE *file, const char *set, const char *reset, const char *line, int len)
 459{
 460        int has_trailing_newline, has_trailing_carriage_return;
 461
 462        has_trailing_newline = (len > 0 && line[len-1] == '\n');
 463        if (has_trailing_newline)
 464                len--;
 465        has_trailing_carriage_return = (len > 0 && line[len-1] == '\r');
 466        if (has_trailing_carriage_return)
 467                len--;
 468
 469        fputs(set, file);
 470        fwrite(line, len, 1, file);
 471        fputs(reset, file);
 472        if (has_trailing_carriage_return)
 473                fputc('\r', file);
 474        if (has_trailing_newline)
 475                fputc('\n', file);
 476}
 477
 478static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len)
 479{
 480        const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE);
 481        const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW);
 482
 483        if (!*ws)
 484                emit_line(ecbdata->file, set, reset, line, len);
 485        else {
 486                /* Emit just the prefix, then the rest. */
 487                emit_line(ecbdata->file, set, reset, line, ecbdata->nparents);
 488                ws_check_emit(line + ecbdata->nparents,
 489                              len - ecbdata->nparents, ecbdata->ws_rule,
 490                              ecbdata->file, set, reset, ws);
 491        }
 492}
 493
 494static unsigned long sane_truncate_line(struct emit_callback *ecb, char *line, unsigned long len)
 495{
 496        const char *cp;
 497        unsigned long allot;
 498        size_t l = len;
 499
 500        if (ecb->truncate)
 501                return ecb->truncate(line, len);
 502        cp = line;
 503        allot = l;
 504        while (0 < l) {
 505                (void) utf8_width(&cp, &l);
 506                if (!cp)
 507                        break; /* truncated in the middle? */
 508        }
 509        return allot - l;
 510}
 511
 512static void fn_out_consume(void *priv, char *line, unsigned long len)
 513{
 514        int i;
 515        int color;
 516        struct emit_callback *ecbdata = priv;
 517        const char *meta = diff_get_color(ecbdata->color_diff, DIFF_METAINFO);
 518        const char *plain = diff_get_color(ecbdata->color_diff, DIFF_PLAIN);
 519        const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET);
 520
 521        *(ecbdata->found_changesp) = 1;
 522
 523        if (ecbdata->label_path[0]) {
 524                const char *name_a_tab, *name_b_tab;
 525
 526                name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : "";
 527                name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : "";
 528
 529                fprintf(ecbdata->file, "%s--- %s%s%s\n",
 530                        meta, ecbdata->label_path[0], reset, name_a_tab);
 531                fprintf(ecbdata->file, "%s+++ %s%s%s\n",
 532                        meta, ecbdata->label_path[1], reset, name_b_tab);
 533                ecbdata->label_path[0] = ecbdata->label_path[1] = NULL;
 534        }
 535
 536        if (diff_suppress_blank_empty
 537            && len == 2 && line[0] == ' ' && line[1] == '\n') {
 538                line[0] = '\n';
 539                len = 1;
 540        }
 541
 542        /* This is not really necessary for now because
 543         * this codepath only deals with two-way diffs.
 544         */
 545        for (i = 0; i < len && line[i] == '@'; i++)
 546                ;
 547        if (2 <= i && i < len && line[i] == ' ') {
 548                ecbdata->nparents = i - 1;
 549                len = sane_truncate_line(ecbdata, line, len);
 550                emit_line(ecbdata->file,
 551                          diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO),
 552                          reset, line, len);
 553                if (line[len-1] != '\n')
 554                        putc('\n', ecbdata->file);
 555                return;
 556        }
 557
 558        if (len < ecbdata->nparents) {
 559                emit_line(ecbdata->file, reset, reset, line, len);
 560                return;
 561        }
 562
 563        color = DIFF_PLAIN;
 564        if (ecbdata->diff_words && ecbdata->nparents != 1)
 565                /* fall back to normal diff */
 566                free_diff_words_data(ecbdata);
 567        if (ecbdata->diff_words) {
 568                if (line[0] == '-') {
 569                        diff_words_append(line, len,
 570                                          &ecbdata->diff_words->minus);
 571                        return;
 572                } else if (line[0] == '+') {
 573                        diff_words_append(line, len,
 574                                          &ecbdata->diff_words->plus);
 575                        return;
 576                }
 577                if (ecbdata->diff_words->minus.text.size ||
 578                    ecbdata->diff_words->plus.text.size)
 579                        diff_words_show(ecbdata->diff_words);
 580                line++;
 581                len--;
 582                emit_line(ecbdata->file, plain, reset, line, len);
 583                return;
 584        }
 585        for (i = 0; i < ecbdata->nparents && len; i++) {
 586                if (line[i] == '-')
 587                        color = DIFF_FILE_OLD;
 588                else if (line[i] == '+')
 589                        color = DIFF_FILE_NEW;
 590        }
 591
 592        if (color != DIFF_FILE_NEW) {
 593                emit_line(ecbdata->file,
 594                          diff_get_color(ecbdata->color_diff, color),
 595                          reset, line, len);
 596                return;
 597        }
 598        emit_add_line(reset, ecbdata, line, len);
 599}
 600
 601static char *pprint_rename(const char *a, const char *b)
 602{
 603        const char *old = a;
 604        const char *new = b;
 605        struct strbuf name = STRBUF_INIT;
 606        int pfx_length, sfx_length;
 607        int len_a = strlen(a);
 608        int len_b = strlen(b);
 609        int a_midlen, b_midlen;
 610        int qlen_a = quote_c_style(a, NULL, NULL, 0);
 611        int qlen_b = quote_c_style(b, NULL, NULL, 0);
 612
 613        if (qlen_a || qlen_b) {
 614                quote_c_style(a, &name, NULL, 0);
 615                strbuf_addstr(&name, " => ");
 616                quote_c_style(b, &name, NULL, 0);
 617                return strbuf_detach(&name, NULL);
 618        }
 619
 620        /* Find common prefix */
 621        pfx_length = 0;
 622        while (*old && *new && *old == *new) {
 623                if (*old == '/')
 624                        pfx_length = old - a + 1;
 625                old++;
 626                new++;
 627        }
 628
 629        /* Find common suffix */
 630        old = a + len_a;
 631        new = b + len_b;
 632        sfx_length = 0;
 633        while (a <= old && b <= new && *old == *new) {
 634                if (*old == '/')
 635                        sfx_length = len_a - (old - a);
 636                old--;
 637                new--;
 638        }
 639
 640        /*
 641         * pfx{mid-a => mid-b}sfx
 642         * {pfx-a => pfx-b}sfx
 643         * pfx{sfx-a => sfx-b}
 644         * name-a => name-b
 645         */
 646        a_midlen = len_a - pfx_length - sfx_length;
 647        b_midlen = len_b - pfx_length - sfx_length;
 648        if (a_midlen < 0)
 649                a_midlen = 0;
 650        if (b_midlen < 0)
 651                b_midlen = 0;
 652
 653        strbuf_grow(&name, pfx_length + a_midlen + b_midlen + sfx_length + 7);
 654        if (pfx_length + sfx_length) {
 655                strbuf_add(&name, a, pfx_length);
 656                strbuf_addch(&name, '{');
 657        }
 658        strbuf_add(&name, a + pfx_length, a_midlen);
 659        strbuf_addstr(&name, " => ");
 660        strbuf_add(&name, b + pfx_length, b_midlen);
 661        if (pfx_length + sfx_length) {
 662                strbuf_addch(&name, '}');
 663                strbuf_add(&name, a + len_a - sfx_length, sfx_length);
 664        }
 665        return strbuf_detach(&name, NULL);
 666}
 667
 668struct diffstat_t {
 669        int nr;
 670        int alloc;
 671        struct diffstat_file {
 672                char *from_name;
 673                char *name;
 674                char *print_name;
 675                unsigned is_unmerged:1;
 676                unsigned is_binary:1;
 677                unsigned is_renamed:1;
 678                unsigned int added, deleted;
 679        } **files;
 680};
 681
 682static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat,
 683                                          const char *name_a,
 684                                          const char *name_b)
 685{
 686        struct diffstat_file *x;
 687        x = xcalloc(sizeof (*x), 1);
 688        if (diffstat->nr == diffstat->alloc) {
 689                diffstat->alloc = alloc_nr(diffstat->alloc);
 690                diffstat->files = xrealloc(diffstat->files,
 691                                diffstat->alloc * sizeof(x));
 692        }
 693        diffstat->files[diffstat->nr++] = x;
 694        if (name_b) {
 695                x->from_name = xstrdup(name_a);
 696                x->name = xstrdup(name_b);
 697                x->is_renamed = 1;
 698        }
 699        else {
 700                x->from_name = NULL;
 701                x->name = xstrdup(name_a);
 702        }
 703        return x;
 704}
 705
 706static void diffstat_consume(void *priv, char *line, unsigned long len)
 707{
 708        struct diffstat_t *diffstat = priv;
 709        struct diffstat_file *x = diffstat->files[diffstat->nr - 1];
 710
 711        if (line[0] == '+')
 712                x->added++;
 713        else if (line[0] == '-')
 714                x->deleted++;
 715}
 716
 717const char mime_boundary_leader[] = "------------";
 718
 719static int scale_linear(int it, int width, int max_change)
 720{
 721        /*
 722         * make sure that at least one '-' is printed if there were deletions,
 723         * and likewise for '+'.
 724         */
 725        if (max_change < 2)
 726                return it;
 727        return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1);
 728}
 729
 730static void show_name(FILE *file,
 731                      const char *prefix, const char *name, int len,
 732                      const char *reset, const char *set)
 733{
 734        fprintf(file, " %s%s%-*s%s |", set, prefix, len, name, reset);
 735}
 736
 737static void show_graph(FILE *file, char ch, int cnt, const char *set, const char *reset)
 738{
 739        if (cnt <= 0)
 740                return;
 741        fprintf(file, "%s", set);
 742        while (cnt--)
 743                putc(ch, file);
 744        fprintf(file, "%s", reset);
 745}
 746
 747static void fill_print_name(struct diffstat_file *file)
 748{
 749        char *pname;
 750
 751        if (file->print_name)
 752                return;
 753
 754        if (!file->is_renamed) {
 755                struct strbuf buf = STRBUF_INIT;
 756                if (quote_c_style(file->name, &buf, NULL, 0)) {
 757                        pname = strbuf_detach(&buf, NULL);
 758                } else {
 759                        pname = file->name;
 760                        strbuf_release(&buf);
 761                }
 762        } else {
 763                pname = pprint_rename(file->from_name, file->name);
 764        }
 765        file->print_name = pname;
 766}
 767
 768static void show_stats(struct diffstat_t* data, struct diff_options *options)
 769{
 770        int i, len, add, del, total, adds = 0, dels = 0;
 771        int max_change = 0, max_len = 0;
 772        int total_files = data->nr;
 773        int width, name_width;
 774        const char *reset, *set, *add_c, *del_c;
 775
 776        if (data->nr == 0)
 777                return;
 778
 779        width = options->stat_width ? options->stat_width : 80;
 780        name_width = options->stat_name_width ? options->stat_name_width : 50;
 781
 782        /* Sanity: give at least 5 columns to the graph,
 783         * but leave at least 10 columns for the name.
 784         */
 785        if (width < 25)
 786                width = 25;
 787        if (name_width < 10)
 788                name_width = 10;
 789        else if (width < name_width + 15)
 790                name_width = width - 15;
 791
 792        /* Find the longest filename and max number of changes */
 793        reset = diff_get_color_opt(options, DIFF_RESET);
 794        set   = diff_get_color_opt(options, DIFF_PLAIN);
 795        add_c = diff_get_color_opt(options, DIFF_FILE_NEW);
 796        del_c = diff_get_color_opt(options, DIFF_FILE_OLD);
 797
 798        for (i = 0; i < data->nr; i++) {
 799                struct diffstat_file *file = data->files[i];
 800                int change = file->added + file->deleted;
 801                fill_print_name(file);
 802                len = strlen(file->print_name);
 803                if (max_len < len)
 804                        max_len = len;
 805
 806                if (file->is_binary || file->is_unmerged)
 807                        continue;
 808                if (max_change < change)
 809                        max_change = change;
 810        }
 811
 812        /* Compute the width of the graph part;
 813         * 10 is for one blank at the beginning of the line plus
 814         * " | count " between the name and the graph.
 815         *
 816         * From here on, name_width is the width of the name area,
 817         * and width is the width of the graph area.
 818         */
 819        name_width = (name_width < max_len) ? name_width : max_len;
 820        if (width < (name_width + 10) + max_change)
 821                width = width - (name_width + 10);
 822        else
 823                width = max_change;
 824
 825        for (i = 0; i < data->nr; i++) {
 826                const char *prefix = "";
 827                char *name = data->files[i]->print_name;
 828                int added = data->files[i]->added;
 829                int deleted = data->files[i]->deleted;
 830                int name_len;
 831
 832                /*
 833                 * "scale" the filename
 834                 */
 835                len = name_width;
 836                name_len = strlen(name);
 837                if (name_width < name_len) {
 838                        char *slash;
 839                        prefix = "...";
 840                        len -= 3;
 841                        name += name_len - len;
 842                        slash = strchr(name, '/');
 843                        if (slash)
 844                                name = slash;
 845                }
 846
 847                if (data->files[i]->is_binary) {
 848                        show_name(options->file, prefix, name, len, reset, set);
 849                        fprintf(options->file, "  Bin ");
 850                        fprintf(options->file, "%s%d%s", del_c, deleted, reset);
 851                        fprintf(options->file, " -> ");
 852                        fprintf(options->file, "%s%d%s", add_c, added, reset);
 853                        fprintf(options->file, " bytes");
 854                        fprintf(options->file, "\n");
 855                        continue;
 856                }
 857                else if (data->files[i]->is_unmerged) {
 858                        show_name(options->file, prefix, name, len, reset, set);
 859                        fprintf(options->file, "  Unmerged\n");
 860                        continue;
 861                }
 862                else if (!data->files[i]->is_renamed &&
 863                         (added + deleted == 0)) {
 864                        total_files--;
 865                        continue;
 866                }
 867
 868                /*
 869                 * scale the add/delete
 870                 */
 871                add = added;
 872                del = deleted;
 873                total = add + del;
 874                adds += add;
 875                dels += del;
 876
 877                if (width <= max_change) {
 878                        add = scale_linear(add, width, max_change);
 879                        del = scale_linear(del, width, max_change);
 880                        total = add + del;
 881                }
 882                show_name(options->file, prefix, name, len, reset, set);
 883                fprintf(options->file, "%5d%s", added + deleted,
 884                                added + deleted ? " " : "");
 885                show_graph(options->file, '+', add, add_c, reset);
 886                show_graph(options->file, '-', del, del_c, reset);
 887                fprintf(options->file, "\n");
 888        }
 889        fprintf(options->file,
 890               "%s %d files changed, %d insertions(+), %d deletions(-)%s\n",
 891               set, total_files, adds, dels, reset);
 892}
 893
 894static void show_shortstats(struct diffstat_t* data, struct diff_options *options)
 895{
 896        int i, adds = 0, dels = 0, total_files = data->nr;
 897
 898        if (data->nr == 0)
 899                return;
 900
 901        for (i = 0; i < data->nr; i++) {
 902                if (!data->files[i]->is_binary &&
 903                    !data->files[i]->is_unmerged) {
 904                        int added = data->files[i]->added;
 905                        int deleted= data->files[i]->deleted;
 906                        if (!data->files[i]->is_renamed &&
 907                            (added + deleted == 0)) {
 908                                total_files--;
 909                        } else {
 910                                adds += added;
 911                                dels += deleted;
 912                        }
 913                }
 914        }
 915        fprintf(options->file, " %d files changed, %d insertions(+), %d deletions(-)\n",
 916               total_files, adds, dels);
 917}
 918
 919static void show_numstat(struct diffstat_t* data, struct diff_options *options)
 920{
 921        int i;
 922
 923        if (data->nr == 0)
 924                return;
 925
 926        for (i = 0; i < data->nr; i++) {
 927                struct diffstat_file *file = data->files[i];
 928
 929                if (file->is_binary)
 930                        fprintf(options->file, "-\t-\t");
 931                else
 932                        fprintf(options->file,
 933                                "%d\t%d\t", file->added, file->deleted);
 934                if (options->line_termination) {
 935                        fill_print_name(file);
 936                        if (!file->is_renamed)
 937                                write_name_quoted(file->name, options->file,
 938                                                  options->line_termination);
 939                        else {
 940                                fputs(file->print_name, options->file);
 941                                putc(options->line_termination, options->file);
 942                        }
 943                } else {
 944                        if (file->is_renamed) {
 945                                putc('\0', options->file);
 946                                write_name_quoted(file->from_name, options->file, '\0');
 947                        }
 948                        write_name_quoted(file->name, options->file, '\0');
 949                }
 950        }
 951}
 952
 953struct dirstat_file {
 954        const char *name;
 955        unsigned long changed;
 956};
 957
 958struct dirstat_dir {
 959        struct dirstat_file *files;
 960        int alloc, nr, percent, cumulative;
 961};
 962
 963static long gather_dirstat(FILE *file, struct dirstat_dir *dir, unsigned long changed, const char *base, int baselen)
 964{
 965        unsigned long this_dir = 0;
 966        unsigned int sources = 0;
 967
 968        while (dir->nr) {
 969                struct dirstat_file *f = dir->files;
 970                int namelen = strlen(f->name);
 971                unsigned long this;
 972                char *slash;
 973
 974                if (namelen < baselen)
 975                        break;
 976                if (memcmp(f->name, base, baselen))
 977                        break;
 978                slash = strchr(f->name + baselen, '/');
 979                if (slash) {
 980                        int newbaselen = slash + 1 - f->name;
 981                        this = gather_dirstat(file, dir, changed, f->name, newbaselen);
 982                        sources++;
 983                } else {
 984                        this = f->changed;
 985                        dir->files++;
 986                        dir->nr--;
 987                        sources += 2;
 988                }
 989                this_dir += this;
 990        }
 991
 992        /*
 993         * We don't report dirstat's for
 994         *  - the top level
 995         *  - or cases where everything came from a single directory
 996         *    under this directory (sources == 1).
 997         */
 998        if (baselen && sources != 1) {
 999                int permille = this_dir * 1000 / changed;
1000                if (permille) {
1001                        int percent = permille / 10;
1002                        if (percent >= dir->percent) {
1003                                fprintf(file, "%4d.%01d%% %.*s\n", percent, permille % 10, baselen, base);
1004                                if (!dir->cumulative)
1005                                        return 0;
1006                        }
1007                }
1008        }
1009        return this_dir;
1010}
1011
1012static int dirstat_compare(const void *_a, const void *_b)
1013{
1014        const struct dirstat_file *a = _a;
1015        const struct dirstat_file *b = _b;
1016        return strcmp(a->name, b->name);
1017}
1018
1019static void show_dirstat(struct diff_options *options)
1020{
1021        int i;
1022        unsigned long changed;
1023        struct dirstat_dir dir;
1024        struct diff_queue_struct *q = &diff_queued_diff;
1025
1026        dir.files = NULL;
1027        dir.alloc = 0;
1028        dir.nr = 0;
1029        dir.percent = options->dirstat_percent;
1030        dir.cumulative = DIFF_OPT_TST(options, DIRSTAT_CUMULATIVE);
1031
1032        changed = 0;
1033        for (i = 0; i < q->nr; i++) {
1034                struct diff_filepair *p = q->queue[i];
1035                const char *name;
1036                unsigned long copied, added, damage;
1037
1038                name = p->one->path ? p->one->path : p->two->path;
1039
1040                if (DIFF_FILE_VALID(p->one) && DIFF_FILE_VALID(p->two)) {
1041                        diff_populate_filespec(p->one, 0);
1042                        diff_populate_filespec(p->two, 0);
1043                        diffcore_count_changes(p->one, p->two, NULL, NULL, 0,
1044                                               &copied, &added);
1045                        diff_free_filespec_data(p->one);
1046                        diff_free_filespec_data(p->two);
1047                } else if (DIFF_FILE_VALID(p->one)) {
1048                        diff_populate_filespec(p->one, 1);
1049                        copied = added = 0;
1050                        diff_free_filespec_data(p->one);
1051                } else if (DIFF_FILE_VALID(p->two)) {
1052                        diff_populate_filespec(p->two, 1);
1053                        copied = 0;
1054                        added = p->two->size;
1055                        diff_free_filespec_data(p->two);
1056                } else
1057                        continue;
1058
1059                /*
1060                 * Original minus copied is the removed material,
1061                 * added is the new material.  They are both damages
1062                 * made to the preimage. In --dirstat-by-file mode, count
1063                 * damaged files, not damaged lines. This is done by
1064                 * counting only a single damaged line per file.
1065                 */
1066                damage = (p->one->size - copied) + added;
1067                if (DIFF_OPT_TST(options, DIRSTAT_BY_FILE) && damage > 0)
1068                        damage = 1;
1069
1070                ALLOC_GROW(dir.files, dir.nr + 1, dir.alloc);
1071                dir.files[dir.nr].name = name;
1072                dir.files[dir.nr].changed = damage;
1073                changed += damage;
1074                dir.nr++;
1075        }
1076
1077        /* This can happen even with many files, if everything was renames */
1078        if (!changed)
1079                return;
1080
1081        /* Show all directories with more than x% of the changes */
1082        qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
1083        gather_dirstat(options->file, &dir, changed, "", 0);
1084}
1085
1086static void free_diffstat_info(struct diffstat_t *diffstat)
1087{
1088        int i;
1089        for (i = 0; i < diffstat->nr; i++) {
1090                struct diffstat_file *f = diffstat->files[i];
1091                if (f->name != f->print_name)
1092                        free(f->print_name);
1093                free(f->name);
1094                free(f->from_name);
1095                free(f);
1096        }
1097        free(diffstat->files);
1098}
1099
1100struct checkdiff_t {
1101        const char *filename;
1102        int lineno;
1103        struct diff_options *o;
1104        unsigned ws_rule;
1105        unsigned status;
1106        int trailing_blanks_start;
1107};
1108
1109static int is_conflict_marker(const char *line, unsigned long len)
1110{
1111        char firstchar;
1112        int cnt;
1113
1114        if (len < 8)
1115                return 0;
1116        firstchar = line[0];
1117        switch (firstchar) {
1118        case '=': case '>': case '<':
1119                break;
1120        default:
1121                return 0;
1122        }
1123        for (cnt = 1; cnt < 7; cnt++)
1124                if (line[cnt] != firstchar)
1125                        return 0;
1126        /* line[0] thru line[6] are same as firstchar */
1127        if (firstchar == '=') {
1128                /* divider between ours and theirs? */
1129                if (len != 8 || line[7] != '\n')
1130                        return 0;
1131        } else if (len < 8 || !isspace(line[7])) {
1132                /* not divider before ours nor after theirs */
1133                return 0;
1134        }
1135        return 1;
1136}
1137
1138static void checkdiff_consume(void *priv, char *line, unsigned long len)
1139{
1140        struct checkdiff_t *data = priv;
1141        int color_diff = DIFF_OPT_TST(data->o, COLOR_DIFF);
1142        const char *ws = diff_get_color(color_diff, DIFF_WHITESPACE);
1143        const char *reset = diff_get_color(color_diff, DIFF_RESET);
1144        const char *set = diff_get_color(color_diff, DIFF_FILE_NEW);
1145        char *err;
1146
1147        if (line[0] == '+') {
1148                unsigned bad;
1149                data->lineno++;
1150                if (!ws_blank_line(line + 1, len - 1, data->ws_rule))
1151                        data->trailing_blanks_start = 0;
1152                else if (!data->trailing_blanks_start)
1153                        data->trailing_blanks_start = data->lineno;
1154                if (is_conflict_marker(line + 1, len - 1)) {
1155                        data->status |= 1;
1156                        fprintf(data->o->file,
1157                                "%s:%d: leftover conflict marker\n",
1158                                data->filename, data->lineno);
1159                }
1160                bad = ws_check(line + 1, len - 1, data->ws_rule);
1161                if (!bad)
1162                        return;
1163                data->status |= bad;
1164                err = whitespace_error_string(bad);
1165                fprintf(data->o->file, "%s:%d: %s.\n",
1166                        data->filename, data->lineno, err);
1167                free(err);
1168                emit_line(data->o->file, set, reset, line, 1);
1169                ws_check_emit(line + 1, len - 1, data->ws_rule,
1170                              data->o->file, set, reset, ws);
1171        } else if (line[0] == ' ') {
1172                data->lineno++;
1173                data->trailing_blanks_start = 0;
1174        } else if (line[0] == '@') {
1175                char *plus = strchr(line, '+');
1176                if (plus)
1177                        data->lineno = strtol(plus, NULL, 10) - 1;
1178                else
1179                        die("invalid diff");
1180                data->trailing_blanks_start = 0;
1181        }
1182}
1183
1184static unsigned char *deflate_it(char *data,
1185                                 unsigned long size,
1186                                 unsigned long *result_size)
1187{
1188        int bound;
1189        unsigned char *deflated;
1190        z_stream stream;
1191
1192        memset(&stream, 0, sizeof(stream));
1193        deflateInit(&stream, zlib_compression_level);
1194        bound = deflateBound(&stream, size);
1195        deflated = xmalloc(bound);
1196        stream.next_out = deflated;
1197        stream.avail_out = bound;
1198
1199        stream.next_in = (unsigned char *)data;
1200        stream.avail_in = size;
1201        while (deflate(&stream, Z_FINISH) == Z_OK)
1202                ; /* nothing */
1203        deflateEnd(&stream);
1204        *result_size = stream.total_out;
1205        return deflated;
1206}
1207
1208static void emit_binary_diff_body(FILE *file, mmfile_t *one, mmfile_t *two)
1209{
1210        void *cp;
1211        void *delta;
1212        void *deflated;
1213        void *data;
1214        unsigned long orig_size;
1215        unsigned long delta_size;
1216        unsigned long deflate_size;
1217        unsigned long data_size;
1218
1219        /* We could do deflated delta, or we could do just deflated two,
1220         * whichever is smaller.
1221         */
1222        delta = NULL;
1223        deflated = deflate_it(two->ptr, two->size, &deflate_size);
1224        if (one->size && two->size) {
1225                delta = diff_delta(one->ptr, one->size,
1226                                   two->ptr, two->size,
1227                                   &delta_size, deflate_size);
1228                if (delta) {
1229                        void *to_free = delta;
1230                        orig_size = delta_size;
1231                        delta = deflate_it(delta, delta_size, &delta_size);
1232                        free(to_free);
1233                }
1234        }
1235
1236        if (delta && delta_size < deflate_size) {
1237                fprintf(file, "delta %lu\n", orig_size);
1238                free(deflated);
1239                data = delta;
1240                data_size = delta_size;
1241        }
1242        else {
1243                fprintf(file, "literal %lu\n", two->size);
1244                free(delta);
1245                data = deflated;
1246                data_size = deflate_size;
1247        }
1248
1249        /* emit data encoded in base85 */
1250        cp = data;
1251        while (data_size) {
1252                int bytes = (52 < data_size) ? 52 : data_size;
1253                char line[70];
1254                data_size -= bytes;
1255                if (bytes <= 26)
1256                        line[0] = bytes + 'A' - 1;
1257                else
1258                        line[0] = bytes - 26 + 'a' - 1;
1259                encode_85(line + 1, cp, bytes);
1260                cp = (char *) cp + bytes;
1261                fputs(line, file);
1262                fputc('\n', file);
1263        }
1264        fprintf(file, "\n");
1265        free(data);
1266}
1267
1268static void emit_binary_diff(FILE *file, mmfile_t *one, mmfile_t *two)
1269{
1270        fprintf(file, "GIT binary patch\n");
1271        emit_binary_diff_body(file, one, two);
1272        emit_binary_diff_body(file, two, one);
1273}
1274
1275static void diff_filespec_load_driver(struct diff_filespec *one)
1276{
1277        if (!one->driver)
1278                one->driver = userdiff_find_by_path(one->path);
1279        if (!one->driver)
1280                one->driver = userdiff_find_by_name("default");
1281}
1282
1283int diff_filespec_is_binary(struct diff_filespec *one)
1284{
1285        if (one->is_binary == -1) {
1286                diff_filespec_load_driver(one);
1287                if (one->driver->binary != -1)
1288                        one->is_binary = one->driver->binary;
1289                else {
1290                        if (!one->data && DIFF_FILE_VALID(one))
1291                                diff_populate_filespec(one, 0);
1292                        if (one->data)
1293                                one->is_binary = buffer_is_binary(one->data,
1294                                                one->size);
1295                        if (one->is_binary == -1)
1296                                one->is_binary = 0;
1297                }
1298        }
1299        return one->is_binary;
1300}
1301
1302static const struct userdiff_funcname *diff_funcname_pattern(struct diff_filespec *one)
1303{
1304        diff_filespec_load_driver(one);
1305        return one->driver->funcname.pattern ? &one->driver->funcname : NULL;
1306}
1307
1308void diff_set_mnemonic_prefix(struct diff_options *options, const char *a, const char *b)
1309{
1310        if (!options->a_prefix)
1311                options->a_prefix = a;
1312        if (!options->b_prefix)
1313                options->b_prefix = b;
1314}
1315
1316static const char *get_textconv(struct diff_filespec *one)
1317{
1318        if (!DIFF_FILE_VALID(one))
1319                return NULL;
1320        diff_filespec_load_driver(one);
1321        return one->driver->textconv;
1322}
1323
1324static void builtin_diff(const char *name_a,
1325                         const char *name_b,
1326                         struct diff_filespec *one,
1327                         struct diff_filespec *two,
1328                         const char *xfrm_msg,
1329                         struct diff_options *o,
1330                         int complete_rewrite)
1331{
1332        mmfile_t mf1, mf2;
1333        const char *lbl[2];
1334        char *a_one, *b_two;
1335        const char *set = diff_get_color_opt(o, DIFF_METAINFO);
1336        const char *reset = diff_get_color_opt(o, DIFF_RESET);
1337        const char *a_prefix, *b_prefix;
1338        const char *textconv_one, *textconv_two;
1339
1340        diff_set_mnemonic_prefix(o, "a/", "b/");
1341        if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
1342                a_prefix = o->b_prefix;
1343                b_prefix = o->a_prefix;
1344        } else {
1345                a_prefix = o->a_prefix;
1346                b_prefix = o->b_prefix;
1347        }
1348
1349        /* Never use a non-valid filename anywhere if at all possible */
1350        name_a = DIFF_FILE_VALID(one) ? name_a : name_b;
1351        name_b = DIFF_FILE_VALID(two) ? name_b : name_a;
1352
1353        a_one = quote_two(a_prefix, name_a + (*name_a == '/'));
1354        b_two = quote_two(b_prefix, name_b + (*name_b == '/'));
1355        lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null";
1356        lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null";
1357        fprintf(o->file, "%sdiff --git %s %s%s\n", set, a_one, b_two, reset);
1358        if (lbl[0][0] == '/') {
1359                /* /dev/null */
1360                fprintf(o->file, "%snew file mode %06o%s\n", set, two->mode, reset);
1361                if (xfrm_msg && xfrm_msg[0])
1362                        fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1363        }
1364        else if (lbl[1][0] == '/') {
1365                fprintf(o->file, "%sdeleted file mode %06o%s\n", set, one->mode, reset);
1366                if (xfrm_msg && xfrm_msg[0])
1367                        fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1368        }
1369        else {
1370                if (one->mode != two->mode) {
1371                        fprintf(o->file, "%sold mode %06o%s\n", set, one->mode, reset);
1372                        fprintf(o->file, "%snew mode %06o%s\n", set, two->mode, reset);
1373                }
1374                if (xfrm_msg && xfrm_msg[0])
1375                        fprintf(o->file, "%s%s%s\n", set, xfrm_msg, reset);
1376                /*
1377                 * we do not run diff between different kind
1378                 * of objects.
1379                 */
1380                if ((one->mode ^ two->mode) & S_IFMT)
1381                        goto free_ab_and_return;
1382                if (complete_rewrite) {
1383                        emit_rewrite_diff(name_a, name_b, one, two, o);
1384                        o->found_changes = 1;
1385                        goto free_ab_and_return;
1386                }
1387        }
1388
1389        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1390                die("unable to read files to diff");
1391
1392        textconv_one = get_textconv(one);
1393        textconv_two = get_textconv(two);
1394
1395        if (!DIFF_OPT_TST(o, TEXT) &&
1396            ( (diff_filespec_is_binary(one) && !textconv_one) ||
1397              (diff_filespec_is_binary(two) && !textconv_two) )) {
1398                /* Quite common confusing case */
1399                if (mf1.size == mf2.size &&
1400                    !memcmp(mf1.ptr, mf2.ptr, mf1.size))
1401                        goto free_ab_and_return;
1402                if (DIFF_OPT_TST(o, BINARY))
1403                        emit_binary_diff(o->file, &mf1, &mf2);
1404                else
1405                        fprintf(o->file, "Binary files %s and %s differ\n",
1406                                lbl[0], lbl[1]);
1407                o->found_changes = 1;
1408        }
1409        else {
1410                /* Crazy xdl interfaces.. */
1411                const char *diffopts = getenv("GIT_DIFF_OPTS");
1412                xpparam_t xpp;
1413                xdemitconf_t xecfg;
1414                xdemitcb_t ecb;
1415                struct emit_callback ecbdata;
1416                const struct userdiff_funcname *pe;
1417
1418                if (textconv_one) {
1419                        size_t size;
1420                        mf1.ptr = run_textconv(textconv_one, one, &size);
1421                        if (!mf1.ptr)
1422                                die("unable to read files to diff");
1423                        mf1.size = size;
1424                }
1425                if (textconv_two) {
1426                        size_t size;
1427                        mf2.ptr = run_textconv(textconv_two, two, &size);
1428                        if (!mf2.ptr)
1429                                die("unable to read files to diff");
1430                        mf2.size = size;
1431                }
1432
1433                pe = diff_funcname_pattern(one);
1434                if (!pe)
1435                        pe = diff_funcname_pattern(two);
1436
1437                memset(&xecfg, 0, sizeof(xecfg));
1438                memset(&ecbdata, 0, sizeof(ecbdata));
1439                ecbdata.label_path = lbl;
1440                ecbdata.color_diff = DIFF_OPT_TST(o, COLOR_DIFF);
1441                ecbdata.found_changesp = &o->found_changes;
1442                ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
1443                ecbdata.file = o->file;
1444                xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1445                xecfg.ctxlen = o->context;
1446                xecfg.flags = XDL_EMIT_FUNCNAMES;
1447                if (pe)
1448                        xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags);
1449                if (!diffopts)
1450                        ;
1451                else if (!prefixcmp(diffopts, "--unified="))
1452                        xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10);
1453                else if (!prefixcmp(diffopts, "-u"))
1454                        xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10);
1455                if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS)) {
1456                        ecbdata.diff_words =
1457                                xcalloc(1, sizeof(struct diff_words_data));
1458                        ecbdata.diff_words->file = o->file;
1459                }
1460                xdi_diff_outf(&mf1, &mf2, fn_out_consume, &ecbdata,
1461                              &xpp, &xecfg, &ecb);
1462                if (DIFF_OPT_TST(o, COLOR_DIFF_WORDS))
1463                        free_diff_words_data(&ecbdata);
1464                if (textconv_one)
1465                        free(mf1.ptr);
1466                if (textconv_two)
1467                        free(mf2.ptr);
1468        }
1469
1470 free_ab_and_return:
1471        diff_free_filespec_data(one);
1472        diff_free_filespec_data(two);
1473        free(a_one);
1474        free(b_two);
1475        return;
1476}
1477
1478static void builtin_diffstat(const char *name_a, const char *name_b,
1479                             struct diff_filespec *one,
1480                             struct diff_filespec *two,
1481                             struct diffstat_t *diffstat,
1482                             struct diff_options *o,
1483                             int complete_rewrite)
1484{
1485        mmfile_t mf1, mf2;
1486        struct diffstat_file *data;
1487
1488        data = diffstat_add(diffstat, name_a, name_b);
1489
1490        if (!one || !two) {
1491                data->is_unmerged = 1;
1492                return;
1493        }
1494        if (complete_rewrite) {
1495                diff_populate_filespec(one, 0);
1496                diff_populate_filespec(two, 0);
1497                data->deleted = count_lines(one->data, one->size);
1498                data->added = count_lines(two->data, two->size);
1499                goto free_and_return;
1500        }
1501        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1502                die("unable to read files to diff");
1503
1504        if (diff_filespec_is_binary(one) || diff_filespec_is_binary(two)) {
1505                data->is_binary = 1;
1506                data->added = mf2.size;
1507                data->deleted = mf1.size;
1508        } else {
1509                /* Crazy xdl interfaces.. */
1510                xpparam_t xpp;
1511                xdemitconf_t xecfg;
1512                xdemitcb_t ecb;
1513
1514                memset(&xecfg, 0, sizeof(xecfg));
1515                xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1516                xdi_diff_outf(&mf1, &mf2, diffstat_consume, diffstat,
1517                              &xpp, &xecfg, &ecb);
1518        }
1519
1520 free_and_return:
1521        diff_free_filespec_data(one);
1522        diff_free_filespec_data(two);
1523}
1524
1525static void builtin_checkdiff(const char *name_a, const char *name_b,
1526                              const char *attr_path,
1527                              struct diff_filespec *one,
1528                              struct diff_filespec *two,
1529                              struct diff_options *o)
1530{
1531        mmfile_t mf1, mf2;
1532        struct checkdiff_t data;
1533
1534        if (!two)
1535                return;
1536
1537        memset(&data, 0, sizeof(data));
1538        data.filename = name_b ? name_b : name_a;
1539        data.lineno = 0;
1540        data.o = o;
1541        data.ws_rule = whitespace_rule(attr_path);
1542
1543        if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1544                die("unable to read files to diff");
1545
1546        /*
1547         * All the other codepaths check both sides, but not checking
1548         * the "old" side here is deliberate.  We are checking the newly
1549         * introduced changes, and as long as the "new" side is text, we
1550         * can and should check what it introduces.
1551         */
1552        if (diff_filespec_is_binary(two))
1553                goto free_and_return;
1554        else {
1555                /* Crazy xdl interfaces.. */
1556                xpparam_t xpp;
1557                xdemitconf_t xecfg;
1558                xdemitcb_t ecb;
1559
1560                memset(&xecfg, 0, sizeof(xecfg));
1561                xecfg.ctxlen = 1; /* at least one context line */
1562                xpp.flags = XDF_NEED_MINIMAL;
1563                xdi_diff_outf(&mf1, &mf2, checkdiff_consume, &data,
1564                              &xpp, &xecfg, &ecb);
1565
1566                if ((data.ws_rule & WS_TRAILING_SPACE) &&
1567                    data.trailing_blanks_start) {
1568                        fprintf(o->file, "%s:%d: ends with blank lines.\n",
1569                                data.filename, data.trailing_blanks_start);
1570                        data.status = 1; /* report errors */
1571                }
1572        }
1573 free_and_return:
1574        diff_free_filespec_data(one);
1575        diff_free_filespec_data(two);
1576        if (data.status)
1577                DIFF_OPT_SET(o, CHECK_FAILED);
1578}
1579
1580struct diff_filespec *alloc_filespec(const char *path)
1581{
1582        int namelen = strlen(path);
1583        struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1);
1584
1585        memset(spec, 0, sizeof(*spec));
1586        spec->path = (char *)(spec + 1);
1587        memcpy(spec->path, path, namelen+1);
1588        spec->count = 1;
1589        spec->is_binary = -1;
1590        return spec;
1591}
1592
1593void free_filespec(struct diff_filespec *spec)
1594{
1595        if (!--spec->count) {
1596                diff_free_filespec_data(spec);
1597                free(spec);
1598        }
1599}
1600
1601void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1,
1602                   unsigned short mode)
1603{
1604        if (mode) {
1605                spec->mode = canon_mode(mode);
1606                hashcpy(spec->sha1, sha1);
1607                spec->sha1_valid = !is_null_sha1(sha1);
1608        }
1609}
1610
1611/*
1612 * Given a name and sha1 pair, if the index tells us the file in
1613 * the work tree has that object contents, return true, so that
1614 * prepare_temp_file() does not have to inflate and extract.
1615 */
1616static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file)
1617{
1618        struct cache_entry *ce;
1619        struct stat st;
1620        int pos, len;
1621
1622        /* We do not read the cache ourselves here, because the
1623         * benchmark with my previous version that always reads cache
1624         * shows that it makes things worse for diff-tree comparing
1625         * two linux-2.6 kernel trees in an already checked out work
1626         * tree.  This is because most diff-tree comparisons deal with
1627         * only a small number of files, while reading the cache is
1628         * expensive for a large project, and its cost outweighs the
1629         * savings we get by not inflating the object to a temporary
1630         * file.  Practically, this code only helps when we are used
1631         * by diff-cache --cached, which does read the cache before
1632         * calling us.
1633         */
1634        if (!active_cache)
1635                return 0;
1636
1637        /* We want to avoid the working directory if our caller
1638         * doesn't need the data in a normal file, this system
1639         * is rather slow with its stat/open/mmap/close syscalls,
1640         * and the object is contained in a pack file.  The pack
1641         * is probably already open and will be faster to obtain
1642         * the data through than the working directory.  Loose
1643         * objects however would tend to be slower as they need
1644         * to be individually opened and inflated.
1645         */
1646        if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL))
1647                return 0;
1648
1649        len = strlen(name);
1650        pos = cache_name_pos(name, len);
1651        if (pos < 0)
1652                return 0;
1653        ce = active_cache[pos];
1654
1655        /*
1656         * This is not the sha1 we are looking for, or
1657         * unreusable because it is not a regular file.
1658         */
1659        if (hashcmp(sha1, ce->sha1) || !S_ISREG(ce->ce_mode))
1660                return 0;
1661
1662        /*
1663         * If ce matches the file in the work tree, we can reuse it.
1664         */
1665        if (ce_uptodate(ce) ||
1666            (!lstat(name, &st) && !ce_match_stat(ce, &st, 0)))
1667                return 1;
1668
1669        return 0;
1670}
1671
1672static int populate_from_stdin(struct diff_filespec *s)
1673{
1674        struct strbuf buf = STRBUF_INIT;
1675        size_t size = 0;
1676
1677        if (strbuf_read(&buf, 0, 0) < 0)
1678                return error("error while reading from stdin %s",
1679                                     strerror(errno));
1680
1681        s->should_munmap = 0;
1682        s->data = strbuf_detach(&buf, &size);
1683        s->size = size;
1684        s->should_free = 1;
1685        return 0;
1686}
1687
1688static int diff_populate_gitlink(struct diff_filespec *s, int size_only)
1689{
1690        int len;
1691        char *data = xmalloc(100);
1692        len = snprintf(data, 100,
1693                "Subproject commit %s\n", sha1_to_hex(s->sha1));
1694        s->data = data;
1695        s->size = len;
1696        s->should_free = 1;
1697        if (size_only) {
1698                s->data = NULL;
1699                free(data);
1700        }
1701        return 0;
1702}
1703
1704/*
1705 * While doing rename detection and pickaxe operation, we may need to
1706 * grab the data for the blob (or file) for our own in-core comparison.
1707 * diff_filespec has data and size fields for this purpose.
1708 */
1709int diff_populate_filespec(struct diff_filespec *s, int size_only)
1710{
1711        int err = 0;
1712        if (!DIFF_FILE_VALID(s))
1713                die("internal error: asking to populate invalid file.");
1714        if (S_ISDIR(s->mode))
1715                return -1;
1716
1717        if (s->data)
1718                return 0;
1719
1720        if (size_only && 0 < s->size)
1721                return 0;
1722
1723        if (S_ISGITLINK(s->mode))
1724                return diff_populate_gitlink(s, size_only);
1725
1726        if (!s->sha1_valid ||
1727            reuse_worktree_file(s->path, s->sha1, 0)) {
1728                struct strbuf buf = STRBUF_INIT;
1729                struct stat st;
1730                int fd;
1731
1732                if (!strcmp(s->path, "-"))
1733                        return populate_from_stdin(s);
1734
1735                if (lstat(s->path, &st) < 0) {
1736                        if (errno == ENOENT) {
1737                        err_empty:
1738                                err = -1;
1739                        empty:
1740                                s->data = (char *)"";
1741                                s->size = 0;
1742                                return err;
1743                        }
1744                }
1745                s->size = xsize_t(st.st_size);
1746                if (!s->size)
1747                        goto empty;
1748                if (size_only)
1749                        return 0;
1750                if (S_ISLNK(st.st_mode)) {
1751                        int ret;
1752                        s->data = xmalloc(s->size);
1753                        s->should_free = 1;
1754                        ret = readlink(s->path, s->data, s->size);
1755                        if (ret < 0) {
1756                                free(s->data);
1757                                goto err_empty;
1758                        }
1759                        return 0;
1760                }
1761                fd = open(s->path, O_RDONLY);
1762                if (fd < 0)
1763                        goto err_empty;
1764                s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
1765                close(fd);
1766                s->should_munmap = 1;
1767
1768                /*
1769                 * Convert from working tree format to canonical git format
1770                 */
1771                if (convert_to_git(s->path, s->data, s->size, &buf, safe_crlf)) {
1772                        size_t size = 0;
1773                        munmap(s->data, s->size);
1774                        s->should_munmap = 0;
1775                        s->data = strbuf_detach(&buf, &size);
1776                        s->size = size;
1777                        s->should_free = 1;
1778                }
1779        }
1780        else {
1781                enum object_type type;
1782                if (size_only)
1783                        type = sha1_object_info(s->sha1, &s->size);
1784                else {
1785                        s->data = read_sha1_file(s->sha1, &type, &s->size);
1786                        s->should_free = 1;
1787                }
1788        }
1789        return 0;
1790}
1791
1792void diff_free_filespec_blob(struct diff_filespec *s)
1793{
1794        if (s->should_free)
1795                free(s->data);
1796        else if (s->should_munmap)
1797                munmap(s->data, s->size);
1798
1799        if (s->should_free || s->should_munmap) {
1800                s->should_free = s->should_munmap = 0;
1801                s->data = NULL;
1802        }
1803}
1804
1805void diff_free_filespec_data(struct diff_filespec *s)
1806{
1807        diff_free_filespec_blob(s);
1808        free(s->cnt_data);
1809        s->cnt_data = NULL;
1810}
1811
1812static void prep_temp_blob(struct diff_tempfile *temp,
1813                           void *blob,
1814                           unsigned long size,
1815                           const unsigned char *sha1,
1816                           int mode)
1817{
1818        int fd;
1819
1820        fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX");
1821        if (fd < 0)
1822                die("unable to create temp-file: %s", strerror(errno));
1823        if (write_in_full(fd, blob, size) != size)
1824                die("unable to write temp-file");
1825        close(fd);
1826        temp->name = temp->tmp_path;
1827        strcpy(temp->hex, sha1_to_hex(sha1));
1828        temp->hex[40] = 0;
1829        sprintf(temp->mode, "%06o", mode);
1830}
1831
1832static void prepare_temp_file(const char *name,
1833                              struct diff_tempfile *temp,
1834                              struct diff_filespec *one)
1835{
1836        if (!DIFF_FILE_VALID(one)) {
1837        not_a_valid_file:
1838                /* A '-' entry produces this for file-2, and
1839                 * a '+' entry produces this for file-1.
1840                 */
1841                temp->name = "/dev/null";
1842                strcpy(temp->hex, ".");
1843                strcpy(temp->mode, ".");
1844                return;
1845        }
1846
1847        if (!one->sha1_valid ||
1848            reuse_worktree_file(name, one->sha1, 1)) {
1849                struct stat st;
1850                if (lstat(name, &st) < 0) {
1851                        if (errno == ENOENT)
1852                                goto not_a_valid_file;
1853                        die("stat(%s): %s", name, strerror(errno));
1854                }
1855                if (S_ISLNK(st.st_mode)) {
1856                        int ret;
1857                        char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */
1858                        size_t sz = xsize_t(st.st_size);
1859                        if (sizeof(buf) <= st.st_size)
1860                                die("symlink too long: %s", name);
1861                        ret = readlink(name, buf, sz);
1862                        if (ret < 0)
1863                                die("readlink(%s)", name);
1864                        prep_temp_blob(temp, buf, sz,
1865                                       (one->sha1_valid ?
1866                                        one->sha1 : null_sha1),
1867                                       (one->sha1_valid ?
1868                                        one->mode : S_IFLNK));
1869                }
1870                else {
1871                        /* we can borrow from the file in the work tree */
1872                        temp->name = name;
1873                        if (!one->sha1_valid)
1874                                strcpy(temp->hex, sha1_to_hex(null_sha1));
1875                        else
1876                                strcpy(temp->hex, sha1_to_hex(one->sha1));
1877                        /* Even though we may sometimes borrow the
1878                         * contents from the work tree, we always want
1879                         * one->mode.  mode is trustworthy even when
1880                         * !(one->sha1_valid), as long as
1881                         * DIFF_FILE_VALID(one).
1882                         */
1883                        sprintf(temp->mode, "%06o", one->mode);
1884                }
1885                return;
1886        }
1887        else {
1888                if (diff_populate_filespec(one, 0))
1889                        die("cannot read data blob for %s", one->path);
1890                prep_temp_blob(temp, one->data, one->size,
1891                               one->sha1, one->mode);
1892        }
1893}
1894
1895static void remove_tempfile(void)
1896{
1897        int i;
1898
1899        for (i = 0; i < 2; i++)
1900                if (diff_temp[i].name == diff_temp[i].tmp_path) {
1901                        unlink(diff_temp[i].name);
1902                        diff_temp[i].name = NULL;
1903                }
1904}
1905
1906static void remove_tempfile_on_signal(int signo)
1907{
1908        remove_tempfile();
1909        signal(SIGINT, SIG_DFL);
1910        raise(signo);
1911}
1912
1913/* An external diff command takes:
1914 *
1915 * diff-cmd name infile1 infile1-sha1 infile1-mode \
1916 *               infile2 infile2-sha1 infile2-mode [ rename-to ]
1917 *
1918 */
1919static void run_external_diff(const char *pgm,
1920                              const char *name,
1921                              const char *other,
1922                              struct diff_filespec *one,
1923                              struct diff_filespec *two,
1924                              const char *xfrm_msg,
1925                              int complete_rewrite)
1926{
1927        const char *spawn_arg[10];
1928        struct diff_tempfile *temp = diff_temp;
1929        int retval;
1930        static int atexit_asked = 0;
1931        const char *othername;
1932        const char **arg = &spawn_arg[0];
1933
1934        othername = (other? other : name);
1935        if (one && two) {
1936                prepare_temp_file(name, &temp[0], one);
1937                prepare_temp_file(othername, &temp[1], two);
1938                if (! atexit_asked &&
1939                    (temp[0].name == temp[0].tmp_path ||
1940                     temp[1].name == temp[1].tmp_path)) {
1941                        atexit_asked = 1;
1942                        atexit(remove_tempfile);
1943                }
1944                signal(SIGINT, remove_tempfile_on_signal);
1945        }
1946
1947        if (one && two) {
1948                *arg++ = pgm;
1949                *arg++ = name;
1950                *arg++ = temp[0].name;
1951                *arg++ = temp[0].hex;
1952                *arg++ = temp[0].mode;
1953                *arg++ = temp[1].name;
1954                *arg++ = temp[1].hex;
1955                *arg++ = temp[1].mode;
1956                if (other) {
1957                        *arg++ = other;
1958                        *arg++ = xfrm_msg;
1959                }
1960        } else {
1961                *arg++ = pgm;
1962                *arg++ = name;
1963        }
1964        *arg = NULL;
1965        fflush(NULL);
1966        retval = run_command_v_opt(spawn_arg, 0);
1967        remove_tempfile();
1968        if (retval) {
1969                fprintf(stderr, "external diff died, stopping at %s.\n", name);
1970                exit(1);
1971        }
1972}
1973
1974static void run_diff_cmd(const char *pgm,
1975                         const char *name,
1976                         const char *other,
1977                         const char *attr_path,
1978                         struct diff_filespec *one,
1979                         struct diff_filespec *two,
1980                         const char *xfrm_msg,
1981                         struct diff_options *o,
1982                         int complete_rewrite)
1983{
1984        if (!DIFF_OPT_TST(o, ALLOW_EXTERNAL))
1985                pgm = NULL;
1986        else {
1987                struct userdiff_driver *drv = userdiff_find_by_path(attr_path);
1988                if (drv && drv->external)
1989                        pgm = drv->external;
1990        }
1991
1992        if (pgm) {
1993                run_external_diff(pgm, name, other, one, two, xfrm_msg,
1994                                  complete_rewrite);
1995                return;
1996        }
1997        if (one && two)
1998                builtin_diff(name, other ? other : name,
1999                             one, two, xfrm_msg, o, complete_rewrite);
2000        else
2001                fprintf(o->file, "* Unmerged path %s\n", name);
2002}
2003
2004static void diff_fill_sha1_info(struct diff_filespec *one)
2005{
2006        if (DIFF_FILE_VALID(one)) {
2007                if (!one->sha1_valid) {
2008                        struct stat st;
2009                        if (!strcmp(one->path, "-")) {
2010                                hashcpy(one->sha1, null_sha1);
2011                                return;
2012                        }
2013                        if (lstat(one->path, &st) < 0)
2014                                die("stat %s", one->path);
2015                        if (index_path(one->sha1, one->path, &st, 0))
2016                                die("cannot hash %s\n", one->path);
2017                }
2018        }
2019        else
2020                hashclr(one->sha1);
2021}
2022
2023static int similarity_index(struct diff_filepair *p)
2024{
2025        return p->score * 100 / MAX_SCORE;
2026}
2027
2028static void strip_prefix(int prefix_length, const char **namep, const char **otherp)
2029{
2030        /* Strip the prefix but do not molest /dev/null and absolute paths */
2031        if (*namep && **namep != '/')
2032                *namep += prefix_length;
2033        if (*otherp && **otherp != '/')
2034                *otherp += prefix_length;
2035}
2036
2037static void run_diff(struct diff_filepair *p, struct diff_options *o)
2038{
2039        const char *pgm = external_diff();
2040        struct strbuf msg;
2041        char *xfrm_msg;
2042        struct diff_filespec *one = p->one;
2043        struct diff_filespec *two = p->two;
2044        const char *name;
2045        const char *other;
2046        const char *attr_path;
2047        int complete_rewrite = 0;
2048
2049        name  = p->one->path;
2050        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2051        attr_path = name;
2052        if (o->prefix_length)
2053                strip_prefix(o->prefix_length, &name, &other);
2054
2055        if (DIFF_PAIR_UNMERGED(p)) {
2056                run_diff_cmd(pgm, name, NULL, attr_path,
2057                             NULL, NULL, NULL, o, 0);
2058                return;
2059        }
2060
2061        diff_fill_sha1_info(one);
2062        diff_fill_sha1_info(two);
2063
2064        strbuf_init(&msg, PATH_MAX * 2 + 300);
2065        switch (p->status) {
2066        case DIFF_STATUS_COPIED:
2067                strbuf_addf(&msg, "similarity index %d%%", similarity_index(p));
2068                strbuf_addstr(&msg, "\ncopy from ");
2069                quote_c_style(name, &msg, NULL, 0);
2070                strbuf_addstr(&msg, "\ncopy to ");
2071                quote_c_style(other, &msg, NULL, 0);
2072                strbuf_addch(&msg, '\n');
2073                break;
2074        case DIFF_STATUS_RENAMED:
2075                strbuf_addf(&msg, "similarity index %d%%", similarity_index(p));
2076                strbuf_addstr(&msg, "\nrename from ");
2077                quote_c_style(name, &msg, NULL, 0);
2078                strbuf_addstr(&msg, "\nrename to ");
2079                quote_c_style(other, &msg, NULL, 0);
2080                strbuf_addch(&msg, '\n');
2081                break;
2082        case DIFF_STATUS_MODIFIED:
2083                if (p->score) {
2084                        strbuf_addf(&msg, "dissimilarity index %d%%\n",
2085                                        similarity_index(p));
2086                        complete_rewrite = 1;
2087                        break;
2088                }
2089                /* fallthru */
2090        default:
2091                /* nothing */
2092                ;
2093        }
2094
2095        if (hashcmp(one->sha1, two->sha1)) {
2096                int abbrev = DIFF_OPT_TST(o, FULL_INDEX) ? 40 : DEFAULT_ABBREV;
2097
2098                if (DIFF_OPT_TST(o, BINARY)) {
2099                        mmfile_t mf;
2100                        if ((!fill_mmfile(&mf, one) && diff_filespec_is_binary(one)) ||
2101                            (!fill_mmfile(&mf, two) && diff_filespec_is_binary(two)))
2102                                abbrev = 40;
2103                }
2104                strbuf_addf(&msg, "index %.*s..%.*s",
2105                                abbrev, sha1_to_hex(one->sha1),
2106                                abbrev, sha1_to_hex(two->sha1));
2107                if (one->mode == two->mode)
2108                        strbuf_addf(&msg, " %06o", one->mode);
2109                strbuf_addch(&msg, '\n');
2110        }
2111
2112        if (msg.len)
2113                strbuf_setlen(&msg, msg.len - 1);
2114        xfrm_msg = msg.len ? msg.buf : NULL;
2115
2116        if (!pgm &&
2117            DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) &&
2118            (S_IFMT & one->mode) != (S_IFMT & two->mode)) {
2119                /* a filepair that changes between file and symlink
2120                 * needs to be split into deletion and creation.
2121                 */
2122                struct diff_filespec *null = alloc_filespec(two->path);
2123                run_diff_cmd(NULL, name, other, attr_path,
2124                             one, null, xfrm_msg, o, 0);
2125                free(null);
2126                null = alloc_filespec(one->path);
2127                run_diff_cmd(NULL, name, other, attr_path,
2128                             null, two, xfrm_msg, o, 0);
2129                free(null);
2130        }
2131        else
2132                run_diff_cmd(pgm, name, other, attr_path,
2133                             one, two, xfrm_msg, o, complete_rewrite);
2134
2135        strbuf_release(&msg);
2136}
2137
2138static void run_diffstat(struct diff_filepair *p, struct diff_options *o,
2139                         struct diffstat_t *diffstat)
2140{
2141        const char *name;
2142        const char *other;
2143        int complete_rewrite = 0;
2144
2145        if (DIFF_PAIR_UNMERGED(p)) {
2146                /* unmerged */
2147                builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0);
2148                return;
2149        }
2150
2151        name = p->one->path;
2152        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2153
2154        if (o->prefix_length)
2155                strip_prefix(o->prefix_length, &name, &other);
2156
2157        diff_fill_sha1_info(p->one);
2158        diff_fill_sha1_info(p->two);
2159
2160        if (p->status == DIFF_STATUS_MODIFIED && p->score)
2161                complete_rewrite = 1;
2162        builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite);
2163}
2164
2165static void run_checkdiff(struct diff_filepair *p, struct diff_options *o)
2166{
2167        const char *name;
2168        const char *other;
2169        const char *attr_path;
2170
2171        if (DIFF_PAIR_UNMERGED(p)) {
2172                /* unmerged */
2173                return;
2174        }
2175
2176        name = p->one->path;
2177        other = (strcmp(name, p->two->path) ? p->two->path : NULL);
2178        attr_path = other ? other : name;
2179
2180        if (o->prefix_length)
2181                strip_prefix(o->prefix_length, &name, &other);
2182
2183        diff_fill_sha1_info(p->one);
2184        diff_fill_sha1_info(p->two);
2185
2186        builtin_checkdiff(name, other, attr_path, p->one, p->two, o);
2187}
2188
2189void diff_setup(struct diff_options *options)
2190{
2191        memset(options, 0, sizeof(*options));
2192
2193        options->file = stdout;
2194
2195        options->line_termination = '\n';
2196        options->break_opt = -1;
2197        options->rename_limit = -1;
2198        options->dirstat_percent = 3;
2199        DIFF_OPT_CLR(options, DIRSTAT_CUMULATIVE);
2200        options->context = 3;
2201
2202        options->change = diff_change;
2203        options->add_remove = diff_addremove;
2204        if (diff_use_color_default > 0)
2205                DIFF_OPT_SET(options, COLOR_DIFF);
2206        else
2207                DIFF_OPT_CLR(options, COLOR_DIFF);
2208        options->detect_rename = diff_detect_rename_default;
2209
2210        if (!diff_mnemonic_prefix) {
2211                options->a_prefix = "a/";
2212                options->b_prefix = "b/";
2213        }
2214}
2215
2216int diff_setup_done(struct diff_options *options)
2217{
2218        int count = 0;
2219
2220        if (options->output_format & DIFF_FORMAT_NAME)
2221                count++;
2222        if (options->output_format & DIFF_FORMAT_NAME_STATUS)
2223                count++;
2224        if (options->output_format & DIFF_FORMAT_CHECKDIFF)
2225                count++;
2226        if (options->output_format & DIFF_FORMAT_NO_OUTPUT)
2227                count++;
2228        if (count > 1)
2229                die("--name-only, --name-status, --check and -s are mutually exclusive");
2230
2231        if (DIFF_OPT_TST(options, FIND_COPIES_HARDER))
2232                options->detect_rename = DIFF_DETECT_COPY;
2233
2234        if (!DIFF_OPT_TST(options, RELATIVE_NAME))
2235                options->prefix = NULL;
2236        if (options->prefix)
2237                options->prefix_length = strlen(options->prefix);
2238        else
2239                options->prefix_length = 0;
2240
2241        if (options->output_format & (DIFF_FORMAT_NAME |
2242                                      DIFF_FORMAT_NAME_STATUS |
2243                                      DIFF_FORMAT_CHECKDIFF |
2244                                      DIFF_FORMAT_NO_OUTPUT))
2245                options->output_format &= ~(DIFF_FORMAT_RAW |
2246                                            DIFF_FORMAT_NUMSTAT |
2247                                            DIFF_FORMAT_DIFFSTAT |
2248                                            DIFF_FORMAT_SHORTSTAT |
2249                                            DIFF_FORMAT_DIRSTAT |
2250                                            DIFF_FORMAT_SUMMARY |
2251                                            DIFF_FORMAT_PATCH);
2252
2253        /*
2254         * These cases always need recursive; we do not drop caller-supplied
2255         * recursive bits for other formats here.
2256         */
2257        if (options->output_format & (DIFF_FORMAT_PATCH |
2258                                      DIFF_FORMAT_NUMSTAT |
2259                                      DIFF_FORMAT_DIFFSTAT |
2260                                      DIFF_FORMAT_SHORTSTAT |
2261                                      DIFF_FORMAT_DIRSTAT |
2262                                      DIFF_FORMAT_SUMMARY |
2263                                      DIFF_FORMAT_CHECKDIFF))
2264                DIFF_OPT_SET(options, RECURSIVE);
2265        /*
2266         * Also pickaxe would not work very well if you do not say recursive
2267         */
2268        if (options->pickaxe)
2269                DIFF_OPT_SET(options, RECURSIVE);
2270
2271        if (options->detect_rename && options->rename_limit < 0)
2272                options->rename_limit = diff_rename_limit_default;
2273        if (options->setup & DIFF_SETUP_USE_CACHE) {
2274                if (!active_cache)
2275                        /* read-cache does not die even when it fails
2276                         * so it is safe for us to do this here.  Also
2277                         * it does not smudge active_cache or active_nr
2278                         * when it fails, so we do not have to worry about
2279                         * cleaning it up ourselves either.
2280                         */
2281                        read_cache();
2282        }
2283        if (options->abbrev <= 0 || 40 < options->abbrev)
2284                options->abbrev = 40; /* full */
2285
2286        /*
2287         * It does not make sense to show the first hit we happened
2288         * to have found.  It does not make sense not to return with
2289         * exit code in such a case either.
2290         */
2291        if (DIFF_OPT_TST(options, QUIET)) {
2292                options->output_format = DIFF_FORMAT_NO_OUTPUT;
2293                DIFF_OPT_SET(options, EXIT_WITH_STATUS);
2294        }
2295
2296        return 0;
2297}
2298
2299static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val)
2300{
2301        char c, *eq;
2302        int len;
2303
2304        if (*arg != '-')
2305                return 0;
2306        c = *++arg;
2307        if (!c)
2308                return 0;
2309        if (c == arg_short) {
2310                c = *++arg;
2311                if (!c)
2312                        return 1;
2313                if (val && isdigit(c)) {
2314                        char *end;
2315                        int n = strtoul(arg, &end, 10);
2316                        if (*end)
2317                                return 0;
2318                        *val = n;
2319                        return 1;
2320                }
2321                return 0;
2322        }
2323        if (c != '-')
2324                return 0;
2325        arg++;
2326        eq = strchr(arg, '=');
2327        if (eq)
2328                len = eq - arg;
2329        else
2330                len = strlen(arg);
2331        if (!len || strncmp(arg, arg_long, len))
2332                return 0;
2333        if (eq) {
2334                int n;
2335                char *end;
2336                if (!isdigit(*++eq))
2337                        return 0;
2338                n = strtoul(eq, &end, 10);
2339                if (*end)
2340                        return 0;
2341                *val = n;
2342        }
2343        return 1;
2344}
2345
2346static int diff_scoreopt_parse(const char *opt);
2347
2348int diff_opt_parse(struct diff_options *options, const char **av, int ac)
2349{
2350        const char *arg = av[0];
2351
2352        /* Output format options */
2353        if (!strcmp(arg, "-p") || !strcmp(arg, "-u"))
2354                options->output_format |= DIFF_FORMAT_PATCH;
2355        else if (opt_arg(arg, 'U', "unified", &options->context))
2356                options->output_format |= DIFF_FORMAT_PATCH;
2357        else if (!strcmp(arg, "--raw"))
2358                options->output_format |= DIFF_FORMAT_RAW;
2359        else if (!strcmp(arg, "--patch-with-raw"))
2360                options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW;
2361        else if (!strcmp(arg, "--numstat"))
2362                options->output_format |= DIFF_FORMAT_NUMSTAT;
2363        else if (!strcmp(arg, "--shortstat"))
2364                options->output_format |= DIFF_FORMAT_SHORTSTAT;
2365        else if (opt_arg(arg, 'X', "dirstat", &options->dirstat_percent))
2366                options->output_format |= DIFF_FORMAT_DIRSTAT;
2367        else if (!strcmp(arg, "--cumulative")) {
2368                options->output_format |= DIFF_FORMAT_DIRSTAT;
2369                DIFF_OPT_SET(options, DIRSTAT_CUMULATIVE);
2370        } else if (opt_arg(arg, 0, "dirstat-by-file",
2371                           &options->dirstat_percent)) {
2372                options->output_format |= DIFF_FORMAT_DIRSTAT;
2373                DIFF_OPT_SET(options, DIRSTAT_BY_FILE);
2374        }
2375        else if (!strcmp(arg, "--check"))
2376                options->output_format |= DIFF_FORMAT_CHECKDIFF;
2377        else if (!strcmp(arg, "--summary"))
2378                options->output_format |= DIFF_FORMAT_SUMMARY;
2379        else if (!strcmp(arg, "--patch-with-stat"))
2380                options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT;
2381        else if (!strcmp(arg, "--name-only"))
2382                options->output_format |= DIFF_FORMAT_NAME;
2383        else if (!strcmp(arg, "--name-status"))
2384                options->output_format |= DIFF_FORMAT_NAME_STATUS;
2385        else if (!strcmp(arg, "-s"))
2386                options->output_format |= DIFF_FORMAT_NO_OUTPUT;
2387        else if (!prefixcmp(arg, "--stat")) {
2388                char *end;
2389                int width = options->stat_width;
2390                int name_width = options->stat_name_width;
2391                arg += 6;
2392                end = (char *)arg;
2393
2394                switch (*arg) {
2395                case '-':
2396                        if (!prefixcmp(arg, "-width="))
2397                                width = strtoul(arg + 7, &end, 10);
2398                        else if (!prefixcmp(arg, "-name-width="))
2399                                name_width = strtoul(arg + 12, &end, 10);
2400                        break;
2401                case '=':
2402                        width = strtoul(arg+1, &end, 10);
2403                        if (*end == ',')
2404                                name_width = strtoul(end+1, &end, 10);
2405                }
2406
2407                /* Important! This checks all the error cases! */
2408                if (*end)
2409                        return 0;
2410                options->output_format |= DIFF_FORMAT_DIFFSTAT;
2411                options->stat_name_width = name_width;
2412                options->stat_width = width;
2413        }
2414
2415        /* renames options */
2416        else if (!prefixcmp(arg, "-B")) {
2417                if ((options->break_opt = diff_scoreopt_parse(arg)) == -1)
2418                        return -1;
2419        }
2420        else if (!prefixcmp(arg, "-M")) {
2421                if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
2422                        return -1;
2423                options->detect_rename = DIFF_DETECT_RENAME;
2424        }
2425        else if (!prefixcmp(arg, "-C")) {
2426                if (options->detect_rename == DIFF_DETECT_COPY)
2427                        DIFF_OPT_SET(options, FIND_COPIES_HARDER);
2428                if ((options->rename_score = diff_scoreopt_parse(arg)) == -1)
2429                        return -1;
2430                options->detect_rename = DIFF_DETECT_COPY;
2431        }
2432        else if (!strcmp(arg, "--no-renames"))
2433                options->detect_rename = 0;
2434        else if (!strcmp(arg, "--relative"))
2435                DIFF_OPT_SET(options, RELATIVE_NAME);
2436        else if (!prefixcmp(arg, "--relative=")) {
2437                DIFF_OPT_SET(options, RELATIVE_NAME);
2438                options->prefix = arg + 11;
2439        }
2440
2441        /* xdiff options */
2442        else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space"))
2443                options->xdl_opts |= XDF_IGNORE_WHITESPACE;
2444        else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change"))
2445                options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE;
2446        else if (!strcmp(arg, "--ignore-space-at-eol"))
2447                options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL;
2448
2449        /* flags options */
2450        else if (!strcmp(arg, "--binary")) {
2451                options->output_format |= DIFF_FORMAT_PATCH;
2452                DIFF_OPT_SET(options, BINARY);
2453        }
2454        else if (!strcmp(arg, "--full-index"))
2455                DIFF_OPT_SET(options, FULL_INDEX);
2456        else if (!strcmp(arg, "-a") || !strcmp(arg, "--text"))
2457                DIFF_OPT_SET(options, TEXT);
2458        else if (!strcmp(arg, "-R"))
2459                DIFF_OPT_SET(options, REVERSE_DIFF);
2460        else if (!strcmp(arg, "--find-copies-harder"))
2461                DIFF_OPT_SET(options, FIND_COPIES_HARDER);
2462        else if (!strcmp(arg, "--follow"))
2463                DIFF_OPT_SET(options, FOLLOW_RENAMES);
2464        else if (!strcmp(arg, "--color"))
2465                DIFF_OPT_SET(options, COLOR_DIFF);
2466        else if (!strcmp(arg, "--no-color"))
2467                DIFF_OPT_CLR(options, COLOR_DIFF);
2468        else if (!strcmp(arg, "--color-words"))
2469                options->flags |= DIFF_OPT_COLOR_DIFF | DIFF_OPT_COLOR_DIFF_WORDS;
2470        else if (!strcmp(arg, "--exit-code"))
2471                DIFF_OPT_SET(options, EXIT_WITH_STATUS);
2472        else if (!strcmp(arg, "--quiet"))
2473                DIFF_OPT_SET(options, QUIET);
2474        else if (!strcmp(arg, "--ext-diff"))
2475                DIFF_OPT_SET(options, ALLOW_EXTERNAL);
2476        else if (!strcmp(arg, "--no-ext-diff"))
2477                DIFF_OPT_CLR(options, ALLOW_EXTERNAL);
2478        else if (!strcmp(arg, "--ignore-submodules"))
2479                DIFF_OPT_SET(options, IGNORE_SUBMODULES);
2480
2481        /* misc options */
2482        else if (!strcmp(arg, "-z"))
2483                options->line_termination = 0;
2484        else if (!prefixcmp(arg, "-l"))
2485                options->rename_limit = strtoul(arg+2, NULL, 10);
2486        else if (!prefixcmp(arg, "-S"))
2487                options->pickaxe = arg + 2;
2488        else if (!strcmp(arg, "--pickaxe-all"))
2489                options->pickaxe_opts = DIFF_PICKAXE_ALL;
2490        else if (!strcmp(arg, "--pickaxe-regex"))
2491                options->pickaxe_opts = DIFF_PICKAXE_REGEX;
2492        else if (!prefixcmp(arg, "-O"))
2493                options->orderfile = arg + 2;
2494        else if (!prefixcmp(arg, "--diff-filter="))
2495                options->filter = arg + 14;
2496        else if (!strcmp(arg, "--abbrev"))
2497                options->abbrev = DEFAULT_ABBREV;
2498        else if (!prefixcmp(arg, "--abbrev=")) {
2499                options->abbrev = strtoul(arg + 9, NULL, 10);
2500                if (options->abbrev < MINIMUM_ABBREV)
2501                        options->abbrev = MINIMUM_ABBREV;
2502                else if (40 < options->abbrev)
2503                        options->abbrev = 40;
2504        }
2505        else if (!prefixcmp(arg, "--src-prefix="))
2506                options->a_prefix = arg + 13;
2507        else if (!prefixcmp(arg, "--dst-prefix="))
2508                options->b_prefix = arg + 13;
2509        else if (!strcmp(arg, "--no-prefix"))
2510                options->a_prefix = options->b_prefix = "";
2511        else if (!prefixcmp(arg, "--output=")) {
2512                options->file = fopen(arg + strlen("--output="), "w");
2513                options->close_file = 1;
2514        } else
2515                return 0;
2516        return 1;
2517}
2518
2519static int parse_num(const char **cp_p)
2520{
2521        unsigned long num, scale;
2522        int ch, dot;
2523        const char *cp = *cp_p;
2524
2525        num = 0;
2526        scale = 1;
2527        dot = 0;
2528        for(;;) {
2529                ch = *cp;
2530                if ( !dot && ch == '.' ) {
2531                        scale = 1;
2532                        dot = 1;
2533                } else if ( ch == '%' ) {
2534                        scale = dot ? scale*100 : 100;
2535                        cp++;   /* % is always at the end */
2536                        break;
2537                } else if ( ch >= '0' && ch <= '9' ) {
2538                        if ( scale < 100000 ) {
2539                                scale *= 10;
2540                                num = (num*10) + (ch-'0');
2541                        }
2542                } else {
2543                        break;
2544                }
2545                cp++;
2546        }
2547        *cp_p = cp;
2548
2549        /* user says num divided by scale and we say internally that
2550         * is MAX_SCORE * num / scale.
2551         */
2552        return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale));
2553}
2554
2555static int diff_scoreopt_parse(const char *opt)
2556{
2557        int opt1, opt2, cmd;
2558
2559        if (*opt++ != '-')
2560                return -1;
2561        cmd = *opt++;
2562        if (cmd != 'M' && cmd != 'C' && cmd != 'B')
2563                return -1; /* that is not a -M, -C nor -B option */
2564
2565        opt1 = parse_num(&opt);
2566        if (cmd != 'B')
2567                opt2 = 0;
2568        else {
2569                if (*opt == 0)
2570                        opt2 = 0;
2571                else if (*opt != '/')
2572                        return -1; /* we expect -B80/99 or -B80 */
2573                else {
2574                        opt++;
2575                        opt2 = parse_num(&opt);
2576                }
2577        }
2578        if (*opt != 0)
2579                return -1;
2580        return opt1 | (opt2 << 16);
2581}
2582
2583struct diff_queue_struct diff_queued_diff;
2584
2585void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp)
2586{
2587        if (queue->alloc <= queue->nr) {
2588                queue->alloc = alloc_nr(queue->alloc);
2589                queue->queue = xrealloc(queue->queue,
2590                                        sizeof(dp) * queue->alloc);
2591        }
2592        queue->queue[queue->nr++] = dp;
2593}
2594
2595struct diff_filepair *diff_queue(struct diff_queue_struct *queue,
2596                                 struct diff_filespec *one,
2597                                 struct diff_filespec *two)
2598{
2599        struct diff_filepair *dp = xcalloc(1, sizeof(*dp));
2600        dp->one = one;
2601        dp->two = two;
2602        if (queue)
2603                diff_q(queue, dp);
2604        return dp;
2605}
2606
2607void diff_free_filepair(struct diff_filepair *p)
2608{
2609        free_filespec(p->one);
2610        free_filespec(p->two);
2611        free(p);
2612}
2613
2614/* This is different from find_unique_abbrev() in that
2615 * it stuffs the result with dots for alignment.
2616 */
2617const char *diff_unique_abbrev(const unsigned char *sha1, int len)
2618{
2619        int abblen;
2620        const char *abbrev;
2621        if (len == 40)
2622                return sha1_to_hex(sha1);
2623
2624        abbrev = find_unique_abbrev(sha1, len);
2625        abblen = strlen(abbrev);
2626        if (abblen < 37) {
2627                static char hex[41];
2628                if (len < abblen && abblen <= len + 2)
2629                        sprintf(hex, "%s%.*s", abbrev, len+3-abblen, "..");
2630                else
2631                        sprintf(hex, "%s...", abbrev);
2632                return hex;
2633        }
2634        return sha1_to_hex(sha1);
2635}
2636
2637static void diff_flush_raw(struct diff_filepair *p, struct diff_options *opt)
2638{
2639        int line_termination = opt->line_termination;
2640        int inter_name_termination = line_termination ? '\t' : '\0';
2641
2642        if (!(opt->output_format & DIFF_FORMAT_NAME_STATUS)) {
2643                fprintf(opt->file, ":%06o %06o %s ", p->one->mode, p->two->mode,
2644                        diff_unique_abbrev(p->one->sha1, opt->abbrev));
2645                fprintf(opt->file, "%s ", diff_unique_abbrev(p->two->sha1, opt->abbrev));
2646        }
2647        if (p->score) {
2648                fprintf(opt->file, "%c%03d%c", p->status, similarity_index(p),
2649                        inter_name_termination);
2650        } else {
2651                fprintf(opt->file, "%c%c", p->status, inter_name_termination);
2652        }
2653
2654        if (p->status == DIFF_STATUS_COPIED ||
2655            p->status == DIFF_STATUS_RENAMED) {
2656                const char *name_a, *name_b;
2657                name_a = p->one->path;
2658                name_b = p->two->path;
2659                strip_prefix(opt->prefix_length, &name_a, &name_b);
2660                write_name_quoted(name_a, opt->file, inter_name_termination);
2661                write_name_quoted(name_b, opt->file, line_termination);
2662        } else {
2663                const char *name_a, *name_b;
2664                name_a = p->one->mode ? p->one->path : p->two->path;
2665                name_b = NULL;
2666                strip_prefix(opt->prefix_length, &name_a, &name_b);
2667                write_name_quoted(name_a, opt->file, line_termination);
2668        }
2669}
2670
2671int diff_unmodified_pair(struct diff_filepair *p)
2672{
2673        /* This function is written stricter than necessary to support
2674         * the currently implemented transformers, but the idea is to
2675         * let transformers to produce diff_filepairs any way they want,
2676         * and filter and clean them up here before producing the output.
2677         */
2678        struct diff_filespec *one = p->one, *two = p->two;
2679
2680        if (DIFF_PAIR_UNMERGED(p))
2681                return 0; /* unmerged is interesting */
2682
2683        /* deletion, addition, mode or type change
2684         * and rename are all interesting.
2685         */
2686        if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) ||
2687            DIFF_PAIR_MODE_CHANGED(p) ||
2688            strcmp(one->path, two->path))
2689                return 0;
2690
2691        /* both are valid and point at the same path.  that is, we are
2692         * dealing with a change.
2693         */
2694        if (one->sha1_valid && two->sha1_valid &&
2695            !hashcmp(one->sha1, two->sha1))
2696                return 1; /* no change */
2697        if (!one->sha1_valid && !two->sha1_valid)
2698                return 1; /* both look at the same file on the filesystem. */
2699        return 0;
2700}
2701
2702static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o)
2703{
2704        if (diff_unmodified_pair(p))
2705                return;
2706
2707        if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2708            (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2709                return; /* no tree diffs in patch format */
2710
2711        run_diff(p, o);
2712}
2713
2714static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o,
2715                            struct diffstat_t *diffstat)
2716{
2717        if (diff_unmodified_pair(p))
2718                return;
2719
2720        if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2721            (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2722                return; /* no tree diffs in patch format */
2723
2724        run_diffstat(p, o, diffstat);
2725}
2726
2727static void diff_flush_checkdiff(struct diff_filepair *p,
2728                struct diff_options *o)
2729{
2730        if (diff_unmodified_pair(p))
2731                return;
2732
2733        if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2734            (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2735                return; /* no tree diffs in patch format */
2736
2737        run_checkdiff(p, o);
2738}
2739
2740int diff_queue_is_empty(void)
2741{
2742        struct diff_queue_struct *q = &diff_queued_diff;
2743        int i;
2744        for (i = 0; i < q->nr; i++)
2745                if (!diff_unmodified_pair(q->queue[i]))
2746                        return 0;
2747        return 1;
2748}
2749
2750#if DIFF_DEBUG
2751void diff_debug_filespec(struct diff_filespec *s, int x, const char *one)
2752{
2753        fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n",
2754                x, one ? one : "",
2755                s->path,
2756                DIFF_FILE_VALID(s) ? "valid" : "invalid",
2757                s->mode,
2758                s->sha1_valid ? sha1_to_hex(s->sha1) : "");
2759        fprintf(stderr, "queue[%d] %s size %lu flags %d\n",
2760                x, one ? one : "",
2761                s->size, s->xfrm_flags);
2762}
2763
2764void diff_debug_filepair(const struct diff_filepair *p, int i)
2765{
2766        diff_debug_filespec(p->one, i, "one");
2767        diff_debug_filespec(p->two, i, "two");
2768        fprintf(stderr, "score %d, status %c rename_used %d broken %d\n",
2769                p->score, p->status ? p->status : '?',
2770                p->one->rename_used, p->broken_pair);
2771}
2772
2773void diff_debug_queue(const char *msg, struct diff_queue_struct *q)
2774{
2775        int i;
2776        if (msg)
2777                fprintf(stderr, "%s\n", msg);
2778        fprintf(stderr, "q->nr = %d\n", q->nr);
2779        for (i = 0; i < q->nr; i++) {
2780                struct diff_filepair *p = q->queue[i];
2781                diff_debug_filepair(p, i);
2782        }
2783}
2784#endif
2785
2786static void diff_resolve_rename_copy(void)
2787{
2788        int i;
2789        struct diff_filepair *p;
2790        struct diff_queue_struct *q = &diff_queued_diff;
2791
2792        diff_debug_queue("resolve-rename-copy", q);
2793
2794        for (i = 0; i < q->nr; i++) {
2795                p = q->queue[i];
2796                p->status = 0; /* undecided */
2797                if (DIFF_PAIR_UNMERGED(p))
2798                        p->status = DIFF_STATUS_UNMERGED;
2799                else if (!DIFF_FILE_VALID(p->one))
2800                        p->status = DIFF_STATUS_ADDED;
2801                else if (!DIFF_FILE_VALID(p->two))
2802                        p->status = DIFF_STATUS_DELETED;
2803                else if (DIFF_PAIR_TYPE_CHANGED(p))
2804                        p->status = DIFF_STATUS_TYPE_CHANGED;
2805
2806                /* from this point on, we are dealing with a pair
2807                 * whose both sides are valid and of the same type, i.e.
2808                 * either in-place edit or rename/copy edit.
2809                 */
2810                else if (DIFF_PAIR_RENAME(p)) {
2811                        /*
2812                         * A rename might have re-connected a broken
2813                         * pair up, causing the pathnames to be the
2814                         * same again. If so, that's not a rename at
2815                         * all, just a modification..
2816                         *
2817                         * Otherwise, see if this source was used for
2818                         * multiple renames, in which case we decrement
2819                         * the count, and call it a copy.
2820                         */
2821                        if (!strcmp(p->one->path, p->two->path))
2822                                p->status = DIFF_STATUS_MODIFIED;
2823                        else if (--p->one->rename_used > 0)
2824                                p->status = DIFF_STATUS_COPIED;
2825                        else
2826                                p->status = DIFF_STATUS_RENAMED;
2827                }
2828                else if (hashcmp(p->one->sha1, p->two->sha1) ||
2829                         p->one->mode != p->two->mode ||
2830                         is_null_sha1(p->one->sha1))
2831                        p->status = DIFF_STATUS_MODIFIED;
2832                else {
2833                        /* This is a "no-change" entry and should not
2834                         * happen anymore, but prepare for broken callers.
2835                         */
2836                        error("feeding unmodified %s to diffcore",
2837                              p->one->path);
2838                        p->status = DIFF_STATUS_UNKNOWN;
2839                }
2840        }
2841        diff_debug_queue("resolve-rename-copy done", q);
2842}
2843
2844static int check_pair_status(struct diff_filepair *p)
2845{
2846        switch (p->status) {
2847        case DIFF_STATUS_UNKNOWN:
2848                return 0;
2849        case 0:
2850                die("internal error in diff-resolve-rename-copy");
2851        default:
2852                return 1;
2853        }
2854}
2855
2856static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt)
2857{
2858        int fmt = opt->output_format;
2859
2860        if (fmt & DIFF_FORMAT_CHECKDIFF)
2861                diff_flush_checkdiff(p, opt);
2862        else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS))
2863                diff_flush_raw(p, opt);
2864        else if (fmt & DIFF_FORMAT_NAME) {
2865                const char *name_a, *name_b;
2866                name_a = p->two->path;
2867                name_b = NULL;
2868                strip_prefix(opt->prefix_length, &name_a, &name_b);
2869                write_name_quoted(name_a, opt->file, opt->line_termination);
2870        }
2871}
2872
2873static void show_file_mode_name(FILE *file, const char *newdelete, struct diff_filespec *fs)
2874{
2875        if (fs->mode)
2876                fprintf(file, " %s mode %06o ", newdelete, fs->mode);
2877        else
2878                fprintf(file, " %s ", newdelete);
2879        write_name_quoted(fs->path, file, '\n');
2880}
2881
2882
2883static void show_mode_change(FILE *file, struct diff_filepair *p, int show_name)
2884{
2885        if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) {
2886                fprintf(file, " mode change %06o => %06o%c", p->one->mode, p->two->mode,
2887                        show_name ? ' ' : '\n');
2888                if (show_name) {
2889                        write_name_quoted(p->two->path, file, '\n');
2890                }
2891        }
2892}
2893
2894static void show_rename_copy(FILE *file, const char *renamecopy, struct diff_filepair *p)
2895{
2896        char *names = pprint_rename(p->one->path, p->two->path);
2897
2898        fprintf(file, " %s %s (%d%%)\n", renamecopy, names, similarity_index(p));
2899        free(names);
2900        show_mode_change(file, p, 0);
2901}
2902
2903static void diff_summary(FILE *file, struct diff_filepair *p)
2904{
2905        switch(p->status) {
2906        case DIFF_STATUS_DELETED:
2907                show_file_mode_name(file, "delete", p->one);
2908                break;
2909        case DIFF_STATUS_ADDED:
2910                show_file_mode_name(file, "create", p->two);
2911                break;
2912        case DIFF_STATUS_COPIED:
2913                show_rename_copy(file, "copy", p);
2914                break;
2915        case DIFF_STATUS_RENAMED:
2916                show_rename_copy(file, "rename", p);
2917                break;
2918        default:
2919                if (p->score) {
2920                        fputs(" rewrite ", file);
2921                        write_name_quoted(p->two->path, file, ' ');
2922                        fprintf(file, "(%d%%)\n", similarity_index(p));
2923                }
2924                show_mode_change(file, p, !p->score);
2925                break;
2926        }
2927}
2928
2929struct patch_id_t {
2930        git_SHA_CTX *ctx;
2931        int patchlen;
2932};
2933
2934static int remove_space(char *line, int len)
2935{
2936        int i;
2937        char *dst = line;
2938        unsigned char c;
2939
2940        for (i = 0; i < len; i++)
2941                if (!isspace((c = line[i])))
2942                        *dst++ = c;
2943
2944        return dst - line;
2945}
2946
2947static void patch_id_consume(void *priv, char *line, unsigned long len)
2948{
2949        struct patch_id_t *data = priv;
2950        int new_len;
2951
2952        /* Ignore line numbers when computing the SHA1 of the patch */
2953        if (!prefixcmp(line, "@@ -"))
2954                return;
2955
2956        new_len = remove_space(line, len);
2957
2958        git_SHA1_Update(data->ctx, line, new_len);
2959        data->patchlen += new_len;
2960}
2961
2962/* returns 0 upon success, and writes result into sha1 */
2963static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
2964{
2965        struct diff_queue_struct *q = &diff_queued_diff;
2966        int i;
2967        git_SHA_CTX ctx;
2968        struct patch_id_t data;
2969        char buffer[PATH_MAX * 4 + 20];
2970
2971        git_SHA1_Init(&ctx);
2972        memset(&data, 0, sizeof(struct patch_id_t));
2973        data.ctx = &ctx;
2974
2975        for (i = 0; i < q->nr; i++) {
2976                xpparam_t xpp;
2977                xdemitconf_t xecfg;
2978                xdemitcb_t ecb;
2979                mmfile_t mf1, mf2;
2980                struct diff_filepair *p = q->queue[i];
2981                int len1, len2;
2982
2983                memset(&xecfg, 0, sizeof(xecfg));
2984                if (p->status == 0)
2985                        return error("internal diff status error");
2986                if (p->status == DIFF_STATUS_UNKNOWN)
2987                        continue;
2988                if (diff_unmodified_pair(p))
2989                        continue;
2990                if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) ||
2991                    (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode)))
2992                        continue;
2993                if (DIFF_PAIR_UNMERGED(p))
2994                        continue;
2995
2996                diff_fill_sha1_info(p->one);
2997                diff_fill_sha1_info(p->two);
2998                if (fill_mmfile(&mf1, p->one) < 0 ||
2999                                fill_mmfile(&mf2, p->two) < 0)
3000                        return error("unable to read files to diff");
3001
3002                len1 = remove_space(p->one->path, strlen(p->one->path));
3003                len2 = remove_space(p->two->path, strlen(p->two->path));
3004                if (p->one->mode == 0)
3005                        len1 = snprintf(buffer, sizeof(buffer),
3006                                        "diff--gita/%.*sb/%.*s"
3007                                        "newfilemode%06o"
3008                                        "---/dev/null"
3009                                        "+++b/%.*s",
3010                                        len1, p->one->path,
3011                                        len2, p->two->path,
3012                                        p->two->mode,
3013                                        len2, p->two->path);
3014                else if (p->two->mode == 0)
3015                        len1 = snprintf(buffer, sizeof(buffer),
3016                                        "diff--gita/%.*sb/%.*s"
3017                                        "deletedfilemode%06o"
3018                                        "---a/%.*s"
3019                                        "+++/dev/null",
3020                                        len1, p->one->path,
3021                                        len2, p->two->path,
3022                                        p->one->mode,
3023                                        len1, p->one->path);
3024                else
3025                        len1 = snprintf(buffer, sizeof(buffer),
3026                                        "diff--gita/%.*sb/%.*s"
3027                                        "---a/%.*s"
3028                                        "+++b/%.*s",
3029                                        len1, p->one->path,
3030                                        len2, p->two->path,
3031                                        len1, p->one->path,
3032                                        len2, p->two->path);
3033                git_SHA1_Update(&ctx, buffer, len1);
3034
3035                xpp.flags = XDF_NEED_MINIMAL;
3036                xecfg.ctxlen = 3;
3037                xecfg.flags = XDL_EMIT_FUNCNAMES;
3038                xdi_diff_outf(&mf1, &mf2, patch_id_consume, &data,
3039                              &xpp, &xecfg, &ecb);
3040        }
3041
3042        git_SHA1_Final(sha1, &ctx);
3043        return 0;
3044}
3045
3046int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1)
3047{
3048        struct diff_queue_struct *q = &diff_queued_diff;
3049        int i;
3050        int result = diff_get_patch_id(options, sha1);
3051
3052        for (i = 0; i < q->nr; i++)
3053                diff_free_filepair(q->queue[i]);
3054
3055        free(q->queue);
3056        q->queue = NULL;
3057        q->nr = q->alloc = 0;
3058
3059        return result;
3060}
3061
3062static int is_summary_empty(const struct diff_queue_struct *q)
3063{
3064        int i;
3065
3066        for (i = 0; i < q->nr; i++) {
3067                const struct diff_filepair *p = q->queue[i];
3068
3069                switch (p->status) {
3070                case DIFF_STATUS_DELETED:
3071                case DIFF_STATUS_ADDED:
3072                case DIFF_STATUS_COPIED:
3073                case DIFF_STATUS_RENAMED:
3074                        return 0;
3075                default:
3076                        if (p->score)
3077                                return 0;
3078                        if (p->one->mode && p->two->mode &&
3079                            p->one->mode != p->two->mode)
3080                                return 0;
3081                        break;
3082                }
3083        }
3084        return 1;
3085}
3086
3087void diff_flush(struct diff_options *options)
3088{
3089        struct diff_queue_struct *q = &diff_queued_diff;
3090        int i, output_format = options->output_format;
3091        int separator = 0;
3092
3093        /*
3094         * Order: raw, stat, summary, patch
3095         * or:    name/name-status/checkdiff (other bits clear)
3096         */
3097        if (!q->nr)
3098                goto free_queue;
3099
3100        if (output_format & (DIFF_FORMAT_RAW |
3101                             DIFF_FORMAT_NAME |
3102                             DIFF_FORMAT_NAME_STATUS |
3103                             DIFF_FORMAT_CHECKDIFF)) {
3104                for (i = 0; i < q->nr; i++) {
3105                        struct diff_filepair *p = q->queue[i];
3106                        if (check_pair_status(p))
3107                                flush_one_pair(p, options);
3108                }
3109                separator++;
3110        }
3111
3112        if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) {
3113                struct diffstat_t diffstat;
3114
3115                memset(&diffstat, 0, sizeof(struct diffstat_t));
3116                for (i = 0; i < q->nr; i++) {
3117                        struct diff_filepair *p = q->queue[i];
3118                        if (check_pair_status(p))
3119                                diff_flush_stat(p, options, &diffstat);
3120                }
3121                if (output_format & DIFF_FORMAT_NUMSTAT)
3122                        show_numstat(&diffstat, options);
3123                if (output_format & DIFF_FORMAT_DIFFSTAT)
3124                        show_stats(&diffstat, options);
3125                if (output_format & DIFF_FORMAT_SHORTSTAT)
3126                        show_shortstats(&diffstat, options);
3127                free_diffstat_info(&diffstat);
3128                separator++;
3129        }
3130        if (output_format & DIFF_FORMAT_DIRSTAT)
3131                show_dirstat(options);
3132
3133        if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) {
3134                for (i = 0; i < q->nr; i++)
3135                        diff_summary(options->file, q->queue[i]);
3136                separator++;
3137        }
3138
3139        if (output_format & DIFF_FORMAT_PATCH) {
3140                if (separator) {
3141                        putc(options->line_termination, options->file);
3142                        if (options->stat_sep) {
3143                                /* attach patch instead of inline */
3144                                fputs(options->stat_sep, options->file);
3145                        }
3146                }
3147
3148                for (i = 0; i < q->nr; i++) {
3149                        struct diff_filepair *p = q->queue[i];
3150                        if (check_pair_status(p))
3151                                diff_flush_patch(p, options);
3152                }
3153        }
3154
3155        if (output_format & DIFF_FORMAT_CALLBACK)
3156                options->format_callback(q, options, options->format_callback_data);
3157
3158        for (i = 0; i < q->nr; i++)
3159                diff_free_filepair(q->queue[i]);
3160free_queue:
3161        free(q->queue);
3162        q->queue = NULL;
3163        q->nr = q->alloc = 0;
3164        if (options->close_file)
3165                fclose(options->file);
3166}
3167
3168static void diffcore_apply_filter(const char *filter)
3169{
3170        int i;
3171        struct diff_queue_struct *q = &diff_queued_diff;
3172        struct diff_queue_struct outq;
3173        outq.queue = NULL;
3174        outq.nr = outq.alloc = 0;
3175
3176        if (!filter)
3177                return;
3178
3179        if (strchr(filter, DIFF_STATUS_FILTER_AON)) {
3180                int found;
3181                for (i = found = 0; !found && i < q->nr; i++) {
3182                        struct diff_filepair *p = q->queue[i];
3183                        if (((p->status == DIFF_STATUS_MODIFIED) &&
3184                             ((p->score &&
3185                               strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3186                              (!p->score &&
3187                               strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3188                            ((p->status != DIFF_STATUS_MODIFIED) &&
3189                             strchr(filter, p->status)))
3190                                found++;
3191                }
3192                if (found)
3193                        return;
3194
3195                /* otherwise we will clear the whole queue
3196                 * by copying the empty outq at the end of this
3197                 * function, but first clear the current entries
3198                 * in the queue.
3199                 */
3200                for (i = 0; i < q->nr; i++)
3201                        diff_free_filepair(q->queue[i]);
3202        }
3203        else {
3204                /* Only the matching ones */
3205                for (i = 0; i < q->nr; i++) {
3206                        struct diff_filepair *p = q->queue[i];
3207
3208                        if (((p->status == DIFF_STATUS_MODIFIED) &&
3209                             ((p->score &&
3210                               strchr(filter, DIFF_STATUS_FILTER_BROKEN)) ||
3211                              (!p->score &&
3212                               strchr(filter, DIFF_STATUS_MODIFIED)))) ||
3213                            ((p->status != DIFF_STATUS_MODIFIED) &&
3214                             strchr(filter, p->status)))
3215                                diff_q(&outq, p);
3216                        else
3217                                diff_free_filepair(p);
3218                }
3219        }
3220        free(q->queue);
3221        *q = outq;
3222}
3223
3224/* Check whether two filespecs with the same mode and size are identical */
3225static int diff_filespec_is_identical(struct diff_filespec *one,
3226                                      struct diff_filespec *two)
3227{
3228        if (S_ISGITLINK(one->mode))
3229                return 0;
3230        if (diff_populate_filespec(one, 0))
3231                return 0;
3232        if (diff_populate_filespec(two, 0))
3233                return 0;
3234        return !memcmp(one->data, two->data, one->size);
3235}
3236
3237static void diffcore_skip_stat_unmatch(struct diff_options *diffopt)
3238{
3239        int i;
3240        struct diff_queue_struct *q = &diff_queued_diff;
3241        struct diff_queue_struct outq;
3242        outq.queue = NULL;
3243        outq.nr = outq.alloc = 0;
3244
3245        for (i = 0; i < q->nr; i++) {
3246                struct diff_filepair *p = q->queue[i];
3247
3248                /*
3249                 * 1. Entries that come from stat info dirtyness
3250                 *    always have both sides (iow, not create/delete),
3251                 *    one side of the object name is unknown, with
3252                 *    the same mode and size.  Keep the ones that
3253                 *    do not match these criteria.  They have real
3254                 *    differences.
3255                 *
3256                 * 2. At this point, the file is known to be modified,
3257                 *    with the same mode and size, and the object
3258                 *    name of one side is unknown.  Need to inspect
3259                 *    the identical contents.
3260                 */
3261                if (!DIFF_FILE_VALID(p->one) || /* (1) */
3262                    !DIFF_FILE_VALID(p->two) ||
3263                    (p->one->sha1_valid && p->two->sha1_valid) ||
3264                    (p->one->mode != p->two->mode) ||
3265                    diff_populate_filespec(p->one, 1) ||
3266                    diff_populate_filespec(p->two, 1) ||
3267                    (p->one->size != p->two->size) ||
3268                    !diff_filespec_is_identical(p->one, p->two)) /* (2) */
3269                        diff_q(&outq, p);
3270                else {
3271                        /*
3272                         * The caller can subtract 1 from skip_stat_unmatch
3273                         * to determine how many paths were dirty only
3274                         * due to stat info mismatch.
3275                         */
3276                        if (!DIFF_OPT_TST(diffopt, NO_INDEX))
3277                                diffopt->skip_stat_unmatch++;
3278                        diff_free_filepair(p);
3279                }
3280        }
3281        free(q->queue);
3282        *q = outq;
3283}
3284
3285void diffcore_std(struct diff_options *options)
3286{
3287        if (options->skip_stat_unmatch)
3288                diffcore_skip_stat_unmatch(options);
3289        if (options->break_opt != -1)
3290                diffcore_break(options->break_opt);
3291        if (options->detect_rename)
3292                diffcore_rename(options);
3293        if (options->break_opt != -1)
3294                diffcore_merge_broken();
3295        if (options->pickaxe)
3296                diffcore_pickaxe(options->pickaxe, options->pickaxe_opts);
3297        if (options->orderfile)
3298                diffcore_order(options->orderfile);
3299        diff_resolve_rename_copy();
3300        diffcore_apply_filter(options->filter);
3301
3302        if (diff_queued_diff.nr)
3303                DIFF_OPT_SET(options, HAS_CHANGES);
3304        else
3305                DIFF_OPT_CLR(options, HAS_CHANGES);
3306}
3307
3308int diff_result_code(struct diff_options *opt, int status)
3309{
3310        int result = 0;
3311        if (!DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3312            !(opt->output_format & DIFF_FORMAT_CHECKDIFF))
3313                return status;
3314        if (DIFF_OPT_TST(opt, EXIT_WITH_STATUS) &&
3315            DIFF_OPT_TST(opt, HAS_CHANGES))
3316                result |= 01;
3317        if ((opt->output_format & DIFF_FORMAT_CHECKDIFF) &&
3318            DIFF_OPT_TST(opt, CHECK_FAILED))
3319                result |= 02;
3320        return result;
3321}
3322
3323void diff_addremove(struct diff_options *options,
3324                    int addremove, unsigned mode,
3325                    const unsigned char *sha1,
3326                    const char *concatpath)
3327{
3328        struct diff_filespec *one, *two;
3329
3330        if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(mode))
3331                return;
3332
3333        /* This may look odd, but it is a preparation for
3334         * feeding "there are unchanged files which should
3335         * not produce diffs, but when you are doing copy
3336         * detection you would need them, so here they are"
3337         * entries to the diff-core.  They will be prefixed
3338         * with something like '=' or '*' (I haven't decided
3339         * which but should not make any difference).
3340         * Feeding the same new and old to diff_change()
3341         * also has the same effect.
3342         * Before the final output happens, they are pruned after
3343         * merged into rename/copy pairs as appropriate.
3344         */
3345        if (DIFF_OPT_TST(options, REVERSE_DIFF))
3346                addremove = (addremove == '+' ? '-' :
3347                             addremove == '-' ? '+' : addremove);
3348
3349        if (options->prefix &&
3350            strncmp(concatpath, options->prefix, options->prefix_length))
3351                return;
3352
3353        one = alloc_filespec(concatpath);
3354        two = alloc_filespec(concatpath);
3355
3356        if (addremove != '+')
3357                fill_filespec(one, sha1, mode);
3358        if (addremove != '-')
3359                fill_filespec(two, sha1, mode);
3360
3361        diff_queue(&diff_queued_diff, one, two);
3362        DIFF_OPT_SET(options, HAS_CHANGES);
3363}
3364
3365void diff_change(struct diff_options *options,
3366                 unsigned old_mode, unsigned new_mode,
3367                 const unsigned char *old_sha1,
3368                 const unsigned char *new_sha1,
3369                 const char *concatpath)
3370{
3371        struct diff_filespec *one, *two;
3372
3373        if (DIFF_OPT_TST(options, IGNORE_SUBMODULES) && S_ISGITLINK(old_mode)
3374                        && S_ISGITLINK(new_mode))
3375                return;
3376
3377        if (DIFF_OPT_TST(options, REVERSE_DIFF)) {
3378                unsigned tmp;
3379                const unsigned char *tmp_c;
3380                tmp = old_mode; old_mode = new_mode; new_mode = tmp;
3381                tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
3382        }
3383
3384        if (options->prefix &&
3385            strncmp(concatpath, options->prefix, options->prefix_length))
3386                return;
3387
3388        one = alloc_filespec(concatpath);
3389        two = alloc_filespec(concatpath);
3390        fill_filespec(one, old_sha1, old_mode);
3391        fill_filespec(two, new_sha1, new_mode);
3392
3393        diff_queue(&diff_queued_diff, one, two);
3394        DIFF_OPT_SET(options, HAS_CHANGES);
3395}
3396
3397void diff_unmerge(struct diff_options *options,
3398                  const char *path,
3399                  unsigned mode, const unsigned char *sha1)
3400{
3401        struct diff_filespec *one, *two;
3402
3403        if (options->prefix &&
3404            strncmp(path, options->prefix, options->prefix_length))
3405                return;
3406
3407        one = alloc_filespec(path);
3408        two = alloc_filespec(path);
3409        fill_filespec(one, sha1, mode);
3410        diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1;
3411}
3412
3413static char *run_textconv(const char *pgm, struct diff_filespec *spec,
3414                size_t *outsize)
3415{
3416        struct diff_tempfile temp;
3417        const char *argv[3];
3418        const char **arg = argv;
3419        struct child_process child;
3420        struct strbuf buf = STRBUF_INIT;
3421
3422        prepare_temp_file(spec->path, &temp, spec);
3423        *arg++ = pgm;
3424        *arg++ = temp.name;
3425        *arg = NULL;
3426
3427        memset(&child, 0, sizeof(child));
3428        child.argv = argv;
3429        child.out = -1;
3430        if (start_command(&child) != 0 ||
3431            strbuf_read(&buf, child.out, 0) < 0 ||
3432            finish_command(&child) != 0) {
3433                if (temp.name == temp.tmp_path)
3434                        unlink(temp.name);
3435                error("error running textconv command '%s'", pgm);
3436                return NULL;
3437        }
3438        if (temp.name == temp.tmp_path)
3439                unlink(temp.name);
3440
3441        return strbuf_detach(&buf, outsize);
3442}