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