diff-tree.con commit [PATCH] Initial support for building a debian package (.deb) (7421abf)
   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_RAW;
  13static int diff_line_termination = '\n';
  14static int detect_rename = 0;
  15static int find_copies_harder = 0;
  16static int diff_setup_opt = 0;
  17static int diff_score_opt = 0;
  18static const char *pickaxe = NULL;
  19static int pickaxe_opts = 0;
  20static int diff_break_opt = -1;
  21static const char *orderfile = NULL;
  22static const char *diff_filter = NULL;
  23static const char *header = NULL;
  24static const char *header_prefix = "";
  25static enum cmit_fmt commit_format = CMIT_FMT_RAW;
  26
  27// What paths are we interested in?
  28static int nr_paths = 0;
  29static const char **paths = NULL;
  30static int *pathlens = NULL;
  31
  32static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base);
  33
  34static void update_tree_entry(void **bufp, unsigned long *sizep)
  35{
  36        void *buf = *bufp;
  37        unsigned long size = *sizep;
  38        int len = strlen(buf) + 1 + 20;
  39
  40        if (size < len)
  41                die("corrupt tree file");
  42        *bufp = buf + len;
  43        *sizep = size - len;
  44}
  45
  46static const unsigned char *extract(void *tree, unsigned long size, const char **pathp, unsigned int *modep)
  47{
  48        int len = strlen(tree)+1;
  49        const unsigned char *sha1 = tree + len;
  50        const char *path = strchr(tree, ' ');
  51        unsigned int mode;
  52
  53        if (!path || size < len + 20 || sscanf(tree, "%o", &mode) != 1)
  54                die("corrupt tree file");
  55        *pathp = path+1;
  56        *modep = DIFF_FILE_CANON_MODE(mode);
  57        return sha1;
  58}
  59
  60static char *malloc_base(const char *base, const char *path, int pathlen)
  61{
  62        int baselen = strlen(base);
  63        char *newbase = xmalloc(baselen + pathlen + 2);
  64        memcpy(newbase, base, baselen);
  65        memcpy(newbase + baselen, path, pathlen);
  66        memcpy(newbase + baselen + pathlen, "/", 2);
  67        return newbase;
  68}
  69
  70static void show_file(const char *prefix, void *tree, unsigned long size, const char *base);
  71static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base);
  72
  73/* A file entry went away or appeared */
  74static void show_file(const char *prefix, void *tree, unsigned long size, const char *base)
  75{
  76        unsigned mode;
  77        const char *path;
  78        const unsigned char *sha1 = extract(tree, size, &path, &mode);
  79
  80        if (recursive && S_ISDIR(mode)) {
  81                char type[20];
  82                unsigned long size;
  83                char *newbase = malloc_base(base, path, strlen(path));
  84                void *tree;
  85
  86                tree = read_sha1_file(sha1, type, &size);
  87                if (!tree || strcmp(type, "tree"))
  88                        die("corrupt tree sha %s", sha1_to_hex(sha1));
  89
  90                show_tree(prefix, tree, size, newbase);
  91
  92                free(tree);
  93                free(newbase);
  94                return;
  95        }
  96
  97        diff_addremove(prefix[0], mode, sha1, base, path);
  98}
  99
 100static int compare_tree_entry(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
 101{
 102        unsigned mode1, mode2;
 103        const char *path1, *path2;
 104        const unsigned char *sha1, *sha2;
 105        int cmp, pathlen1, pathlen2;
 106
 107        sha1 = extract(tree1, size1, &path1, &mode1);
 108        sha2 = extract(tree2, size2, &path2, &mode2);
 109
 110        pathlen1 = strlen(path1);
 111        pathlen2 = strlen(path2);
 112        cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
 113        if (cmp < 0) {
 114                show_file("-", tree1, size1, base);
 115                return -1;
 116        }
 117        if (cmp > 0) {
 118                show_file("+", tree2, size2, base);
 119                return 1;
 120        }
 121        if (!find_copies_harder && !memcmp(sha1, sha2, 20) && mode1 == mode2)
 122                return 0;
 123
 124        /*
 125         * If the filemode has changed to/from a directory from/to a regular
 126         * file, we need to consider it a remove and an add.
 127         */
 128        if (S_ISDIR(mode1) != S_ISDIR(mode2)) {
 129                show_file("-", tree1, size1, base);
 130                show_file("+", tree2, size2, base);
 131                return 0;
 132        }
 133
 134        if (recursive && S_ISDIR(mode1)) {
 135                int retval;
 136                char *newbase = malloc_base(base, path1, pathlen1);
 137                if (show_tree_entry_in_recursive)
 138                        diff_change(mode1, mode2, sha1, sha2, base, path1);
 139                retval = diff_tree_sha1(sha1, sha2, newbase);
 140                free(newbase);
 141                return retval;
 142        }
 143
 144        diff_change(mode1, mode2, sha1, sha2, base, path1);
 145        return 0;
 146}
 147
 148static int interesting(void *tree, unsigned long size, const char *base)
 149{
 150        const char *path;
 151        unsigned mode;
 152        int i;
 153        int baselen, pathlen;
 154
 155        if (!nr_paths)
 156                return 1;
 157
 158        (void)extract(tree, size, &path, &mode);
 159
 160        pathlen = strlen(path);
 161        baselen = strlen(base);
 162
 163        for (i=0; i < nr_paths; i++) {
 164                const char *match = paths[i];
 165                int matchlen = pathlens[i];
 166
 167                if (baselen >= matchlen) {
 168                        /* If it doesn't match, move along... */
 169                        if (strncmp(base, match, matchlen))
 170                                continue;
 171
 172                        /* The base is a subdirectory of a path which was specified. */
 173                        return 1;
 174                }
 175
 176                /* Does the base match? */
 177                if (strncmp(base, match, baselen))
 178                        continue;
 179
 180                match += baselen;
 181                matchlen -= baselen;
 182
 183                if (pathlen > matchlen)
 184                        continue;
 185
 186                if (matchlen > pathlen) {
 187                        if (match[pathlen] != '/')
 188                                continue;
 189                        if (!S_ISDIR(mode))
 190                                continue;
 191                }
 192
 193                if (strncmp(path, match, pathlen))
 194                        continue;
 195
 196                return 1;
 197        }
 198        return 0; /* No matches */
 199}
 200
 201/* A whole sub-tree went away or appeared */
 202static void show_tree(const char *prefix, void *tree, unsigned long size, const char *base)
 203{
 204        while (size) {
 205                if (interesting(tree, size, base))
 206                        show_file(prefix, tree, size, base);
 207                update_tree_entry(&tree, &size);
 208        }
 209}
 210
 211static int diff_tree(void *tree1, unsigned long size1, void *tree2, unsigned long size2, const char *base)
 212{
 213        while (size1 | size2) {
 214                if (nr_paths && size1 && !interesting(tree1, size1, base)) {
 215                        update_tree_entry(&tree1, &size1);
 216                        continue;
 217                }
 218                if (nr_paths && size2 && !interesting(tree2, size2, base)) {
 219                        update_tree_entry(&tree2, &size2);
 220                        continue;
 221                }
 222                if (!size1) {
 223                        show_file("+", tree2, size2, base);
 224                        update_tree_entry(&tree2, &size2);
 225                        continue;
 226                }
 227                if (!size2) {
 228                        show_file("-", tree1, size1, base);
 229                        update_tree_entry(&tree1, &size1);
 230                        continue;
 231                }
 232                switch (compare_tree_entry(tree1, size1, tree2, size2, base)) {
 233                case -1:
 234                        update_tree_entry(&tree1, &size1);
 235                        continue;
 236                case 0:
 237                        update_tree_entry(&tree1, &size1);
 238                        /* Fallthrough */
 239                case 1:
 240                        update_tree_entry(&tree2, &size2);
 241                        continue;
 242                }
 243                die("git-diff-tree: internal error");
 244        }
 245        return 0;
 246}
 247
 248static int diff_tree_sha1(const unsigned char *old, const unsigned char *new, const char *base)
 249{
 250        void *tree1, *tree2;
 251        unsigned long size1, size2;
 252        int retval;
 253
 254        tree1 = read_object_with_reference(old, "tree", &size1, NULL);
 255        if (!tree1)
 256                die("unable to read source tree (%s)", sha1_to_hex(old));
 257        tree2 = read_object_with_reference(new, "tree", &size2, NULL);
 258        if (!tree2)
 259                die("unable to read destination tree (%s)", sha1_to_hex(new));
 260        retval = diff_tree(tree1, size1, tree2, size2, base);
 261        free(tree1);
 262        free(tree2);
 263        return retval;
 264}
 265
 266static void call_diff_setup(void)
 267{
 268        diff_setup(diff_setup_opt);
 269}
 270
 271static int call_diff_flush(void)
 272{
 273        diffcore_std(NULL,
 274                     detect_rename, diff_score_opt,
 275                     pickaxe, pickaxe_opts,
 276                     diff_break_opt,
 277                     orderfile,
 278                     diff_filter);
 279        if (diff_queue_is_empty()) {
 280                diff_flush(DIFF_FORMAT_NO_OUTPUT, diff_line_termination);
 281                return 0;
 282        }
 283        if (header) {
 284                printf("%s%c", header, diff_line_termination);
 285                header = NULL;
 286        }
 287        diff_flush(diff_output_format, diff_line_termination);
 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 const 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        if (!verbose_header)
 324                return commit;
 325
 326        offset = sprintf(this_header, "%s%s (from %s)\n", header_prefix, commit, parent);
 327        offset += pretty_print_commit(commit_format, msg, len, this_header + offset, sizeof(this_header) - offset);
 328        return this_header;
 329}
 330
 331static int diff_tree_commit(const unsigned char *commit, const char *name)
 332{
 333        unsigned long size, offset;
 334        char *buf = read_object_with_reference(commit, "commit", &size, NULL);
 335
 336        if (!buf)
 337                return -1;
 338
 339        if (!name) {
 340                static char commit_name[60];
 341                strcpy(commit_name, sha1_to_hex(commit));
 342                name = commit_name;
 343        }
 344
 345        /* Root commit? */
 346        if (show_root_diff && memcmp(buf + 46, "parent ", 7)) {
 347                header = generate_header(name, "root", buf, size);
 348                diff_root_tree(commit, "");
 349        }
 350
 351        /* More than one parent? */
 352        if (ignore_merges) {
 353                if (!memcmp(buf + 46 + 48, "parent ", 7))
 354                        return 0;
 355        }
 356
 357        offset = 46;
 358        while (offset + 48 < size && !memcmp(buf + offset, "parent ", 7)) {
 359                unsigned char parent[20];
 360                if (get_sha1_hex(buf + offset + 7, parent))
 361                        return -1;
 362                header = generate_header(name, sha1_to_hex(parent), buf, size);
 363                diff_tree_sha1_top(parent, commit, "");
 364                if (!header && verbose_header) {
 365                        header_prefix = "\ndiff-tree ";
 366                        /*
 367                         * Don't print multiple merge entries if we
 368                         * don't print the diffs.
 369                         */
 370                }
 371                offset += 48;
 372        }
 373        return 0;
 374}
 375
 376static int diff_tree_stdin(char *line)
 377{
 378        int len = strlen(line);
 379        unsigned char commit[20], parent[20];
 380        static char this_header[1000];
 381
 382        if (!len || line[len-1] != '\n')
 383                return -1;
 384        line[len-1] = 0;
 385        if (get_sha1_hex(line, commit))
 386                return -1;
 387        if (isspace(line[40]) && !get_sha1_hex(line+41, parent)) {
 388                line[40] = 0;
 389                line[81] = 0;
 390                sprintf(this_header, "%s (from %s)\n", line, line+41);
 391                header = this_header;
 392                return diff_tree_sha1_top(parent, commit, "");
 393        }
 394        line[40] = 0;
 395        return diff_tree_commit(commit, line);
 396}
 397
 398static char *diff_tree_usage =
 399"git-diff-tree [--stdin] [-m] [-s] [-v] [--pretty] [-t] "
 400"[<common diff options>] <tree-ish> <tree-ish>"
 401COMMON_DIFF_OPTIONS_HELP;
 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") || !strcmp(arg, "-u")) {
 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 (!strncmp(arg, "--diff-filter=", 14)) {
 458                        diff_filter = arg + 14;
 459                        continue;
 460                }
 461                if (!strcmp(arg, "--pickaxe-all")) {
 462                        pickaxe_opts = DIFF_PICKAXE_ALL;
 463                        continue;
 464                }
 465                if (!strncmp(arg, "-M", 2)) {
 466                        detect_rename = DIFF_DETECT_RENAME;
 467                        if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
 468                                usage(diff_tree_usage);
 469                        continue;
 470                }
 471                if (!strncmp(arg, "-C", 2)) {
 472                        detect_rename = DIFF_DETECT_COPY;
 473                        if ((diff_score_opt = diff_scoreopt_parse(arg)) == -1)
 474                                usage(diff_tree_usage);
 475                        continue;
 476                }
 477                if (!strncmp(arg, "-B", 2)) {
 478                        if ((diff_break_opt = diff_scoreopt_parse(arg)) == -1)
 479                                usage(diff_tree_usage);
 480                        continue;
 481                }
 482                if (!strcmp(arg, "--find-copies-harder")) {
 483                        find_copies_harder = 1;
 484                        continue;
 485                }
 486                if (!strcmp(arg, "--name-only")) {
 487                        diff_output_format = DIFF_FORMAT_NAME;
 488                        continue;
 489                }
 490                if (!strcmp(arg, "-z")) {
 491                        diff_line_termination = 0;
 492                        continue;
 493                }
 494                if (!strcmp(arg, "-m")) {
 495                        ignore_merges = 0;
 496                        continue;
 497                }
 498                if (!strcmp(arg, "-s")) {
 499                        diff_output_format = DIFF_FORMAT_NO_OUTPUT;
 500                        continue;
 501                }
 502                if (!strcmp(arg, "-v")) {
 503                        verbose_header = 1;
 504                        header_prefix = "diff-tree ";
 505                        continue;
 506                }
 507                if (!strncmp(arg, "--pretty", 8)) {
 508                        verbose_header = 1;
 509                        header_prefix = "diff-tree ";
 510                        commit_format = get_commit_format(arg+8);
 511                        continue;
 512                }
 513                if (!strcmp(arg, "--stdin")) {
 514                        read_stdin = 1;
 515                        continue;
 516                }
 517                if (!strcmp(arg, "--root")) {
 518                        show_root_diff = 1;
 519                        continue;
 520                }
 521                usage(diff_tree_usage);
 522        }
 523        if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
 524                usage(diff_tree_usage);
 525
 526        if (argc > 0) {
 527                int i;
 528
 529                paths = argv;
 530                nr_paths = argc;
 531                pathlens = xmalloc(nr_paths * sizeof(int));
 532                for (i=0; i<nr_paths; i++)
 533                        pathlens[i] = strlen(paths[i]);
 534        }
 535
 536        switch (nr_sha1) {
 537        case 0:
 538                if (!read_stdin)
 539                        usage(diff_tree_usage);
 540                break;
 541        case 1:
 542                diff_tree_commit(sha1[0], NULL);
 543                break;
 544        case 2:
 545                diff_tree_sha1_top(sha1[0], sha1[1], "");
 546                break;
 547        }
 548
 549        if (!read_stdin)
 550                return 0;
 551
 552        if (detect_rename)
 553                diff_setup_opt |= (DIFF_SETUP_USE_SIZE_CACHE |
 554                                   DIFF_SETUP_USE_CACHE);
 555        while (fgets(line, sizeof(line), stdin))
 556                diff_tree_stdin(line);
 557
 558        return 0;
 559}