9d94e0cc58be314017d570f75fc3169197f46a9e
   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 const char *header = NULL;
  15static const char *header_prefix = "";
  16
  17// What paths are we interested in?
  18static int nr_paths = 0;
  19static char **paths = NULL;
  20static int *pathlens = NULL;
  21
  22static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
  23
  24static void update_tree_entry(void **bufp, unsigned long *sizep)
  25{
  26        void *buf = *bufp;
  27        unsigned long size = *sizep;
  28        int len = strlen(buf) + 1 + 20;
  29
  30        if (size < len)
  31                die("corrupt tree file");
  32        *bufp = buf + len;
  33        *sizep = size - len;
  34}
  35
  36static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
  37{
  38        int len = strlen(tree)+1;
  39        const unsigned char *sha1 = tree + len;
  40        const char *path = strchr(tree, ' ');
  41
  42        if (!path || size < len + 20 || sscanf(tree, "%o", modep) != 1)
  43                die("corrupt tree file");
  44        *pathp = path+1;
  45        return sha1;
  46}
  47
  48static char *malloc_base(const char *base, const char *path, int pathlen)
  49{
  50        int baselen = strlen(base);
  51        char *newbase = xmalloc(baselen + pathlen + 2);
  52        memcpy(newbase, base, baselen);
  53        memcpy(newbase + baselen, path, pathlen);
  54        memcpy(newbase + baselen + pathlen, "/", 2);
  55        return newbase;
  56}
  57
  58static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
  59static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
  60
  61/* A file entry went away or appeared */
  62static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
  63{
  64        unsigned mode;
  65        const char *path;
  66        const unsigned char *sha1 = extract(tree, size, &path, &mode);
  67
  68        if (header) {
  69                printf("%s", header);
  70                header = NULL;
  71        }
  72
  73        if (silent)
  74                return;
  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        if (generate_patch) {
  94                if (!S_ISDIR(mode))
  95                        diff_addremove(prefix[0], mode, sha1, base, path);
  96        }
  97        else
  98                printf("%s%06o\t%s\t%s\t%s%s%c", prefix, mode,
  99                       S_ISDIR(mode) ? "tree" : "blob",
 100                       sha1_to_hex(sha1), base, path,
 101                       line_termination);
 102}
 103
 104static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
 105{
 106        unsigned mode1, mode2;
 107        const char *path1, *path2;
 108        const unsigned char *sha1, *sha2;
 109        int cmp, pathlen1, pathlen2;
 110        char old_sha1_hex[50];
 111
 112        sha1 = extract(tree1, size1, &path1, &mode1);
 113        sha2 = extract(tree2, size2, &path2, &mode2);
 114
 115        pathlen1 = strlen(path1);
 116        pathlen2 = strlen(path2);
 117        cmp = cache_name_compare(path1, pathlen1, path2, pathlen2);
 118        if (cmp < 0) {
 119                show_file("-", tree1, size1, base);
 120                return -1;
 121        }
 122        if (cmp > 0) {
 123                show_file("+", tree2, size2, base);
 124                return 1;
 125        }
 126        if (!memcmp(sha1, sha2, 20) && mode1 == mode2)
 127                return 0;
 128
 129        /*
 130         * If the filemode has changed to/from a directory from/to a regular
 131         * file, we need to consider it a remove and an add.
 132         */
 133        if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
 134                show_file("-", tree1, size1, base);
 135                show_file("+", tree2, size2, base);
 136                return 0;
 137        }
 138
 139        if (recursive && S_ISDIR(mode1)) {
 140                int retval;
 141                char *newbase = malloc_base(base, path1, pathlen1);
 142                retval = diff_tree_sha1(sha1, sha2, newbase);
 143                free(newbase);
 144                return retval;
 145        }
 146
 147        if (header) {
 148                printf("%s", header);
 149                header = NULL;
 150        }
 151        if (silent)
 152                return 0;
 153
 154        if (generate_patch) {
 155                if (!S_ISDIR(mode1))
 156                        diff_change(mode1, mode2, sha1, sha2, base, path1);
 157        }
 158        else {
 159                strcpy(old_sha1_hex, sha1_to_hex(sha1));
 160                printf("*%06o->%06o\t%s\t%s->%s\t%s%s%c", mode1, mode2,
 161                       S_ISDIR(mode1) ? "tree" : "blob",
 162                       old_sha1_hex, sha1_to_hex(sha2), base, path1,
 163                       line_termination);
 164        }
 165        return 0;
 166}
 167
 168static int interesting(void *tree, unsigned long size, const char *base)
 169{
 170        const char *path;
 171        unsigned mode;
 172        int i;
 173        int baselen, pathlen;
 174
 175        if (!nr_paths)
 176                return 1;
 177
 178        (void)extract(tree, size, &path, &mode);
 179
 180        pathlen = strlen(path);
 181        baselen = strlen(base);
 182
 183        for (i=0; i < nr_paths; i++) {
 184                const char *match = paths[i];
 185                int matchlen = pathlens[i];
 186
 187                if (baselen >= matchlen) {
 188                        /* If it doesn't match, move along... */
 189                        if (strncmp(base, match, matchlen))
 190                                continue;
 191
 192                        /* The base is a subdirectory of a path which was specified. */
 193                        return 1;
 194                }
 195
 196                /* Does the base match? */
 197                if (strncmp(base, match, baselen))
 198                        continue;
 199
 200                match += baselen;
 201                matchlen -= baselen;
 202
 203                if (pathlen > matchlen)
 204                        continue;
 205
 206                if (matchlen > pathlen) {
 207                        if (match[pathlen] != '/')
 208                                continue;
 209                        if (!S_ISDIR(mode))
 210                                continue;
 211                }
 212
 213                if (strncmp(path, match, pathlen))
 214                        continue;
 215
 216                return 1;
 217        }
 218        return 0; /* No matches */
 219}
 220
 221/* A whole sub-tree went away or appeared */
 222static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
 223{
 224        while (size) {
 225                if (interesting(tree, size, base))
 226                        show_file(prefix, tree, size, base);
 227                update_tree_entry(&tree, &size);
 228        }
 229}
 230
 231static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
 232{
 233        while (size1 | size2) {
 234                if (nr_paths && size1 && !interesting(tree1, size1, base)) {
 235                        update_tree_entry(&tree1, &size1);
 236                        continue;
 237                }
 238                if (nr_paths && size2 && !interesting(tree2, size2, base)) {
 239                        update_tree_entry(&tree2, &size2);
 240                        continue;
 241                }
 242                if (!size1) {
 243                        show_file("+", tree2, size2, base);
 244                        update_tree_entry(&tree2, &size2);
 245                        continue;
 246                }
 247                if (!size2) {
 248                        show_file("-", tree1, size1, base);
 249                        update_tree_entry(&tree1, &size1);
 250                        continue;
 251                }
 252                switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
 253                case -1:
 254                        update_tree_entry(&tree1, &size1);
 255                        continue;
 256                case 0:
 257                        update_tree_entry(&tree1, &size1);
 258                        /* Fallthrough */
 259                case 1:
 260                        update_tree_entry(&tree2, &size2);
 261                        continue;
 262                }
 263                die("git-diff-tree: internal error");
 264        }
 265        return 0;
 266}
 267
 268static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
 269{
 270        void *tree1, *tree2;
 271        unsigned long size1, size2;
 272        int retval;
 273
 274        tree1 = read_object_with_reference(old, "tree", &size1, 0);
 275        if (!tree1)
 276                die("unable to read source tree (%s)", sha1_to_hex(old));
 277        tree2 = read_object_with_reference(new, "tree", &size2, 0);
 278        if (!tree2)
 279                die("unable to read destination tree (%s)", sha1_to_hex(new));
 280        retval = diff_tree(tree1, size1, tree2, size2, base);
 281        free(tree1);
 282        free(tree2);
 283        return retval;
 284}
 285
 286static int diff_tree_sha1_top(const unsigned char *old,
 287                              const unsigned char *new, const char *base)
 288{
 289        int ret;
 290        if (generate_patch)
 291                diff_setup(detect_rename, 0, 0, 0, 0);
 292        ret = diff_tree_sha1(old, new, base);
 293        if (generate_patch)
 294                diff_flush();
 295        return ret;
 296}
 297
 298static int diff_root_tree(const unsigned char *new, const char *base)
 299{
 300        int retval;
 301        void *tree;
 302        unsigned long size;
 303
 304        if (generate_patch)
 305                diff_setup(detect_rename, 0, 0, 0, 0);
 306        tree = read_object_with_reference(new, "tree", &size, 0);
 307        if (!tree)
 308                die("unable to read root tree (%s)", sha1_to_hex(new));
 309        retval = diff_tree("", 0, tree, size, base);
 310        free(tree);
 311        if (generate_patch)
 312                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[1000];
 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                        if (offset + linelen + 10 > sizeof(this_header))
 364                                break;
 365
 366                        msg += linelen;
 367                        len -= linelen;
 368                        if (linelen == 1)
 369                                hdr = 0;
 370                        if (hdr) {
 371                                if (!memcmp(line, "author ", 7))
 372                                        offset += add_author_info(this_header + offset, line, linelen);
 373                                continue;
 374                        }
 375                        memset(this_header + offset, ' ', 4);
 376                        memcpy(this_header + offset + 4, line, linelen);
 377                        offset += linelen + 4;
 378                }
 379                this_header[offset++] = '\n';
 380                this_header[offset] = 0;
 381        }
 382
 383        return this_header;
 384}
 385
 386static int diff_tree_commit(const unsigned char *commit, const char *name)
 387{
 388        unsigned long size, offset;
 389        char *buf = read_object_with_reference(commit, "commit", &size, NULL);
 390
 391        if (!buf)
 392                return -1;
 393
 394        if (!name) {
 395                static char commit_name[60];
 396                strcpy(commit_name, sha1_to_hex(commit));
 397                name = commit_name;
 398        }
 399
 400        /* Root commit? */
 401        if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
 402                header = generate_header(name, "root", buf, size);
 403                diff_root_tree(commit, "");
 404        }
 405
 406        /* More than one parent? */
 407        if (ignore_merges) {
 408                if (!memcmp(buf + 46 + 48, "parent ", 7))
 409                        return 0;
 410        }
 411
 412        offset = 46;
 413        while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
 414                unsigned char parent[20];
 415                if (get_sha1_hex(buf + offset + 7, parent))
 416                        return -1;
 417                header = generate_header(name, sha1_to_hex(parent), buf, size);
 418                diff_tree_sha1_top(parent, commit, "");
 419                if (!header && verbose_header)
 420                        header_prefix = "\ndiff-tree ";
 421                offset += 48;
 422        }
 423        return 0;
 424}
 425
 426static int diff_tree_stdin(char *line)
 427{
 428        int len = strlen(line);
 429        unsigned char commit[20], parent[20];
 430        static char this_header[1000];
 431
 432        if (!len || line[len-1] != '\n')
 433                return -1;
 434        line[len-1] = 0;
 435        if (get_sha1_hex(line, commit))
 436                return -1;
 437        if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
 438                line[40] = 0;
 439                line[81] = 0;
 440                sprintf(this_header, "%s (from %s)\n", line, line+41);
 441                header = this_header;
 442                return diff_tree_sha1_top(parent, commit, "");
 443        }
 444        line[40] = 0;
 445        return diff_tree_commit(commit, line);
 446}
 447
 448static char *diff_tree_usage =
 449"git-diff-tree [-p] [-r] [-z] [--stdin] [-M] [-m] [-s] [-v] <tree-ish> <tree-ish>";
 450
 451int main(int argc, char **argv)
 452{
 453        int nr_sha1;
 454        char line[1000];
 455        unsigned char sha1[2][20];
 456
 457        nr_sha1 = 0;
 458        for (;;) {
 459                char *arg;
 460
 461                argv++;
 462                argc--;
 463                arg = *argv;
 464                if (!arg)
 465                        break;
 466
 467                if (*arg != '-') {
 468                        if (nr_sha1 < 2 && !get_sha1(arg, sha1[nr_sha1])) {
 469                                nr_sha1++;
 470                                continue;
 471                        }
 472                        break;
 473                }
 474
 475                if (!strcmp(arg, "--")) {
 476                        argv++;
 477                        argc--;
 478                        break;
 479                }
 480                if (!strcmp(arg, "-r")) {
 481                        recursive = 1;
 482                        continue;
 483                }
 484                if (!strcmp(arg, "-p")) {
 485                        recursive = generate_patch = 1;
 486                        continue;
 487                }
 488                if (!strcmp(arg, "-M")) {
 489                        detect_rename = recursive = generate_patch = 1;
 490                        continue;
 491                }
 492                if (!strcmp(arg, "-z")) {
 493                        line_termination = '\0';
 494                        continue;
 495                }
 496                if (!strcmp(arg, "-m")) {
 497                        ignore_merges = 0;
 498                        continue;
 499                }
 500                if (!strcmp(arg, "-s")) {
 501                        silent = 1;
 502                        continue;
 503                }
 504                if (!strcmp(arg, "-v")) {
 505                        verbose_header = 1;
 506                        header_prefix = "diff-tree ";
 507                        continue;
 508                }
 509                if (!strcmp(arg, "--stdin")) {
 510                        read_stdin = 1;
 511                        continue;
 512                }
 513                if (!strcmp(arg, "--root")) {
 514                        show_root_diff = 1;
 515                        continue;
 516                }
 517                usage(diff_tree_usage);
 518        }
 519
 520        if (argc > 0) {
 521                int i;
 522
 523                paths = argv;
 524                nr_paths = argc;
 525                pathlens = xmalloc(nr_paths * sizeof(int));
 526                for (i=0; i<nr_paths; i++)
 527                        pathlens[i] = strlen(paths[i]);
 528        }
 529
 530        switch (nr_sha1) {
 531        case 0:
 532                if (!read_stdin)
 533                        usage(diff_tree_usage);
 534                break;
 535        case 1:
 536                diff_tree_commit(sha1[0], NULL);
 537                break;
 538        case 2:
 539                diff_tree_sha1_top(sha1[0], sha1[1], "");
 540                break;
 541        }
 542
 543        if (!read_stdin)
 544                return 0;
 545
 546        while (fgets(line, sizeof(line), stdin))
 547                diff_tree_stdin(line);
 548
 549        return 0;
 550}