Skip to contentSkip to conversation

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

PATCH In review Thomas Gummerer 6 participants 30 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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

git diff --no-index ... currently reads the index, during setup, when calling gitmodules_config(). In the usual case this gives us some performance drawbacks, but it's especially annoying if there is a broken index file.

Avoid calling the unnecessary gitmodules_config() when the --no-index option is given. Also add a test to guard against similar breakages in the future.

---
 builtin/diff.c           | 13 +++++++++++--
 t/t4053-diff-no-index.sh |  6 ++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
builtin/diff.c +11 -2
index adb93a9..47c0833 100644
@@ -257,7 +257,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	int blobs = 0, paths = 0;
 	const char *path = NULL;
 	struct blobinfo blob[2];
-	int nongit;
+	int nongit, no_index = 0;
 	int result = 0;
 
 	/*
@@ -282,9 +282,18 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 *
 	 * Other cases are errors.
 	 */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--"))
+			break;
+		if (!strcmp(argv[i], "--no-index")) {
+			no_index = 1;
+			break;
+		}
+	}
 
 	prefix = setup_git_directory_gently(&nongit);
-	gitmodules_config();
+	if (!no_index)
+		gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
t/t4053-diff-no-index.sh +6 -0
index 979e983..a24ae4d 100755
@@ -29,4 +29,10 @@ test_expect_success 'git diff --no-index relative path outside repo' '
 	)
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+	cd repo &&
+	echo broken >.git/index &&
+	test_expect_code 0 git diff --no-index a ../non/git/a
+'
+
 test_done
Signature · 1 line
1.8.4.2
#1 lore raw
Quoted 3 lines · Thomas Gummerer

git diff --no-index ... currently reads the index, during setup, when calling gitmodules_config(). In the usual case this gives us some performance drawbacks,

Makes sense.

Quoted 2 lines

but it's especially annoying if there is a broken
index file.

Is this really a normal case? It makes sense that as a side-effect it is easier to use "git diff --no-index" as a general-purpose tool while investigating a broken repo, but I would have thought that quickly learning a repo is broken is a good thing in any case.

A little more information about the context where this came up would be helpful, I guess.

[...]

Quoted 2 lines
builtin/diff.c +0 -0

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 291

Quoted 11 lines
builtin/diff.c +7 -0
@@ -282,9 +282,18 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 *
 	 * Other cases are errors.
 	 */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--"))
+			break;
+		if (!strcmp(argv[i], "--no-index")) {
+			no_index = 1;
+			break;
+		}

setup_revisions() uses the same logic that doesn't handle options that take arguments in their "unstuck" form (e.g., "--word-diff-regex --"), so this is probably not a regression, though I haven't checked. :)

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 296

Quoted 4 lines
builtin/diff.c +2 -1
 	prefix = setup_git_directory_gently(&nongit);
-	gitmodules_config();
+	if (!no_index)
+		gitmodules_config();

Perhaps we can improve performance and behavior by skipping the setup_git_directory_gently() call, too?

That would help with the repairing-broken-repository case by working even if .git/config or .git/HEAD is broken, and I think it is more intuitive that the repository-local configuration is ignored by "git diff --no-index". It would also help with performance by avoiding some filesystem access.

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 298

Quoted 4 lines
builtin/diff.c +4 -0
+test_expect_success 'git diff --no-index with broken index' '
+	cd repo &&
+	echo broken >.git/index &&
+	test_expect_code 0 git diff --no-index a ../non/git/a

Clever. I wouldn't use "test_expect_code 0", since that's already implied by including the "git diff --no-index" call in the && chain.

Thanks and hope that helps,
Jonathan

#2 lore raw

Am 09.12.2013 16:16, schrieb Jonathan Nieder:

Quoted 7 lines
Quoted 3 lines · Thomas Gummerer

git diff --no-index ... currently reads the index, during setup, when calling gitmodules_config(). In the usual case this gives us some performance drawbacks,

Makes sense.

Hmm, but this will disable the submodule specific ignore configuration options defined in the .gitmodules file, no? (E.g. when diffing two directories containing submodules)

Quoted 7 lines
Quoted 2 lines

but it's especially annoying if there is a broken
index file.

Is this really a normal case? It makes sense that as a side-effect it is easier to use "git diff --no-index" as a general-purpose tool while investigating a broken repo, but I would have thought that quickly learning a repo is broken is a good thing in any case.

But I agree that dying with "index file corrupt" is a bit strange when calling diff with --no-index. Wouldn't adding a "gently" option (which could then warn instead of dying) to gitmodules_config() be a better solution here?

#3 lore raw
Quoted 7 lines · Jens Lehmann

Am 09.12.2013 16:16, schrieb Jonathan Nieder:

Quoted 6 lines
Quoted 3 lines · Thomas Gummerer

git diff --no-index ... currently reads the index, during setup, when calling gitmodules_config(). In the usual case this gives us some performance drawbacks,

Quoted 5 lines
Quoted 1 line

Makes sense.

Hmm, but this will disable the submodule specific ignore configuration options defined in the .gitmodules file, no? (E.g. when diffing two directories containing submodules)

Yes. That seems like a good behavior.

"git diff --no-index" was invented as just a fancy version of 'diff -uR", without any awareness of the current git repository. That means that at least in principle, .gitmodules and .gitignore should not affect it.

[...]

Quoted 3 lines

Wouldn't adding a "gently" option (which
could then warn instead of dying) to gitmodules_config() be a better solution here?

I don't think so.

Thanks and hope that helps,
Jonathan

#4 lore raw
In reply to Jonathan Nieder
Quoted 11 lines · Jonathan Nieder
Quoted 3 lines · Thomas Gummerer

git diff --no-index ... currently reads the index, during setup, when calling gitmodules_config(). In the usual case this gives us some performance drawbacks,

Makes sense.

Quoted 2 lines

but it's especially annoying if there is a broken
index file.

Quoted 7 lines

Is this really a normal case? It makes sense that as a side-effect it is easier to use "git diff --no-index" as a general-purpose tool while investigating a broken repo, but I would have thought that quickly learning a repo is broken is a good thing in any case.

A little more information about the context where this came up would be helpful, I guess.

It came up while I was working on index-v5, where I had to investigate quite a few repositories where the index was broken, especially when I was changing the index format slightly. For example I would take one version, use test-dump-cache-tree to dump the cache tree to a file, change the format slightly, use test-dump-cache-tree again, and check the difference with "git diff --no-index".

This might not be a very common use case, but maybe the patch might help someone else too. (In addition to the performance improvements)

I'm not sure how much diff --no-index is used normally, but when the index is broken that would be detected relatively soon anyway. I'm not so worried about one more command working when it's broken.

Quoted 4 lines

[...]

Quoted 2 lines
builtin/diff.c +0 -0

[...]

Quoted 15 lines

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 291

Quoted 11 lines
builtin/diff.c +7 -0
@@ -282,9 +282,18 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 *
 	 * Other cases are errors.
 	 */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--"))
+			break;
+		if (!strcmp(argv[i], "--no-index")) {
+			no_index = 1;
+			break;
+		}

setup_revisions() uses the same logic that doesn't handle options that take arguments in their "unstuck" form (e.g., "--word-diff-regex --"), so this is probably not a regression, though I haven't checked. :)

The same logic is used in diff_no_index(), so I think it should be fine.

Quoted 9 lines

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 296

Quoted 4 lines
builtin/diff.c +2 -1
 	prefix = setup_git_directory_gently(&nongit);
-	gitmodules_config();
+	if (!no_index)
+		gitmodules_config();

Perhaps we can improve performance and behavior by skipping the setup_git_directory_gently() call, too?

Quoted 5 lines

That would help with the repairing-broken-repository case by working even if .git/config or .git/HEAD is broken, and I think it is more intuitive that the repository-local configuration is ignored by "git diff --no-index". It would also help with performance by avoiding some filesystem access.

Yes, I think that would make sense, thanks. I tested it, and it didn't change the performance, but it's still a good change for the broken repository case. Will change in the re-roll and add a test for that.

Quoted 9 lines

[...]

builtin/diff.c

Quoted 4 lines
builtin/diff.c +4 -0
+test_expect_success 'git diff --no-index with broken index' '
+	cd repo &&
+	echo broken >.git/index &&
+	test_expect_code 0 git diff --no-index a ../non/git/a

Clever. I wouldn't use "test_expect_code 0", since that's already implied by including the "git diff --no-index" call in the && chain.

Thanks, will change. Thanks a lot for your review. Will send a re-roll soon.

Quoted 2 lines

Thanks and hope that helps,
Jonathan

--
Thomas

#5 lore raw
Quoted 4 lines · Thomas Gummerer

For example I would take one
version, use test-dump-cache-tree to dump the cache tree to a file, change the format slightly, use test-dump-cache-tree again, and check the difference with "git diff --no-index".

Makes a lot of sense. Thanks for explaining.

Regards,
Jonathan

#6 lore raw

PATCH v2 1/1 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.

---

Thanks to Jonathan and Jens for comments on the previous round. Changes:
- Don't all setup_git_directory_gently when --no-index is given
- Add performance test
- Commit message improvements

 builtin/diff.c                | 16 +++++++++++++---
 t/perf/p4001-diff-no-index.sh | 17 +++++++++++++++++
 t/t4053-diff-no-index.sh      |  6 ++++++
 3 files changed, 36 insertions(+), 3 deletions(-)
 create mode 100755 t/perf/p4001-diff-no-index.sh
builtin/diff.c +13 -3
index adb93a9..5f09a0b 100644
@@ -257,7 +257,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	int blobs = 0, paths = 0;
 	const char *path = NULL;
 	struct blobinfo blob[2];
-	int nongit;
+	int nongit, no_index = 0;
 	int result = 0;
 
 	/*
@@ -282,9 +282,19 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 *
 	 * Other cases are errors.
 	 */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--"))
+			break;
+		if (!strcmp(argv[i], "--no-index")) {
+			no_index = 1;
+			break;
+		}
+	}
 
-	prefix = setup_git_directory_gently(&nongit);
-	gitmodules_config();
+	if (!no_index) {
+		prefix = setup_git_directory_gently(&nongit);
+		gitmodules_config();
+	}
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
t/perf/p4001-diff-no-index.sh new file +17 -0
new file mode 100755
index 0000000..81c7aa0
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+test_description="Test diff --no-index performance"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+file1=$(git ls-files | tail -n 2 | head -1)
+file2=$(git ls-files | tail -n 1 | head -1)
+
+test_perf "diff --no-index" "
+	git diff --no-index $file1 $file2 >/dev/null
+"
+
+test_done
t/t4053-diff-no-index.sh +6 -0
index 979e983..d3dbf6b 100755
@@ -29,4 +29,10 @@ test_expect_success 'git diff --no-index relative path outside repo' '
 	)
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+	cd repo &&
+	echo broken >.git/index &&
+	git diff --no-index a ../non/git/a &&
+'
+
 test_done
Signature · 1 line
1.8.5.4.g8639e57
#7 lore raw

t/t4053-diff-no-index.sh test_expect_success 'git diff --no-index relative path outside repo' ' · line 35

Quoted 5 lines · Thomas Gummerer
t/t4053-diff-no-index.sh +4 -0
+test_expect_success 'git diff --no-index with broken index' '
+	cd repo &&
+	echo broken >.git/index &&
+	git diff --no-index a ../non/git/a &&
                                           ^^

I'm confused: Does this work with the trailing && ?

(and when we use
"cd repo"
it could be good to use a sub-shell (even when this is the last test case)

#8 lore raw
In reply to Thomas Gummerer
Quoted 29 lines · Thomas Gummerer

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.

---

t/t4053-diff-no-index.sh +0 -0
index 979e983..d3dbf6b 100755

t/t4053-diff-no-index.sh test_expect_success 'git diff --no-index relative path outside repo' ' · line 36

Quoted 8 lines
t/t4053-diff-no-index.sh +5 -0
@@ -29,4 +29,10 @@ test_expect_success 'git diff --no-index relative path outside repo' '
        )
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+       cd repo &&
+       echo broken >.git/index &&
+       git diff --no-index a ../non/git/a &&
+'

Stray && on the last line of the test.

Also, don't you want to do the 'cd' and subsequent commands inside a subshell so that tests added after this one won't have to worry about cd'ing back to the top-level?

Quoted 4 lines
t/t4053-diff-no-index.sh +1 -1
+
 test_done
--

1.8.5.4.g8639e57

#9 lore raw
Quoted 41 lines · Eric Sunshine
Quoted 29 lines · Thomas Gummerer

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.

---

t/t4053-diff-no-index.sh +0 -0
index 979e983..d3dbf6b 100755

t/t4053-diff-no-index.sh test_expect_success 'git diff --no-index relative path outside repo' ' · line 36

Quoted 8 lines
t/t4053-diff-no-index.sh +5 -0
@@ -29,4 +29,10 @@ test_expect_success 'git diff --no-index relative path outside repo' '
        )
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+       cd repo &&
+       echo broken >.git/index &&
+       git diff --no-index a ../non/git/a &&
+'

Stray && on the last line of the test.

Quoted 3 lines

Also, don't you want to do the 'cd' and subsequent commands inside a subshell so that tests added after this one won't have to worry about cd'ing back to the top-level?

Thanks both to you and Torsten for catching both issues, I'll fix them in a re-roll.

Quoted 4 lines
Quoted 4 lines
t/t4053-diff-no-index.sh +1 -1
+
 test_done
--

1.8.5.4.g8639e57

--
Thomas

#10 lore raw
In reply to Thomas Gummerer
Quoted 29 lines · Thomas Gummerer

git diff --no-index ... currently reads the index, during setup, when calling gitmodules_config(). In the usual case this gives us some performance drawbacks, but it's especially annoying if there is a broken index file.

Avoid calling the unnecessary gitmodules_config() when the --no-index option is given. Also add a test to guard against similar breakages in the future.

---
 builtin/diff.c           | 13 +++++++++++--
 t/t4053-diff-no-index.sh |  6 ++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
builtin/diff.c +1 -1
index adb93a9..47c0833 100644
@@ -257,7 +257,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	int blobs = 0, paths = 0;
 	const char *path = NULL;
 	struct blobinfo blob[2];
-	int nongit;
+	int nongit, no_index = 0;
 	int result = 0;
 
 	/*
@@ -282,9 +282,18 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 *

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 292

Quoted 10 lines
builtin/diff.c +8 -0
 	 * Other cases are errors.
 	 */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--"))
+			break;
+		if (!strcmp(argv[i], "--no-index")) {
+			no_index = 1;
+			break;
+		}
+	}

This seems to duplicate only half the logic at the beginning of diff_no_index(), right? E.g., running "git diff /var/tmp/[12]" inside a working tree that is controlled by a Git repository when /var/tmp/ is outside, we do want to behave as if the command line were "git diff --no-index /var/tmp/[12]", but this half duplication makes these two behave differently, no?

I think the issue you are trying to address is worth tackling, but I wonder if a bit of preparatory refactoring is necessary to avoid the partial duplication.

Quoted 22 lines
builtin/diff.c +2 -1
 	prefix = setup_git_directory_gently(&nongit);
-	gitmodules_config();
+	if (!no_index)
+		gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
t/t4053-diff-no-index.sh +6 -0
index 979e983..a24ae4d 100755
@@ -29,4 +29,10 @@ test_expect_success 'git diff --no-index relative path outside repo' '
 	)
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+	cd repo &&
+	echo broken >.git/index &&
+	test_expect_code 0 git diff --no-index a ../non/git/a
+'
+
 test_done
#11 lore raw

[Sorry for not seeing this before sending out v2.]

Quoted 40 lines · Junio C Hamano
Quoted 38 lines · Thomas Gummerer

git diff --no-index ... currently reads the index, during setup, when calling gitmodules_config(). In the usual case this gives us some performance drawbacks, but it's especially annoying if there is a broken index file.

Avoid calling the unnecessary gitmodules_config() when the --no-index option is given. Also add a test to guard against similar breakages in the future.

---
 builtin/diff.c           | 13 +++++++++++--
 t/t4053-diff-no-index.sh |  6 ++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
builtin/diff.c +8 -1
index adb93a9..47c0833 100644
@@ -257,7 +257,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	int blobs = 0, paths = 0;
 	const char *path = NULL;
 	struct blobinfo blob[2];
-	int nongit;
+	int nongit, no_index = 0;
 	int result = 0;
 
 	/*
@@ -282,9 +282,18 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 *
 	 * Other cases are errors.
 	 */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--"))
+			break;
+		if (!strcmp(argv[i], "--no-index")) {
+			no_index = 1;
+			break;
+		}
Quoted 8 lines

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 292

Quoted 1 line
builtin/diff.c +1 -0
+	}

This seems to duplicate only half the logic at the beginning of diff_no_index(), right? E.g., running "git diff /var/tmp/[12]" inside a working tree that is controlled by a Git repository when /var/tmp/ is outside, we do want to behave as if the command line were "git diff --no-index /var/tmp/[12]", but this half duplication makes these two behave differently, no?

Yes you're right, I missed that. "git diff /var/tmp/[12]" inside a working tree with a broken index has the same problems, which should be fixed too. I'll try to fix that and send a new patch afterwards.

Quoted 19 lines

I think the issue you are trying to address is worth tackling, but I wonder if a bit of preparatory refactoring is necessary to avoid the partial duplication.

Quoted 15 lines
builtin/diff.c +2 -1
 	prefix = setup_git_directory_gently(&nongit);
-	gitmodules_config();
+	if (!no_index)
+		gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
t/t4053-diff-no-index.sh +0 -0
index 979e983..a24ae4d 100755
@@ -29,4 +29,10 @@ test_expect_success 'git diff --no-index relative path outside repo' '
 	)
 '
 
Quoted 7 lines

t/t4053-diff-no-index.sh test_expect_success 'git diff --no-index relative path outside repo' ' · line 38

Quoted 7 lines
t/t4053-diff-no-index.sh +6 -0
+test_expect_success 'git diff --no-index with broken index' '
+	cd repo &&
+	echo broken >.git/index &&
+	test_expect_code 0 git diff --no-index a ../non/git/a
+'
+
 test_done

--
Thomas

#12 lore raw

PATCH v3 1/2 diff: move no-index detection to builtin/diff.c

In reply to Junio C Hamano

Currently the --no-index option is parsed in diff_no_index(). Move the detection if a no-index diff should be executed to builtin/diff.c, where we can use it for executing diff_no_index() conditionally. This will also allow us to execute other operations conditionally, which will be done in the next patch.

---

Thanks to Junio, Torsten and Eric for comments on the previous round. I've added this refactoring patch, to avoid the partial duplication of the logic. I've also fixed the tests, that now use a sub-shell for executing and fix the stray && at the end of the test.

 builtin/diff.c  | 45 ++++++++++++++++++++++++++++++++++++++++++---
 diff-no-index.c | 48 +++---------------------------------------------
 diff.h          |  2 +-
 3 files changed, 46 insertions(+), 49 deletions(-)
builtin/diff.c +42 -3
index adb93a9..7220b2c 100644
@@ -257,7 +257,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	int blobs = 0, paths = 0;
 	const char *path = NULL;
 	struct blobinfo blob[2];
-	int nongit;
+	int nongit, no_index = 0;
 	int result = 0;
 
 	/*
@@ -283,14 +283,53 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 * Other cases are errors.
 	 */
 
+	/* Were we asked to do --no-index explicitly? */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--")) {
+			i++;
+			break;
+		}
+		if (!strcmp(argv[i], "--no-index"))
+			no_index = 1;
+		if (argv[i][0] != '-')
+			break;
+	}
+
 	prefix = setup_git_directory_gently(&nongit);
+	/*
+	 * Treat git diff with at least one path outside of the
+	 * repo the same as if the command would have been executed
+	 * outside of a git repository.  In this case it behaves
+	 * the same way as "git diff --no-index <a> <b>", which acts
+	 * as a colourful "diff" replacement.
+	 */
+	nongit |= (argc == i + 2) && !(path_inside_repo(prefix, argv[i]) &&
+				       path_inside_repo(prefix, argv[i + 1]));
 	gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
 
-	/* If this is a no-index diff, just run it and exit there. */
-	diff_no_index(&rev, argc, argv, nongit, prefix);
+	if (no_index || nongit) {
+		if (argc != i + 2) {
+			if (!no_index) {
+				/*
+				 * There was no --no-index and there were not two
+				 * paths. It is possible that the user intended
+				 * to do an inside-repository operation.
+				 */
+				fprintf(stderr, "Not a git repository\n");
+				fprintf(stderr,
+					"To compare two paths outside a working tree:\n");
+			}
+			/* Give the usage message for non-repository usage and exit. */
+			usagef("git diff %s <path> <path>",
+			       no_index ? "--no-index" : "[--no-index]");
+
+		}
+		/* If this is a no-index diff, just run it and exit there. */
+		diff_no_index(&rev, argc, argv, nongit, no_index, prefix);
+	}
 
 	/* Otherwise, we are doing the usual "git" diff */
 	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
diff-no-index.c +3 -45

59 lines not on this page · raw message

diff.h +1 -1
index e342325..3e1828a 100644
@@ -330,7 +330,7 @@ extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
 
 extern int diff_result_code(struct diff_options *, int);
 
-extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);
+extern int diff_no_index(struct rev_info *, int, const char **, int, int, const char *);
 
 extern int index_differs_from(const char *def, int diff_flags);
 
Signature · 1 line
1.8.5.4.g8639e57
#13 lore raw

PATCH v3 2/2 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.

---
 builtin/diff.c                |  7 +++++--
 t/perf/p4001-diff-no-index.sh | 17 +++++++++++++++++
 t/t4053-diff-no-index.sh      | 15 +++++++++++++++
 3 files changed, 37 insertions(+), 2 deletions(-)
 create mode 100755 t/perf/p4001-diff-no-index.sh
builtin/diff.c +5 -2
index 7220b2c..9ce8bec 100644
@@ -295,7 +295,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 			break;
 	}
 
-	prefix = setup_git_directory_gently(&nongit);
+	if (!no_index)
+		prefix = setup_git_directory_gently(&nongit);
+
 	/*
 	 * Treat git diff with at least one path outside of the
 	 * repo the same as if the command would have been executed
@@ -305,7 +307,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 */
 	nongit |= (argc == i + 2) && !(path_inside_repo(prefix, argv[i]) &&
 				       path_inside_repo(prefix, argv[i + 1]));
-	gitmodules_config();
+	if (!no_index && !nongit)
+		gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
t/perf/p4001-diff-no-index.sh new file +17 -0
new file mode 100755
index 0000000..81c7aa0
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+test_description="Test diff --no-index performance"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+file1=$(git ls-files | tail -n 2 | head -1)
+file2=$(git ls-files | tail -n 1 | head -1)
+
+test_perf "diff --no-index" "
+	git diff --no-index $file1 $file2 >/dev/null
+"
+
+test_done
t/t4053-diff-no-index.sh +15 -0
index 979e983..077c775 100755
@@ -29,4 +29,19 @@ test_expect_success 'git diff --no-index relative path outside repo' '
 	)
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+	(
+		cd repo &&
+		echo broken >.git/index &&
+		git diff --no-index a ../non/git/a
+	)
+'
+
+test_expect_success 'git diff outside repo with broken index' '
+	(
+		cd repo &&
+		git diff ../non/git/a ../non/git/b
+	)
+'
+
 test_done
Signature · 1 line
1.8.5.4.g8639e57
#14 lore raw

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 299

Quoted 9 lines · Thomas Gummerer
builtin/diff.c +2 -1
@@ -295,7 +295,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 			break;
 	}
 
-	prefix = setup_git_directory_gently(&nongit);
+	if (!no_index)
+		prefix = setup_git_directory_gently(&nongit);

What is the value of nongit in the no_index case?

#15 lore raw
Quoted 8 lines · Jonathan Nieder
Quoted 6 lines · Thomas Gummerer
builtin/diff.c +0 -0
@@ -295,7 +295,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 			break;
 	}
 
Quoted 5 lines

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 299

Quoted 3 lines
builtin/diff.c +2 -1
-	prefix = setup_git_directory_gently(&nongit);
+	if (!no_index)
+		prefix = setup_git_directory_gently(&nongit);

What is the value of nongit in the no_index case?

In the no_index case it doesn't matter, since no_index is always checked first. The only other place where it is used, when no_index is set is after diff_no_index, which can't be reached if no_index is set. I could initialize nongit to 0, but I don't think that would change anything.

I've also seen that the no_index and nongit parameters to diff_no_index are not needed anymore, and will remove them in the re-roll therefore.

I'll wait to see if there are any more comments and then send a re-roll.

Thanks!

--
Thomas

#16 lore raw

Re: [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c

In reply to Thomas Gummerer

(pruning cc list)
Hi,

Quoted 2 lines · Thomas Gummerer

Currently the --no-index option is parsed in diff_no_index(). Move the detection if a no-index diff should be executed to builtin/diff.c

No functional change intended, I assume?

[...]

Quoted 3 lines
builtin/diff.c +0 -0
@@ -283,14 +283,53 @@ int cmd_diff(int argc, const char **argv, const char *prefix)

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 307

Quoted 10 lines
builtin/diff.c +9 -0
 	prefix = setup_git_directory_gently(&nongit);
+	/*
+	 * Treat git diff with at least one path outside of the
+	 * repo the same as if the command would have been executed
+	 * outside of a git repository.  In this case it behaves
+	 * the same way as "git diff --no-index <a> <b>", which acts
+	 * as a colourful "diff" replacement.
+	 */
+	nongit |= (argc == i + 2) && !(path_inside_repo(prefix, argv[i]) &&
+				       path_inside_repo(prefix, argv[i + 1]));

I would find this easier to read as

	if (argc == i + 2 &&
	    (!path_inside_repo(prefix, argv[i]) ||
	     !path_inside_repo(prefix, argv[i + 1])))
		nongit = 1;

Or maybe using a different variable than 'nongit':

#define DIFF_NO_INDEX_EXPLICIT 1
#define DIFF_NO_INDEX_IMPLICIT 2

	...
	if (argc == i + 2 && ...)
		no_index = DIFF_NO_INDEX_IMPLICIT;

[...]

Quoted 8 lines
builtin/diff.c +1 -2
 	gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
 
-	/* If this is a no-index diff, just run it and exit there. */
-	diff_no_index(&rev, argc, argv, nongit, prefix);
+	if (no_index || nongit) {

[...]

builtin/diff.c

Quoted 1 line
builtin/diff.c +1 -0
+	}

Ok.

[...]

diff-no-index.c static int queue_diff(struct diff_options *o, · line 184

Quoted 8 lines
diff-no-index.c +1 -1
@@ -181,56 +181,14 @@ static int queue_diff(struct diff_options *o,
 	}
 }
 
-void diff_no_index(struct rev_info *revs,
+int diff_no_index(struct rev_info *revs,

Why the change in return type?

Hope that helps,
Jonathan

#17 lore raw

PATCH v4 1/2 diff: move no-index detection to builtin/diff.c

Currently the --no-index option is parsed in diff_no_index(). Move the detection if a no-index diff should be executed to builtin/diff.c, where we can use it for executing diff_no_index() conditionally. This will also allow us to execute other operations conditionally, which will be done in the next patch.

There are no functional changes.

---

Thanks to Jonathan for the suggestions in the previous round. Since the last roud I've made the no_index detection easier to read, and use two different values for no_index to indicate whether the no_index option is given explicitly, or if a no_index diff should be executed implicitly.

 builtin/diff.c  | 52 +++++++++++++++++++++++++++++++++++++++++++++++++---
 diff-no-index.c | 44 +-------------------------------------------
 diff.h          |  2 +-
 3 files changed, 51 insertions(+), 47 deletions(-)
builtin/diff.c +49 -3
index adb93a9..da69e4a 100644
@@ -16,6 +16,9 @@
 #include "submodule.h"
 #include "sha1-array.h"
 
+#define DIFF_NO_INDEX_EXPLICIT 1
+#define DIFF_NO_INDEX_IMPLICIT 2
+
 struct blobinfo {
 	unsigned char sha1[20];
 	const char *name;
@@ -257,7 +260,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	int blobs = 0, paths = 0;
 	const char *path = NULL;
 	struct blobinfo blob[2];
-	int nongit;
+	int nongit = 0, no_index = 0;
 	int result = 0;
 
 	/*
@@ -283,14 +286,57 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 * Other cases are errors.
 	 */
 
+	/* Were we asked to do --no-index explicitly? */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--")) {
+			i++;
+			break;
+		}
+		if (!strcmp(argv[i], "--no-index"))
+			no_index = DIFF_NO_INDEX_EXPLICIT;
+		if (argv[i][0] != '-')
+			break;
+	}
+
 	prefix = setup_git_directory_gently(&nongit);
+	/*
+	 * Treat git diff with at least one path outside of the
+	 * repo the same as if the command would have been executed
+	 * outside of a git repository.  In this case it behaves
+	 * the same way as "git diff --no-index <a> <b>", which acts
+	 * as a colourful "diff" replacement.
+	 */
+	if (nongit || ((argc == i + 2) &&
+		       (!path_inside_repo(prefix, argv[i]) ||
+			!path_inside_repo(prefix, argv[i + 1]))))
+		no_index = DIFF_NO_INDEX_IMPLICIT;
+
 	gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
 
-	/* If this is a no-index diff, just run it and exit there. */
-	diff_no_index(&rev, argc, argv, nongit, prefix);
+	if (no_index) {
+		if (argc != i + 2) {
+			if (no_index == DIFF_NO_INDEX_IMPLICIT) {
+				/*
+				 * There was no --no-index and there were not two
+				 * paths. It is possible that the user intended
+				 * to do an inside-repository operation.
+				 */
+				fprintf(stderr, "Not a git repository\n");
+				fprintf(stderr,
+					"To compare two paths outside a working tree:\n");
+			}
+			/* Give the usage message for non-repository usage and exit. */
+			usagef("git diff %s <path> <path>",
+			       no_index == DIFF_NO_INDEX_EXPLICIT ?
+					"--no-index" : "[--no-index]");
+
+		}
+		/* If this is a no-index diff, just run it and exit there. */
+		diff_no_index(&rev, argc, argv, prefix);
+	}
 
 	/* Otherwise, we are doing the usual "git" diff */
 	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
diff-no-index.c +1 -43

55 lines not on this page · raw message

diff.h +1 -1
index e342325..de105d3 100644
@@ -330,7 +330,7 @@ extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
 
 extern int diff_result_code(struct diff_options *, int);
 
-extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);
+extern void diff_no_index(struct rev_info *, int, const char **, const char *);
 
 extern int index_differs_from(const char *def, int diff_flags);
 
Signature · 1 line
1.8.5.4.g8639e57
#18 lore raw

PATCH v4 2/2 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.

---
 builtin/diff.c                |  7 +++++--
 t/perf/p4001-diff-no-index.sh | 22 ++++++++++++++++++++++
 t/t4053-diff-no-index.sh      | 15 +++++++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100755 t/perf/p4001-diff-no-index.sh
builtin/diff.c +5 -2
index da69e4a..ea1dd65 100644
@@ -298,7 +298,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 			break;
 	}
 
-	prefix = setup_git_directory_gently(&nongit);
+	if (!no_index)
+		prefix = setup_git_directory_gently(&nongit);
+
 	/*
 	 * Treat git diff with at least one path outside of the
 	 * 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)
 			!path_inside_repo(prefix, argv[i + 1]))))
 		no_index = DIFF_NO_INDEX_IMPLICIT;
 
-	gitmodules_config();
+	if (!no_index)
+		gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
t/perf/p4001-diff-no-index.sh new file +22 -0
new file mode 100755
index 0000000..683be69
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description="Test diff --no-index performance"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+file1=$(git ls-files | tail -n 2 | head -1)
+file2=$(git ls-files | tail -n 1 | head -1)
+
+test_expect_success "empty files, so they take no time to diff" "
+	echo >$file1 &&
+	echo >$file2
+"
+
+test_perf "diff --no-index" "
+	git diff --no-index $file1 $file2 >/dev/null
+"
+
+test_done
t/t4053-diff-no-index.sh +15 -0
index 979e983..077c775 100755
@@ -29,4 +29,19 @@ test_expect_success 'git diff --no-index relative path outside repo' '
 	)
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+	(
+		cd repo &&
+		echo broken >.git/index &&
+		git diff --no-index a ../non/git/a
+	)
+'
+
+test_expect_success 'git diff outside repo with broken index' '
+	(
+		cd repo &&
+		git diff ../non/git/a ../non/git/b
+	)
+'
+
 test_done
Signature · 1 line
1.8.5.4.g8639e57
#29 lore raw

Re: [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c

In reply to Jonathan Nieder
Quoted 9 lines · Jonathan Nieder

(pruning cc list)
Hi,

Quoted 2 lines · Thomas Gummerer

Currently the --no-index option is parsed in diff_no_index(). Move the detection if a no-index diff should be executed to builtin/diff.c

No functional change intended, I assume?

Correct, I thought I'd split the refactoring from the functionality changes.

Quoted 23 lines

[...]

Quoted 3 lines
builtin/diff.c +0 -0
@@ -283,14 +283,53 @@ int cmd_diff(int argc, const char **argv, const char *prefix)

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 307

Quoted 10 lines
builtin/diff.c +9 -0
 	prefix = setup_git_directory_gently(&nongit);
+	/*
+	 * Treat git diff with at least one path outside of the
+	 * repo the same as if the command would have been executed
+	 * outside of a git repository.  In this case it behaves
+	 * the same way as "git diff --no-index <a> <b>", which acts
+	 * as a colourful "diff" replacement.
+	 */
+	nongit |= (argc == i + 2) && !(path_inside_repo(prefix, argv[i]) &&
+				       path_inside_repo(prefix, argv[i + 1]));

I would find this easier to read as

	if (argc == i + 2 &&
	    (!path_inside_repo(prefix, argv[i]) ||
	     !path_inside_repo(prefix, argv[i + 1])))
		nongit = 1;
Quoted 8 lines

Or maybe using a different variable than 'nongit':

#define DIFF_NO_INDEX_EXPLICIT 1
#define DIFF_NO_INDEX_IMPLICIT 2

	...
	if (argc == i + 2 && ...)
		no_index = DIFF_NO_INDEX_IMPLICIT;

Thanks, that makes sense, will change in the re-roll.

Quoted 21 lines

[...]

Quoted 8 lines
builtin/diff.c +1 -2
 	gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
 
-	/* If this is a no-index diff, just run it and exit there. */
-	diff_no_index(&rev, argc, argv, nongit, prefix);
+	if (no_index || nongit) {

[...]

builtin/diff.c

Quoted 1 line
builtin/diff.c +1 -0
+	}

Ok.

[...]

Quoted 6 lines
diff-no-index.c +0 -0
@@ -181,56 +181,14 @@ static int queue_diff(struct diff_options *o,
 	}
 }
 
Quoted 4 lines

diff-no-index.c static int queue_diff(struct diff_options *o, · line 184

Quoted 2 lines
diff-no-index.c +1 -1
-void diff_no_index(struct rev_info *revs,
+int diff_no_index(struct rev_info *revs,

Why the change in return type?

That was an oversight, no change is required. Will fix in the re-roll. Thanks for taking the time for reviewing this.

Quoted 2 lines

Hope that helps,
Jonathan

--
Thomas

#20 lore raw
In reply to Thomas Gummerer
Quoted 19 lines · Thomas Gummerer

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.

Quoted 5 lines

Also add a test to guard against future breakages, and a performance test to show the improvements.

---

Looks reasonable.

Thanks. Will queue.

Quoted 83 lines
 builtin/diff.c                |  7 +++++--
 t/perf/p4001-diff-no-index.sh | 22 ++++++++++++++++++++++
 t/t4053-diff-no-index.sh      | 15 +++++++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100755 t/perf/p4001-diff-no-index.sh
builtin/diff.c +5 -2
index da69e4a..ea1dd65 100644
@@ -298,7 +298,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 			break;
 	}
 
-	prefix = setup_git_directory_gently(&nongit);
+	if (!no_index)
+		prefix = setup_git_directory_gently(&nongit);
+
 	/*
 	 * Treat git diff with at least one path outside of the
 	 * 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)
 			!path_inside_repo(prefix, argv[i + 1]))))
 		no_index = DIFF_NO_INDEX_IMPLICIT;
 
-	gitmodules_config();
+	if (!no_index)
+		gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
t/perf/p4001-diff-no-index.sh new file +22 -0
new file mode 100755
index 0000000..683be69
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description="Test diff --no-index performance"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+file1=$(git ls-files | tail -n 2 | head -1)
+file2=$(git ls-files | tail -n 1 | head -1)
+
+test_expect_success "empty files, so they take no time to diff" "
+	echo >$file1 &&
+	echo >$file2
+"
+
+test_perf "diff --no-index" "
+	git diff --no-index $file1 $file2 >/dev/null
+"
+
+test_done
t/t4053-diff-no-index.sh +15 -0
index 979e983..077c775 100755
@@ -29,4 +29,19 @@ test_expect_success 'git diff --no-index relative path outside repo' '
 	)
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+	(
+		cd repo &&
+		echo broken >.git/index &&
+		git diff --no-index a ../non/git/a
+	)
+'
+
+test_expect_success 'git diff outside repo with broken index' '
+	(
+		cd repo &&
+		git diff ../non/git/a ../non/git/b
+	)
+'
+
 test_done
#21 lore raw

Re: [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c

In reply to Thomas Gummerer

Hi,

Quoted 1 line · Thomas Gummerer

Thanks, and sorry for the slow follow-up.

[...]

Quoted 3 lines
builtin/diff.c +0 -0
@@ -16,6 +16,9 @@

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 296

Quoted 12 lines
builtin/diff.c +8 -0
@@ -283,14 +286,57 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 * Other cases are errors.
 	 */
 
+	/* Were we asked to do --no-index explicitly? */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--")) {
+			i++;
+			break;
+		}
+		if (!strcmp(argv[i], "--no-index"))
+			no_index = DIFF_NO_INDEX_EXPLICIT;

Neat.

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 312

Quoted 11 lines
builtin/diff.c +11 -0
+	/*
+	 * Treat git diff with at least one path outside of the
+	 * repo the same as if the command would have been executed
+	 * outside of a git repository.  In this case it behaves
+	 * the same way as "git diff --no-index <a> <b>", which acts
+	 * as a colourful "diff" replacement.
+	 */
+	if (nongit || ((argc == i + 2) &&
+		       (!path_inside_repo(prefix, argv[i]) ||
+			!path_inside_repo(prefix, argv[i + 1]))))
+		no_index = DIFF_NO_INDEX_IMPLICIT;

What happens if I run 'git diff --no-index /tmp git.c git.c' from within a git repository? With this, I fear I will get

Not a git repository
To compare two paths outside a working tree:
usage: git diff [--no-index] <path> <path>

instead of the expected

	usage: git diff --no-index <path> <path>

Something like

	if (no_index)
		;
	else if (nongit)
		no_index = DIFF_NO_INDEX_IMPLICIT;
	else if (argc != i + 2)
		;
	else if (!path_inside_repo(prefix, argv[i]) ||
		 !path_inside_repo(prefix, argv[i + 1]))
		no_index = DIFF_NO_INDEX_IMPLICIT;

should work. Or:

	if (!no_index) {
		/*
		 * Treat git diff with ...
		 */
		if (nongit || ...)
			no_index = DIFF_NO_INDEX_IMPLICIT;
	}

Or the '!no_index &&' condition inserted some other way.

Quoted 4 lines
builtin/diff.c +2 -2
-	/* If this is a no-index diff, just run it and exit there. */
-	diff_no_index(&rev, argc, argv, nongit, prefix);
+	if (no_index) {
+		if (argc != i + 2) {

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 339

Quoted 9 lines
builtin/diff.c +9 -0
+			/* Give the usage message for non-repository usage and exit. */
+			usagef("git diff %s <path> <path>",
+			       no_index == DIFF_NO_INDEX_EXPLICIT ?
+					"--no-index" : "[--no-index]");
+
+		}
+		/* If this is a no-index diff, just run it and exit there. */
+		diff_no_index(&rev, argc, argv, prefix);
+	}

Perhaps, to avoid some nesting:

	/* A no-index diff takes exactly two path arguments. */
	if (no_index && argc != i + 2) {
		...
		usagef(...);
	}

	if (no_index)
		/* Just run the diff and exit. */
		diff_no_index(...);

Jonathan

#22 lore raw

Re: [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c

Quoted 29 lines · Jonathan Nieder

Hi,

Quoted 1 line · Thomas Gummerer

Thanks, and sorry for the slow follow-up.

[...]

Quoted 3 lines
builtin/diff.c +0 -0
@@ -16,6 +16,9 @@

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 296

Quoted 12 lines
builtin/diff.c +8 -0
@@ -283,14 +286,57 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 * Other cases are errors.
 	 */
 
+	/* Were we asked to do --no-index explicitly? */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--")) {
+			i++;
+			break;
+		}
+		if (!strcmp(argv[i], "--no-index"))
+			no_index = DIFF_NO_INDEX_EXPLICIT;

Neat.

[...]

Quoted 14 lines

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 312

Quoted 11 lines
builtin/diff.c +11 -0
+	/*
+	 * Treat git diff with at least one path outside of the
+	 * repo the same as if the command would have been executed
+	 * outside of a git repository.  In this case it behaves
+	 * the same way as "git diff --no-index <a> <b>", which acts
+	 * as a colourful "diff" replacement.
+	 */
+	if (nongit || ((argc == i + 2) &&
+		       (!path_inside_repo(prefix, argv[i]) ||
+			!path_inside_repo(prefix, argv[i + 1]))))
+		no_index = DIFF_NO_INDEX_IMPLICIT;

What happens if I run 'git diff --no-index /tmp git.c git.c' from within a git repository? With this, I fear I will get

Thanks, I've missed that one. It only happens when run outside a git repository, but the same comments still apply. Will fix and send a re-roll.

Quoted 59 lines

Not a git repository
To compare two paths outside a working tree:
usage: git diff [--no-index] <path> <path>

instead of the expected

	usage: git diff --no-index <path> <path>

Something like

	if (no_index)
		;
	else if (nongit)
		no_index = DIFF_NO_INDEX_IMPLICIT;
	else if (argc != i + 2)
		;
	else if (!path_inside_repo(prefix, argv[i]) ||
		 !path_inside_repo(prefix, argv[i + 1]))
		no_index = DIFF_NO_INDEX_IMPLICIT;

should work. Or:

	if (!no_index) {
		/*
		 * Treat git diff with ...
		 */
		if (nongit || ...)
			no_index = DIFF_NO_INDEX_IMPLICIT;
	}

Or the '!no_index &&' condition inserted some other way.

Quoted 4 lines
builtin/diff.c +2 -2
-	/* If this is a no-index diff, just run it and exit there. */
-	diff_no_index(&rev, argc, argv, nongit, prefix);
+	if (no_index) {
+		if (argc != i + 2) {

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 339

Quoted 9 lines
builtin/diff.c +9 -0
+			/* Give the usage message for non-repository usage and exit. */
+			usagef("git diff %s <path> <path>",
+			       no_index == DIFF_NO_INDEX_EXPLICIT ?
+					"--no-index" : "[--no-index]");
+
+		}
+		/* If this is a no-index diff, just run it and exit there. */
+		diff_no_index(&rev, argc, argv, prefix);
+	}

Perhaps, to avoid some nesting:

	/* A no-index diff takes exactly two path arguments. */
	if (no_index && argc != i + 2) {
		...
		usagef(...);
	}

	if (no_index)
		/* Just run the diff and exit. */
		diff_no_index(...);
Quoted 1 line

Jonathan

Thanks, will change in the re-roll.

--
Thomas

#23 lore raw

Re: [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c

Quoted 6 lines · Thomas Gummerer
Quoted 2 lines

What happens if I run 'git diff --no-index /tmp git.c git.c' from within a git repository? With this, I fear I will get

Thanks, I've missed that one. It only happens when run outside a git repository, but the same comments still apply. Will fix and send a re-roll.

Please don't, as the last round has already been pushed on 'next'.

An incremental change on top would also illustrate more clearly what breakage needed to be fixed, which would be another good thing. It could even come with a new test that makes sure that the above command line is diagnosed correctly as a mistake ;-).

Thanks.

Quoted 66 lines
Quoted 60 lines

Not a git repository
To compare two paths outside a working tree:
usage: git diff [--no-index] <path> <path>

instead of the expected

	usage: git diff --no-index <path> <path>

Something like

	if (no_index)
		;
	else if (nongit)
		no_index = DIFF_NO_INDEX_IMPLICIT;
	else if (argc != i + 2)
		;
	else if (!path_inside_repo(prefix, argv[i]) ||
		 !path_inside_repo(prefix, argv[i + 1]))
		no_index = DIFF_NO_INDEX_IMPLICIT;

should work. Or:

	if (!no_index) {
		/*
		 * Treat git diff with ...
		 */
		if (nongit || ...)
			no_index = DIFF_NO_INDEX_IMPLICIT;
	}

Or the '!no_index &&' condition inserted some other way.

Quoted 4 lines
builtin/diff.c +2 -2
-	/* If this is a no-index diff, just run it and exit there. */
-	diff_no_index(&rev, argc, argv, nongit, prefix);
+	if (no_index) {
+		if (argc != i + 2) {

[...]

builtin/diff.c int cmd_diff(int argc, const char **argv, const char *prefix) · line 339

Quoted 9 lines
builtin/diff.c +9 -0
+			/* Give the usage message for non-repository usage and exit. */
+			usagef("git diff %s <path> <path>",
+			       no_index == DIFF_NO_INDEX_EXPLICIT ?
+					"--no-index" : "[--no-index]");
+
+		}
+		/* If this is a no-index diff, just run it and exit there. */
+		diff_no_index(&rev, argc, argv, prefix);
+	}

Perhaps, to avoid some nesting:

	/* A no-index diff takes exactly two path arguments. */
	if (no_index && argc != i + 2) {
		...
		usagef(...);
	}

	if (no_index)
		/* Just run the diff and exit. */
		diff_no_index(...);
Quoted 1 line

Jonathan

Thanks, will change in the re-roll.

--
Thomas

#27 lore raw

PATCH v5 1/2 diff: move no-index detection to builtin/diff.c

In reply to Jonathan Nieder

Currently the --no-index option is parsed in diff_no_index(). Move the detection if a no-index diff should be executed to builtin/diff.c, where we can use it for executing diff_no_index() conditionally. This will also allow us to execute other operations conditionally, which will be done in the next patch.

There are no functional changes.

---

Thanks Jonathan for reviewing the last round, this version addresses those comments, fixing the error message when git diff --no-index is run outside of a git directory and avoids some nesting as suggested.

 builtin/diff.c  | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 diff-no-index.c | 44 +-------------------------------------------
 diff.h          |  2 +-
 3 files changed, 52 insertions(+), 47 deletions(-)
builtin/diff.c +50 -3
index adb93a9..f49a938 100644
@@ -16,6 +16,9 @@
 #include "submodule.h"
 #include "sha1-array.h"
 
+#define DIFF_NO_INDEX_EXPLICIT 1
+#define DIFF_NO_INDEX_IMPLICIT 2
+
 struct blobinfo {
 	unsigned char sha1[20];
 	const char *name;
@@ -257,7 +260,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	int blobs = 0, paths = 0;
 	const char *path = NULL;
 	struct blobinfo blob[2];
-	int nongit;
+	int nongit = 0, no_index = 0;
 	int result = 0;
 
 	/*
@@ -283,14 +286,58 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 	 * Other cases are errors.
 	 */
 
+	/* Were we asked to do --no-index explicitly? */
+	for (i = 1; i < argc; i++) {
+		if (!strcmp(argv[i], "--")) {
+			i++;
+			break;
+		}
+		if (!strcmp(argv[i], "--no-index"))
+			no_index = DIFF_NO_INDEX_EXPLICIT;
+		if (argv[i][0] != '-')
+			break;
+	}
+
 	prefix = setup_git_directory_gently(&nongit);
+	if (!no_index) {
+		/*
+		 * Treat git diff with at least one path outside of the
+		 * repo the same as if the command would have been executed
+		 * outside of a git repository.  In this case it behaves
+		 * the same way as "git diff --no-index <a> <b>", which acts
+		 * as a colourful "diff" replacement.
+		 */
+		if (nongit || ((argc == i + 2) &&
+			       (!path_inside_repo(prefix, argv[i]) ||
+				!path_inside_repo(prefix, argv[i + 1]))))
+			no_index = DIFF_NO_INDEX_IMPLICIT;
+	}
+
 	gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
 
-	/* If this is a no-index diff, just run it and exit there. */
-	diff_no_index(&rev, argc, argv, nongit, prefix);
+	if (no_index && argc != i + 2) {
+		if (no_index == DIFF_NO_INDEX_IMPLICIT) {
+			/*
+			 * There was no --no-index and there were not two
+			 * paths. It is possible that the user intended
+			 * to do an inside-repository operation.
+			 */
+			fprintf(stderr, "Not a git repository\n");
+			fprintf(stderr,
+				"To compare two paths outside a working tree:\n");
+		}
+		/* Give the usage message for non-repository usage and exit. */
+		usagef("git diff %s <path> <path>",
+		       no_index == DIFF_NO_INDEX_EXPLICIT ?
+		       "--no-index" : "[--no-index]");
+
+	}
+	if (no_index)
+		/* If this is a no-index diff, just run it and exit there. */
+		diff_no_index(&rev, argc, argv, prefix);
 
 	/* Otherwise, we are doing the usual "git" diff */
 	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
diff-no-index.c +1 -43

55 lines not on this page · raw message

diff.h +1 -1
index e342325..de105d3 100644
@@ -330,7 +330,7 @@ extern int diff_flush_patch_id(struct diff_options *, unsigned char *);
 
 extern int diff_result_code(struct diff_options *, int);
 
-extern void diff_no_index(struct rev_info *, int, const char **, int, const char *);
+extern void diff_no_index(struct rev_info *, int, const char **, const char *);
 
 extern int index_differs_from(const char *def, int diff_flags);
 
Signature · 1 line
1.8.5.4.g8639e57
#28 lore raw

PATCH v5 2/2 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.

---
 builtin/diff.c                |  5 +++--
 t/perf/p4001-diff-no-index.sh | 22 ++++++++++++++++++++++
 t/t4053-diff-no-index.sh      | 15 +++++++++++++++
 3 files changed, 40 insertions(+), 2 deletions(-)
 create mode 100755 t/perf/p4001-diff-no-index.sh
builtin/diff.c +3 -2
index f49a938..a8569a4 100644
@@ -298,8 +298,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 			break;
 	}
 
-	prefix = setup_git_directory_gently(&nongit);
 	if (!no_index) {
+		prefix = setup_git_directory_gently(&nongit);
 		/*
 		 * Treat git diff with at least one path outside of the
 		 * repo the same as if the command would have been executed
@@ -313,7 +313,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 			no_index = DIFF_NO_INDEX_IMPLICIT;
 	}
 
-	gitmodules_config();
+	if (!no_index)
+		gitmodules_config();
 	git_config(git_diff_ui_config, NULL);
 
 	init_revisions(&rev, prefix);
t/perf/p4001-diff-no-index.sh new file +22 -0
new file mode 100755
index 0000000..683be69
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description="Test diff --no-index performance"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+file1=$(git ls-files | tail -n 2 | head -1)
+file2=$(git ls-files | tail -n 1 | head -1)
+
+test_expect_success "empty files, so they take no time to diff" "
+	echo >$file1 &&
+	echo >$file2
+"
+
+test_perf "diff --no-index" "
+	git diff --no-index $file1 $file2 >/dev/null
+"
+
+test_done
t/t4053-diff-no-index.sh +15 -0
index 979e983..077c775 100755
@@ -29,4 +29,19 @@ test_expect_success 'git diff --no-index relative path outside repo' '
 	)
 '
 
+test_expect_success 'git diff --no-index with broken index' '
+	(
+		cd repo &&
+		echo broken >.git/index &&
+		git diff --no-index a ../non/git/a
+	)
+'
+
+test_expect_success 'git diff outside repo with broken index' '
+	(
+		cd repo &&
+		git diff ../non/git/a ../non/git/b
+	)
+'
+
 test_done
Signature · 1 line
1.8.5.4.g8639e57
#24 lore raw

PATCH 1/2 diff: add test for --no-index executed outside repo

In reply to Junio C Hamano

470faf9 diff: move no-index detection to builtin/diff.c breaks the error message for "git diff --no-index", when the command is executed outside of a git repository and the wrong number of arguments are given. 6df5762
diff: don't read index when --no-index is given fixes the problem.

Add a test to guard against similar breakages in the future.

---

Quoted 5 lines
Quoted 3 lines

Thanks, I've missed that one. It only happens when run outside a git repository, but the same comments still apply. Will fix and send a re-roll.

Please don't, as the last round has already been pushed on 'next'.

Sorry about that, should have checked first.

Quoted 4 lines

An incremental change on top would also illustrate more clearly what breakage needed to be fixed, which would be another good thing. It could even come with a new test that makes sure that the above command line is diagnosed correctly as a mistake ;-).

The breakage is actually fixed with the second patch as described in the commit message above, so here is just a test against future breakages. This test only works when the test root is outside of a git repository, as otherwise nongit will not be set. Is there another way to write it?

t/t4053-diff-no-index.sh | 7 +++++++

 1 file changed, 7 insertions(+)
t/t4053-diff-no-index.sh +7 -0
index 077c775..eb4f380 100755
@@ -44,4 +44,11 @@ test_expect_success 'git diff outside repo with broken index' '
 	)
 '
 
+test_expect_success 'git diff --no-index executed outside repo gives correct error message' '
+	rm -rf .git &&
+	test_must_fail git diff --no-index a b b 2>actual.err &&
+	echo "usage: git diff --no-index <path> <path>" >expect.err &&
+	test_cmp expect.err actual.err
+'
+
 test_done
Signature · 1 line
1.8.5.4.g8639e57
#25 lore raw

PATCH 2/2 diff: avoid some nesting

Avoid some nesting in builtin/diff.c, to make the code easier to read. No functional changes.

---

This is based on comments by Jonathan on the version that is already next.

builtin/diff.c | 35 +++++++++++++++++------------------

 1 file changed, 17 insertions(+), 18 deletions(-)
builtin/diff.c +17 -18
index ea1dd65..24d6271 100644
@@ -319,27 +319,26 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 
 	init_revisions(&rev, prefix);
 
-	if (no_index) {
-		if (argc != i + 2) {
-			if (no_index == DIFF_NO_INDEX_IMPLICIT) {
-				/*
-				 * There was no --no-index and there were not two
-				 * paths. It is possible that the user intended
-				 * to do an inside-repository operation.
-				 */
-				fprintf(stderr, "Not a git repository\n");
-				fprintf(stderr,
-					"To compare two paths outside a working tree:\n");
-			}
-			/* Give the usage message for non-repository usage and exit. */
-			usagef("git diff %s <path> <path>",
-			       no_index == DIFF_NO_INDEX_EXPLICIT ?
-					"--no-index" : "[--no-index]");
-
+	if (no_index && argc != i + 2) {
+		if (no_index == DIFF_NO_INDEX_IMPLICIT) {
+			/*
+			 * There was no --no-index and there were not two
+			 * paths. It is possible that the user intended
+			 * to do an inside-repository operation.
+			 */
+			fprintf(stderr, "Not a git repository\n");
+			fprintf(stderr,
+				"To compare two paths outside a working tree:\n");
 		}
+		/* Give the usage message for non-repository usage and exit. */
+		usagef("git diff %s <path> <path>",
+		       no_index == DIFF_NO_INDEX_EXPLICIT ?
+		       "--no-index" : "[--no-index]");
+
+	}
+	if (no_index)
 		/* If this is a no-index diff, just run it and exit there. */
 		diff_no_index(&rev, argc, argv, prefix);
-	}
 
 	/* Otherwise, we are doing the usual "git" diff */
 	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
Signature · 1 line
1.8.5.4.g8639e57
#26 lore raw

Re: [PATCH 1/2] diff: add test for --no-index executed outside repo

In reply to Thomas Gummerer
Quoted 23 lines · Thomas Gummerer

470faf9 diff: move no-index detection to builtin/diff.c breaks the error message for "git diff --no-index", when the command is executed outside of a git repository and the wrong number of arguments are given. 6df5762
diff: don't read index when --no-index is given fixes the problem.

Add a test to guard against similar breakages in the future.

---

Quoted 5 lines
Quoted 3 lines

Thanks, I've missed that one. It only happens when run outside a git repository, but the same comments still apply. Will fix and send a re-roll.

Please don't, as the last round has already been pushed on 'next'.

Sorry about that, should have checked first.

Quoted 4 lines

An incremental change on top would also illustrate more clearly what breakage needed to be fixed, which would be another good thing. It could even come with a new test that makes sure that the above command line is diagnosed correctly as a mistake ;-).

Quoted 5 lines

The breakage is actually fixed with the second patch as described in the commit message above, so here is just a test against future breakages. This test only works when the test root is outside of a git repository, as otherwise nongit will not be set. Is there another way to write it?

Perhaps use CEILING, like this (untested)?

	mkdir -p test-outside/non/git &&
        (
                GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/test-outside &&
		export GIT_CEILING_DIRECTORIES &&
		cd test-outside/non/git &&
		do whatever non-git thing here
	)