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