diff: don't read index when --no-index is given

git diff --no-index ... currently reads the index, during setup, when calling gitmodules_config(). This results in worse performance when the index is not actually needed. This patch avoids calling gitmodules_config() when the --no-index option is given. The times for executing "git diff --no-index" in the WebKit repository are improved as follows: Test HEAD~3 HEAD ------------------------------------------------------------------ 4001.1: diff --no-index 0.24(0.15+0.09) 0.01(0.00+0.00) -95.8% An additional improvement of this patch is that "git diff --no-index" no longer breaks when the index file is corrupt, which makes it possible to use it for investigating the broken repository. To improve the possible usage as investigation tool for broken repositories, setup_git_directory_gently() is also not called when the --no-index option is given. Also add a test to guard against future breakages, and a performance test to show the improvements. Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Gummerer committed Dec 11, 2013 at 10:58 UTC 6df5762db354ca55a0cf77451d06b332b7de0b82
3 files changed +42 -2
builtin/diff.c
+5 -2
@@ -298,7 +298,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
298 break;
299 }
300
301 - prefix = setup_git_directory_gently(&nongit);
301 + if (!no_index)
302 + prefix = setup_git_directory_gently(&nongit);
303 +
304 /*
305 * Treat git diff with at least one path outside of the
306 * repo the same as if the command would have been executed
@@ -311,7 +313,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
313 !path_inside_repo(prefix, argv[i + 1]))))
314 no_index = DIFF_NO_INDEX_IMPLICIT;
315
314 - gitmodules_config();
316 + if (!no_index)
317 + gitmodules_config();
318 git_config(git_diff_ui_config, NULL);
319
320 init_revisions(&rev, prefix);
t/perf/p4001-diff-no-index.sh new
+22
@@ -0,0 +1,22 @@
1 +#!/bin/sh
2 +
3 +test_description="Test diff --no-index performance"
4 +
5 +. ./perf-lib.sh
6 +
7 +test_perf_large_repo
8 +test_checkout_worktree
9 +
10 +file1=$(git ls-files | tail -n 2 | head -1)
11 +file2=$(git ls-files | tail -n 1 | head -1)
12 +
13 +test_expect_success "empty files, so they take no time to diff" "
14 + echo >$file1 &&
15 + echo >$file2
16 +"
17 +
18 +test_perf "diff --no-index" "
19 + git diff --no-index $file1 $file2 >/dev/null
20 +"
21 +
22 +test_done
t/t4053-diff-no-index.sh
+15
@@ -29,4 +29,19 @@ test_expect_success 'git diff --no-index relative path outside repo' '
29 )
30 '
31
32 +test_expect_success 'git diff --no-index with broken index' '
33 + (
34 + cd repo &&
35 + echo broken >.git/index &&
36 + git diff --no-index a ../non/git/a
37 + )
38 +'
39 +
40 +test_expect_success 'git diff outside repo with broken index' '
41 + (
42 + cd repo &&
43 + git diff ../non/git/a ../non/git/b
44 + )
45 +'
46 +
47 test_done