builtin / diff.con commit get-tar-commit-id: parse comment record (3548726)
   1/*
   2 * Builtin "git diff"
   3 *
   4 * Copyright (c) 2006 Junio C Hamano
   5 */
   6#define USE_THE_INDEX_COMPATIBILITY_MACROS
   7#include "cache.h"
   8#include "config.h"
   9#include "lockfile.h"
  10#include "color.h"
  11#include "commit.h"
  12#include "blob.h"
  13#include "tag.h"
  14#include "diff.h"
  15#include "diffcore.h"
  16#include "revision.h"
  17#include "log-tree.h"
  18#include "builtin.h"
  19#include "submodule.h"
  20#include "sha1-array.h"
  21
  22#define DIFF_NO_INDEX_EXPLICIT 1
  23#define DIFF_NO_INDEX_IMPLICIT 2
  24
  25static const char builtin_diff_usage[] =
  26"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
  27
  28static const char *blob_path(struct object_array_entry *entry)
  29{
  30        return entry->path ? entry->path : entry->name;
  31}
  32
  33static void stuff_change(struct diff_options *opt,
  34                         unsigned old_mode, unsigned new_mode,
  35                         const struct object_id *old_oid,
  36                         const struct object_id *new_oid,
  37                         int old_oid_valid,
  38                         int new_oid_valid,
  39                         const char *old_path,
  40                         const char *new_path)
  41{
  42        struct diff_filespec *one, *two;
  43
  44        if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
  45            oideq(old_oid, new_oid) && (old_mode == new_mode))
  46                return;
  47
  48        if (opt->flags.reverse_diff) {
  49                SWAP(old_mode, new_mode);
  50                SWAP(old_oid, new_oid);
  51                SWAP(old_path, new_path);
  52        }
  53
  54        if (opt->prefix &&
  55            (strncmp(old_path, opt->prefix, opt->prefix_length) ||
  56             strncmp(new_path, opt->prefix, opt->prefix_length)))
  57                return;
  58
  59        one = alloc_filespec(old_path);
  60        two = alloc_filespec(new_path);
  61        fill_filespec(one, old_oid, old_oid_valid, old_mode);
  62        fill_filespec(two, new_oid, new_oid_valid, new_mode);
  63
  64        diff_queue(&diff_queued_diff, one, two);
  65}
  66
  67static int builtin_diff_b_f(struct rev_info *revs,
  68                            int argc, const char **argv,
  69                            struct object_array_entry **blob)
  70{
  71        /* Blob vs file in the working tree*/
  72        struct stat st;
  73        const char *path;
  74
  75        if (argc > 1)
  76                usage(builtin_diff_usage);
  77
  78        GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
  79        path = revs->prune_data.items[0].match;
  80
  81        if (lstat(path, &st))
  82                die_errno(_("failed to stat '%s'"), path);
  83        if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
  84                die(_("'%s': not a regular file or symlink"), path);
  85
  86        diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");
  87
  88        if (blob[0]->mode == S_IFINVALID)
  89                blob[0]->mode = canon_mode(st.st_mode);
  90
  91        stuff_change(&revs->diffopt,
  92                     blob[0]->mode, canon_mode(st.st_mode),
  93                     &blob[0]->item->oid, &null_oid,
  94                     1, 0,
  95                     blob[0]->path ? blob[0]->path : path,
  96                     path);
  97        diffcore_std(&revs->diffopt);
  98        diff_flush(&revs->diffopt);
  99        return 0;
 100}
 101
 102static int builtin_diff_blobs(struct rev_info *revs,
 103                              int argc, const char **argv,
 104                              struct object_array_entry **blob)
 105{
 106        const unsigned mode = canon_mode(S_IFREG | 0644);
 107
 108        if (argc > 1)
 109                usage(builtin_diff_usage);
 110
 111        if (blob[0]->mode == S_IFINVALID)
 112                blob[0]->mode = mode;
 113
 114        if (blob[1]->mode == S_IFINVALID)
 115                blob[1]->mode = mode;
 116
 117        stuff_change(&revs->diffopt,
 118                     blob[0]->mode, blob[1]->mode,
 119                     &blob[0]->item->oid, &blob[1]->item->oid,
 120                     1, 1,
 121                     blob_path(blob[0]), blob_path(blob[1]));
 122        diffcore_std(&revs->diffopt);
 123        diff_flush(&revs->diffopt);
 124        return 0;
 125}
 126
 127static int builtin_diff_index(struct rev_info *revs,
 128                              int argc, const char **argv)
 129{
 130        int cached = 0;
 131        while (1 < argc) {
 132                const char *arg = argv[1];
 133                if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
 134                        cached = 1;
 135                else
 136                        usage(builtin_diff_usage);
 137                argv++; argc--;
 138        }
 139        /*
 140         * Make sure there is one revision (i.e. pending object),
 141         * and there is no revision filtering parameters.
 142         */
 143        if (revs->pending.nr != 1 ||
 144            revs->max_count != -1 || revs->min_age != -1 ||
 145            revs->max_age != -1)
 146                usage(builtin_diff_usage);
 147        if (!cached) {
 148                setup_work_tree();
 149                if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
 150                        perror("read_cache_preload");
 151                        return -1;
 152                }
 153        } else if (read_cache() < 0) {
 154                perror("read_cache");
 155                return -1;
 156        }
 157        return run_diff_index(revs, cached);
 158}
 159
 160static int builtin_diff_tree(struct rev_info *revs,
 161                             int argc, const char **argv,
 162                             struct object_array_entry *ent0,
 163                             struct object_array_entry *ent1)
 164{
 165        const struct object_id *(oid[2]);
 166        int swap = 0;
 167
 168        if (argc > 1)
 169                usage(builtin_diff_usage);
 170
 171        /*
 172         * We saw two trees, ent0 and ent1.  If ent1 is uninteresting,
 173         * swap them.
 174         */
 175        if (ent1->item->flags & UNINTERESTING)
 176                swap = 1;
 177        oid[swap] = &ent0->item->oid;
 178        oid[1 - swap] = &ent1->item->oid;
 179        diff_tree_oid(oid[0], oid[1], "", &revs->diffopt);
 180        log_tree_diff_flush(revs);
 181        return 0;
 182}
 183
 184static int builtin_diff_combined(struct rev_info *revs,
 185                                 int argc, const char **argv,
 186                                 struct object_array_entry *ent,
 187                                 int ents)
 188{
 189        struct oid_array parents = OID_ARRAY_INIT;
 190        int i;
 191
 192        if (argc > 1)
 193                usage(builtin_diff_usage);
 194
 195        if (!revs->dense_combined_merges && !revs->combine_merges)
 196                revs->dense_combined_merges = revs->combine_merges = 1;
 197        for (i = 1; i < ents; i++)
 198                oid_array_append(&parents, &ent[i].item->oid);
 199        diff_tree_combined(&ent[0].item->oid, &parents,
 200                           revs->dense_combined_merges, revs);
 201        oid_array_clear(&parents);
 202        return 0;
 203}
 204
 205static void refresh_index_quietly(void)
 206{
 207        struct lock_file lock_file = LOCK_INIT;
 208        int fd;
 209
 210        fd = hold_locked_index(&lock_file, 0);
 211        if (fd < 0)
 212                return;
 213        discard_cache();
 214        read_cache();
 215        refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
 216        repo_update_index_if_able(the_repository, &lock_file);
 217}
 218
 219static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
 220{
 221        unsigned int options = 0;
 222
 223        while (1 < argc && argv[1][0] == '-') {
 224                if (!strcmp(argv[1], "--base"))
 225                        revs->max_count = 1;
 226                else if (!strcmp(argv[1], "--ours"))
 227                        revs->max_count = 2;
 228                else if (!strcmp(argv[1], "--theirs"))
 229                        revs->max_count = 3;
 230                else if (!strcmp(argv[1], "-q"))
 231                        options |= DIFF_SILENT_ON_REMOVED;
 232                else if (!strcmp(argv[1], "-h"))
 233                        usage(builtin_diff_usage);
 234                else
 235                        return error(_("invalid option: %s"), argv[1]);
 236                argv++; argc--;
 237        }
 238
 239        /*
 240         * "diff --base" should not combine merges because it was not
 241         * asked to.  "diff -c" should not densify (if the user wants
 242         * dense one, --cc can be explicitly asked for, or just rely
 243         * on the default).
 244         */
 245        if (revs->max_count == -1 && !revs->combine_merges &&
 246            (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
 247                revs->combine_merges = revs->dense_combined_merges = 1;
 248
 249        setup_work_tree();
 250        if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
 251                perror("read_cache_preload");
 252                return -1;
 253        }
 254        return run_diff_files(revs, options);
 255}
 256
 257int cmd_diff(int argc, const char **argv, const char *prefix)
 258{
 259        int i;
 260        struct rev_info rev;
 261        struct object_array ent = OBJECT_ARRAY_INIT;
 262        int blobs = 0, paths = 0;
 263        struct object_array_entry *blob[2];
 264        int nongit = 0, no_index = 0;
 265        int result = 0;
 266
 267        /*
 268         * We could get N tree-ish in the rev.pending_objects list.
 269         * Also there could be M blobs there, and P pathspecs.
 270         *
 271         * N=0, M=0:
 272         *      cache vs files (diff-files)
 273         * N=0, M=2:
 274         *      compare two random blobs.  P must be zero.
 275         * N=0, M=1, P=1:
 276         *      compare a blob with a working tree file.
 277         *
 278         * N=1, M=0:
 279         *      tree vs cache (diff-index --cached)
 280         *
 281         * N=2, M=0:
 282         *      tree vs tree (diff-tree)
 283         *
 284         * N=0, M=0, P=2:
 285         *      compare two filesystem entities (aka --no-index).
 286         *
 287         * Other cases are errors.
 288         */
 289
 290        /* Were we asked to do --no-index explicitly? */
 291        for (i = 1; i < argc; i++) {
 292                if (!strcmp(argv[i], "--")) {
 293                        i++;
 294                        break;
 295                }
 296                if (!strcmp(argv[i], "--no-index"))
 297                        no_index = DIFF_NO_INDEX_EXPLICIT;
 298                if (argv[i][0] != '-')
 299                        break;
 300        }
 301
 302        prefix = setup_git_directory_gently(&nongit);
 303
 304        if (!no_index) {
 305                /*
 306                 * Treat git diff with at least one path outside of the
 307                 * repo the same as if the command would have been executed
 308                 * outside of a git repository.  In this case it behaves
 309                 * the same way as "git diff --no-index <a> <b>", which acts
 310                 * as a colourful "diff" replacement.
 311                 */
 312                if (nongit || ((argc == i + 2) &&
 313                               (!path_inside_repo(prefix, argv[i]) ||
 314                                !path_inside_repo(prefix, argv[i + 1]))))
 315                        no_index = DIFF_NO_INDEX_IMPLICIT;
 316        }
 317
 318        init_diff_ui_defaults();
 319        git_config(git_diff_ui_config, NULL);
 320        precompose_argv(argc, argv);
 321
 322        repo_init_revisions(the_repository, &rev, prefix);
 323
 324        if (no_index && argc != i + 2) {
 325                if (no_index == DIFF_NO_INDEX_IMPLICIT) {
 326                        /*
 327                         * There was no --no-index and there were not two
 328                         * paths. It is possible that the user intended
 329                         * to do an inside-repository operation.
 330                         */
 331                        fprintf(stderr, "Not a git repository\n");
 332                        fprintf(stderr,
 333                                "To compare two paths outside a working tree:\n");
 334                }
 335                /* Give the usage message for non-repository usage and exit. */
 336                usagef("git diff %s <path> <path>",
 337                       no_index == DIFF_NO_INDEX_EXPLICIT ?
 338                       "--no-index" : "[--no-index]");
 339
 340        }
 341
 342        /* Set up defaults that will apply to both no-index and regular diffs. */
 343        rev.diffopt.stat_width = -1;
 344        rev.diffopt.stat_graph_width = -1;
 345        rev.diffopt.flags.allow_external = 1;
 346        rev.diffopt.flags.allow_textconv = 1;
 347
 348        /* If this is a no-index diff, just run it and exit there. */
 349        if (no_index)
 350                diff_no_index(&rev, argc, argv);
 351
 352        /*
 353         * Otherwise, we are doing the usual "git" diff; set up any
 354         * further defaults that apply to regular diffs.
 355         */
 356        rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
 357
 358        /*
 359         * Default to intent-to-add entries invisible in the
 360         * index. This makes them show up as new files in diff-files
 361         * and not at all in diff-cached.
 362         */
 363        rev.diffopt.ita_invisible_in_index = 1;
 364
 365        if (nongit)
 366                die(_("Not a git repository"));
 367        argc = setup_revisions(argc, argv, &rev, NULL);
 368        if (!rev.diffopt.output_format) {
 369                rev.diffopt.output_format = DIFF_FORMAT_PATCH;
 370                diff_setup_done(&rev.diffopt);
 371        }
 372
 373        rev.diffopt.flags.recursive = 1;
 374
 375        setup_diff_pager(&rev.diffopt);
 376
 377        /*
 378         * Do we have --cached and not have a pending object, then
 379         * default to HEAD by hand.  Eek.
 380         */
 381        if (!rev.pending.nr) {
 382                int i;
 383                for (i = 1; i < argc; i++) {
 384                        const char *arg = argv[i];
 385                        if (!strcmp(arg, "--"))
 386                                break;
 387                        else if (!strcmp(arg, "--cached") ||
 388                                 !strcmp(arg, "--staged")) {
 389                                add_head_to_pending(&rev);
 390                                if (!rev.pending.nr) {
 391                                        struct tree *tree;
 392                                        tree = lookup_tree(the_repository,
 393                                                           the_repository->hash_algo->empty_tree);
 394                                        add_pending_object(&rev, &tree->object, "HEAD");
 395                                }
 396                                break;
 397                        }
 398                }
 399        }
 400
 401        for (i = 0; i < rev.pending.nr; i++) {
 402                struct object_array_entry *entry = &rev.pending.objects[i];
 403                struct object *obj = entry->item;
 404                const char *name = entry->name;
 405                int flags = (obj->flags & UNINTERESTING);
 406                if (!obj->parsed)
 407                        obj = parse_object(the_repository, &obj->oid);
 408                obj = deref_tag(the_repository, obj, NULL, 0);
 409                if (!obj)
 410                        die(_("invalid object '%s' given."), name);
 411                if (obj->type == OBJ_COMMIT)
 412                        obj = &get_commit_tree(((struct commit *)obj))->object;
 413
 414                if (obj->type == OBJ_TREE) {
 415                        obj->flags |= flags;
 416                        add_object_array(obj, name, &ent);
 417                } else if (obj->type == OBJ_BLOB) {
 418                        if (2 <= blobs)
 419                                die(_("more than two blobs given: '%s'"), name);
 420                        blob[blobs] = entry;
 421                        blobs++;
 422
 423                } else {
 424                        die(_("unhandled object '%s' given."), name);
 425                }
 426        }
 427        if (rev.prune_data.nr)
 428                paths += rev.prune_data.nr;
 429
 430        /*
 431         * Now, do the arguments look reasonable?
 432         */
 433        if (!ent.nr) {
 434                switch (blobs) {
 435                case 0:
 436                        result = builtin_diff_files(&rev, argc, argv);
 437                        break;
 438                case 1:
 439                        if (paths != 1)
 440                                usage(builtin_diff_usage);
 441                        result = builtin_diff_b_f(&rev, argc, argv, blob);
 442                        break;
 443                case 2:
 444                        if (paths)
 445                                usage(builtin_diff_usage);
 446                        result = builtin_diff_blobs(&rev, argc, argv, blob);
 447                        break;
 448                default:
 449                        usage(builtin_diff_usage);
 450                }
 451        }
 452        else if (blobs)
 453                usage(builtin_diff_usage);
 454        else if (ent.nr == 1)
 455                result = builtin_diff_index(&rev, argc, argv);
 456        else if (ent.nr == 2)
 457                result = builtin_diff_tree(&rev, argc, argv,
 458                                           &ent.objects[0], &ent.objects[1]);
 459        else if (ent.objects[0].item->flags & UNINTERESTING) {
 460                /*
 461                 * diff A...B where there is at least one merge base
 462                 * between A and B.  We have ent.objects[0] ==
 463                 * merge-base, ent.objects[ents-2] == A, and
 464                 * ent.objects[ents-1] == B.  Show diff between the
 465                 * base and B.  Note that we pick one merge base at
 466                 * random if there are more than one.
 467                 */
 468                result = builtin_diff_tree(&rev, argc, argv,
 469                                           &ent.objects[0],
 470                                           &ent.objects[ent.nr-1]);
 471        } else
 472                result = builtin_diff_combined(&rev, argc, argv,
 473                                               ent.objects, ent.nr);
 474        result = diff_result_code(&rev.diffopt, result);
 475        if (1 < rev.diffopt.skip_stat_unmatch)
 476                refresh_index_quietly();
 477        UNLEAK(rev);
 478        UNLEAK(ent);
 479        UNLEAK(blob);
 480        return result;
 481}