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