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