From 194996be30b2b8c72c0648f7fd67be44cc41539d Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Tue, 30 Apr 2019 21:07:33 +0200 Subject: [PATCH] G: Use .git if found. --- G/G | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/G/G b/G/G index 6482fdf..5125fba 100755 --- a/G/G +++ b/G/G @@ -31,6 +31,9 @@ or with B<-g> (search full files matching expressions): Search current subtree for files that match all expressions - but not necessarily on the same line. +If a parent dir contains a B<.git> dir B is used. + + =item I All B options. @@ -97,8 +100,30 @@ for(@ARGV) { } if($opt::g and @cmd) { + sub gitdir { + # Find .git dir somewhere in parent + my $dir = shift; + -d "$dir/.git" and return "$dir/.git"; + if(join(":",stat $dir) eq join(":",stat "$dir/..")) { + # This is root + return undef; + } + return gitdir("../$dir") + } + sub cache_gitdir { + # cat all files in .git to /dev/null + my $dir = shift; + `find "$dir" -type f -print0 | xargs -0 cat >/dev/null`; + } my $a = shift @cmd; - my $run = 'find . -type f | parallel --lb -Xq grep -l '.shell_quote(@$a); + + my $gitdir = gitdir("."); + if($gitdir) { + cache_gitdir($gitdir); + $run = 'git grep --threads 30 -l '.shell_quote(@$a); + } else { + $run = 'find . -type f | parallel --lb -Xq grep -l '.shell_quote(@$a); + } if(@cmd) { $run .= '|' . join"|", map { 'xargs -d"\n" grep -l '.