mirror of
git://projects.qi-hardware.com/wernermisc.git
synced 2024-11-21 21:25:54 +02:00
bin/grepchunk: process diff -u output and select/drop (-v) chunks like grep would
... if each chunk was a single line.
This commit is contained in:
parent
0508e23104
commit
8ffb473237
64
bin/grepchunk
Executable file
64
bin/grepchunk
Executable file
@ -0,0 +1,64 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
|
||||
sub usage
|
||||
{
|
||||
print STDERR "usage: $0 [-v] pattern [file ...]\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
if ($ARGV[0] eq "-v") {
|
||||
$invert = 1;
|
||||
shift @ARGV;
|
||||
}
|
||||
&usage if $ARGV[0] =~ /^-/;
|
||||
|
||||
$pattern = shift @ARGV;
|
||||
&usage unless defined $pattern;
|
||||
|
||||
|
||||
sub flush
|
||||
{
|
||||
my $mismatch = !($chunk =~ /$pattern/m);
|
||||
|
||||
if ($mismatch == $invert) {
|
||||
print $head;
|
||||
print $chunk;
|
||||
}
|
||||
undef $head;
|
||||
undef $chunk;
|
||||
}
|
||||
|
||||
|
||||
$HEAD = 0;
|
||||
$CHUNK = 1;
|
||||
$TAIL = 2;
|
||||
|
||||
|
||||
$state = $HEAD;
|
||||
while (<>) {
|
||||
$state = $HEAD if /^diff /;
|
||||
if (/^@@ -\d+,(\d+) \+\d+,(\d+)/) {
|
||||
$state = $CHUNK;
|
||||
($in, $out) = ($1, $2);
|
||||
$chunk = $_;
|
||||
next;
|
||||
}
|
||||
if ($state == $HEAD) {
|
||||
$head .= $_;
|
||||
next;
|
||||
}
|
||||
next if $state == $TAIL;
|
||||
$in-- if /^-/;
|
||||
$out-- if /^\+/;
|
||||
($in--, $out--) if /^\s/;
|
||||
die "in $in out $out" if $in < 0 || $out < 0;
|
||||
if ($in+$out == 0) {
|
||||
$state = $TAIL;
|
||||
&flush;
|
||||
} else {
|
||||
$chunk .= $_;
|
||||
}
|
||||
}
|
||||
&flush;
|
Loading…
Reference in New Issue
Block a user