diff.con commit [PATCH] delta check (d1af002)
   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 <limits.h>
   8#include "cache.h"
   9#include "diff.h"
  10#include "delta.h"
  11
  12static const char *diff_opts = "-pu";
  13static unsigned char null_sha1[20] = { 0, };
  14#define MAX_SCORE 10000
  15#define DEFAULT_MINIMUM_SCORE 5000
  16
  17static const char *external_diff(void)
  18{
  19        static const char *external_diff_cmd = NULL;
  20        static int done_preparing = 0;
  21
  22        if (done_preparing)
  23                return external_diff_cmd;
  24
  25        /*
  26         * Default values above are meant to match the
  27         * Linux kernel development style.  Examples of
  28         * alternative styles you can specify via environment
  29         * variables are:
  30         *
  31         * GIT_DIFF_OPTS="-c";
  32         */
  33        if (gitenv("GIT_EXTERNAL_DIFF"))
  34                external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF");
  35
  36        /* In case external diff fails... */
  37        diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts;
  38
  39        done_preparing = 1;
  40        return external_diff_cmd;
  41}
  42
  43/* Help to copy the thing properly quoted for the shell safety.
  44 * any single quote is replaced with '\'', and the caller is
  45 * expected to enclose the result within a single quote pair.
  46 *
  47 * E.g.
  48 *  original     sq_expand     result
  49 *  name     ==> name      ==> 'name'
  50 *  a b      ==> a b       ==> 'a b'
  51 *  a'b      ==> a'\''b    ==> 'a'\''b'
  52 */
  53static char *sq_expand(const char *src)
  54{
  55        static char *buf = NULL;
  56        int cnt, c;
  57        const char *cp;
  58        char *bp;
  59
  60        /* count bytes needed to store the quoted string. */
  61        for (cnt = 1, cp = src; *cp; cnt++, cp++)
  62                if (*cp == '\'')
  63                        cnt += 3;
  64
  65        buf = xmalloc(cnt);
  66        bp = buf;
  67        while ((c = *src++)) {
  68                if (c != '\'')
  69                        *bp++ = c;
  70                else {
  71                        bp = strcpy(bp, "'\\''");
  72                        bp += 4;
  73                }
  74        }
  75        *bp = 0;
  76        return buf;
  77}
  78
  79static struct diff_tempfile {
  80        const char *name;
  81        char hex[41];
  82        char mode[10];
  83        char tmp_path[50];
  84} diff_temp[2];
  85
  86struct diff_spec {
  87        unsigned char blob_sha1[20];
  88        unsigned short mode;     /* file mode */
  89        unsigned sha1_valid : 1; /* if true, use blob_sha1 and trust mode;
  90                                  * if false, use the name and read from
  91                                  * the filesystem.
  92                                  */
  93        unsigned file_valid : 1; /* if false the file does not exist */
  94};
  95
  96static void builtin_diff(const char *name_a,
  97                         const char *name_b,
  98                         struct diff_tempfile *temp,
  99                         int rename_score)
 100{
 101        int i, next_at, cmd_size;
 102        const char *diff_cmd = "diff -L'%s%s' -L'%s%s'";
 103        const char *diff_arg  = "'%s' '%s'||:"; /* "||:" is to return 0 */
 104        const char *input_name_sq[2];
 105        const char *path0[2];
 106        const char *path1[2];
 107        const char *name_sq[2];
 108        char *cmd;
 109
 110        name_sq[0] = sq_expand(name_a);
 111        name_sq[1] = sq_expand(name_b);
 112
 113        /* diff_cmd and diff_arg have 6 %s in total which makes
 114         * the sum of these strings 12 bytes larger than required.
 115         * we use 2 spaces around diff-opts, and we need to count
 116         * terminating NUL, so we subtract 9 here.
 117         */
 118        cmd_size = (strlen(diff_cmd) + strlen(diff_opts) +
 119                        strlen(diff_arg) - 9);
 120        for (i = 0; i < 2; i++) {
 121                input_name_sq[i] = sq_expand(temp[i].name);
 122                if (!strcmp(temp[i].name, "/dev/null")) {
 123                        path0[i] = "/dev/null";
 124                        path1[i] = "";
 125                } else {
 126                        path0[i] = i ? "b/" : "a/";
 127                        path1[i] = name_sq[i];
 128                }
 129                cmd_size += (strlen(path0[i]) + strlen(path1[i]) +
 130                             strlen(input_name_sq[i]));
 131        }
 132
 133        cmd = xmalloc(cmd_size);
 134
 135        next_at = 0;
 136        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 137                            diff_cmd,
 138                            path0[0], path1[0], path0[1], path1[1]);
 139        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 140                            " %s ", diff_opts);
 141        next_at += snprintf(cmd+next_at, cmd_size-next_at,
 142                            diff_arg, input_name_sq[0], input_name_sq[1]);
 143
 144        printf("diff --git a/%s b/%s\n", name_a, name_b);
 145        if (!path1[0][0])
 146                printf("new file mode %s\n", temp[1].mode);
 147        else if (!path1[1][0])
 148                printf("deleted file mode %s\n", temp[0].mode);
 149        else {
 150                if (strcmp(temp[0].mode, temp[1].mode)) {
 151                        printf("old mode %s\n", temp[0].mode);
 152                        printf("new mode %s\n", temp[1].mode);
 153                }
 154                if (strcmp(name_a, name_b)) {
 155                        if (0 < rename_score)
 156                                printf("rename similarity index %d%%\n",
 157                                       (int)(0.5+
 158                                             rename_score*100.0/MAX_SCORE));
 159                        printf("rename old %s\n", name_a);
 160                        printf("rename new %s\n", name_b);
 161                }
 162                if (strncmp(temp[0].mode, temp[1].mode, 3))
 163                        /* we do not run diff between different kind
 164                         * of objects.
 165                         */
 166                        exit(0);
 167        }
 168        fflush(NULL);
 169        execlp("/bin/sh","sh", "-c", cmd, NULL);
 170}
 171
 172/*
 173 * Given a name and sha1 pair, if the dircache tells us the file in
 174 * the work tree has that object contents, return true, so that
 175 * prepare_temp_file() does not have to inflate and extract.
 176 */
 177static int work_tree_matches(const char *name, const unsigned char *sha1)
 178{
 179        struct cache_entry *ce;
 180        struct stat st;
 181        int pos, len;
 182
 183        /* We do not read the cache ourselves here, because the
 184         * benchmark with my previous version that always reads cache
 185         * shows that it makes things worse for diff-tree comparing
 186         * two linux-2.6 kernel trees in an already checked out work
 187         * tree.  This is because most diff-tree comparisons deal with
 188         * only a small number of files, while reading the cache is
 189         * expensive for a large project, and its cost outweighs the
 190         * savings we get by not inflating the object to a temporary
 191         * file.  Practically, this code only helps when we are used
 192         * by diff-cache --cached, which does read the cache before
 193         * calling us.
 194         */
 195        if (!active_cache)
 196                return 0;
 197
 198        len = strlen(name);
 199        pos = cache_name_pos(name, len);
 200        if (pos < 0)
 201                return 0;
 202        ce = active_cache[pos];
 203        if ((lstat(name, &st) < 0) ||
 204            !S_ISREG(st.st_mode) ||
 205            ce_match_stat(ce, &st) ||
 206            memcmp(sha1, ce->sha1, 20))
 207                return 0;
 208        return 1;
 209}
 210
 211static void prep_temp_blob(struct diff_tempfile *temp,
 212                           void *blob,
 213                           unsigned long size,
 214                           unsigned char *sha1,
 215                           int mode)
 216{
 217        int fd;
 218
 219        strcpy(temp->tmp_path, ".diff_XXXXXX");
 220        fd = mkstemp(temp->tmp_path);
 221        if (fd < 0)
 222                die("unable to create temp-file");
 223        if (write(fd, blob, size) != size)
 224                die("unable to write temp-file");
 225        close(fd);
 226        temp->name = temp->tmp_path;
 227        strcpy(temp->hex, sha1_to_hex(sha1));
 228        temp->hex[40] = 0;
 229        sprintf(temp->mode, "%06o", mode);
 230}
 231
 232static void prepare_temp_file(const char *name,
 233                              struct diff_tempfile *temp,
 234                              struct diff_spec *one)
 235{
 236        if (!one->file_valid) {
 237        not_a_valid_file:
 238                /* A '-' entry produces this for file-2, and
 239                 * a '+' entry produces this for file-1.
 240                 */
 241                temp->name = "/dev/null";
 242                strcpy(temp->hex, ".");
 243                strcpy(temp->mode, ".");
 244                return;
 245        }
 246
 247        if (!one->sha1_valid ||
 248            work_tree_matches(name, one->blob_sha1)) {
 249                struct stat st;
 250                temp->name = name;
 251                if (lstat(temp->name, &st) < 0) {
 252                        if (errno == ENOENT)
 253                                goto not_a_valid_file;
 254                        die("stat(%s): %s", temp->name, strerror(errno));
 255                }
 256                if (S_ISLNK(st.st_mode)) {
 257                        int ret;
 258                        char *buf, buf_[1024];
 259                        buf = ((sizeof(buf_) < st.st_size) ?
 260                               xmalloc(st.st_size) : buf_);
 261                        ret = readlink(name, buf, st.st_size);
 262                        if (ret < 0)
 263                                die("readlink(%s)", name);
 264                        prep_temp_blob(temp, buf, st.st_size,
 265                                       (one->sha1_valid ?
 266                                        one->blob_sha1 : null_sha1),
 267                                       (one->sha1_valid ?
 268                                        one->mode : S_IFLNK));
 269                }
 270                else {
 271                        if (!one->sha1_valid)
 272                                strcpy(temp->hex, sha1_to_hex(null_sha1));
 273                        else
 274                                strcpy(temp->hex, sha1_to_hex(one->blob_sha1));
 275                        sprintf(temp->mode, "%06o",
 276                                S_IFREG |ce_permissions(st.st_mode));
 277                }
 278                return;
 279        }
 280        else {
 281                void *blob;
 282                char type[20];
 283                unsigned long size;
 284
 285                blob = read_sha1_file(one->blob_sha1, type, &size);
 286                if (!blob || strcmp(type, "blob"))
 287                        die("unable to read blob object for %s (%s)",
 288                            name, sha1_to_hex(one->blob_sha1));
 289                prep_temp_blob(temp, blob, size, one->blob_sha1, one->mode);
 290                free(blob);
 291        }
 292}
 293
 294static void remove_tempfile(void)
 295{
 296        int i;
 297
 298        for (i = 0; i < 2; i++)
 299                if (diff_temp[i].name == diff_temp[i].tmp_path) {
 300                        unlink(diff_temp[i].name);
 301                        diff_temp[i].name = NULL;
 302                }
 303}
 304
 305static void remove_tempfile_on_signal(int signo)
 306{
 307        remove_tempfile();
 308}
 309
 310static int detect_rename;
 311static int reverse_diff;
 312static int diff_raw_output = -1;
 313static const char **pathspec;
 314static int speccnt;
 315static int minimum_score;
 316
 317static int matches_pathspec(const char *name)
 318{
 319        int i;
 320        int namelen;
 321
 322        if (speccnt == 0)
 323                return 1;
 324
 325        namelen = strlen(name);
 326        for (i = 0; i < speccnt; i++) {
 327                int speclen = strlen(pathspec[i]);
 328                if (! strncmp(pathspec[i], name, speclen) &&
 329                    speclen <= namelen &&
 330                    (name[speclen] == 0 || name[speclen] == '/'))
 331                        return 1;
 332        }
 333        return 0;
 334}
 335
 336/* An external diff command takes:
 337 *
 338 * diff-cmd name infile1 infile1-sha1 infile1-mode \
 339 *               infile2 infile2-sha1 infile2-mode [ rename-to ]
 340 *
 341 */
 342static void run_external_diff(const char *name,
 343                              const char *other,
 344                              struct diff_spec *one,
 345                              struct diff_spec *two,
 346                              int rename_score)
 347{
 348        struct diff_tempfile *temp = diff_temp;
 349        pid_t pid;
 350        int status;
 351        static int atexit_asked = 0;
 352
 353        if (!matches_pathspec(name) && (!other || !matches_pathspec(other)))
 354                return;
 355
 356        if (one && two) {
 357                prepare_temp_file(name, &temp[0], one);
 358                prepare_temp_file(other ? : name, &temp[1], two);
 359                if (! atexit_asked &&
 360                    (temp[0].name == temp[0].tmp_path ||
 361                     temp[1].name == temp[1].tmp_path)) {
 362                        atexit_asked = 1;
 363                        atexit(remove_tempfile);
 364                }
 365                signal(SIGINT, remove_tempfile_on_signal);
 366        }
 367
 368        fflush(NULL);
 369        pid = fork();
 370        if (pid < 0)
 371                die("unable to fork");
 372        if (!pid) {
 373                const char *pgm = external_diff();
 374                if (pgm) {
 375                        if (one && two) {
 376                                const char *exec_arg[9];
 377                                const char **arg = &exec_arg[0];
 378                                *arg++ = pgm;
 379                                *arg++ = name;
 380                                *arg++ = temp[0].name;
 381                                *arg++ = temp[0].hex;
 382                                *arg++ = temp[0].mode;
 383                                *arg++ = temp[1].name;
 384                                *arg++ = temp[1].hex;
 385                                *arg++ = temp[1].mode;
 386                                if (other)
 387                                        *arg++ = other;
 388                                *arg = NULL;
 389                                execvp(pgm, (char *const*) exec_arg);
 390                        }
 391                        else
 392                                execlp(pgm, pgm, name, NULL);
 393                }
 394                /*
 395                 * otherwise we use the built-in one.
 396                 */
 397                if (one && two)
 398                        builtin_diff(name, other ? : name, temp, rename_score);
 399                else
 400                        printf("* Unmerged path %s\n", name);
 401                exit(0);
 402        }
 403        if (waitpid(pid, &status, 0) < 0 ||
 404            !WIFEXITED(status) || WEXITSTATUS(status)) {
 405                /* Earlier we did not check the exit status because
 406                 * diff exits non-zero if files are different, and
 407                 * we are not interested in knowing that.  It was a
 408                 * mistake which made it harder to quit a diff-*
 409                 * session that uses the git-apply-patch-script as
 410                 * the GIT_EXTERNAL_DIFF.  A custom GIT_EXTERNAL_DIFF
 411                 * should also exit non-zero only when it wants to
 412                 * abort the entire diff-* session.
 413                 */
 414                remove_tempfile();
 415                fprintf(stderr, "external diff died, stopping at %s.\n", name);
 416                exit(1);
 417        }
 418        remove_tempfile();
 419}
 420
 421/*
 422 * We do not detect circular renames.  Just hold created and deleted
 423 * entries and later attempt to match them up.  If they do not match,
 424 * then spit them out as deletes or creates as original.
 425 */
 426
 427static struct diff_spec_hold {
 428        struct diff_spec_hold *next;
 429        struct diff_spec it;
 430        unsigned long size;
 431        int flags;
 432#define MATCHED 1
 433#define SHOULD_FREE 2
 434#define SHOULD_MUNMAP 4
 435        void *data;
 436        char path[1];
 437} *createdfile, *deletedfile;
 438
 439static void hold_diff(const char *name,
 440                      struct diff_spec *one,
 441                      struct diff_spec *two)
 442{
 443        struct diff_spec_hold **list, *elem;
 444
 445        if (one->file_valid && two->file_valid)
 446                die("internal error");
 447
 448        if (!detect_rename) {
 449                run_external_diff(name, NULL, one, two, -1);
 450                return;
 451        }
 452        elem = xmalloc(sizeof(*elem) + strlen(name));
 453        strcpy(elem->path, name);
 454        elem->size = 0;
 455        elem->data = NULL;
 456        elem->flags = 0;
 457        if (one->file_valid) {
 458                list = &deletedfile;
 459                elem->it = *one;
 460        }
 461        else {
 462                list = &createdfile;
 463                elem->it = *two;
 464        }
 465        elem->next = *list;
 466        *list = elem;
 467}
 468
 469static int populate_data(struct diff_spec_hold *s)
 470{
 471        char type[20];
 472
 473        if (s->data)
 474                return 0;
 475        if (s->it.sha1_valid) {
 476                s->data = read_sha1_file(s->it.blob_sha1, type, &s->size);
 477                s->flags |= SHOULD_FREE;
 478        }
 479        else {
 480                struct stat st;
 481                int fd;
 482                fd = open(s->path, O_RDONLY);
 483                if (fd < 0)
 484                        return -1;
 485                if (fstat(fd, &st)) {
 486                        close(fd);
 487                        return -1;
 488                }
 489                s->size = st.st_size;
 490                s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0);
 491                close(fd);
 492                if (!s->size)
 493                        s->data = "";
 494                else
 495                        s->flags |= SHOULD_MUNMAP;
 496        }
 497        return 0;
 498}
 499
 500static void free_data(struct diff_spec_hold *s)
 501{
 502        if (s->flags & SHOULD_FREE)
 503                free(s->data);
 504        else if (s->flags & SHOULD_MUNMAP)
 505                munmap(s->data, s->size);
 506        s->flags &= ~(SHOULD_FREE|SHOULD_MUNMAP);
 507        s->data = NULL;
 508}
 509
 510static void flush_remaining_diff(struct diff_spec_hold *elem,
 511                                 int on_created_list)
 512{
 513        static struct diff_spec null_file_spec;
 514
 515        null_file_spec.file_valid = 0;
 516        for ( ; elem ; elem = elem->next) {
 517                free_data(elem);
 518                if (elem->flags & MATCHED)
 519                        continue;
 520                if (on_created_list)
 521                        run_external_diff(elem->path, NULL,
 522                                          &null_file_spec, &elem->it, -1);
 523                else
 524                        run_external_diff(elem->path, NULL,
 525                                          &elem->it, &null_file_spec, -1);
 526        }
 527}
 528
 529static int is_exact_match(struct diff_spec_hold *src,
 530                          struct diff_spec_hold *dst)
 531{
 532        if (src->it.sha1_valid && dst->it.sha1_valid &&
 533            !memcmp(src->it.blob_sha1, dst->it.blob_sha1, 20))
 534                return 1;
 535        if (populate_data(src) || populate_data(dst))
 536                /* this is an error but will be caught downstream */
 537                return 0;
 538        if (src->size == dst->size &&
 539            !memcmp(src->data, dst->data, src->size))
 540                return 1;
 541        return 0;
 542}
 543
 544static int estimate_similarity(struct diff_spec_hold *src, struct diff_spec_hold *dst)
 545{
 546        /* src points at a deleted file and dst points at a created
 547         * file.  They may be quite similar, in which case we want to
 548         * say src is renamed to dst.
 549         *
 550         * Compare them and return how similar they are, representing
 551         * the score as an integer between 0 and 10000, except
 552         * where they match exactly it is considered better than anything
 553         * else.
 554         */
 555        void *delta;
 556        unsigned long delta_size;
 557        int score;
 558
 559        delta_size = ((src->size < dst->size) ?
 560                      (dst->size - src->size) : (src->size - dst->size));
 561
 562        /* We would not consider rename followed by more than
 563         * minimum_score/MAX_SCORE edits; that is, delta_size must be smaller
 564         * than (src->size + dst->size)/2 * minimum_score/MAX_SCORE,
 565         * which means...
 566         */
 567
 568        if ((src->size+dst->size)*minimum_score < delta_size*MAX_SCORE*2)
 569                return 0;
 570
 571        delta = diff_delta(src->data, src->size,
 572                           dst->data, dst->size,
 573                           &delta_size);
 574        free(delta);
 575
 576        /* This "delta" is really xdiff with adler32 and all the
 577         * overheads but it is a quick and dirty approximation.
 578         *
 579         * Now we will give some score to it.  100% edit gets
 580         * 0 points and 0% edit gets MAX_SCORE points.  That is, every
 581         * 1/MAX_SCORE edit gets 1 point penalty.  The amount of penalty is:
 582         *
 583         * (delta_size * 2 / (src->size + dst->size)) * MAX_SCORE
 584         *
 585         */
 586        score = MAX_SCORE-(MAX_SCORE*2*delta_size/(src->size+dst->size));
 587        if (score < 0) return 0;
 588        if (MAX_SCORE < score) return MAX_SCORE;
 589        return score;
 590}
 591
 592struct diff_score {
 593        struct diff_spec_hold *src;
 594        struct diff_spec_hold *dst;
 595        int score;
 596};
 597
 598static int score_compare(const void *a_, const void *b_)
 599{
 600        const struct diff_score *a = a_, *b = b_;
 601        return b->score - a->score;
 602}
 603
 604static void flush_rename_pair(struct diff_spec_hold *src,
 605                              struct diff_spec_hold *dst,
 606                              int rename_score)
 607{
 608        src->flags |= MATCHED;
 609        dst->flags |= MATCHED;
 610        free_data(src);
 611        free_data(dst);
 612        run_external_diff(src->path, dst->path,
 613                          &src->it, &dst->it, rename_score);
 614}
 615
 616static void free_held_diff(struct diff_spec_hold *list)
 617{
 618        struct diff_spec_hold *h;
 619        for (h = list; list; list = h) {
 620                h = list->next;
 621                free_data(list);
 622                free(list);
 623        }
 624}
 625
 626void diff_flush(void)
 627{
 628        int num_create, num_delete, c, d;
 629        struct diff_spec_hold *elem, *src, *dst;
 630        struct diff_score *mx;
 631
 632        /* We really want to cull the candidates list early
 633         * with cheap tests in order to avoid doing deltas.
 634         *
 635         * With the current callers, we should not have already
 636         * matched entries at this point, but it is nonetheless
 637         * checked for sanity.
 638         */
 639        for (dst = createdfile; dst; dst = dst->next) {
 640                if (dst->flags & MATCHED)
 641                        continue;
 642                for (src = deletedfile; src; src = src->next) {
 643                        if (src->flags & MATCHED)
 644                                continue;
 645                        if (! is_exact_match(src, dst))
 646                                continue;
 647                        flush_rename_pair(src, dst, MAX_SCORE);
 648                        break;
 649                }
 650        }
 651
 652        /* Count surviving candidates */
 653        for (num_create = 0, elem = createdfile; elem; elem = elem->next)
 654                if (!(elem->flags & MATCHED))
 655                        num_create++;
 656
 657        for (num_delete = 0, elem = deletedfile; elem; elem = elem->next)
 658                if (!(elem->flags & MATCHED))
 659                        num_delete++;
 660
 661        if (num_create == 0 ||  num_delete == 0)
 662                goto exit_path;
 663
 664        mx = xmalloc(sizeof(*mx) * num_create * num_delete);
 665        for (c = 0, dst = createdfile; dst; dst = dst->next) {
 666                int base = c * num_delete;
 667                if (dst->flags & MATCHED)
 668                        continue;
 669                for (d = 0, src = deletedfile; src; src = src->next) {
 670                        struct diff_score *m = &mx[base+d];
 671                        if (src->flags & MATCHED)
 672                                continue;
 673                        m->src = src;
 674                        m->dst = dst;
 675                        m->score = estimate_similarity(src, dst);
 676                        d++;
 677                }
 678                c++;
 679        }
 680        qsort(mx, num_create*num_delete, sizeof(*mx), score_compare);
 681
 682#if 0
 683        for (c = 0; c < num_create * num_delete; c++) {
 684                src = mx[c].src;
 685                dst = mx[c].dst;
 686                if ((src->flags & MATCHED) || (dst->flags & MATCHED))
 687                        continue;
 688                fprintf(stderr,
 689                        "**score ** %d %s %s\n",
 690                        mx[c].score, src->path, dst->path);
 691        }
 692#endif
 693
 694        for (c = 0; c < num_create * num_delete; c++) {
 695                src = mx[c].src;
 696                dst = mx[c].dst;
 697                if ((src->flags & MATCHED) || (dst->flags & MATCHED))
 698                        continue;
 699                if (mx[c].score < minimum_score)
 700                        break;
 701                flush_rename_pair(src, dst, mx[c].score);
 702        }
 703        free(mx);
 704
 705 exit_path:
 706        flush_remaining_diff(createdfile, 1);
 707        flush_remaining_diff(deletedfile, 0);
 708        free_held_diff(createdfile);
 709        free_held_diff(deletedfile);
 710        createdfile = deletedfile = NULL;
 711}
 712
 713int diff_scoreopt_parse(const char *opt)
 714{
 715        int diglen, num, scale, i;
 716        if (opt[0] != '-' || opt[1] != 'M')
 717                return -1; /* that is not -M option */
 718        diglen = strspn(opt+2, "0123456789");
 719        if (diglen == 0 || strlen(opt+2) != diglen)
 720                return 0; /* use default */
 721        sscanf(opt+2, "%d", &num);
 722        for (i = 0, scale = 1; i < diglen; i++)
 723                scale *= 10;
 724
 725        /* user says num divided by scale and we say internally that
 726         * is MAX_SCORE * num / scale.
 727         */
 728        return MAX_SCORE * num / scale;
 729}
 730
 731void diff_setup(int detect_rename_, int minimum_score_, int reverse_diff_,
 732                int diff_raw_output_,
 733                const char **pathspec_, int speccnt_)
 734{
 735        free_held_diff(createdfile);
 736        free_held_diff(deletedfile);
 737        createdfile = deletedfile = NULL;
 738
 739        detect_rename = detect_rename_;
 740        reverse_diff = reverse_diff_;
 741        pathspec = pathspec_;
 742        diff_raw_output = diff_raw_output_;
 743        speccnt = speccnt_;
 744        minimum_score = minimum_score_ ? : DEFAULT_MINIMUM_SCORE;
 745}
 746
 747static const char *git_object_type(unsigned mode)
 748{
 749        return S_ISDIR(mode) ? "tree" : "blob";
 750}
 751
 752void diff_addremove(int addremove, unsigned mode,
 753                    const unsigned char *sha1,
 754                    const char *base, const char *path)
 755{
 756        char concatpath[PATH_MAX];
 757        struct diff_spec spec[2], *one, *two;
 758
 759        if (reverse_diff)
 760                addremove = (addremove == '+' ? '-' : '+');
 761
 762        if (0 <= diff_raw_output) {
 763                if (!path)
 764                        path = "";
 765                printf("%c%06o %s %s %s%s%c",
 766                       addremove,
 767                       mode,
 768                       git_object_type(mode), sha1_to_hex(sha1),
 769                       base, path, diff_raw_output);
 770                return;
 771        }
 772        if (S_ISDIR(mode))
 773                return;
 774
 775        memcpy(spec[0].blob_sha1, sha1, 20);
 776        spec[0].mode = mode;
 777        spec[0].sha1_valid = !!memcmp(sha1, null_sha1, 20);
 778        spec[0].file_valid = 1;
 779        spec[1].file_valid = 0;
 780
 781        if (addremove == '+') {
 782                one = spec + 1; two = spec;
 783        } else {
 784                one = spec; two = one + 1;
 785        }
 786
 787        if (path) {
 788                strcpy(concatpath, base);
 789                strcat(concatpath, path);
 790        }
 791        hold_diff(path ? concatpath : base, one, two);
 792}
 793
 794void diff_change(unsigned old_mode, unsigned new_mode,
 795                 const unsigned char *old_sha1,
 796                 const unsigned char *new_sha1,
 797                 const char *base, const char *path) {
 798        char concatpath[PATH_MAX];
 799        struct diff_spec spec[2];
 800
 801        if (reverse_diff) {
 802                unsigned tmp;
 803                const unsigned char *tmp_c;
 804                tmp = old_mode; old_mode = new_mode; new_mode = tmp;
 805                tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c;
 806        }
 807
 808        if (0 <= diff_raw_output) {
 809                char old_hex[41];
 810                strcpy(old_hex, sha1_to_hex(old_sha1));
 811
 812                if (!path)
 813                        path = "";
 814                printf("*%06o->%06o %s %s->%s %s%s%c",
 815                       old_mode, new_mode,
 816                       git_object_type(new_mode),
 817                       old_hex, sha1_to_hex(new_sha1),
 818                       base, path, diff_raw_output);
 819                return;
 820        }
 821        if (S_ISDIR(new_mode))
 822                return;
 823
 824        if (path) {
 825                strcpy(concatpath, base);
 826                strcat(concatpath, path);
 827        }
 828
 829        memcpy(spec[0].blob_sha1, old_sha1, 20);
 830        spec[0].mode = old_mode;
 831        memcpy(spec[1].blob_sha1, new_sha1, 20);
 832        spec[1].mode = new_mode;
 833        spec[0].sha1_valid = !!memcmp(old_sha1, null_sha1, 20);
 834        spec[1].sha1_valid = !!memcmp(new_sha1, null_sha1, 20);
 835        spec[1].file_valid = spec[0].file_valid = 1;
 836
 837        /* We do not look at changed files as candidate for
 838         * rename detection ever.
 839         */
 840        run_external_diff(path ? concatpath : base, NULL,
 841                          &spec[0], &spec[1], -1);
 842}
 843
 844void diff_unmerge(const char *path)
 845{
 846        if (0 <= diff_raw_output) {
 847                printf("U %s%c", path, diff_raw_output);
 848                return;
 849        }
 850        run_external_diff(path, NULL, NULL, NULL, -1);
 851}