diff: reuse diff setup for --no-index case
authorJeff King <peff@peff.net>
Sat, 16 Feb 2019 06:57:56 +0000 (01:57 -0500)
committerJunio C Hamano <gitster@pobox.com>
Sun, 24 Feb 2019 15:08:34 +0000 (07:08 -0800)
When "--no-index" is in effect (or implied by the arguments), git-diff
jumps early to a special code path to perform that diff. This means we
miss out on some settings like enabling --ext-diff and --textconv by
default.

Let's jump to the no-index path _after_ we've done more setup on
rev.diffopt. Since some of the options don't affect us (e.g., items
related to the index), let's re-order the setup into two blocks (see the
in-code comments).

Note that we also need to stop re-initializing the diffopt struct in
diff_no_index(). This should not be necessary, as it will already have
been initialized by cmd_diff() (and there are no other callers). That in
turn lets us drop the "repository" argument from diff_no_index (which
never made much sense, since the whole point is that you don't need a
repository).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/diff.c
diff-no-index.c
diff.h
t/t4053-diff-no-index.sh
index f0393bba23a7d95c67470508a6b63e4e41e8d46b..777ca8715626f30937d399326e45bd693ead6661 100644 (file)
@@ -337,21 +337,23 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
                       "--no-index" : "[--no-index]");
 
        }
-       if (no_index)
-               /* If this is a no-index diff, just run it and exit there. */
-               diff_no_index(the_repository, &rev, argc, argv);
-
-       /* Otherwise, we are doing the usual "git" diff */
-       rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
 
-       /* Scale to real terminal size and respect statGraphWidth config */
+       /* Set up defaults that will apply to both no-index and regular diffs. */
        rev.diffopt.stat_width = -1;
        rev.diffopt.stat_graph_width = -1;
-
-       /* Default to let external and textconv be used */
        rev.diffopt.flags.allow_external = 1;
        rev.diffopt.flags.allow_textconv = 1;
 
+       /* If this is a no-index diff, just run it and exit there. */
+       if (no_index)
+               diff_no_index(&rev, argc, argv);
+
+       /*
+        * Otherwise, we are doing the usual "git" diff; set up any
+        * further defaults that apply to regular diffs.
+        */
+       rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
+
        /*
         * Default to intent-to-add entries invisible in the
         * index. This makes them show up as new files in diff-files
index 9414e922d164e96ffedfa2504045930b058dbc80..6001baecd4c61ae22dd0aaec11bd4bc48a9152f8 100644 (file)
@@ -233,8 +233,7 @@ static void fixup_paths(const char **path, struct strbuf *replacement)
        }
 }
 
-void diff_no_index(struct repository *r,
-                  struct rev_info *revs,
+void diff_no_index(struct rev_info *revs,
                   int argc, const char **argv)
 {
        int i;
@@ -242,11 +241,6 @@ void diff_no_index(struct repository *r,
        struct strbuf replacement = STRBUF_INIT;
        const char *prefix = revs->prefix;
 
-       /*
-        * FIXME: --no-index should not look at index and we should be
-        * able to pass NULL repo. Maybe later.
-        */
-       repo_diff_setup(r, &revs->diffopt);
        for (i = 1; i < argc - 2; ) {
                int j;
                if (!strcmp(argv[i], "--no-index"))
diff --git a/diff.h b/diff.h
index ce5e8a8183e848b2e36d50bea271f687a450e3af..6e6a6730f562ec6107136868f83d569858d6c427 100644 (file)
--- a/diff.h
+++ b/diff.h
@@ -434,7 +434,7 @@ int diff_flush_patch_id(struct diff_options *, struct object_id *, int);
 
 int diff_result_code(struct diff_options *, int);
 
-void diff_no_index(struct repository *, struct rev_info *, int, const char **);
+void diff_no_index(struct rev_info *, int, const char **);
 
 int index_differs_from(const char *def, const struct diff_flags *flags,
                       int ita_invisible_in_index);
index 6e0dd6f9e5c482631147bb4770498dc4e7a8165f..4331b3118a07f900305fa783914091f73653bb45 100755 (executable)
@@ -137,4 +137,12 @@ test_expect_success 'diff --no-index from repo subdir with absolute paths' '
        test_cmp expect actual
 '
 
+test_expect_success 'diff --no-index allows external diff' '
+       test_expect_code 1 \
+               env GIT_EXTERNAL_DIFF="echo external ;:" \
+               git diff --no-index non/git/a non/git/b >actual &&
+       echo external >expect &&
+       test_cmp expect actual
+'
+
 test_done