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