Subject: [PATCH 3/3] grep: stop looking at random places for .gitattributes

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 10 Oct 2012 18:34:54 +0700

grep searches for .gitattributes using "name" field in struct
grep_source but that field is not real on-disk path name. For example,
"grep pattern rev" fills the field with "rev:path", which is
non-existent usually until somebody exploits it to drive git away.

attr does not support looking up .gitattributes from a tree object.
Making "git grep pattern <rev>" support .gitattributes could be a big
work. Just note in document what we support for now. The document
changes in this patch are to be reverted once support is in place.

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 Documentation/git-grep.txt | 7 +++++--
 grep.c                     | 3 ++-
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/Documentation/git-grep.txt b/Documentation/git-grep.txt
index cfecf84..a4c66ee 100644
--- a/Documentation/git-grep.txt
+++ b/Documentation/git-grep.txt
@@ -86,7 +86,9 @@ OPTIONS
 	files.
 
 -I::
-	Don't match the pattern in binary files.
+	Don't match the pattern in binary files. Note that binary
+	detection via .gitattributes only works with searching files
+	in working directory.
 
 --max-depth <depth>::
 	For each <pathspec> given on command line, descend at most <depth>
@@ -189,7 +191,8 @@ OPTIONS
 	the match, unless the matching line is a function name itself.
 	The name is determined in the same way as 'git diff' works out
 	patch hunk headers (see 'Defining a custom hunk-header' in
-	linkgit:gitattributes[5]).
+	linkgit:gitattributes[5]). Note that .gitattributes are only
+	support for searching files in working directory.
 
 -<num>::
 -C <num>::
diff --git a/grep.c b/grep.c
index 06bc1c6..e36c01b 100644
--- a/grep.c
+++ b/grep.c
@@ -1505,7 +1505,8 @@ void grep_source_load_driver(struct grep_source *gs)
 		return;
 
 	grep_attr_lock();
-	gs->driver = userdiff_find_by_path(gs->name);
+	if (gs->path)
+		gs->driver = userdiff_find_by_path(gs->path);
 	if (!gs->driver)
 		gs->driver = userdiff_find_by_name("default");
 	grep_attr_unlock();
-- 
1.7.12.1.406.g6ab07c4