git-apply --whitespace=fix: fix whitespace fuzz introduced by previous run
[gitweb.git] / builtin-diff.c
index 6ed7b6842ec533902427f2d47790d57aa5082365..8d7a5697f2b429f6240a89e28422f48ef18aee3b 100644 (file)
@@ -35,7 +35,7 @@ static void stuff_change(struct diff_options *opt,
            !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
                return;
 
-       if (opt->reverse_diff) {
+       if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
                unsigned tmp;
                const unsigned char *tmp_u;
                const char *tmp_c;
@@ -176,16 +176,24 @@ static int builtin_diff_combined(struct rev_info *revs,
        return 0;
 }
 
-void add_head(struct rev_info *revs)
+static void refresh_index_quietly(void)
 {
-       unsigned char sha1[20];
-       struct object *obj;
-       if (get_sha1("HEAD", sha1))
-               return;
-       obj = parse_object(sha1);
-       if (!obj)
+       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;
-       add_pending_object(revs, obj, "HEAD");
+       discard_cache();
+       read_cache();
+       refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
+
+       if (active_cache_changed &&
+           !write_cache(fd, active_cache, active_nr))
+               commit_locked_index(lock_file);
+
+       rollback_lock_file(lock_file);
 }
 
 int cmd_diff(int argc, const char **argv, const char *prefix)
@@ -222,7 +230,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
        prefix = setup_git_directory_gently(&nongit);
        git_config(git_diff_ui_config);
        init_revisions(&rev, prefix);
-       rev.diffopt.skip_stat_unmatch = 1;
+       rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
 
        if (!setup_diff_no_index(&rev, argc, argv, nongit, prefix))
                argc = 0;
@@ -233,13 +241,14 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
                if (diff_setup_done(&rev.diffopt) < 0)
                        die("diff_setup_done failed");
        }
-       rev.diffopt.allow_external = 1;
-       rev.diffopt.recursive = 1;
+       DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
+       DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
 
-       /* If the user asked for our exit code then don't start a
+       /*
+        * 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)
+       if (!DIFF_OPT_TST(&rev.diffopt, EXIT_WITH_STATUS))
                setup_pager();
 
        /* Do we have --cached and not have a pending object, then
@@ -252,7 +261,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
                        if (!strcmp(arg, "--"))
                                break;
                        else if (!strcmp(arg, "--cached")) {
-                               add_head(&rev);
+                               add_head_to_pending(&rev);
                                if (!rev.pending.nr)
                                        die("No HEAD commit to compare with (yet)");
                                break;
@@ -343,14 +352,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
        else
                result = builtin_diff_combined(&rev, argc, argv,
                                             ent, ents);
-       if (rev.diffopt.exit_with_status)
-               result = rev.diffopt.has_changes;
-
-       if ((rev.diffopt.output_format & DIFF_FORMAT_PATCH)
-           && (1 < rev.diffopt.skip_stat_unmatch))
-               printf("Warning: %d path%s touched but unmodified. "
-                      "Consider running git-status.\n",
-                      rev.diffopt.skip_stat_unmatch - 1,
-                      rev.diffopt.skip_stat_unmatch == 2 ? "" : "s");
+       result = diff_result_code(&rev.diffopt, result);
+       if (1 < rev.diffopt.skip_stat_unmatch)
+               refresh_index_quietly();
        return result;
 }