All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] diff: don't read index when --no-index is given
@ 2013-12-09 12:05 Thomas Gummerer
  2013-12-09 15:16 ` Jonathan Nieder
  2013-12-09 20:30 ` [PATCH] " Junio C Hamano
  0 siblings, 2 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-09 12:05 UTC (permalink / raw)
  To: git
  Cc: Thomas Gummerer, Alexey Borzenkov, René Scharfe,
	Michael Haggerty, Tim Henigan, Junio C Hamano, Bobby Powers,
	Jens Lehmann, Jeff King

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.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 builtin/diff.c           | 13 +++++++++++--
 t/t4053-diff-no-index.sh |  6 ++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/builtin/diff.c b/builtin/diff.c
index adb93a9..47c0833 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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);
diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 979e983..a24ae4d 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -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
-- 
1.8.4.2

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH] diff: don't read index when --no-index is given
  2013-12-09 12:05 [PATCH] diff: don't read index when --no-index is given Thomas Gummerer
@ 2013-12-09 15:16 ` Jonathan Nieder
  2013-12-09 17:27   ` Jens Lehmann
  2013-12-09 19:14   ` Thomas Gummerer
  2013-12-09 20:30 ` [PATCH] " Junio C Hamano
  1 sibling, 2 replies; 30+ messages in thread
From: Jonathan Nieder @ 2013-12-09 15:16 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Alexey Borzenkov, Ren?? Scharfe, Michael Haggerty,
	Tim Henigan, Junio C Hamano, Bobby Powers, Jens Lehmann,
	Jeff King

Thomas Gummerer wrote:

> 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.

>                        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.

[...]
> --- a/builtin/diff.c
> +++ b/builtin/diff.c
[...]
> @@ -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. :)

[...]
>  	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.

[...]
> +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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH] diff: don't read index when --no-index is given
  2013-12-09 15:16 ` Jonathan Nieder
@ 2013-12-09 17:27   ` Jens Lehmann
  2013-12-09 19:06     ` Jonathan Nieder
  2013-12-09 19:14   ` Thomas Gummerer
  1 sibling, 1 reply; 30+ messages in thread
From: Jens Lehmann @ 2013-12-09 17:27 UTC (permalink / raw)
  To: Jonathan Nieder, Thomas Gummerer
  Cc: git, Alexey Borzenkov, Ren?? Scharfe, Michael Haggerty,
	Tim Henigan, Junio C Hamano, Bobby Powers, Jeff King

Am 09.12.2013 16:16, schrieb Jonathan Nieder:
> Thomas Gummerer wrote:
> 
>> 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)

>>                        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?

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH] diff: don't read index when --no-index is given
  2013-12-09 17:27   ` Jens Lehmann
@ 2013-12-09 19:06     ` Jonathan Nieder
  0 siblings, 0 replies; 30+ messages in thread
From: Jonathan Nieder @ 2013-12-09 19:06 UTC (permalink / raw)
  To: Jens Lehmann
  Cc: Thomas Gummerer, git, Alexey Borzenkov, Ren?? Scharfe,
	Michael Haggerty, Tim Henigan, Junio C Hamano, Bobby Powers,
	Jeff King

Jens Lehmann wrote:
> Am 09.12.2013 16:16, schrieb Jonathan Nieder:
>> Thomas Gummerer wrote:

>>> 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)

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.

[...]
>                               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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH] diff: don't read index when --no-index is given
  2013-12-09 15:16 ` Jonathan Nieder
  2013-12-09 17:27   ` Jens Lehmann
@ 2013-12-09 19:14   ` Thomas Gummerer
  2013-12-09 19:20     ` Jonathan Nieder
  1 sibling, 1 reply; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-09 19:14 UTC (permalink / raw)
  To: Jonathan Nieder
  Cc: git, Alexey Borzenkov, Ren?? Scharfe, Michael Haggerty,
	Tim Henigan, Junio C Hamano, Bobby Powers, Jens Lehmann,
	Jeff King

Jonathan Nieder <jrnieder@gmail.com> writes:

> Thomas Gummerer wrote:
>
>> 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.
>
>>                        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.

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.

> [...]
>> --- a/builtin/diff.c
>> +++ b/builtin/diff.c
> [...]
>> @@ -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.

> [...]
>>  	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.

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.

> [...]
>> +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.

> Thanks and hope that helps,
> Jonathan

--
Thomas

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH] diff: don't read index when --no-index is given
  2013-12-09 19:14   ` Thomas Gummerer
@ 2013-12-09 19:20     ` Jonathan Nieder
  2013-12-09 20:40       ` [PATCH v2] " Thomas Gummerer
  0 siblings, 1 reply; 30+ messages in thread
From: Jonathan Nieder @ 2013-12-09 19:20 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Alexey Borzenkov, Ren?? Scharfe, Michael Haggerty,
	Tim Henigan, Junio C Hamano, Bobby Powers, Jens Lehmann,
	Jeff King

Thomas Gummerer wrote:

>                                          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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH] diff: don't read index when --no-index is given
  2013-12-09 12:05 [PATCH] diff: don't read index when --no-index is given Thomas Gummerer
  2013-12-09 15:16 ` Jonathan Nieder
@ 2013-12-09 20:30 ` Junio C Hamano
  2013-12-09 21:13   ` Thomas Gummerer
  2013-12-10 17:52   ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
  1 sibling, 2 replies; 30+ messages in thread
From: Junio C Hamano @ 2013-12-09 20:30 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Alexey Borzenkov, René Scharfe, Michael Haggerty,
	Tim Henigan, Bobby Powers, Jens Lehmann, Jeff King

Thomas Gummerer <t.gummerer@gmail.com> writes:

> 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.
>
> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> ---
>  builtin/diff.c           | 13 +++++++++++--
>  t/t4053-diff-no-index.sh |  6 ++++++
>  2 files changed, 17 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/diff.c b/builtin/diff.c
> index adb93a9..47c0833 100644
> --- a/builtin/diff.c
> +++ b/builtin/diff.c
> @@ -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;
> +		}
> +	}

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.

>  	prefix = setup_git_directory_gently(&nongit);
> -	gitmodules_config();
> +	if (!no_index)
> +		gitmodules_config();
>  	git_config(git_diff_ui_config, NULL);
>  
>  	init_revisions(&rev, prefix);
> diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
> index 979e983..a24ae4d 100755
> --- a/t/t4053-diff-no-index.sh
> +++ b/t/t4053-diff-no-index.sh
> @@ -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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v2] diff: don't read index when --no-index is given
  2013-12-09 19:20     ` Jonathan Nieder
@ 2013-12-09 20:40       ` Thomas Gummerer
  2013-12-09 20:55         ` Torsten Bögershausen
  2013-12-09 20:57         ` Eric Sunshine
  0 siblings, 2 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-09 20:40 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, Thomas Gummerer, Jens Lehmann,
	René Scharfe, Tim Henigan, Junio C Hamano, Alexey Borzenkov,
	Bobby Powers, Michael Haggerty, Jeff King

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>
---

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

diff --git a/builtin/diff.c b/builtin/diff.c
index adb93a9..5f09a0b 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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);
diff --git a/t/perf/p4001-diff-no-index.sh b/t/perf/p4001-diff-no-index.sh
new file mode 100755
index 0000000..81c7aa0
--- /dev/null
+++ b/t/perf/p4001-diff-no-index.sh
@@ -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
diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 979e983..d3dbf6b 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -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
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v2] diff: don't read index when --no-index is given
  2013-12-09 20:40       ` [PATCH v2] " Thomas Gummerer
@ 2013-12-09 20:55         ` Torsten Bögershausen
  2013-12-09 20:57         ` Eric Sunshine
  1 sibling, 0 replies; 30+ messages in thread
From: Torsten Bögershausen @ 2013-12-09 20:55 UTC (permalink / raw)
  To: Thomas Gummerer, git
  Cc: Jonathan Nieder, Jens Lehmann, René Scharfe, Tim Henigan,
	Junio C Hamano, Alexey Borzenkov, Bobby Powers, Michael Haggerty,
	Jeff King

On 2013-12-09 21.40, Thomas Gummerer wrote:
>  
> +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)

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2] diff: don't read index when --no-index is given
  2013-12-09 20:40       ` [PATCH v2] " Thomas Gummerer
  2013-12-09 20:55         ` Torsten Bögershausen
@ 2013-12-09 20:57         ` Eric Sunshine
  2013-12-09 21:17           ` Thomas Gummerer
  1 sibling, 1 reply; 30+ messages in thread
From: Eric Sunshine @ 2013-12-09 20:57 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: Git List, Jonathan Nieder, Jens Lehmann, René Scharfe,
	Tim Henigan, Junio C Hamano, Alexey Borzenkov, Bobby Powers,
	Michael Haggerty, Jeff King

On Mon, Dec 9, 2013 at 3:40 PM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
> 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>
> ---
> diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
> index 979e983..d3dbf6b 100755
> --- a/t/t4053-diff-no-index.sh
> +++ b/t/t4053-diff-no-index.sh
> @@ -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?

> +
>  test_done
> --
> 1.8.5.4.g8639e57

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH] diff: don't read index when --no-index is given
  2013-12-09 20:30 ` [PATCH] " Junio C Hamano
@ 2013-12-09 21:13   ` Thomas Gummerer
  2013-12-10 17:52   ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
  1 sibling, 0 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-09 21:13 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Alexey Borzenkov, René Scharfe, Michael Haggerty,
	Tim Henigan, Bobby Powers, Jens Lehmann, Jeff King


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

Junio C Hamano <gitster@pobox.com> writes:

> Thomas Gummerer <t.gummerer@gmail.com> writes:
>
>> 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.
>>
>> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
>> ---
>>  builtin/diff.c           | 13 +++++++++++--
>>  t/t4053-diff-no-index.sh |  6 ++++++
>>  2 files changed, 17 insertions(+), 2 deletions(-)
>>
>> diff --git a/builtin/diff.c b/builtin/diff.c
>> index adb93a9..47c0833 100644
>> --- a/builtin/diff.c
>> +++ b/builtin/diff.c
>> @@ -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;
>> +		}
>> +	}
>
> 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.

> 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.
>
>>  	prefix = setup_git_directory_gently(&nongit);
>> -	gitmodules_config();
>> +	if (!no_index)
>> +		gitmodules_config();
>>  	git_config(git_diff_ui_config, NULL);
>>
>>  	init_revisions(&rev, prefix);
>> diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
>> index 979e983..a24ae4d 100755
>> --- a/t/t4053-diff-no-index.sh
>> +++ b/t/t4053-diff-no-index.sh
>> @@ -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

--
Thomas

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v2] diff: don't read index when --no-index is given
  2013-12-09 20:57         ` Eric Sunshine
@ 2013-12-09 21:17           ` Thomas Gummerer
  0 siblings, 0 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-09 21:17 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Jonathan Nieder, Jens Lehmann, René Scharfe,
	Tim Henigan, Junio C Hamano, Alexey Borzenkov, Bobby Powers,
	Michael Haggerty, Jeff King, Torsten Bögershausen

Eric Sunshine <sunshine@sunshineco.com> writes:

> On Mon, Dec 9, 2013 at 3:40 PM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
>> 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>
>> ---
>> diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
>> index 979e983..d3dbf6b 100755
>> --- a/t/t4053-diff-no-index.sh
>> +++ b/t/t4053-diff-no-index.sh
>> @@ -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?

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

>> +
>>  test_done
>> --
>> 1.8.5.4.g8639e57

--
Thomas

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c
  2013-12-09 20:30 ` [PATCH] " Junio C Hamano
  2013-12-09 21:13   ` Thomas Gummerer
@ 2013-12-10 17:52   ` Thomas Gummerer
  2013-12-10 17:52     ` [PATCH v3 2/2] diff: don't read index when --no-index is given Thomas Gummerer
  2013-12-10 18:16     ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
  1 sibling, 2 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-10 17:52 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, Jens Lehmann, René Scharfe, Tim Henigan,
	Junio C Hamano, Alexey Borzenkov, Bobby Powers, Michael Haggerty,
	Jeff King, Torsten Bögershausen, Eric Sunshine,
	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, 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.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

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(-)

diff --git a/builtin/diff.c b/builtin/diff.c
index adb93a9..7220b2c 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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 --git a/diff-no-index.c b/diff-no-index.c
index 00a8eef..78e3090 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -181,56 +181,14 @@ static int queue_diff(struct diff_options *o,
 	}
 }
 
-void diff_no_index(struct rev_info *revs,
-		   int argc, const char **argv,
-		   int nongit, const char *prefix)
+int diff_no_index(struct rev_info *revs,
+		  int argc, const char **argv,
+		  int nongit, int no_index, const char *prefix)
 {
 	int i, prefixlen;
-	int no_index = 0;
 	unsigned deprecated_show_diff_q_option_used = 0;
 	const char *paths[2];
 
-	/* 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;
-	}
-
-	if (!no_index && !nongit) {
-		/*
-		 * Inside a git repository, without --no-index.  Only
-		 * when a path outside the repository is given,
-		 * e.g. "git diff /var/tmp/[12]", or "git diff
-		 * Makefile /var/tmp/Makefile", allow it to be used as
-		 * a colourful "diff" replacement.
-		 */
-		if ((argc != i + 2) ||
-		    (path_inside_repo(prefix, argv[i]) &&
-		     path_inside_repo(prefix, argv[i+1])))
-			return;
-	}
-	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]");
-	}
-
 	diff_setup(&revs->diffopt);
 	for (i = 1; i < argc - 2; ) {
 		int j;
diff --git a/diff.h b/diff.h
index e342325..3e1828a 100644
--- a/diff.h
+++ b/diff.h
@@ -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);
 
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v3 2/2] diff: don't read index when --no-index is given
  2013-12-10 17:52   ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
@ 2013-12-10 17:52     ` Thomas Gummerer
  2013-12-10 18:18       ` Jonathan Nieder
  2013-12-10 18:16     ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
  1 sibling, 1 reply; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-10 17:52 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, Jens Lehmann, René Scharfe, Tim Henigan,
	Junio C Hamano, Alexey Borzenkov, Bobby Powers, Michael Haggerty,
	Jeff King, Torsten Bögershausen, Eric Sunshine,
	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.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 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

diff --git a/builtin/diff.c b/builtin/diff.c
index 7220b2c..9ce8bec 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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);
diff --git a/t/perf/p4001-diff-no-index.sh b/t/perf/p4001-diff-no-index.sh
new file mode 100755
index 0000000..81c7aa0
--- /dev/null
+++ b/t/perf/p4001-diff-no-index.sh
@@ -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
diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 979e983..077c775 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -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
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c
  2013-12-10 17:52   ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
  2013-12-10 17:52     ` [PATCH v3 2/2] diff: don't read index when --no-index is given Thomas Gummerer
@ 2013-12-10 18:16     ` Jonathan Nieder
  2013-12-10 18:46       ` Thomas Gummerer
  2013-12-11  9:58       ` [PATCH v4 " Thomas Gummerer
  1 sibling, 2 replies; 30+ messages in thread
From: Jonathan Nieder @ 2013-12-10 18:16 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Jens Lehmann, Junio C Hamano, Torsten Bögershausen,
	Eric Sunshine

(pruning cc list)
Hi,

Thomas Gummerer wrote:

> 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?

[...]
> --- a/builtin/diff.c
> +++ b/builtin/diff.c
> @@ -283,14 +283,53 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
[...]
>  	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;

[...]
>  	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) {
[...]
> +	}

Ok.

[...]
> --- a/diff-no-index.c
> +++ b/diff-no-index.c
> @@ -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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 2/2] diff: don't read index when --no-index is given
  2013-12-10 17:52     ` [PATCH v3 2/2] diff: don't read index when --no-index is given Thomas Gummerer
@ 2013-12-10 18:18       ` Jonathan Nieder
  2013-12-10 18:55         ` Thomas Gummerer
  0 siblings, 1 reply; 30+ messages in thread
From: Jonathan Nieder @ 2013-12-10 18:18 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Jens Lehmann, René Scharfe, Tim Henigan,
	Junio C Hamano, Alexey Borzenkov, Bobby Powers, Michael Haggerty,
	Jeff King, Torsten Bögershausen, Eric Sunshine

Thomas Gummerer wrote:

> --- a/builtin/diff.c
> +++ b/builtin/diff.c
> @@ -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?

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c
  2013-12-10 18:16     ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
@ 2013-12-10 18:46       ` Thomas Gummerer
  2013-12-11  9:58       ` [PATCH v4 " Thomas Gummerer
  1 sibling, 0 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-10 18:46 UTC (permalink / raw)
  To: Jonathan Nieder
  Cc: git, Jens Lehmann, Junio C Hamano, Torsten Bögershausen,
	Eric Sunshine

Jonathan Nieder <jrnieder@gmail.com> writes:

> (pruning cc list)
> Hi,
>
> Thomas Gummerer wrote:
>
>> 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.

> [...]
>> --- a/builtin/diff.c
>> +++ b/builtin/diff.c
>> @@ -283,14 +283,53 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
> [...]
>>  	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;

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

> [...]
>>  	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) {
> [...]
>> +	}
>
> Ok.
>
> [...]
>> --- a/diff-no-index.c
>> +++ b/diff-no-index.c
>> @@ -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?

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

> Hope that helps,
> Jonathan

--
Thomas

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v3 2/2] diff: don't read index when --no-index is given
  2013-12-10 18:18       ` Jonathan Nieder
@ 2013-12-10 18:55         ` Thomas Gummerer
  0 siblings, 0 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-10 18:55 UTC (permalink / raw)
  To: Jonathan Nieder
  Cc: git, Jens Lehmann, René Scharfe, Tim Henigan,
	Junio C Hamano, Alexey Borzenkov, Bobby Powers, Michael Haggerty,
	Jeff King, Torsten Bögershausen, Eric Sunshine

Jonathan Nieder <jrnieder@gmail.com> writes:

> Thomas Gummerer wrote:
>
>> --- a/builtin/diff.c
>> +++ b/builtin/diff.c
>> @@ -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?

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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c
  2013-12-10 18:16     ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
  2013-12-10 18:46       ` Thomas Gummerer
@ 2013-12-11  9:58       ` Thomas Gummerer
  2013-12-11  9:58         ` [PATCH v4 2/2] diff: don't read index when --no-index is given Thomas Gummerer
  2013-12-14  0:43         ` [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
  1 sibling, 2 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-11  9:58 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, Jens Lehmann, Junio C Hamano,
	Torsten Bögershausen, Eric Sunshine, 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, 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.

Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

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(-)

diff --git a/builtin/diff.c b/builtin/diff.c
index adb93a9..da69e4a 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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 --git a/diff-no-index.c b/diff-no-index.c
index 00a8eef..33e5982 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -183,54 +183,12 @@ static int queue_diff(struct diff_options *o,
 
 void diff_no_index(struct rev_info *revs,
 		   int argc, const char **argv,
-		   int nongit, const char *prefix)
+		   const char *prefix)
 {
 	int i, prefixlen;
-	int no_index = 0;
 	unsigned deprecated_show_diff_q_option_used = 0;
 	const char *paths[2];
 
-	/* 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;
-	}
-
-	if (!no_index && !nongit) {
-		/*
-		 * Inside a git repository, without --no-index.  Only
-		 * when a path outside the repository is given,
-		 * e.g. "git diff /var/tmp/[12]", or "git diff
-		 * Makefile /var/tmp/Makefile", allow it to be used as
-		 * a colourful "diff" replacement.
-		 */
-		if ((argc != i + 2) ||
-		    (path_inside_repo(prefix, argv[i]) &&
-		     path_inside_repo(prefix, argv[i+1])))
-			return;
-	}
-	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]");
-	}
-
 	diff_setup(&revs->diffopt);
 	for (i = 1; i < argc - 2; ) {
 		int j;
diff --git a/diff.h b/diff.h
index e342325..de105d3 100644
--- a/diff.h
+++ b/diff.h
@@ -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);
 
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v4 2/2] diff: don't read index when --no-index is given
  2013-12-11  9:58       ` [PATCH v4 " Thomas Gummerer
@ 2013-12-11  9:58         ` Thomas Gummerer
  2013-12-12 20:25           ` Junio C Hamano
  2013-12-14  0:44           ` Jonathan Nieder
  2013-12-14  0:43         ` [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
  1 sibling, 2 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-11  9:58 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, Jens Lehmann, Junio C Hamano,
	Torsten Bögershausen, Eric Sunshine, 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.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 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

diff --git a/builtin/diff.c b/builtin/diff.c
index da69e4a..ea1dd65 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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);
diff --git a/t/perf/p4001-diff-no-index.sh b/t/perf/p4001-diff-no-index.sh
new file mode 100755
index 0000000..683be69
--- /dev/null
+++ b/t/perf/p4001-diff-no-index.sh
@@ -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
diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 979e983..077c775 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -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
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 2/2] diff: don't read index when --no-index is given
  2013-12-11  9:58         ` [PATCH v4 2/2] diff: don't read index when --no-index is given Thomas Gummerer
@ 2013-12-12 20:25           ` Junio C Hamano
  2013-12-14  0:44           ` Jonathan Nieder
  1 sibling, 0 replies; 30+ messages in thread
From: Junio C Hamano @ 2013-12-12 20:25 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Jonathan Nieder, Jens Lehmann, Torsten Bögershausen,
	Eric Sunshine

Thomas Gummerer <t.gummerer@gmail.com> writes:

> 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>
> ---

Looks reasonable.

Thanks.  Will queue.

>  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
>
> diff --git a/builtin/diff.c b/builtin/diff.c
> index da69e4a..ea1dd65 100644
> --- a/builtin/diff.c
> +++ b/builtin/diff.c
> @@ -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);
> diff --git a/t/perf/p4001-diff-no-index.sh b/t/perf/p4001-diff-no-index.sh
> new file mode 100755
> index 0000000..683be69
> --- /dev/null
> +++ b/t/perf/p4001-diff-no-index.sh
> @@ -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
> diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
> index 979e983..077c775 100755
> --- a/t/t4053-diff-no-index.sh
> +++ b/t/t4053-diff-no-index.sh
> @@ -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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c
  2013-12-11  9:58       ` [PATCH v4 " Thomas Gummerer
  2013-12-11  9:58         ` [PATCH v4 2/2] diff: don't read index when --no-index is given Thomas Gummerer
@ 2013-12-14  0:43         ` Jonathan Nieder
  2013-12-14 10:42           ` Thomas Gummerer
  2013-12-14 13:07           ` [PATCH v5 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
  1 sibling, 2 replies; 30+ messages in thread
From: Jonathan Nieder @ 2013-12-14  0:43 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Jens Lehmann, Junio C Hamano, Torsten Bögershausen,
	Eric Sunshine

Hi,

Thomas Gummerer wrote:

> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>

Thanks, and sorry for the slow follow-up.

[...]
> --- a/builtin/diff.c
> +++ b/builtin/diff.c
> @@ -16,6 +16,9 @@
[...]
> @@ -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.

[...]
> +	/*
> +	 * 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.

> -	/* 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) {
[...]
> +			/* 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

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 2/2] diff: don't read index when --no-index is given
  2013-12-11  9:58         ` [PATCH v4 2/2] diff: don't read index when --no-index is given Thomas Gummerer
  2013-12-12 20:25           ` Junio C Hamano
@ 2013-12-14  0:44           ` Jonathan Nieder
  1 sibling, 0 replies; 30+ messages in thread
From: Jonathan Nieder @ 2013-12-14  0:44 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Jens Lehmann, Junio C Hamano, Torsten Bögershausen,
	Eric Sunshine

Thomas Gummerer wrote:

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

Very nice.

> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>

^ permalink raw reply	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c
  2013-12-14  0:43         ` [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
@ 2013-12-14 10:42           ` Thomas Gummerer
  2013-12-16 17:48             ` Junio C Hamano
  2013-12-14 13:07           ` [PATCH v5 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
  1 sibling, 1 reply; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-14 10:42 UTC (permalink / raw)
  To: Jonathan Nieder
  Cc: git, Jens Lehmann, Junio C Hamano, Torsten Bögershausen,
	Eric Sunshine

Jonathan Nieder <jrnieder@gmail.com> writes:

> Hi,
>
> Thomas Gummerer wrote:
>
>> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
>
> Thanks, and sorry for the slow follow-up.
>
> [...]
>> --- a/builtin/diff.c
>> +++ b/builtin/diff.c
>> @@ -16,6 +16,9 @@
> [...]
>> @@ -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.
>
> [...]
>> +	/*
>> +	 * 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.

> 	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.
>
>> -	/* 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) {
> [...]
>> +			/* 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

Thanks, will change in the re-roll.

--
Thomas

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH v5 1/2] diff: move no-index detection to builtin/diff.c
  2013-12-14  0:43         ` [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
  2013-12-14 10:42           ` Thomas Gummerer
@ 2013-12-14 13:07           ` Thomas Gummerer
  2013-12-14 13:07             ` [PATCH v5 2/2] diff: don't read index when --no-index is given Thomas Gummerer
  1 sibling, 1 reply; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-14 13:07 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, Jens Lehmann, Junio C Hamano,
	Torsten Bögershausen, Eric Sunshine, 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, 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.

Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

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(-)

diff --git a/builtin/diff.c b/builtin/diff.c
index adb93a9..f49a938 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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 --git a/diff-no-index.c b/diff-no-index.c
index 00a8eef..33e5982 100644
--- a/diff-no-index.c
+++ b/diff-no-index.c
@@ -183,54 +183,12 @@ static int queue_diff(struct diff_options *o,
 
 void diff_no_index(struct rev_info *revs,
 		   int argc, const char **argv,
-		   int nongit, const char *prefix)
+		   const char *prefix)
 {
 	int i, prefixlen;
-	int no_index = 0;
 	unsigned deprecated_show_diff_q_option_used = 0;
 	const char *paths[2];
 
-	/* 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;
-	}
-
-	if (!no_index && !nongit) {
-		/*
-		 * Inside a git repository, without --no-index.  Only
-		 * when a path outside the repository is given,
-		 * e.g. "git diff /var/tmp/[12]", or "git diff
-		 * Makefile /var/tmp/Makefile", allow it to be used as
-		 * a colourful "diff" replacement.
-		 */
-		if ((argc != i + 2) ||
-		    (path_inside_repo(prefix, argv[i]) &&
-		     path_inside_repo(prefix, argv[i+1])))
-			return;
-	}
-	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]");
-	}
-
 	diff_setup(&revs->diffopt);
 	for (i = 1; i < argc - 2; ) {
 		int j;
diff --git a/diff.h b/diff.h
index e342325..de105d3 100644
--- a/diff.h
+++ b/diff.h
@@ -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);
 
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH v5 2/2] diff: don't read index when --no-index is given
  2013-12-14 13:07           ` [PATCH v5 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
@ 2013-12-14 13:07             ` Thomas Gummerer
  0 siblings, 0 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-14 13:07 UTC (permalink / raw)
  To: git
  Cc: Jonathan Nieder, Jens Lehmann, Junio C Hamano,
	Torsten Bögershausen, Eric Sunshine, 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.

Reviewed-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 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

diff --git a/builtin/diff.c b/builtin/diff.c
index f49a938..a8569a4 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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);
diff --git a/t/perf/p4001-diff-no-index.sh b/t/perf/p4001-diff-no-index.sh
new file mode 100755
index 0000000..683be69
--- /dev/null
+++ b/t/perf/p4001-diff-no-index.sh
@@ -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
diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 979e983..077c775 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -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
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c
  2013-12-14 10:42           ` Thomas Gummerer
@ 2013-12-16 17:48             ` Junio C Hamano
  2013-12-16 19:23               ` [PATCH 1/2] diff: add test for --no-index executed outside repo Thomas Gummerer
  0 siblings, 1 reply; 30+ messages in thread
From: Junio C Hamano @ 2013-12-16 17:48 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: Jonathan Nieder, git, Jens Lehmann, Torsten Bögershausen,
	Eric Sunshine

Thomas Gummerer <t.gummerer@gmail.com> writes:

>> 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.

>
>> 	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.
>>
>>> -	/* 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) {
>> [...]
>>> +			/* 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
>
> Thanks, will change in the re-roll.
>
> --
> Thomas

^ permalink raw reply	[flat|nested] 30+ messages in thread

* [PATCH 1/2] diff: add test for --no-index executed outside repo
  2013-12-16 17:48             ` Junio C Hamano
@ 2013-12-16 19:23               ` Thomas Gummerer
  2013-12-16 19:23                 ` [PATCH 2/2] diff: avoid some nesting Thomas Gummerer
  2013-12-16 19:42                 ` [PATCH 1/2] diff: add test for --no-index executed outside repo Junio C Hamano
  0 siblings, 2 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-16 19:23 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jonathan Nieder, Jens Lehmann, Torsten Bögershausen,
	Eric Sunshine, 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.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

>> 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.

> 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(+)

diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
index 077c775..eb4f380 100755
--- a/t/t4053-diff-no-index.sh
+++ b/t/t4053-diff-no-index.sh
@@ -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
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* [PATCH 2/2] diff: avoid some nesting
  2013-12-16 19:23               ` [PATCH 1/2] diff: add test for --no-index executed outside repo Thomas Gummerer
@ 2013-12-16 19:23                 ` Thomas Gummerer
  2013-12-16 19:42                 ` [PATCH 1/2] diff: add test for --no-index executed outside repo Junio C Hamano
  1 sibling, 0 replies; 30+ messages in thread
From: Thomas Gummerer @ 2013-12-16 19:23 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jonathan Nieder, Jens Lehmann, Torsten Bögershausen,
	Eric Sunshine, Thomas Gummerer

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

Helped-by: Jonathan Nieder <jrnieder@gmail.com>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

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(-)

diff --git a/builtin/diff.c b/builtin/diff.c
index ea1dd65..24d6271 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -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;
-- 
1.8.5.4.g8639e57

^ permalink raw reply related	[flat|nested] 30+ messages in thread

* Re: [PATCH 1/2] diff: add test for --no-index executed outside repo
  2013-12-16 19:23               ` [PATCH 1/2] diff: add test for --no-index executed outside repo Thomas Gummerer
  2013-12-16 19:23                 ` [PATCH 2/2] diff: avoid some nesting Thomas Gummerer
@ 2013-12-16 19:42                 ` Junio C Hamano
  1 sibling, 0 replies; 30+ messages in thread
From: Junio C Hamano @ 2013-12-16 19:42 UTC (permalink / raw)
  To: Thomas Gummerer
  Cc: git, Jonathan Nieder, Jens Lehmann, Torsten Bögershausen,
	Eric Sunshine

Thomas Gummerer <t.gummerer@gmail.com> writes:

> 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.
>
> Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> ---
>
>>> 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.
>
>> 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?

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
	)

^ permalink raw reply	[flat|nested] 30+ messages in thread

end of thread, other threads:[~2013-12-16 19:42 UTC | newest]

Thread overview: 30+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-09 12:05 [PATCH] diff: don't read index when --no-index is given Thomas Gummerer
2013-12-09 15:16 ` Jonathan Nieder
2013-12-09 17:27   ` Jens Lehmann
2013-12-09 19:06     ` Jonathan Nieder
2013-12-09 19:14   ` Thomas Gummerer
2013-12-09 19:20     ` Jonathan Nieder
2013-12-09 20:40       ` [PATCH v2] " Thomas Gummerer
2013-12-09 20:55         ` Torsten Bögershausen
2013-12-09 20:57         ` Eric Sunshine
2013-12-09 21:17           ` Thomas Gummerer
2013-12-09 20:30 ` [PATCH] " Junio C Hamano
2013-12-09 21:13   ` Thomas Gummerer
2013-12-10 17:52   ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
2013-12-10 17:52     ` [PATCH v3 2/2] diff: don't read index when --no-index is given Thomas Gummerer
2013-12-10 18:18       ` Jonathan Nieder
2013-12-10 18:55         ` Thomas Gummerer
2013-12-10 18:16     ` [PATCH v3 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
2013-12-10 18:46       ` Thomas Gummerer
2013-12-11  9:58       ` [PATCH v4 " Thomas Gummerer
2013-12-11  9:58         ` [PATCH v4 2/2] diff: don't read index when --no-index is given Thomas Gummerer
2013-12-12 20:25           ` Junio C Hamano
2013-12-14  0:44           ` Jonathan Nieder
2013-12-14  0:43         ` [PATCH v4 1/2] diff: move no-index detection to builtin/diff.c Jonathan Nieder
2013-12-14 10:42           ` Thomas Gummerer
2013-12-16 17:48             ` Junio C Hamano
2013-12-16 19:23               ` [PATCH 1/2] diff: add test for --no-index executed outside repo Thomas Gummerer
2013-12-16 19:23                 ` [PATCH 2/2] diff: avoid some nesting Thomas Gummerer
2013-12-16 19:42                 ` [PATCH 1/2] diff: add test for --no-index executed outside repo Junio C Hamano
2013-12-14 13:07           ` [PATCH v5 1/2] diff: move no-index detection to builtin/diff.c Thomas Gummerer
2013-12-14 13:07             ` [PATCH v5 2/2] diff: don't read index when --no-index is given Thomas Gummerer

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.