diff-tree.con commit [PATCH] Update git-diff-cache documentation. (b1c006d)
   1#include <ctype.h>
   2#include "cache.h"
   3#include "diff.h"
   4
   5static int silent = 0;
   6static int show_root_diff = 0;
   7static int verbose_header = 0;
   8static int ignore_merges = 1;
   9static int recursive = 0;
  10static int read_stdin = 0;
  11static int diff_output_format = DIFF_FORMAT_HUMAN;
  12static int detect_rename = 0;
  13static int reverse_diff = 0;
  14static int diff_score_opt = 0;
  15static const char *pickaxe = NULL;
  16static const char *header = NULL;
  17static const char *header_prefix = "";
  18
  19// What paths are we interested in?
  20static int nr_paths = 0;
  21static const char **paths = NULL;
  22static int *pathlens = NULL;
  23
  24static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
  25
  26static void update_tree_entry(void **bufp, unsigned long *sizep)
  27{
  28        void *buf = *bufp;
  29        unsigned long size = *sizep;
  30        int len = strlen(buf) + 1 + 20;
  31
  32        if (size < len)
  33                die("corrupt tree file");
  34        *bufp = buf + len;
  35        *sizep = size - len;
  36}
  37
  38static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
  39{
  40        int len = strlen(tree)+1;
  41        const unsigned char *sha1 = tree + len;
  42        const char *path = strchr(tree, ' ');
  43
  44        if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
  45                die("corrupt tree file");
  46        *pathp = path+1;
  47        return sha1;
  48}
  49
  50static char *malloc_base(const char *base, const char *path, int pathlen)
  51{
  52        int baselen = strlen(base);
  53        char *newbase = xmalloc(baselen + pathlen + 2);
  54        memcpy(newbase, base, baselen);
  55        memcpy(newbase + baselen, path, pathlen);
  56        memcpy(newbase + baselen + pathlen, "/", 2);
  57        return newbase;
  58}
  59
  60static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
  61static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
  62
  63/* A file entry went away or appeared */
  64static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
  65{
  66        unsigned mode;
  67        const char *path;
  68        const unsigned char *sha1 = extract(tree, size, &path, &mode);
  69
  70        if (silent)
  71                return;
  72
  73        if (recursive && S_ISDIR(mode)) {
  74                char type[20];
  75                unsigned long size;
  76                char *newbase = malloc_base(base, path, strlen(path));
  77                void *tree;
  78
  79                tree = read_sha1_file(sha1, type, &size);
  80                if (!tree || strcmp(type, "tree"))
  81                        die("corrupt tree sha %s", sha1_to_hex(sha1));
  82
  83                show_tree(prefix, tree, size, newbase);
  84
  85                free(tree);
  86                free(newbase);
  87                return;
  88        }
  89
  90        diff_addremove(prefix[0], mode, sha1, base, path);
  91}
  92
  93static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
  94{
  95        unsigned mode1, mode2;
  96        const char *path1, *path2;
  97        const unsigned char *sha1, *sha2;
  98        int cmp, pathlen1, pathlen2;
  99
 100        sha1 = extract(tree1, size1, &path1, &mode1);
 101        sha2 = extract(tree2, size2, &path2, &mode2);
 102
 103        pathlen1 = strlen(path1);
 104        pathlen2 = strlen(path2);
 105        cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
 106        if (cmp < 0) {
 107                show_file("-", tree1, size1, base);
 108                return -1;
 109        }
 110        if (cmp > 0) {
 111                show_file("+", tree2, size2, base);
 112                return 1;
 113        }
 114        if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
 115                return 0;
 116
 117        /*
 118         * If the filemode has changed to/from a directory from/to a regular
 119         * file, we need to consider it a remove and an add.
 120         */
 121        if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
 122                show_file("-", tree1, size1, base);
 123                show_file("+", tree2, size2, base);
 124                return 0;
 125        }
 126
 127        if (recursive && S_ISDIR(mode1)) {
 128                int retval;
 129                char *newbase = malloc_base(base, path1, pathlen1);
 130                retval = diff_tree_sha1(sha1, sha2, newbase);
 131                free(newbase);
 132                return retval;
 133        }
 134
 135        if (silent)
 136                return 0;
 137
 138        diff_change(mode1, mode2, sha1, sha2, base, path1);
 139        return 0;
 140}
 141
 142static int interesting(void *tree, unsigned long size, const char *base)
 143{
 144        const char *path;
 145        unsigned mode;
 146        int i;
 147        int baselen, pathlen;
 148
 149        if (!nr_paths)
 150                return 1;
 151
 152        (void)extract(tree, size, &path, &mode);
 153
 154        pathlen = strlen(path);
 155        baselen = strlen(base);
 156
 157        for (i=0; i < nr_paths; i++) {
 158                const char *match = paths[i];
 159                int matchlen = pathlens[i];
 160
 161                if (baselen >= matchlen) {
 162                        /* If it doesn't match, move along... */
 163                        if (strncmp(base, match, matchlen))
 164                                continue;
 165
 166                        /* The base is a subdirectory of a path which was specified. */
 167                        return 1;
 168                }
 169
 170                /* Does the base match? */
 171                if (strncmp(base, match, baselen))
 172                        continue;
 173
 174                match += baselen;
 175                matchlen -= baselen;
 176
 177                if (pathlen > matchlen)
 178                        continue;
 179
 180                if (matchlen > pathlen) {
 181                        if (match[pathlen] != '/')
 182                                continue;
 183                        if (!S_ISDIR(mode))
 184                                continue;
 185                }
 186
 187                if (strncmp(path, match, pathlen))
 188                        continue;
 189
 190                return 1;
 191        }
 192        return 0; /* No matches */
 193}
 194
 195/* A whole sub-tree went away or appeared */
 196static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
 197{
 198        while (size) {
 199                if (interesting(tree, size, base))
 200                        show_file(prefix, tree, size, base);
 201                update_tree_entry(&tree, &size);
 202        }
 203}
 204
 205static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
 206{
 207        while (size1 | size2) {
 208                if (nr_paths && size1 && !interesting(tree1, size1, base)) {
 209                        update_tree_entry(&tree1, &size1);
 210                        continue;
 211                }
 212                if (nr_paths && size2 && !interesting(tree2, size2, base)) {
 213                        update_tree_entry(&tree2, &size2);
 214                        continue;
 215                }
 216                if (!size1) {
 217                        show_file("+", tree2, size2, base);
 218                        update_tree_entry(&tree2, &size2);
 219                        continue;
 220                }
 221                if (!size2) {
 222                        show_file("-", tree1, size1, base);
 223                        update_tree_entry(&tree1, &size1);
 224                        continue;
 225                }
 226                switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
 227                case -1:
 228                        update_tree_entry(&tree1, &size1);
 229                        continue;
 230                case 0:
 231                        update_tree_entry(&tree1, &size1);
 232                        /* Fallthrough */
 233                case 1:
 234                        update_tree_entry(&tree2, &size2);
 235                        continue;
 236                }
 237                die("git-diff-tree: internal error");
 238        }
 239        return 0;
 240}
 241
 242static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
 243{
 244        void *tree1, *tree2;
 245        unsigned long size1, size2;
 246        int retval;
 247
 248        tree1 = read_object_with_reference(old, "tree", &size1, NULL);
 249        if (!tree1)
 250                die("unable to read source tree (%s)", sha1_to_hex(old));
 251        tree2 = read_object_with_reference(new, "tree", &size2, NULL);
 252        if (!tree2)
 253                die("unable to read destination tree (%s)", sha1_to_hex(new));
 254        retval = diff_tree(tree1, size1, tree2, size2, base);
 255        free(tree1);
 256        free(tree2);
 257        return retval;
 258}
 259
 260static void call_diff_setup(void)
 261{
 262        diff_setup(reverse_diff);
 263}
 264
 265static int call_diff_flush(void)
 266{
 267        if (detect_rename)
 268                diffcore_rename(detect_rename, diff_score_opt);
 269        if (pickaxe)
 270                diffcore_pickaxe(pickaxe);
 271        if (diff_queue_is_empty()) {
 272                diff_flush(DIFF_FORMAT_NO_OUTPUT, 0);
 273                return 0;
 274        }
 275        if (nr_paths)
 276                diffcore_pathspec(paths);
 277        if (header) {
 278                if (diff_output_format == DIFF_FORMAT_MACHINE) {
 279                        const char *ep, *cp;
 280                        for (cp = header; *cp; cp = ep) {
 281                                ep = strchr(cp, '\n');
 282                                if (ep == 0) ep = cp + strlen(cp);
 283                                printf("%.*s%c", ep-cp, cp, 0);
 284                                if (*ep) ep++;
 285                        }
 286                }
 287                else {
 288                        printf("%s", header);
 289                }
 290                header = NULL;
 291        }
 292        diff_flush(diff_output_format, 1);
 293        return 1;
 294}
 295
 296static int diff_tree_sha1_top(const unsigned char *old,
 297                              const unsigned char *new, const char *base)
 298{
 299        int ret;
 300
 301        call_diff_setup();
 302        ret = diff_tree_sha1(old, new, base);
 303        call_diff_flush();
 304        return ret;
 305}
 306
 307static int diff_root_tree(const unsigned char *new, const char *base)
 308{
 309        int retval;
 310        void *tree;
 311        unsigned long size;
 312
 313        call_diff_setup();
 314        tree = read_object_with_reference(new, "tree", &size, NULL);
 315        if (!tree)
 316                die("unable to read root tree (%s)", sha1_to_hex(new));
 317        retval = diff_tree("", 0, tree, size, base);
 318        free(tree);
 319        call_diff_flush();
 320        return retval;
 321}
 322
 323static int get_one_line(const char *msg, unsigned long len)
 324{
 325        int ret = 0;
 326
 327        while (len--) {
 328                ret++;
 329                if (*msg++ == '\n')
 330                        break;
 331        }
 332        return ret;
 333}
 334
 335static int add_author_info(char *buf, const char *line, int len)
 336{
 337        char *date;
 338        unsigned int namelen;
 339        unsigned long time;
 340        int tz;
 341
 342        line += strlen("author ");
 343        date = strchr(line, '>');
 344        if (!date)
 345                return 0;
 346        namelen = ++date - line;
 347        time = strtoul(date, &date, 10);
 348        tz = strtol(date, NULL, 10);
 349
 350        return sprintf(buf, "Author: %.*s\nDate:   %s\n",
 351                namelen, line,
 352                show_date(time, tz));
 353}
 354
 355static char *generate_header(const char *commit, const char *parent, const char *msg, unsigned long len)
 356{
 357        static char this_header[16384];
 358        int offset;
 359
 360        offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
 361        if (verbose_header) {
 362                int hdr = 1;
 363
 364                for (;;) {
 365                        const char *line = msg;
 366                        int linelen = get_one_line(msg, len);
 367
 368                        if (!linelen)
 369                                break;
 370
 371                        /*
 372                         * We want some slop for indentation and a possible
 373                         * final "...". Thus the "+ 20".
 374                         */
 375                        if (offset + linelen + 20 > sizeof(this_header)) {
 376                                memcpy(this_header + offset, "    ...\n", 8);
 377                                offset += 8;
 378                                break;
 379                        }
 380
 381                        msg += linelen;
 382                        len -= linelen;
 383                        if (linelen == 1)
 384                                hdr = 0;
 385                        if (hdr) {
 386                                if (!memcmp(line, "author ", 7))
 387                                        offset += add_author_info(this_header + offset, line, linelen);
 388                                continue;
 389                        }
 390                        memset(this_header + offset, ' ', 4);
 391                        memcpy(this_header + offset + 4, line, linelen);
 392                        offset += linelen + 4;
 393                }
 394                /* Make sure there is an EOLN */
 395                if (this_header[offset-1] != '\n')
 396                        this_header[offset++] = '\n';
 397                /* Add _another_ EOLN if we are doing diff output */
 398                if (!silent)
 399                        this_header[offset++] = '\n';
 400                this_header[offset] = 0;
 401        }
 402
 403        return this_header;
 404}
 405
 406static int diff_tree_commit(const unsigned char *commit, const char *name)
 407{
 408        unsigned long size, offset;
 409        char *buf = read_object_with_reference(commit, "commit", &size, NULL);
 410
 411        if (!buf)
 412                return -1;
 413
 414        if (!name) {
 415                static char commit_name[60];
 416                strcpy(commit_name, sha1_to_hex(commit));
 417                name = commit_name;
 418        }
 419
 420        /* Root commit? */
 421        if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
 422                header = generate_header(name, "root", buf, size);
 423                diff_root_tree(commit, "");
 424        }
 425
 426        /* More than one parent? */
 427        if (ignore_merges) {
 428                if (!memcmp(buf + 46 + 48, "parent ", 7))
 429                        return 0;
 430        }
 431
 432        offset = 46;
 433        while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
 434                unsigned char parent[20];
 435                if (get_sha1_hex(buf + offset + 7, parent))
 436                        return -1;
 437                header = generate_header(name, sha1_to_hex(parent), buf, size);
 438                diff_tree_sha1_top(parent, commit, "");
 439                if (!header && verbose_header) {
 440                        header_prefix = "\ndiff-tree ";
 441                        /*
 442                         * Don't print multiple merge entries if we
 443                         * don't print the diffs.
 444                         */
 445                        if (silent)
 446                                break;
 447                }
 448                offset += 48;
 449        }
 450        return 0;
 451}
 452
 453static int diff_tree_stdin(char *line)
 454{
 455        int len = strlen(line);
 456        unsigned char commit[20], parent[20];
 457        static char this_header[1000];
 458
 459        if (!len || line[len-1] != '\n')
 460                return -1;
 461        line[len-1] = 0;
 462        if (get_sha1_hex(line, commit))
 463                return -1;
 464        if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
 465                line[40] = 0;
 466                line[81] = 0;
 467                sprintf(this_header, "%s (from %s)\n", line, line+41);
 468                header = this_header;
 469                return diff_tree_sha1_top(parent, commit, "");
 470        }
 471        line[40] = 0;
 472        return diff_tree_commit(commit, line);
 473}
 474
 475static char *diff_tree_usage =
 476"git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-C] [-R] [-S<string>] [-m] [-s] [-v] <tree-ish> <tree-ish>";
 477
 478int main(int argc, const char **argv)
 479{
 480        int nr_sha1;
 481        char line[1000];
 482        unsigned char sha1[2][20];
 483
 484        nr_sha1 = 0;
 485        for (;;) {
 486                const char *arg;
 487
 488                argv++;
 489                argc--;
 490                arg = *argv;
 491                if (!arg)
 492                        break;
 493
 494                if (*arg != '-') {
 495                        if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
 496                                nr_sha1++;
 497                                continue;
 498                        }
 499                        break;
 500                }
 501
 502                if (!strcmp(arg, "--")) {
 503                        argv++;
 504                        argc--;
 505                        break;
 506                }
 507                if (!strcmp(arg, "-r")) {
 508                        recursive = 1;
 509                        continue;
 510                }
 511                if (!strcmp(arg, "-R")) {
 512                        reverse_diff = 1;
 513                        continue;
 514                }
 515                if (!strcmp(arg, "-p")) {
 516                        diff_output_format = DIFF_FORMAT_PATCH;
 517                        recursive = 1;
 518                        continue;
 519                }
 520                if (!strncmp(arg, "-S", 2)) {
 521                        pickaxe = arg + 2;
 522                        continue;
 523                }
 524                if (!strncmp(arg, "-M", 2)) {
 525                        detect_rename = DIFF_DETECT_RENAME;
 526                        diff_score_opt = diff_scoreopt_parse(arg);
 527                        continue;
 528                }
 529                if (!strncmp(arg, "-C", 2)) {
 530                        detect_rename = DIFF_DETECT_COPY;
 531                        diff_score_opt = diff_scoreopt_parse(arg);
 532                        continue;
 533                }
 534                if (!strcmp(arg, "-z")) {
 535                        diff_output_format = DIFF_FORMAT_MACHINE;
 536                        continue;
 537                }
 538                if (!strcmp(arg, "-m")) {
 539                        ignore_merges = 0;
 540                        continue;
 541                }
 542                if (!strcmp(arg, "-s")) {
 543                        silent = 1;
 544                        continue;
 545                }
 546                if (!strcmp(arg, "-v")) {
 547                        verbose_header = 1;
 548                        header_prefix = "diff-tree ";
 549                        continue;
 550                }
 551                if (!strcmp(arg, "--stdin")) {
 552                        read_stdin = 1;
 553                        continue;
 554                }
 555                if (!strcmp(arg, "--root")) {
 556                        show_root_diff = 1;
 557                        continue;
 558                }
 559                usage(diff_tree_usage);
 560        }
 561
 562        if (argc > 0) {
 563                int i;
 564
 565                paths = argv;
 566                nr_paths = argc;
 567                pathlens = xmalloc(nr_paths * sizeof(int));
 568                for (i=0; i<nr_paths; i++)
 569                        pathlens[i] = strlen(paths[i]);
 570        }
 571
 572        switch (nr_sha1) {
 573        case 0:
 574                if (!read_stdin)
 575                        usage(diff_tree_usage);
 576                break;
 577        case 1:
 578                diff_tree_commit(sha1[0], NULL);
 579                break;
 580        case 2:
 581                diff_tree_sha1_top(sha1[0], sha1[1], "");
 582                break;
 583        }
 584
 585        if (!read_stdin)
 586                return 0;
 587
 588        while (fgets(line, sizeof(line), stdin))
 589                diff_tree_stdin(line);
 590
 591        return 0;
 592}