builtin-diff.con commit Merge branch 'ts/doctar' (3532998)
   1/*
   2 * Builtin "git diff"
   3 *
   4 * Copyright (c) 2006 Junio C Hamano
   5 */
   6#include "cache.h"
   7#include "commit.h"
   8#include "blob.h"
   9#include "tag.h"
  10#include "diff.h"
  11#include "diffcore.h"
  12#include "revision.h"
  13#include "log-tree.h"
  14#include "builtin.h"
  15
  16/* NEEDSWORK: struct object has place for name but we _do_
  17 * know mode when we extracted the blob out of a tree, which
  18 * we currently lose.
  19 */
  20struct blobinfo {
  21        unsigned char sha1[20];
  22        const char *name;
  23};
  24
  25static const char builtin_diff_usage[] =
  26"diff <options> <rev>{0,2} -- <path>*";
  27
  28static int builtin_diff_files(struct rev_info *revs,
  29                              int argc, const char **argv)
  30{
  31        int silent = 0;
  32        while (1 < argc) {
  33                const char *arg = argv[1];
  34                if (!strcmp(arg, "--base"))
  35                        revs->max_count = 1;
  36                else if (!strcmp(arg, "--ours"))
  37                        revs->max_count = 2;
  38                else if (!strcmp(arg, "--theirs"))
  39                        revs->max_count = 3;
  40                else if (!strcmp(arg, "-q"))
  41                        silent = 1;
  42                else if (!strcmp(arg, "--raw"))
  43                        revs->diffopt.output_format = DIFF_FORMAT_RAW;
  44                else
  45                        usage(builtin_diff_usage);
  46                argv++; argc--;
  47        }
  48        /*
  49         * Make sure there are NO revision (i.e. pending object) parameter,
  50         * specified rev.max_count is reasonable (0 <= n <= 3), and
  51         * there is no other revision filtering parameter.
  52         */
  53        if (revs->pending_objects ||
  54            revs->min_age != -1 ||
  55            revs->max_age != -1 ||
  56            3 < revs->max_count)
  57                usage(builtin_diff_usage);
  58        if (revs->max_count < 0 &&
  59            (revs->diffopt.output_format == DIFF_FORMAT_PATCH))
  60                revs->combine_merges = revs->dense_combined_merges = 1;
  61        /*
  62         * Backward compatibility wart - "diff-files -s" used to
  63         * defeat the common diff option "-s" which asked for
  64         * DIFF_FORMAT_NO_OUTPUT.
  65         */
  66        if (revs->diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
  67                revs->diffopt.output_format = DIFF_FORMAT_RAW;
  68        return run_diff_files(revs, silent);
  69}
  70
  71static void stuff_change(struct diff_options *opt,
  72                         unsigned old_mode, unsigned new_mode,
  73                         const unsigned char *old_sha1,
  74                         const unsigned char *new_sha1,
  75                         const char *old_name,
  76                         const char *new_name)
  77{
  78        struct diff_filespec *one, *two;
  79
  80        if (memcmp(null_sha1, old_sha1, 20) &&
  81            memcmp(null_sha1, new_sha1, 20) &&
  82            !memcmp(old_sha1, new_sha1, 20))
  83                return;
  84
  85        if (opt->reverse_diff) {
  86                unsigned tmp;
  87                const unsigned char *tmp_u;
  88                const char *tmp_c;
  89                tmp = old_mode; old_mode = new_mode; new_mode = tmp;
  90                tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
  91                tmp_c = old_name; old_name = new_name; new_name = tmp_c;
  92        }
  93        one = alloc_filespec(old_name);
  94        two = alloc_filespec(new_name);
  95        fill_filespec(one, old_sha1, old_mode);
  96        fill_filespec(two, new_sha1, new_mode);
  97
  98        /* NEEDSWORK: shouldn't this part of diffopt??? */
  99        diff_queue(&diff_queued_diff, one, two);
 100}
 101
 102static int builtin_diff_b_f(struct rev_info *revs,
 103                            int argc, const char **argv,
 104                            struct blobinfo *blob,
 105                            const char *path)
 106{
 107        /* Blob vs file in the working tree*/
 108        struct stat st;
 109
 110        while (1 < argc) {
 111                const char *arg = argv[1];
 112                if (!strcmp(arg, "--raw"))
 113                        revs->diffopt.output_format = DIFF_FORMAT_RAW;
 114                else
 115                        usage(builtin_diff_usage);
 116                argv++; argc--;
 117        }
 118        if (lstat(path, &st))
 119                die("'%s': %s", path, strerror(errno));
 120        if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
 121                die("'%s': not a regular file or symlink", path);
 122        stuff_change(&revs->diffopt,
 123                     canon_mode(st.st_mode), canon_mode(st.st_mode),
 124                     blob[0].sha1, null_sha1,
 125                     path, path);
 126        diffcore_std(&revs->diffopt);
 127        diff_flush(&revs->diffopt);
 128        return 0;
 129}
 130
 131static int builtin_diff_blobs(struct rev_info *revs,
 132                              int argc, const char **argv,
 133                              struct blobinfo *blob)
 134{
 135        /* Blobs: the arguments are reversed when setup_revisions()
 136         * picked them up.
 137         */
 138        unsigned mode = canon_mode(S_IFREG | 0644);
 139
 140        while (1 < argc) {
 141                const char *arg = argv[1];
 142                if (!strcmp(arg, "--raw"))
 143                        revs->diffopt.output_format = DIFF_FORMAT_RAW;
 144                else
 145                        usage(builtin_diff_usage);
 146                argv++; argc--;
 147        }
 148        stuff_change(&revs->diffopt,
 149                     mode, mode,
 150                     blob[1].sha1, blob[0].sha1,
 151                     blob[0].name, blob[0].name);
 152        diffcore_std(&revs->diffopt);
 153        diff_flush(&revs->diffopt);
 154        return 0;
 155}
 156
 157static int builtin_diff_index(struct rev_info *revs,
 158                              int argc, const char **argv)
 159{
 160        int cached = 0;
 161        while (1 < argc) {
 162                const char *arg = argv[1];
 163                if (!strcmp(arg, "--cached"))
 164                        cached = 1;
 165                else if (!strcmp(arg, "--raw"))
 166                        revs->diffopt.output_format = DIFF_FORMAT_RAW;
 167                else
 168                        usage(builtin_diff_usage);
 169                argv++; argc--;
 170        }
 171        /*
 172         * Make sure there is one revision (i.e. pending object),
 173         * and there is no revision filtering parameters.
 174         */
 175        if (!revs->pending_objects || revs->pending_objects->next ||
 176            revs->max_count != -1 || revs->min_age != -1 ||
 177            revs->max_age != -1)
 178                usage(builtin_diff_usage);
 179        return run_diff_index(revs, cached);
 180}
 181
 182static int builtin_diff_tree(struct rev_info *revs,
 183                             int argc, const char **argv,
 184                             struct object_list *ent)
 185{
 186        const unsigned char *(sha1[2]);
 187        int swap = 1;
 188        while (1 < argc) {
 189                const char *arg = argv[1];
 190                if (!strcmp(arg, "--raw"))
 191                        revs->diffopt.output_format = DIFF_FORMAT_RAW;
 192                else
 193                        usage(builtin_diff_usage);
 194                argv++; argc--;
 195        }
 196
 197        /* We saw two trees, ent[0] and ent[1].
 198         * unless ent[0] is unintesting, they are swapped
 199         */
 200        if (ent[0].item->flags & UNINTERESTING)
 201                swap = 0;
 202        sha1[swap] = ent[0].item->sha1;
 203        sha1[1-swap] = ent[1].item->sha1;
 204        diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
 205        log_tree_diff_flush(revs);
 206        return 0;
 207}
 208
 209static int builtin_diff_combined(struct rev_info *revs,
 210                                 int argc, const char **argv,
 211                                 struct object_list *ent,
 212                                 int ents)
 213{
 214        const unsigned char (*parent)[20];
 215        int i;
 216
 217        while (1 < argc) {
 218                const char *arg = argv[1];
 219                if (!strcmp(arg, "--raw"))
 220                        revs->diffopt.output_format = DIFF_FORMAT_RAW;
 221                else
 222                        usage(builtin_diff_usage);
 223                argv++; argc--;
 224        }
 225        if (!revs->dense_combined_merges && !revs->combine_merges)
 226                revs->dense_combined_merges = revs->combine_merges = 1;
 227        parent = xmalloc(ents * sizeof(*parent));
 228        /* Again, the revs are all reverse */
 229        for (i = 0; i < ents; i++)
 230                memcpy(parent + i, ent[ents - 1 - i].item->sha1, 20);
 231        diff_tree_combined(parent[0], parent + 1, ents - 1,
 232                           revs->dense_combined_merges, revs);
 233        return 0;
 234}
 235
 236static void add_head(struct rev_info *revs)
 237{
 238        unsigned char sha1[20];
 239        struct object *obj;
 240        if (get_sha1("HEAD", sha1))
 241                return;
 242        obj = parse_object(sha1);
 243        if (!obj)
 244                return;
 245        add_object(obj, &revs->pending_objects, NULL, "HEAD");
 246}
 247
 248int cmd_diff(int argc, const char **argv, char **envp)
 249{
 250        struct rev_info rev;
 251        struct object_list *list, ent[100];
 252        int ents = 0, blobs = 0, paths = 0;
 253        const char *path = NULL;
 254        struct blobinfo blob[2];
 255
 256        /*
 257         * We could get N tree-ish in the rev.pending_objects list.
 258         * Also there could be M blobs there, and P pathspecs.
 259         *
 260         * N=0, M=0:
 261         *      cache vs files (diff-files)
 262         * N=0, M=2:
 263         *      compare two random blobs.  P must be zero.
 264         * N=0, M=1, P=1:
 265         *      compare a blob with a working tree file.
 266         *
 267         * N=1, M=0:
 268         *      tree vs cache (diff-index --cached)
 269         *
 270         * N=2, M=0:
 271         *      tree vs tree (diff-tree)
 272         *
 273         * Other cases are errors.
 274         */
 275
 276        git_config(git_diff_config);
 277        init_revisions(&rev);
 278        rev.diffopt.output_format = DIFF_FORMAT_PATCH;
 279
 280        argc = setup_revisions(argc, argv, &rev, NULL);
 281        /* Do we have --cached and not have a pending object, then
 282         * default to HEAD by hand.  Eek.
 283         */
 284        if (!rev.pending_objects) {
 285                int i;
 286                for (i = 1; i < argc; i++) {
 287                        const char *arg = argv[i];
 288                        if (!strcmp(arg, "--"))
 289                                break;
 290                        else if (!strcmp(arg, "--cached")) {
 291                                add_head(&rev);
 292                                break;
 293                        }
 294                }
 295        }
 296
 297        for (list = rev.pending_objects; list; list = list->next) {
 298                struct object *obj = list->item;
 299                const char *name = list->name;
 300                int flags = (obj->flags & UNINTERESTING);
 301                if (!obj->parsed)
 302                        obj = parse_object(obj->sha1);
 303                obj = deref_tag(obj, NULL, 0);
 304                if (!obj)
 305                        die("invalid object '%s' given.", name);
 306                if (!strcmp(obj->type, commit_type))
 307                        obj = &((struct commit *)obj)->tree->object;
 308                if (!strcmp(obj->type, tree_type)) {
 309                        if (ARRAY_SIZE(ent) <= ents)
 310                                die("more than %d trees given: '%s'",
 311                                    (int) ARRAY_SIZE(ent), name);
 312                        obj->flags |= flags;
 313                        ent[ents].item = obj;
 314                        ent[ents].name = name;
 315                        ents++;
 316                        continue;
 317                }
 318                if (!strcmp(obj->type, blob_type)) {
 319                        if (2 <= blobs)
 320                                die("more than two blobs given: '%s'", name);
 321                        memcpy(blob[blobs].sha1, obj->sha1, 20);
 322                        blob[blobs].name = name;
 323                        blobs++;
 324                        continue;
 325
 326                }
 327                die("unhandled object '%s' given.", name);
 328        }
 329        if (rev.prune_data) {
 330                const char **pathspec = rev.prune_data;
 331                while (*pathspec) {
 332                        if (!path)
 333                                path = *pathspec;
 334                        paths++;
 335                        pathspec++;
 336                }
 337        }
 338
 339        /*
 340         * Now, do the arguments look reasonable?
 341         */
 342        if (!ents) {
 343                switch (blobs) {
 344                case 0:
 345                        return builtin_diff_files(&rev, argc, argv);
 346                        break;
 347                case 1:
 348                        if (paths != 1)
 349                                usage(builtin_diff_usage);
 350                        return builtin_diff_b_f(&rev, argc, argv, blob, path);
 351                        break;
 352                case 2:
 353                        if (paths)
 354                                usage(builtin_diff_usage);
 355                        return builtin_diff_blobs(&rev, argc, argv, blob);
 356                        break;
 357                default:
 358                        usage(builtin_diff_usage);
 359                }
 360        }
 361        else if (blobs)
 362                usage(builtin_diff_usage);
 363        else if (ents == 1)
 364                return builtin_diff_index(&rev, argc, argv);
 365        else if (ents == 2)
 366                return builtin_diff_tree(&rev, argc, argv, ent);
 367        else
 368                return builtin_diff_combined(&rev, argc, argv, ent, ents);
 369        usage(builtin_diff_usage);
 370}