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