{"thread":{"id":"42755","subject":"[PATCH v3 3/3] grep: fix grepping for \"intent to add\" files","startedAt":"2016-06-30T10:49:57Z","lastAt":"2016-07-01T20:40:31Z","messageCount":7,"participants":["Charles Bailey","Junio C Hamano"],"isPatch":true,"patchVersion":3,"patchTotal":3},"messages":[{"id":"290588","messageId":"20160630101348.15665-4-charles@hashpling.org","threadId":"42755","inReplyTo":"20160630101348.15665-1-charles@hashpling.org","subject":"[PATCH v3 3/3] grep: fix grepping for \"intent to add\" files","fromName":"Charles Bailey","fromEmail":"charles@hashpling.org","sentAt":"2016-06-30T10:13:48Z","receivedAt":"2016-06-30T10:49:57Z","isPatch":true,"sender":{"key":"charles@hashpling.org","avatar":"https://avatars.githubusercontent.com/u/1668475?v=4"},"body":"From: Charles Bailey <cbailey32@bloomberg.net>\n\nThis reverts commit 4d5520053 (grep: make it clear i-t-a entries are\nignored, 2015-12-27) and adds an alternative fix to maintain the -L\n--cached behavior.\n\n4d5520053 caused 'git grep' to no longer find matches in new files in\nthe working tree where the corresponding index entry had the \"intent to\nadd\" bit set, despite the fact that these files are tracked.\n\nThe content in the index of a file for which the \"intent to add\" bit is\nset is considered indeterminate and not empty. For most grep queries we\nwant these to behave the same, however for -L --cached (files without a\nmatch) we don't want to respond positively for \"intent to add\" files as\ntheir contents are indeterminate. This is in contrast to files with\nempty contents in the index (no lines implies no matches for any grep\nquery expression) which should be reported in the output of a grep -L\n--cached invocation.\n\nAdd tests to cover this case and a few related cases which previously\nlacked coverage.\n\nHelped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Charles Bailey <cbailey32@bloomberg.net>\n---\n builtin/grep.c  |  4 ++--\n t/t7810-grep.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n 2 files changed, 60 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex 462e607..ae73831 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -386,7 +386,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int\n \n \tfor (nr = 0; nr < active_nr; nr++) {\n \t\tconst struct cache_entry *ce = active_cache[nr];\n-\t\tif (!S_ISREG(ce->ce_mode) || ce_intent_to_add(ce))\n+\t\tif (!S_ISREG(ce->ce_mode))\n \t\t\tcontinue;\n \t\tif (!ce_path_match(ce, pathspec, NULL))\n \t\t\tcontinue;\n@@ -396,7 +396,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int\n \t\t * cache version instead\n \t\t */\n \t\tif (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {\n-\t\t\tif (ce_stage(ce))\n+\t\t\tif (ce_stage(ce) || ce_intent_to_add(ce))\n \t\t\t\tcontinue;\n \t\t\thit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name);\n \t\t}\ndiff --git a/t/t7810-grep.sh b/t/t7810-grep.sh\nindex 6e6eaa4..c18e954 100755\n--- a/t/t7810-grep.sh\n+++ b/t/t7810-grep.sh\n@@ -1364,4 +1364,62 @@ test_expect_success 'grep --color -e A --and -e B -p with context' '\n \ttest_cmp expected actual\n '\n \n+test_expect_success 'grep can find things only in the work tree' '\n+\t: >work-tree-only &&\n+\tgit add work-tree-only &&\n+\ttest_when_finished \"git rm -f work-tree-only\" &&\n+\techo \"find in work tree\" >work-tree-only &&\n+\tgit grep --quiet \"find in work tree\" &&\n+\ttest_must_fail git grep --quiet --cached \"find in work tree\" &&\n+\ttest_must_fail git grep --quiet \"find in work tree\" HEAD\n+'\n+\n+test_expect_success 'grep can find things only in the work tree (i-t-a)' '\n+\techo \"intend to add this\" >intend-to-add &&\n+\tgit add -N intend-to-add &&\n+\ttest_when_finished \"git rm -f intend-to-add\" &&\n+\tgit grep --quiet \"intend to add this\" &&\n+\ttest_must_fail git grep --quiet --cached \"intend to add this\" &&\n+\ttest_must_fail git grep --quiet \"intend to add this\" HEAD\n+'\n+\n+test_expect_success 'grep does not search work tree with assume unchanged' '\n+\techo \"intend to add this\" >intend-to-add &&\n+\tgit add -N intend-to-add &&\n+\tgit update-index --assume-unchanged intend-to-add &&\n+\ttest_when_finished \"git rm -f intend-to-add\" &&\n+\ttest_must_fail git grep --quiet \"intend to add this\" &&\n+\ttest_must_fail git grep --quiet --cached \"intend to add this\" &&\n+\ttest_must_fail git grep --quiet \"intend to add this\" HEAD\n+'\n+\n+test_expect_success 'grep can find things only in the index' '\n+\techo \"only in the index\" >cache-this &&\n+\tgit add cache-this &&\n+\trm cache-this &&\n+\ttest_when_finished \"git rm --cached cache-this\" &&\n+\ttest_must_fail git grep --quiet \"only in the index\" &&\n+\tgit grep --quiet --cached \"only in the index\" &&\n+\ttest_must_fail git grep --quiet \"only in the index\" HEAD\n+'\n+\n+test_expect_success 'grep does not report i-t-a with -L --cached' '\n+\techo \"intend to add this\" >intend-to-add &&\n+\tgit add -N intend-to-add &&\n+\ttest_when_finished \"git rm -f intend-to-add\" &&\n+\tgit ls-files | grep -v \"^intend-to-add\\$\" >expected &&\n+\tgit grep -L --cached \"nonexistent_string\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n+test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '\n+\techo \"intend to add this\" >intend-to-add-assume-unchanged &&\n+\tgit add -N intend-to-add-assume-unchanged &&\n+\ttest_when_finished \"git rm -f intend-to-add-assume-unchanged\" &&\n+\tgit update-index --assume-unchanged intend-to-add-assume-unchanged &&\n+\tgit ls-files | grep -v \"^intend-to-add-assume-unchanged\\$\" >expected &&\n+\tgit grep -L \"nonexistent_string\" >actual &&\n+\ttest_cmp expected actual\n+'\n+\n test_done\n-- \n2.8.2.311.gee88674\n\n"},{"id":"290589","messageId":"20160630101348.15665-1-charles@hashpling.org","threadId":"42755","inReplyTo":null,"subject":"[PATCH v3 0/3] Grepping with intent to add","fromName":"Charles Bailey","fromEmail":"charles@hashpling.org","sentAt":"2016-06-30T10:13:45Z","receivedAt":"2016-06-30T10:50:23Z","isPatch":true,"sender":{"key":"charles@hashpling.org","avatar":"https://avatars.githubusercontent.com/u/1668475?v=4"},"body":"So I've got back around to this topic again.\n\nI've applied fixes to the tests as suggested by Eric and Junio.\n\nI came up with a test case that demonstrates a difference between the\nadditional fix that Duy suggested and the alternative that Junio\nsuggested.\n\nI've kept Duy's fix because I think it makes more sense, although it's\na sufficiently obscure case that I don't feel strongly that it's\ndefinitely the best behavior.\n\nThe fix ensures that if you have a file which is both \"intend to add\"\nand \"assume unchanged\" that it is not listed if you \"grep -L\" for for\nsomething. In effect, we are applying the \"contents indeterminate\" state\nof the index to the working tree file.\n\nCharles Bailey (3):\n  t7810-grep.sh: fix duplicated test name\n  t7810-grep.sh: fix a whitespace inconsistency\n  grep: fix grepping for \"intent to add\" files\n\n builtin/grep.c  |  4 ++--\n t/t7810-grep.sh | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--\n 2 files changed, 62 insertions(+), 4 deletions(-)\n\n-- \n2.8.2.311.gee88674\n\n"},{"id":"290590","messageId":"20160630101348.15665-3-charles@hashpling.org","threadId":"42755","inReplyTo":"20160630101348.15665-1-charles@hashpling.org","subject":"[PATCH v3 2/3] t7810-grep.sh: fix a whitespace inconsistency","fromName":"Charles Bailey","fromEmail":"charles@hashpling.org","sentAt":"2016-06-30T10:13:47Z","receivedAt":"2016-06-30T10:50:27Z","isPatch":true,"sender":{"key":"charles@hashpling.org","avatar":"https://avatars.githubusercontent.com/u/1668475?v=4"},"body":"Signed-off-by: Charles Bailey <charles@hashpling.org>\n---\n t/t7810-grep.sh | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/t/t7810-grep.sh b/t/t7810-grep.sh\nindex c4302ed..6e6eaa4 100755\n--- a/t/t7810-grep.sh\n+++ b/t/t7810-grep.sh\n@@ -175,7 +175,7 @@ do\n \n \ttest_expect_success \"grep -c $L (no /dev/null)\" '\n \t\t! git grep -c test $H | grep /dev/null\n-        '\n+\t'\n \n \ttest_expect_success \"grep --max-depth -1 $L\" '\n \t\t{\n-- \n2.8.2.311.gee88674\n\n"},{"id":"290591","messageId":"20160630101348.15665-2-charles@hashpling.org","threadId":"42755","inReplyTo":"20160630101348.15665-1-charles@hashpling.org","subject":"[PATCH v3 1/3] t7810-grep.sh: fix duplicated test name","fromName":"Charles Bailey","fromEmail":"charles@hashpling.org","sentAt":"2016-06-30T10:13:46Z","receivedAt":"2016-06-30T11:18:54Z","isPatch":true,"sender":{"key":"charles@hashpling.org","avatar":"https://avatars.githubusercontent.com/u/1668475?v=4"},"body":"Signed-off-by: Charles Bailey <charles@hashpling.org>\n---\n t/t7810-grep.sh | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/t/t7810-grep.sh b/t/t7810-grep.sh\nindex 1e72971..c4302ed 100755\n--- a/t/t7810-grep.sh\n+++ b/t/t7810-grep.sh\n@@ -353,7 +353,7 @@ test_expect_success 'grep -l -C' '\n cat >expected <<EOF\n file:5\n EOF\n-test_expect_success 'grep -l -C' '\n+test_expect_success 'grep -c -C' '\n \tgit grep -c -C1 foo >actual &&\n \ttest_cmp expected actual\n '\n-- \n2.8.2.311.gee88674\n\n"},{"id":"290710","messageId":"xmqqtwg9w1cd.fsf@gitster.mtv.corp.google.com","threadId":"42755","inReplyTo":"20160630101348.15665-2-charles@hashpling.org","subject":"Re: [PATCH v3 1/3] t7810-grep.sh: fix duplicated test name","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-07-01T20:27:30Z","receivedAt":"2016-07-01T20:28:02Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Charles Bailey <charles@hashpling.org> writes:\n\n> @@ -353,7 +353,7 @@ test_expect_success 'grep -l -C' '\n>  cat >expected <<EOF\n>  file:5\n>  EOF\n> -test_expect_success 'grep -l -C' '\n> +test_expect_success 'grep -c -C' '\n>  \tgit grep -c -C1 foo >actual &&\n>  \ttest_cmp expected actual\n>  '\n\nMakes sense.  The previous one (outside the pre-context) is about\nrunning \"grep -l -C\", but this one is about \"grep -c -C\".\n\n\n\n"},{"id":"290711","messageId":"xmqqy45lw1eb.fsf@gitster.mtv.corp.google.com","threadId":"42755","inReplyTo":"20160630101348.15665-1-charles@hashpling.org","subject":"Re: [PATCH v3 0/3] Grepping with intent to add","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-07-01T20:26:20Z","receivedAt":"2016-07-01T20:30:23Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Charles Bailey <charles@hashpling.org> writes:\n\n> So I've got back around to this topic again.\n>\n> I've applied fixes to the tests as suggested by Eric and Junio.\n>\n> I came up with a test case that demonstrates a difference between the\n> additional fix that Duy suggested and the alternative that Junio\n> suggested.\n\nThanks, will queue.\n"},{"id":"290712","messageId":"xmqqpoqxw11d.fsf@gitster.mtv.corp.google.com","threadId":"42755","inReplyTo":"20160630101348.15665-4-charles@hashpling.org","subject":"Re: [PATCH v3 3/3] grep: fix grepping for \"intent to add\" files","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-07-01T20:34:06Z","receivedAt":"2016-07-01T20:40:31Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Charles Bailey <charles@hashpling.org> writes:\n\n> +test_expect_success 'grep can find things only in the work tree' '\n> +\t: >work-tree-only &&\n> +\tgit add work-tree-only &&\n> +\ttest_when_finished \"git rm -f work-tree-only\" &&\n\nIt is better to have the test_when_finished before \"git add\", and\npossibly before the creation of the test file.\n\nIt is not worth a reroll and it is perfectly fine to leave it as a\nlow-hanging-fruit for later clean-up material, because this test is\nnot about catchign a \"git add\" that suddenly starts failing in a\nstrange way (e.g. adds to the index but exits with non-zero).\n\nSame comment for the use of test_when_finished in the remainder of\nthe patch.\n\nThanks.\n\n> +\techo \"find in work tree\" >work-tree-only &&\n> +\tgit grep --quiet \"find in work tree\" &&\n> +\ttest_must_fail git grep --quiet --cached \"find in work tree\" &&\n> +\ttest_must_fail git grep --quiet \"find in work tree\" HEAD\n> +'\n> +\n> +test_expect_success 'grep can find things only in the work tree (i-t-a)' '\n> +\techo \"intend to add this\" >intend-to-add &&\n> +\tgit add -N intend-to-add &&\n> +\ttest_when_finished \"git rm -f intend-to-add\" &&\n> +\tgit grep --quiet \"intend to add this\" &&\n> +\ttest_must_fail git grep --quiet --cached \"intend to add this\" &&\n> +\ttest_must_fail git grep --quiet \"intend to add this\" HEAD\n> +'\n> +\n> +test_expect_success 'grep does not search work tree with assume unchanged' '\n> +\techo \"intend to add this\" >intend-to-add &&\n> +\tgit add -N intend-to-add &&\n> +\tgit update-index --assume-unchanged intend-to-add &&\n> +\ttest_when_finished \"git rm -f intend-to-add\" &&\n> +\ttest_must_fail git grep --quiet \"intend to add this\" &&\n> +\ttest_must_fail git grep --quiet --cached \"intend to add this\" &&\n> +\ttest_must_fail git grep --quiet \"intend to add this\" HEAD\n> +'\n> +\n> +test_expect_success 'grep can find things only in the index' '\n> +\techo \"only in the index\" >cache-this &&\n> +\tgit add cache-this &&\n> +\trm cache-this &&\n> +\ttest_when_finished \"git rm --cached cache-this\" &&\n> +\ttest_must_fail git grep --quiet \"only in the index\" &&\n> +\tgit grep --quiet --cached \"only in the index\" &&\n> +\ttest_must_fail git grep --quiet \"only in the index\" HEAD\n> +'\n> +\n> +test_expect_success 'grep does not report i-t-a with -L --cached' '\n> +\techo \"intend to add this\" >intend-to-add &&\n> +\tgit add -N intend-to-add &&\n> +\ttest_when_finished \"git rm -f intend-to-add\" &&\n> +\tgit ls-files | grep -v \"^intend-to-add\\$\" >expected &&\n> +\tgit grep -L --cached \"nonexistent_string\" >actual &&\n> +\ttest_cmp expected actual\n> +'\n> +\n> +test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '\n> +\techo \"intend to add this\" >intend-to-add-assume-unchanged &&\n> +\tgit add -N intend-to-add-assume-unchanged &&\n> +\ttest_when_finished \"git rm -f intend-to-add-assume-unchanged\" &&\n> +\tgit update-index --assume-unchanged intend-to-add-assume-unchanged &&\n> +\tgit ls-files | grep -v \"^intend-to-add-assume-unchanged\\$\" >expected &&\n> +\tgit grep -L \"nonexistent_string\" >actual &&\n> +\ttest_cmp expected actual\n> +'\n> +\n>  test_done\n"}]}