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