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