Git log with perl regex
I am trying to do a search with a git log -G
regex that includes a negative lookbehind. For example:
git log -G "(?<!sup)port" --all
It doesn't work either based on my searches, I guess because it -G
uses a POSIX regex, which doesn't support negative searches.
I think it requires Perl compatible regex support. git log
has a flag --perl-regexp
, but from the documentation and examples, I only see how to use it to look up the commit message for fields like author, not code.
How can I use Perl regex to search for code using git log
?
source to share
You can simulate the program's capabilities below. It takes two arguments: a coarse filter that can return more results than you want, and then a full Perl pattern to narrow the results down to what you want.
For the example in your question, you will run it like in
logrx port '(?<!sup)port'
which assumes the program is in logrx
, executable, in yours PATH
, etc.
#! /usr/bin/env perl
use strict;
use warnings;
no warnings 'exec';
die "Usage: $0 pickaxe-string perl-pattern\n"
unless @ARGV == 2;
my($_S,$_G) = @ARGV;
my $filter = eval 'qr/^[-+]' . $_G . "/m"
or die "$0: invalid perl pattern: $@";
sub filter { local($_) = @_; print if /$filter/ }
open my $log, "-|", "git", "log", "-p", "-S" . $_S, "--all"
or die "$0: failed to start git log: $!";
my $commit;
while (<$log>) {
if (/^commit [0-9a-f]{40}$/) {
filter $commit if defined $commit;
$commit = $_;
}
else {
$commit .= $_;
}
}
filter $commit if defined $commit;
source to share