git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v3 0/3] Grepping with intent to add
@ 2016-06-30 10:13 Charles Bailey
  2016-06-30 10:13 ` [PATCH v3 1/3] t7810-grep.sh: fix duplicated test name Charles Bailey
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Charles Bailey @ 2016-06-30 10:13 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Eric Sunshine,
	Nguyễn Thái Ngọc Duy

So I've got back around to this topic again.

I've applied fixes to the tests as suggested by Eric and Junio.

I came up with a test case that demonstrates a difference between the
additional fix that Duy suggested and the alternative that Junio
suggested.

I've kept Duy's fix because I think it makes more sense, although it's
a sufficiently obscure case that I don't feel strongly that it's
definitely the best behavior.

The fix ensures that if you have a file which is both "intend to add"
and "assume unchanged" that it is not listed if you "grep -L" for for
something. In effect, we are applying the "contents indeterminate" state
of the index to the working tree file.

Charles Bailey (3):
  t7810-grep.sh: fix duplicated test name
  t7810-grep.sh: fix a whitespace inconsistency
  grep: fix grepping for "intent to add" files

 builtin/grep.c  |  4 ++--
 t/t7810-grep.sh | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 62 insertions(+), 4 deletions(-)

-- 
2.8.2.311.gee88674


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

* [PATCH v3 1/3] t7810-grep.sh: fix duplicated test name
  2016-06-30 10:13 [PATCH v3 0/3] Grepping with intent to add Charles Bailey
@ 2016-06-30 10:13 ` Charles Bailey
  2016-07-01 20:27   ` Junio C Hamano
  2016-06-30 10:13 ` [PATCH v3 2/3] t7810-grep.sh: fix a whitespace inconsistency Charles Bailey
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Charles Bailey @ 2016-06-30 10:13 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Eric Sunshine,
	Nguyễn Thái Ngọc Duy

Signed-off-by: Charles Bailey <charles@hashpling.org>
---
 t/t7810-grep.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 1e72971..c4302ed 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -353,7 +353,7 @@ test_expect_success 'grep -l -C' '
 cat >expected <<EOF
 file:5
 EOF
-test_expect_success 'grep -l -C' '
+test_expect_success 'grep -c -C' '
 	git grep -c -C1 foo >actual &&
 	test_cmp expected actual
 '
-- 
2.8.2.311.gee88674


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

* [PATCH v3 2/3] t7810-grep.sh: fix a whitespace inconsistency
  2016-06-30 10:13 [PATCH v3 0/3] Grepping with intent to add Charles Bailey
  2016-06-30 10:13 ` [PATCH v3 1/3] t7810-grep.sh: fix duplicated test name Charles Bailey
@ 2016-06-30 10:13 ` Charles Bailey
  2016-06-30 10:13 ` [PATCH v3 3/3] grep: fix grepping for "intent to add" files Charles Bailey
  2016-07-01 20:26 ` [PATCH v3 0/3] Grepping with intent to add Junio C Hamano
  3 siblings, 0 replies; 7+ messages in thread
From: Charles Bailey @ 2016-06-30 10:13 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Eric Sunshine,
	Nguyễn Thái Ngọc Duy

Signed-off-by: Charles Bailey <charles@hashpling.org>
---
 t/t7810-grep.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index c4302ed..6e6eaa4 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -175,7 +175,7 @@ do
 
 	test_expect_success "grep -c $L (no /dev/null)" '
 		! git grep -c test $H | grep /dev/null
-        '
+	'
 
 	test_expect_success "grep --max-depth -1 $L" '
 		{
-- 
2.8.2.311.gee88674


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

* [PATCH v3 3/3] grep: fix grepping for "intent to add" files
  2016-06-30 10:13 [PATCH v3 0/3] Grepping with intent to add Charles Bailey
  2016-06-30 10:13 ` [PATCH v3 1/3] t7810-grep.sh: fix duplicated test name Charles Bailey
  2016-06-30 10:13 ` [PATCH v3 2/3] t7810-grep.sh: fix a whitespace inconsistency Charles Bailey
@ 2016-06-30 10:13 ` Charles Bailey
  2016-07-01 20:34   ` Junio C Hamano
  2016-07-01 20:26 ` [PATCH v3 0/3] Grepping with intent to add Junio C Hamano
  3 siblings, 1 reply; 7+ messages in thread
From: Charles Bailey @ 2016-06-30 10:13 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Eric Sunshine,
	Nguyễn Thái Ngọc Duy

From: Charles Bailey <cbailey32@bloomberg.net>

This reverts commit 4d5520053 (grep: make it clear i-t-a entries are
ignored, 2015-12-27) and adds an alternative fix to maintain the -L
--cached behavior.

4d5520053 caused 'git grep' to no longer find matches in new files in
the working tree where the corresponding index entry had the "intent to
add" bit set, despite the fact that these files are tracked.

The content in the index of a file for which the "intent to add" bit is
set is considered indeterminate and not empty. For most grep queries we
want these to behave the same, however for -L --cached (files without a
match) we don't want to respond positively for "intent to add" files as
their contents are indeterminate. This is in contrast to files with
empty contents in the index (no lines implies no matches for any grep
query expression) which should be reported in the output of a grep -L
--cached invocation.

Add tests to cover this case and a few related cases which previously
lacked coverage.

Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Charles Bailey <cbailey32@bloomberg.net>
---
 builtin/grep.c  |  4 ++--
 t/t7810-grep.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/builtin/grep.c b/builtin/grep.c
index 462e607..ae73831 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -386,7 +386,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
 
 	for (nr = 0; nr < active_nr; nr++) {
 		const struct cache_entry *ce = active_cache[nr];
-		if (!S_ISREG(ce->ce_mode) || ce_intent_to_add(ce))
+		if (!S_ISREG(ce->ce_mode))
 			continue;
 		if (!ce_path_match(ce, pathspec, NULL))
 			continue;
@@ -396,7 +396,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
 		 * cache version instead
 		 */
 		if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
-			if (ce_stage(ce))
+			if (ce_stage(ce) || ce_intent_to_add(ce))
 				continue;
 			hit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name);
 		}
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 6e6eaa4..c18e954 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -1364,4 +1364,62 @@ test_expect_success 'grep --color -e A --and -e B -p with context' '
 	test_cmp expected actual
 '
 
+test_expect_success 'grep can find things only in the work tree' '
+	: >work-tree-only &&
+	git add work-tree-only &&
+	test_when_finished "git rm -f work-tree-only" &&
+	echo "find in work tree" >work-tree-only &&
+	git grep --quiet "find in work tree" &&
+	test_must_fail git grep --quiet --cached "find in work tree" &&
+	test_must_fail git grep --quiet "find in work tree" HEAD
+'
+
+test_expect_success 'grep can find things only in the work tree (i-t-a)' '
+	echo "intend to add this" >intend-to-add &&
+	git add -N intend-to-add &&
+	test_when_finished "git rm -f intend-to-add" &&
+	git grep --quiet "intend to add this" &&
+	test_must_fail git grep --quiet --cached "intend to add this" &&
+	test_must_fail git grep --quiet "intend to add this" HEAD
+'
+
+test_expect_success 'grep does not search work tree with assume unchanged' '
+	echo "intend to add this" >intend-to-add &&
+	git add -N intend-to-add &&
+	git update-index --assume-unchanged intend-to-add &&
+	test_when_finished "git rm -f intend-to-add" &&
+	test_must_fail git grep --quiet "intend to add this" &&
+	test_must_fail git grep --quiet --cached "intend to add this" &&
+	test_must_fail git grep --quiet "intend to add this" HEAD
+'
+
+test_expect_success 'grep can find things only in the index' '
+	echo "only in the index" >cache-this &&
+	git add cache-this &&
+	rm cache-this &&
+	test_when_finished "git rm --cached cache-this" &&
+	test_must_fail git grep --quiet "only in the index" &&
+	git grep --quiet --cached "only in the index" &&
+	test_must_fail git grep --quiet "only in the index" HEAD
+'
+
+test_expect_success 'grep does not report i-t-a with -L --cached' '
+	echo "intend to add this" >intend-to-add &&
+	git add -N intend-to-add &&
+	test_when_finished "git rm -f intend-to-add" &&
+	git ls-files | grep -v "^intend-to-add\$" >expected &&
+	git grep -L --cached "nonexistent_string" >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
+	echo "intend to add this" >intend-to-add-assume-unchanged &&
+	git add -N intend-to-add-assume-unchanged &&
+	test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
+	git update-index --assume-unchanged intend-to-add-assume-unchanged &&
+	git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
+	git grep -L "nonexistent_string" >actual &&
+	test_cmp expected actual
+'
+
 test_done
-- 
2.8.2.311.gee88674


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

* Re: [PATCH v3 0/3] Grepping with intent to add
  2016-06-30 10:13 [PATCH v3 0/3] Grepping with intent to add Charles Bailey
                   ` (2 preceding siblings ...)
  2016-06-30 10:13 ` [PATCH v3 3/3] grep: fix grepping for "intent to add" files Charles Bailey
@ 2016-07-01 20:26 ` Junio C Hamano
  3 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2016-07-01 20:26 UTC (permalink / raw)
  To: Charles Bailey; +Cc: git, Eric Sunshine, Nguyễn Thái Ngọc Duy

Charles Bailey <charles@hashpling.org> writes:

> So I've got back around to this topic again.
>
> I've applied fixes to the tests as suggested by Eric and Junio.
>
> I came up with a test case that demonstrates a difference between the
> additional fix that Duy suggested and the alternative that Junio
> suggested.

Thanks, will queue.

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

* Re: [PATCH v3 1/3] t7810-grep.sh: fix duplicated test name
  2016-06-30 10:13 ` [PATCH v3 1/3] t7810-grep.sh: fix duplicated test name Charles Bailey
@ 2016-07-01 20:27   ` Junio C Hamano
  0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2016-07-01 20:27 UTC (permalink / raw)
  To: Charles Bailey; +Cc: git, Eric Sunshine, Nguyễn Thái Ngọc Duy

Charles Bailey <charles@hashpling.org> writes:

> @@ -353,7 +353,7 @@ test_expect_success 'grep -l -C' '
>  cat >expected <<EOF
>  file:5
>  EOF
> -test_expect_success 'grep -l -C' '
> +test_expect_success 'grep -c -C' '
>  	git grep -c -C1 foo >actual &&
>  	test_cmp expected actual
>  '

Makes sense.  The previous one (outside the pre-context) is about
running "grep -l -C", but this one is about "grep -c -C".




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

* Re: [PATCH v3 3/3] grep: fix grepping for "intent to add" files
  2016-06-30 10:13 ` [PATCH v3 3/3] grep: fix grepping for "intent to add" files Charles Bailey
@ 2016-07-01 20:34   ` Junio C Hamano
  0 siblings, 0 replies; 7+ messages in thread
From: Junio C Hamano @ 2016-07-01 20:34 UTC (permalink / raw)
  To: Charles Bailey; +Cc: git, Eric Sunshine, Nguyễn Thái Ngọc Duy

Charles Bailey <charles@hashpling.org> writes:

> +test_expect_success 'grep can find things only in the work tree' '
> +	: >work-tree-only &&
> +	git add work-tree-only &&
> +	test_when_finished "git rm -f work-tree-only" &&

It is better to have the test_when_finished before "git add", and
possibly before the creation of the test file.

It is not worth a reroll and it is perfectly fine to leave it as a
low-hanging-fruit for later clean-up material, because this test is
not about catchign a "git add" that suddenly starts failing in a
strange way (e.g. adds to the index but exits with non-zero).

Same comment for the use of test_when_finished in the remainder of
the patch.

Thanks.

> +	echo "find in work tree" >work-tree-only &&
> +	git grep --quiet "find in work tree" &&
> +	test_must_fail git grep --quiet --cached "find in work tree" &&
> +	test_must_fail git grep --quiet "find in work tree" HEAD
> +'
> +
> +test_expect_success 'grep can find things only in the work tree (i-t-a)' '
> +	echo "intend to add this" >intend-to-add &&
> +	git add -N intend-to-add &&
> +	test_when_finished "git rm -f intend-to-add" &&
> +	git grep --quiet "intend to add this" &&
> +	test_must_fail git grep --quiet --cached "intend to add this" &&
> +	test_must_fail git grep --quiet "intend to add this" HEAD
> +'
> +
> +test_expect_success 'grep does not search work tree with assume unchanged' '
> +	echo "intend to add this" >intend-to-add &&
> +	git add -N intend-to-add &&
> +	git update-index --assume-unchanged intend-to-add &&
> +	test_when_finished "git rm -f intend-to-add" &&
> +	test_must_fail git grep --quiet "intend to add this" &&
> +	test_must_fail git grep --quiet --cached "intend to add this" &&
> +	test_must_fail git grep --quiet "intend to add this" HEAD
> +'
> +
> +test_expect_success 'grep can find things only in the index' '
> +	echo "only in the index" >cache-this &&
> +	git add cache-this &&
> +	rm cache-this &&
> +	test_when_finished "git rm --cached cache-this" &&
> +	test_must_fail git grep --quiet "only in the index" &&
> +	git grep --quiet --cached "only in the index" &&
> +	test_must_fail git grep --quiet "only in the index" HEAD
> +'
> +
> +test_expect_success 'grep does not report i-t-a with -L --cached' '
> +	echo "intend to add this" >intend-to-add &&
> +	git add -N intend-to-add &&
> +	test_when_finished "git rm -f intend-to-add" &&
> +	git ls-files | grep -v "^intend-to-add\$" >expected &&
> +	git grep -L --cached "nonexistent_string" >actual &&
> +	test_cmp expected actual
> +'
> +
> +test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
> +	echo "intend to add this" >intend-to-add-assume-unchanged &&
> +	git add -N intend-to-add-assume-unchanged &&
> +	test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
> +	git update-index --assume-unchanged intend-to-add-assume-unchanged &&
> +	git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
> +	git grep -L "nonexistent_string" >actual &&
> +	test_cmp expected actual
> +'
> +
>  test_done

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

end of thread, other threads:[~2016-07-01 20:40 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-06-30 10:13 [PATCH v3 0/3] Grepping with intent to add Charles Bailey
2016-06-30 10:13 ` [PATCH v3 1/3] t7810-grep.sh: fix duplicated test name Charles Bailey
2016-07-01 20:27   ` Junio C Hamano
2016-06-30 10:13 ` [PATCH v3 2/3] t7810-grep.sh: fix a whitespace inconsistency Charles Bailey
2016-06-30 10:13 ` [PATCH v3 3/3] grep: fix grepping for "intent to add" files Charles Bailey
2016-07-01 20:34   ` Junio C Hamano
2016-07-01 20:26 ` [PATCH v3 0/3] Grepping with intent to add Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).