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