builtin-diff.con commit Merge branch 'maint' (8807d32)
   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"git-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
  43                        usage(builtin_diff_usage);
  44                argv++; argc--;
  45        }
  46        /*
  47         * Make sure there are NO revision (i.e. pending object) parameter,
  48         * specified rev.max_count is reasonable (0 <= n <= 3), and
  49         * there is no other revision filtering parameter.
  50         */
  51        if (revs->pending.nr ||
  52            revs->min_age != -1 ||
  53            revs->max_age != -1 ||
  54            3 < revs->max_count)
  55                usage(builtin_diff_usage);
  56        if (revs->max_count < 0 &&
  57            (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
  58                revs->combine_merges = revs->dense_combined_merges = 1;
  59        return run_diff_files(revs, silent);
  60}
  61
  62static void stuff_change(struct diff_options *opt,
  63                         unsigned old_mode, unsigned new_mode,
  64                         const unsigned char *old_sha1,
  65                         const unsigned char *new_sha1,
  66                         const char *old_name,
  67                         const char *new_name)
  68{
  69        struct diff_filespec *one, *two;
  70
  71        if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
  72            !hashcmp(old_sha1, new_sha1))
  73                return;
  74
  75        if (opt->reverse_diff) {
  76                unsigned tmp;
  77                const unsigned char *tmp_u;
  78                const char *tmp_c;
  79                tmp = old_mode; old_mode = new_mode; new_mode = tmp;
  80                tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
  81                tmp_c = old_name; old_name = new_name; new_name = tmp_c;
  82        }
  83        one = alloc_filespec(old_name);
  84        two = alloc_filespec(new_name);
  85        fill_filespec(one, old_sha1, old_mode);
  86        fill_filespec(two, new_sha1, new_mode);
  87
  88        /* NEEDSWORK: shouldn't this part of diffopt??? */
  89        diff_queue(&diff_queued_diff, one, two);
  90}
  91
  92static int builtin_diff_b_f(struct rev_info *revs,
  93                            int argc, const char **argv,
  94                            struct blobinfo *blob,
  95                            const char *path)
  96{
  97        /* Blob vs file in the working tree*/
  98        struct stat st;
  99
 100        if (argc > 1)
 101                usage(builtin_diff_usage);
 102
 103        if (lstat(path, &st))
 104                die("'%s': %s", path, strerror(errno));
 105        if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
 106                die("'%s': not a regular file or symlink", path);
 107        stuff_change(&revs->diffopt,
 108                     canon_mode(st.st_mode), canon_mode(st.st_mode),
 109                     blob[0].sha1, null_sha1,
 110                     path, path);
 111        diffcore_std(&revs->diffopt);
 112        diff_flush(&revs->diffopt);
 113        return 0;
 114}
 115
 116static int builtin_diff_blobs(struct rev_info *revs,
 117                              int argc, const char **argv,
 118                              struct blobinfo *blob)
 119{
 120        unsigned mode = canon_mode(S_IFREG | 0644);
 121
 122        if (argc > 1)
 123                usage(builtin_diff_usage);
 124
 125        stuff_change(&revs->diffopt,
 126                     mode, mode,
 127                     blob[0].sha1, blob[1].sha1,
 128                     blob[0].name, blob[1].name);
 129        diffcore_std(&revs->diffopt);
 130        diff_flush(&revs->diffopt);
 131        return 0;
 132}
 133
 134static int builtin_diff_index(struct rev_info *revs,
 135                              int argc, const char **argv)
 136{
 137        int cached = 0;
 138        while (1 < argc) {
 139                const char *arg = argv[1];
 140                if (!strcmp(arg, "--cached"))
 141                        cached = 1;
 142                else
 143                        usage(builtin_diff_usage);
 144                argv++; argc--;
 145        }
 146        /*
 147         * Make sure there is one revision (i.e. pending object),
 148         * and there is no revision filtering parameters.
 149         */
 150        if (revs->pending.nr != 1 ||
 151            revs->max_count != -1 || revs->min_age != -1 ||
 152            revs->max_age != -1)
 153                usage(builtin_diff_usage);
 154        return run_diff_index(revs, cached);
 155}
 156
 157static int builtin_diff_tree(struct rev_info *revs,
 158                             int argc, const char **argv,
 159                             struct object_array_entry *ent)
 160{
 161        const unsigned char *(sha1[2]);
 162        int swap = 0;
 163
 164        if (argc > 1)
 165                usage(builtin_diff_usage);
 166
 167        /* We saw two trees, ent[0] and ent[1].
 168         * if ent[1] is uninteresting, they are swapped
 169         */
 170        if (ent[1].item->flags & UNINTERESTING)
 171                swap = 1;
 172        sha1[swap] = ent[0].item->sha1;
 173        sha1[1-swap] = ent[1].item->sha1;
 174        diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
 175        log_tree_diff_flush(revs);
 176        return 0;
 177}
 178
 179static int builtin_diff_combined(struct rev_info *revs,
 180                                 int argc, const char **argv,
 181                                 struct object_array_entry *ent,
 182                                 int ents)
 183{
 184        const unsigned char (*parent)[20];
 185        int i;
 186
 187        if (argc > 1)
 188                usage(builtin_diff_usage);
 189
 190        if (!revs->dense_combined_merges && !revs->combine_merges)
 191                revs->dense_combined_merges = revs->combine_merges = 1;
 192        parent = xmalloc(ents * sizeof(*parent));
 193        /* Again, the revs are all reverse */
 194        for (i = 0; i < ents; i++)
 195                hashcpy((unsigned char *)(parent + i),
 196                        ent[ents - 1 - i].item->sha1);
 197        diff_tree_combined(parent[0], parent + 1, ents - 1,
 198                           revs->dense_combined_merges, revs);
 199        return 0;
 200}
 201
 202void add_head(struct rev_info *revs)
 203{
 204        unsigned char sha1[20];
 205        struct object *obj;
 206        if (get_sha1("HEAD", sha1))
 207                return;
 208        obj = parse_object(sha1);
 209        if (!obj)
 210                return;
 211        add_pending_object(revs, obj, "HEAD");
 212}
 213
 214int cmd_diff(int argc, const char **argv, const char *prefix)
 215{
 216        int i;
 217        struct rev_info rev;
 218        struct object_array_entry ent[100];
 219        int ents = 0, blobs = 0, paths = 0;
 220        const char *path = NULL;
 221        struct blobinfo blob[2];
 222
 223        /*
 224         * We could get N tree-ish in the rev.pending_objects list.
 225         * Also there could be M blobs there, and P pathspecs.
 226         *
 227         * N=0, M=0:
 228         *      cache vs files (diff-files)
 229         * N=0, M=2:
 230         *      compare two random blobs.  P must be zero.
 231         * N=0, M=1, P=1:
 232         *      compare a blob with a working tree file.
 233         *
 234         * N=1, M=0:
 235         *      tree vs cache (diff-index --cached)
 236         *
 237         * N=2, M=0:
 238         *      tree vs tree (diff-tree)
 239         *
 240         * Other cases are errors.
 241         */
 242
 243        git_config(git_diff_ui_config);
 244        init_revisions(&rev, prefix);
 245
 246        argc = setup_revisions(argc, argv, &rev, NULL);
 247        if (!rev.diffopt.output_format) {
 248                rev.diffopt.output_format = DIFF_FORMAT_PATCH;
 249                if (diff_setup_done(&rev.diffopt) < 0)
 250                        die("diff_setup_done failed");
 251        }
 252
 253        /* Do we have --cached and not have a pending object, then
 254         * default to HEAD by hand.  Eek.
 255         */
 256        if (!rev.pending.nr) {
 257                int i;
 258                for (i = 1; i < argc; i++) {
 259                        const char *arg = argv[i];
 260                        if (!strcmp(arg, "--"))
 261                                break;
 262                        else if (!strcmp(arg, "--cached")) {
 263                                add_head(&rev);
 264                                if (!rev.pending.nr)
 265                                        die("No HEAD commit to compare with (yet)");
 266                                break;
 267                        }
 268                }
 269        }
 270
 271        for (i = 0; i < rev.pending.nr; i++) {
 272                struct object_array_entry *list = rev.pending.objects+i;
 273                struct object *obj = list->item;
 274                const char *name = list->name;
 275                int flags = (obj->flags & UNINTERESTING);
 276                if (!obj->parsed)
 277                        obj = parse_object(obj->sha1);
 278                obj = deref_tag(obj, NULL, 0);
 279                if (!obj)
 280                        die("invalid object '%s' given.", name);
 281                if (obj->type == OBJ_COMMIT)
 282                        obj = &((struct commit *)obj)->tree->object;
 283                if (obj->type == OBJ_TREE) {
 284                        if (ARRAY_SIZE(ent) <= ents)
 285                                die("more than %d trees given: '%s'",
 286                                    (int) ARRAY_SIZE(ent), name);
 287                        obj->flags |= flags;
 288                        ent[ents].item = obj;
 289                        ent[ents].name = name;
 290                        ents++;
 291                        continue;
 292                }
 293                if (obj->type == OBJ_BLOB) {
 294                        if (2 <= blobs)
 295                                die("more than two blobs given: '%s'", name);
 296                        hashcpy(blob[blobs].sha1, obj->sha1);
 297                        blob[blobs].name = name;
 298                        blobs++;
 299                        continue;
 300
 301                }
 302                die("unhandled object '%s' given.", name);
 303        }
 304        if (rev.prune_data) {
 305                const char **pathspec = rev.prune_data;
 306                while (*pathspec) {
 307                        if (!path)
 308                                path = *pathspec;
 309                        paths++;
 310                        pathspec++;
 311                }
 312        }
 313
 314        /*
 315         * Now, do the arguments look reasonable?
 316         */
 317        if (!ents) {
 318                switch (blobs) {
 319                case 0:
 320                        return builtin_diff_files(&rev, argc, argv);
 321                        break;
 322                case 1:
 323                        if (paths != 1)
 324                                usage(builtin_diff_usage);
 325                        return builtin_diff_b_f(&rev, argc, argv, blob, path);
 326                        break;
 327                case 2:
 328                        if (paths)
 329                                usage(builtin_diff_usage);
 330                        return builtin_diff_blobs(&rev, argc, argv, blob);
 331                        break;
 332                default:
 333                        usage(builtin_diff_usage);
 334                }
 335        }
 336        else if (blobs)
 337                usage(builtin_diff_usage);
 338        else if (ents == 1)
 339                return builtin_diff_index(&rev, argc, argv);
 340        else if (ents == 2)
 341                return builtin_diff_tree(&rev, argc, argv, ent);
 342        else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
 343                /* diff A...B where there is one sane merge base between
 344                 * A and B.  We have ent[0] == merge-base, ent[1] == A,
 345                 * and ent[2] == B.  Show diff between the base and B.
 346                 */
 347                ent[1] = ent[2];
 348                return builtin_diff_tree(&rev, argc, argv, ent);
 349        }
 350        else
 351                return builtin_diff_combined(&rev, argc, argv,
 352                                             ent, ents);
 353        usage(builtin_diff_usage);
 354}