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