git-commit: initialize TMP_INDEX just to be sure.
[gitweb.git] / builtin-diff.c
index ae901dd25ea4d8d82752db5b28f354fd2eadfb56..f77352b40decdc4cf8d2db29102703cb1193b49c 100644 (file)
 #include "log-tree.h"
 #include "builtin.h"
 
-/* NEEDSWORK: struct object has place for name but we _do_
- * know mode when we extracted the blob out of a tree, which
- * we currently lose.
- */
 struct blobinfo {
        unsigned char sha1[20];
        const char *name;
+       unsigned mode;
 };
 
 static const char builtin_diff_usage[] =
-"diff <options> <rev>{0,2} -- <path>*";
-
-static int builtin_diff_files(struct rev_info *revs,
-                             int argc, const char **argv)
-{
-       int silent = 0;
-       while (1 < argc) {
-               const char *arg = argv[1];
-               if (!strcmp(arg, "--base"))
-                       revs->max_count = 1;
-               else if (!strcmp(arg, "--ours"))
-                       revs->max_count = 2;
-               else if (!strcmp(arg, "--theirs"))
-                       revs->max_count = 3;
-               else if (!strcmp(arg, "-q"))
-                       silent = 1;
-               else
-                       usage(builtin_diff_usage);
-               argv++; argc--;
-       }
-       /*
-        * Make sure there are NO revision (i.e. pending object) parameter,
-        * specified rev.max_count is reasonable (0 <= n <= 3), and
-        * there is no other revision filtering parameter.
-        */
-       if (revs->pending.nr ||
-           revs->min_age != -1 ||
-           revs->max_age != -1 ||
-           3 < revs->max_count)
-               usage(builtin_diff_usage);
-       if (revs->max_count < 0 &&
-           (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
-               revs->combine_merges = revs->dense_combined_merges = 1;
-       /*
-        * Backward compatibility wart - "diff-files -s" used to
-        * defeat the common diff option "-s" which asked for
-        * DIFF_FORMAT_NO_OUTPUT.
-        */
-       if (revs->diffopt.output_format == DIFF_FORMAT_NO_OUTPUT)
-               revs->diffopt.output_format = DIFF_FORMAT_RAW;
-       return run_diff_files(revs, silent);
-}
+"git-diff <options> <rev>{0,2} -- <path>*";
 
 static void stuff_change(struct diff_options *opt,
                         unsigned old_mode, unsigned new_mode,
@@ -75,9 +31,8 @@ static void stuff_change(struct diff_options *opt,
 {
        struct diff_filespec *one, *two;
 
-       if (memcmp(null_sha1, old_sha1, 20) &&
-           memcmp(null_sha1, new_sha1, 20) &&
-           !memcmp(old_sha1, new_sha1, 20))
+       if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
+           !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
                return;
 
        if (opt->reverse_diff) {
@@ -112,8 +67,12 @@ static int builtin_diff_b_f(struct rev_info *revs,
                die("'%s': %s", path, strerror(errno));
        if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
                die("'%s': not a regular file or symlink", path);
+
+       if (blob[0].mode == S_IFINVALID)
+               blob[0].mode = canon_mode(st.st_mode);
+
        stuff_change(&revs->diffopt,
-                    canon_mode(st.st_mode), canon_mode(st.st_mode),
+                    blob[0].mode, canon_mode(st.st_mode),
                     blob[0].sha1, null_sha1,
                     path, path);
        diffcore_std(&revs->diffopt);
@@ -125,18 +84,21 @@ static int builtin_diff_blobs(struct rev_info *revs,
                              int argc, const char **argv,
                              struct blobinfo *blob)
 {
-       /* Blobs: the arguments are reversed when setup_revisions()
-        * picked them up.
-        */
        unsigned mode = canon_mode(S_IFREG | 0644);
 
        if (argc > 1)
                usage(builtin_diff_usage);
 
+       if (blob[0].mode == S_IFINVALID)
+               blob[0].mode = mode;
+
+       if (blob[1].mode == S_IFINVALID)
+               blob[1].mode = mode;
+
        stuff_change(&revs->diffopt,
-                    mode, mode,
-                    blob[1].sha1, blob[0].sha1,
-                    blob[0].name, blob[0].name);
+                    blob[0].mode, blob[1].mode,
+                    blob[0].sha1, blob[1].sha1,
+                    blob[0].name, blob[1].name);
        diffcore_std(&revs->diffopt);
        diff_flush(&revs->diffopt);
        return 0;
@@ -162,6 +124,10 @@ static int builtin_diff_index(struct rev_info *revs,
            revs->max_count != -1 || revs->min_age != -1 ||
            revs->max_age != -1)
                usage(builtin_diff_usage);
+       if (read_cache() < 0) {
+               perror("read_cache");
+               return -1;
+       }
        return run_diff_index(revs, cached);
 }
 
@@ -203,7 +169,8 @@ static int builtin_diff_combined(struct rev_info *revs,
        parent = xmalloc(ents * sizeof(*parent));
        /* Again, the revs are all reverse */
        for (i = 0; i < ents; i++)
-               memcpy(parent + i, ent[ents - 1 - i].item->sha1, 20);
+               hashcpy((unsigned char *)(parent + i),
+                       ent[ents - 1 - i].item->sha1);
        diff_tree_combined(parent[0], parent + 1, ents - 1,
                           revs->dense_combined_merges, revs);
        return 0;
@@ -221,7 +188,31 @@ void add_head(struct rev_info *revs)
        add_pending_object(revs, obj, "HEAD");
 }
 
-int cmd_diff(int argc, const char **argv, char **envp)
+static void refresh_index_quietly(void)
+{
+       struct lock_file *lock_file;
+       int fd;
+
+       lock_file = xcalloc(1, sizeof(struct lock_file));
+       fd = hold_locked_index(lock_file, 0);
+       if (fd < 0)
+               return;
+       discard_cache();
+       read_cache();
+       refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
+       if (active_cache_changed) {
+               if (write_cache(fd, active_cache, active_nr) ||
+                   close(fd) ||
+                   commit_locked_index(lock_file))
+                       ; /*
+                          * silently ignore it -- we haven't mucked
+                          * with the real index.
+                          */
+       }
+       rollback_lock_file(lock_file);
+}
+
+int cmd_diff(int argc, const char **argv, const char *prefix)
 {
        int i;
        struct rev_info rev;
@@ -229,6 +220,8 @@ int cmd_diff(int argc, const char **argv, char **envp)
        int ents = 0, blobs = 0, paths = 0;
        const char *path = NULL;
        struct blobinfo blob[2];
+       int nongit = 0;
+       int result = 0;
 
        /*
         * We could get N tree-ish in the rev.pending_objects list.
@@ -250,14 +243,28 @@ int cmd_diff(int argc, const char **argv, char **envp)
         * Other cases are errors.
         */
 
+       prefix = setup_git_directory_gently(&nongit);
        git_config(git_diff_ui_config);
-       init_revisions(&rev);
+       init_revisions(&rev, prefix);
+       rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
 
-       argc = setup_revisions(argc, argv, &rev, NULL);
+       if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
+               argc = 0;
+       else
+               argc = setup_revisions(argc, argv, &rev, NULL);
        if (!rev.diffopt.output_format) {
                rev.diffopt.output_format = DIFF_FORMAT_PATCH;
-               diff_setup_done(&rev.diffopt);
+               if (diff_setup_done(&rev.diffopt) < 0)
+                       die("diff_setup_done failed");
        }
+       rev.diffopt.allow_external = 1;
+       rev.diffopt.recursive = 1;
+
+       /* If the user asked for our exit code then don't start a
+        * pager or we would end up reporting its exit code instead.
+        */
+       if (!rev.diffopt.exit_with_status)
+               setup_pager();
 
        /* Do we have --cached and not have a pending object, then
         * default to HEAD by hand.  Eek.
@@ -270,6 +277,8 @@ int cmd_diff(int argc, const char **argv, char **envp)
                                break;
                        else if (!strcmp(arg, "--cached")) {
                                add_head(&rev);
+                               if (!rev.pending.nr)
+                                       die("No HEAD commit to compare with (yet)");
                                break;
                        }
                }
@@ -285,9 +294,9 @@ int cmd_diff(int argc, const char **argv, char **envp)
                obj = deref_tag(obj, NULL, 0);
                if (!obj)
                        die("invalid object '%s' given.", name);
-               if (obj->type == TYPE_COMMIT)
+               if (obj->type == OBJ_COMMIT)
                        obj = &((struct commit *)obj)->tree->object;
-               if (obj->type == TYPE_TREE) {
+               if (obj->type == OBJ_TREE) {
                        if (ARRAY_SIZE(ent) <= ents)
                                die("more than %d trees given: '%s'",
                                    (int) ARRAY_SIZE(ent), name);
@@ -297,11 +306,12 @@ int cmd_diff(int argc, const char **argv, char **envp)
                        ents++;
                        continue;
                }
-               if (obj->type == TYPE_BLOB) {
+               if (obj->type == OBJ_BLOB) {
                        if (2 <= blobs)
                                die("more than two blobs given: '%s'", name);
-                       memcpy(blob[blobs].sha1, obj->sha1, 20);
+                       hashcpy(blob[blobs].sha1, obj->sha1);
                        blob[blobs].name = name;
+                       blob[blobs].mode = list->mode;
                        blobs++;
                        continue;
 
@@ -324,17 +334,17 @@ int cmd_diff(int argc, const char **argv, char **envp)
        if (!ents) {
                switch (blobs) {
                case 0:
-                       return builtin_diff_files(&rev, argc, argv);
+                       result = run_diff_files_cmd(&rev, argc, argv);
                        break;
                case 1:
                        if (paths != 1)
                                usage(builtin_diff_usage);
-                       return builtin_diff_b_f(&rev, argc, argv, blob, path);
+                       result = builtin_diff_b_f(&rev, argc, argv, blob, path);
                        break;
                case 2:
                        if (paths)
                                usage(builtin_diff_usage);
-                       return builtin_diff_blobs(&rev, argc, argv, blob);
+                       result = builtin_diff_blobs(&rev, argc, argv, blob);
                        break;
                default:
                        usage(builtin_diff_usage);
@@ -343,10 +353,24 @@ int cmd_diff(int argc, const char **argv, char **envp)
        else if (blobs)
                usage(builtin_diff_usage);
        else if (ents == 1)
-               return builtin_diff_index(&rev, argc, argv);
+               result = builtin_diff_index(&rev, argc, argv);
        else if (ents == 2)
-               return builtin_diff_tree(&rev, argc, argv, ent);
+               result = builtin_diff_tree(&rev, argc, argv, ent);
+       else if ((ents == 3) && (ent[0].item->flags & UNINTERESTING)) {
+               /* diff A...B where there is one sane merge base between
+                * A and B.  We have ent[0] == merge-base, ent[1] == A,
+                * and ent[2] == B.  Show diff between the base and B.
+                */
+               ent[1] = ent[2];
+               result = builtin_diff_tree(&rev, argc, argv, ent);
+       }
        else
-               return builtin_diff_combined(&rev, argc, argv, ent, ents);
-       usage(builtin_diff_usage);
+               result = builtin_diff_combined(&rev, argc, argv,
+                                            ent, ents);
+       if (rev.diffopt.exit_with_status)
+               result = rev.diffopt.has_changes;
+
+       if (1 < rev.diffopt.skip_stat_unmatch)
+               refresh_index_quietly();
+       return result;
 }