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