grep should detect binary files like diff
RFC 1/1 grep should detect binary files like diff
Hi all,
The problem I have is a large number of fixture files, in text-based formats, that pollute the output of various git commands. I can mitigate this in git-diff using gitattributes mechanism, but there's no similar configuration for git-grep.
There are two issues with the naive approach to implementing this that I've attached below. Firstly the configuration variable is namespaced under "diff", and secondly the attributes mechanism is not thread-safe.
I'm inclined to think that the namespacing issue is not too significant; it would just require some documentation. If it is an issue then maybe a new "grep" attribute could be created. Are there any places where you might want git-grep and git-diff to treat different sets of files as binary?
The thread-safety of the attributes mechanism is a much bigger problem, and is the only reason I made the behaviour depend on a configuration option below. You can either have a multi-threaded grep, or a grep that detects binary files properly :(. I'm not sure how to even start resolving an issue like that, though I'm happy to accept pointers. Does anyone, Junio?, know what it would take to fix?
Conrad
--- Documentation/config.txt | 5 ++++ Documentation/git-grep.txt | 5 ++++ Documentation/gitattributes.txt | 3 ++ builtin/grep.c | 5 ++++ grep.c | 48 +++++++++++++++++++++----------------- grep.h | 1 + t/t7008-grep-binary.sh | 18 ++++++++++++++ 7 files changed, 63 insertions(+), 22 deletions(-)
Documentation/config.txt +5 -0
@@ -1070,6 +1070,11 @@ grep.lineNumber:: grep.extendedRegexp:: If set to true, enable '--extended-regexp' option by default. +grep.binaryFiles:: + If set to true, linkgit:git-grep[1] will treat files as binary under + the same circumstances as linkgit:git-diff[1]. See the + "Marking files as binary" section of linkgit:gitattributes[5]. + gui.commitmsgwidth:: Defines how wide the commit message window is in the linkgit:git-gui[1]. "75" is the default.
Documentation/git-grep.txt +5 -0
@@ -41,6 +41,11 @@ grep.lineNumber:: grep.extendedRegexp:: If set to true, enable '--extended-regexp' option by default. +grep.binaryFiles:: + If set to true, linkgit:git-grep[1] will treat files as binary under + the same circumstances as linkgit:git-diff[1]. See the + "Marking files as binary" section of linkgit:gitattributes[5]. + OPTIONS -------
Documentation/gitattributes.txt +3 -0
@@ -644,6 +644,9 @@ attribute in the `.gitattributes` file: This will cause git to generate `Binary files differ` (or a binary patch, if binary patches are enabled) instead of a regular diff. +If the grep.binaryFiles configuration variable is set, linkgit:git-grep[1] +will also treat such files as binary, by default printing +`Binary file matches` instead of the matching line. However, one may also want to specify other diff driver attributes. For example, you might want to use `textconv` to convert postscript files to
builtin/grep.c +5 -0
@@ -324,6 +324,11 @@ static int grep_config(const char *var, const char *value, void *cb)return 0 ; } +if (!strcmp (var,"grep.binaryfiles" )) { + opt->userdiff_binary =git_config_bool (var, value); +return 0 ; + } +if (!strcmp (var,"color.grep" )) opt->color =git_config_colorbool (var, value,-1 );else if (!strcmp (var,"color.grep.context" ))
grep.c +26 -22
71 lines not on this page · raw message
grep.h +1 -0
@@ -90,6 +90,7 @@ struct grep_opt {#define GREP_BINARY_NOMATCH 1#define GREP_BINARY_TEXT 2int binary; +int userdiff_binary;int extended;int pcre;int relative;
t/t7008-grep-binary.sh +18 -0
@@ -99,4 +99,22 @@ test_expect_failure 'git grep y<NUL>x a' " test_must_fail git grep -f f a " +test_expect_success 'git -c grep.binaryFiles=1 grep ina a' " + echo 'a diff' > .gitattributes && + printf 'binaryQfile' | q_to_nul >a && + echo 'a:binaryQfile' | q_to_nul >expect && + git -c grep.binaryFiles=1 grep ina a > actual && + rm .gitattributes && + test_cmp expect actual +" +test_expect_success 'git -c grep.binaryFiles=1 grep tex t' " + echo 'text' > t && + git add t && + echo 't -diff' > .gitattributes && + echo Binary file t matches >expect && + git -c grep.binaryFiles=1 grep tex t >actual && + rm .gitattributes && + test_cmp expect actual +" + test_done
Signature · 1 line
1.7.6.409.ge7a85