Skip to content

Commit c12c71f

Browse files
committed
Merge branch 'nd/ita-cleanup' into maint
Git does not know what the contents in the index should be for a path added with "git add -N" yet, so "git grep --cached" should not show hits (or show lack of hits, with -L) in such a path, but that logic does not apply to "git grep", i.e. searching in the working tree files. But we did so by mistake, which has been corrected. * nd/ita-cleanup: grep: fix grepping for "intent to add" files t7810-grep.sh: fix a whitespace inconsistency t7810-grep.sh: fix duplicated test name
2 parents 4966b58 + b8e47d1 commit c12c71f

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

builtin/grep.c

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

387387
for (nr = 0; nr < active_nr; nr++) {
388388
const struct cache_entry *ce = active_cache[nr];
389-
if (!S_ISREG(ce->ce_mode) || ce_intent_to_add(ce))
389+
if (!S_ISREG(ce->ce_mode))
390390
continue;
391391
if (!ce_path_match(ce, pathspec, NULL))
392392
continue;
@@ -396,7 +396,7 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
396396
* cache version instead
397397
*/
398398
if (cached || (ce->ce_flags & CE_VALID) || ce_skip_worktree(ce)) {
399-
if (ce_stage(ce))
399+
if (ce_stage(ce) || ce_intent_to_add(ce))
400400
continue;
401401
hit |= grep_sha1(opt, ce->sha1, ce->name, 0, ce->name);
402402
}

t/t7810-grep.sh

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ do
177177

178178
test_expect_success "grep -c $L (no /dev/null)" '
179179
! git grep -c test $H | grep /dev/null
180-
'
180+
'
181181

182182
test_expect_success "grep --max-depth -1 $L" '
183183
{
@@ -1377,4 +1377,62 @@ test_expect_success 'grep --color -e A --and -e B -p with context' '
13771377
test_cmp expected actual
13781378
'
13791379

1380+
test_expect_success 'grep can find things only in the work tree' '
1381+
: >work-tree-only &&
1382+
git add work-tree-only &&
1383+
test_when_finished "git rm -f work-tree-only" &&
1384+
echo "find in work tree" >work-tree-only &&
1385+
git grep --quiet "find in work tree" &&
1386+
test_must_fail git grep --quiet --cached "find in work tree" &&
1387+
test_must_fail git grep --quiet "find in work tree" HEAD
1388+
'
1389+
1390+
test_expect_success 'grep can find things only in the work tree (i-t-a)' '
1391+
echo "intend to add this" >intend-to-add &&
1392+
git add -N intend-to-add &&
1393+
test_when_finished "git rm -f intend-to-add" &&
1394+
git grep --quiet "intend to add this" &&
1395+
test_must_fail git grep --quiet --cached "intend to add this" &&
1396+
test_must_fail git grep --quiet "intend to add this" HEAD
1397+
'
1398+
1399+
test_expect_success 'grep does not search work tree with assume unchanged' '
1400+
echo "intend to add this" >intend-to-add &&
1401+
git add -N intend-to-add &&
1402+
git update-index --assume-unchanged intend-to-add &&
1403+
test_when_finished "git rm -f intend-to-add" &&
1404+
test_must_fail git grep --quiet "intend to add this" &&
1405+
test_must_fail git grep --quiet --cached "intend to add this" &&
1406+
test_must_fail git grep --quiet "intend to add this" HEAD
1407+
'
1408+
1409+
test_expect_success 'grep can find things only in the index' '
1410+
echo "only in the index" >cache-this &&
1411+
git add cache-this &&
1412+
rm cache-this &&
1413+
test_when_finished "git rm --cached cache-this" &&
1414+
test_must_fail git grep --quiet "only in the index" &&
1415+
git grep --quiet --cached "only in the index" &&
1416+
test_must_fail git grep --quiet "only in the index" HEAD
1417+
'
1418+
1419+
test_expect_success 'grep does not report i-t-a with -L --cached' '
1420+
echo "intend to add this" >intend-to-add &&
1421+
git add -N intend-to-add &&
1422+
test_when_finished "git rm -f intend-to-add" &&
1423+
git ls-files | grep -v "^intend-to-add\$" >expected &&
1424+
git grep -L --cached "nonexistent_string" >actual &&
1425+
test_cmp expected actual
1426+
'
1427+
1428+
test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
1429+
echo "intend to add this" >intend-to-add-assume-unchanged &&
1430+
git add -N intend-to-add-assume-unchanged &&
1431+
test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
1432+
git update-index --assume-unchanged intend-to-add-assume-unchanged &&
1433+
git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
1434+
git grep -L "nonexistent_string" >actual &&
1435+
test_cmp expected actual
1436+
'
1437+
13801438
test_done

0 commit comments

Comments
 (0)