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