git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] grep: grep cache entries if they are "assume unchanged"

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Dec 27, 2008, 08:21 UTC
Message-ID
<1230366064-1306-1-git-send-email-pclouds@gmail.com>

"Assume unchanged" bit means "please pretend that I have never touched this file", so if user removes the file, we should not care.

This patch teaches "git grep" to use cache version in such situations. External grep case has not been fixed yet. But given that on the platform that CE_VALID bit may be used like Windows, external grep is not available anyway, I would wait for people to raise their hands before touching it.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 I will do git-grep fixes for sparse checkout later because it will require
 nd/narrow. This series does not, so it can be applied separatedly.
 builtin-grep.c  |    7 ++++++-
 t/t7002-grep.sh |    7 +++++++
 2 files changed, 13 insertions(+), 1 deletions(-)
diff --git a/builtin-grep.c b/builtin-grep.c
index 3c97c2c..bebf15c 100644
--- a/builtin-grep.c
+++ b/builtin-grep.c
@@ -404,7 +404,12 @@ static int grep_cache(struct grep_opt *opt, const char **paths, int cached)
 			continue;
 		if (!pathspec_matches(paths, ce->name))
 			continue;
-		if (cached) {
+		/*
+		 * If CE_VALID is on, we assume worktree file and its cache entry
+		 * are identical, even if worktree file has been modified, so use
+		 * cache version instead
+		 */
+		if (cached || (ce->ce_flags & CE_VALID)) {
 			if (ce_stage(ce))
 				continue;
 			hit |= grep_sha1(opt, ce->sha1, ce->name, 0);
diff --git a/t/t7002-grep.sh b/t/t7002-grep.sh
index 18fe6f2..e12b5dc 100755
--- a/t/t7002-grep.sh
+++ b/t/t7002-grep.sh
@@ -161,7 +161,14 @@ test_expect_success 'log grep (6)' '
 	git log --author=-0700  --pretty=tformat:%s >actual &&
 	>expect &&
 	test_cmp expect actual
+'
 
+test_expect_success 'grep with CE_VALID file' '
+	git update-index --assume-unchanged t/t &&
+	rm t/t &&
+	test "$(git grep --no-ext-grep t)" = "t/t:test" &&
+	git update-index --no-assume-unchanged t/t &&
+	git checkout t/t
 '
 
 test_done
-- 
1.6.0.4.1116.g25b13
Next: Nguyễn Thái Ngọc Duy
Message 1 of 4 in “grep: grep cache entries if they are "assume unchanged"”
  1. 2/2 grep: grep cache entries if they are "assume unchanged"Nguyễn Thái Ngọc Duy, Dec 27, 2008
  2. 1/2 grep: support --no-ext-grep to test builtin grepNguyễn Thái Ngọc Duy, Dec 27, 2008
  3. Junio C HamanoDec 27, 2008
  4. Junio C HamanoDec 28, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.