Skip to content

Commit b8e47d1

Browse files
hashplinggitster
authored andcommitted
grep: fix grepping for "intent to add" files
This reverts commit 4d55200 (grep: make it clear i-t-a entries are ignored, 2015-12-27) and adds an alternative fix to maintain the -L --cached behavior. 4d55200 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 <[email protected]> Signed-off-by: Charles Bailey <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 89e6410 commit b8e47d1

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

builtin/grep.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
375375

376376
for (nr = 0; nr < active_nr; nr++) {
377377
const struct cache_entry *ce = active_cache[nr];
378-
if (!S_ISREG(ce->ce_mode) || ce_intent_to_add(ce))
378+
if (!S_ISREG(ce->ce_mode))
379379
continue;
380380
if (!ce_path_match(ce, pathspec, NULL))
381381
continue;
@@ -385,7 +385,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
385385
* cache version instead
386386
*/
387387
if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
388-
if (ce_stage(ce))
388+
if (ce_stage(ce) || ce_intent_to_add(ce))
389389
continue;
390390
hit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name);
391391
}

t/t7810-grep.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,4 +1296,62 @@ test_expect_success 'grep --color -e A --and -e B -p with context' '
12961296
test_cmp expected actual
12971297
'
12981298

1299+
test_expect_success 'grep can find things only in the work tree' '
1300+
: >work-tree-only &&
1301+
git add work-tree-only &&
1302+
test_when_finished "git rm -f work-tree-only" &&
1303+
echo "find in work tree" >work-tree-only &&
1304+
git grep --quiet "find in work tree" &&
1305+
test_must_fail git grep --quiet --cached "find in work tree" &&
1306+
test_must_fail git grep --quiet "find in work tree" HEAD
1307+
'
1308+
1309+
test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1310+
echo "intend to add this" >intend-to-add &&
1311+
git add -N intend-to-add &&
1312+
test_when_finished "git rm -f intend-to-add" &&
1313+
git grep --quiet "intend to add this" &&
1314+
test_must_fail git grep --quiet --cached "intend to add this" &&
1315+
test_must_fail git grep --quiet "intend to add this" HEAD
1316+
'
1317+
1318+
test_expect_success 'grep does not search work tree with assume unchanged' '
1319+
echo "intend to add this" >intend-to-add &&
1320+
git add -N intend-to-add &&
1321+
git update-index --assume-unchanged intend-to-add &&
1322+
test_when_finished "git rm -f intend-to-add" &&
1323+
test_must_fail git grep --quiet "intend to add this" &&
1324+
test_must_fail git grep --quiet --cached "intend to add this" &&
1325+
test_must_fail git grep --quiet "intend to add this" HEAD
1326+
'
1327+
1328+
test_expect_success 'grep can find things only in the index' '
1329+
echo "only in the index" >cache-this &&
1330+
git add cache-this &&
1331+
rm cache-this &&
1332+
test_when_finished "git rm --cached cache-this" &&
1333+
test_must_fail git grep --quiet "only in the index" &&
1334+
git grep --quiet --cached "only in the index" &&
1335+
test_must_fail git grep --quiet "only in the index" HEAD
1336+
'
1337+
1338+
test_expect_success 'grep does not report i-t-a with -L --cached' '
1339+
echo "intend to add this" >intend-to-add &&
1340+
git add -N intend-to-add &&
1341+
test_when_finished "git rm -f intend-to-add" &&
1342+
git ls-files | grep -v "^intend-to-add\$" >expected &&
1343+
git grep -L --cached "nonexistent_string" >actual &&
1344+
test_cmp expected actual
1345+
'
1346+
1347+
test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1348+
echo "intend to add this" >intend-to-add-assume-unchanged &&
1349+
git add -N intend-to-add-assume-unchanged &&
1350+
test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1351+
git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1352+
git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1353+
git grep -L "nonexistent_string" >actual &&
1354+
test_cmp expected actual
1355+
'
1356+
12991357
test_done

0 commit comments

Comments
 (0)