| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | use strict; |
|---|
| 4 | use warnings; |
|---|
| 5 | |
|---|
| 6 | my $reg_old = undef; |
|---|
| 7 | my $reg_new = undef; |
|---|
| 8 | my $require_both_hits = 0; |
|---|
| 9 | |
|---|
| 10 | my $args = scalar(@ARGV); |
|---|
| 11 | if ($args>0) { |
|---|
| 12 | while (scalar(@ARGV)) { |
|---|
| 13 | my $arg = shift @ARGV; |
|---|
| 14 | if ($arg eq '--help' or $arg eq '-h') { |
|---|
| 15 | print "Usage: diff2grep.pl [switches]\n"; |
|---|
| 16 | print "Converts a patch into grep-alike output\n"; |
|---|
| 17 | print "switches:\n"; |
|---|
| 18 | print " --old regexp only list old lines matching regexp\n"; |
|---|
| 19 | print " --new regexp only list new lines matching regexp\n"; |
|---|
| 20 | print " --both regexp only list if old and new lines match regexp\n"; |
|---|
| 21 | print " (specifying --old and --new has same effect as --both)\n"; |
|---|
| 22 | exit(0); |
|---|
| 23 | } |
|---|
| 24 | elsif ($arg eq '--old') { |
|---|
| 25 | my $reg = shift @ARGV; |
|---|
| 26 | die "--old expects argument 'regexp'" if not defined $reg; |
|---|
| 27 | $reg_old = qr/$reg/; |
|---|
| 28 | } |
|---|
| 29 | elsif ($arg eq '--new') { |
|---|
| 30 | my $reg = shift @ARGV; |
|---|
| 31 | die "--new expects argument 'regexp'" if not defined $reg; |
|---|
| 32 | $reg_new = qr/$reg/; |
|---|
| 33 | } |
|---|
| 34 | elsif ($arg eq '--both') { |
|---|
| 35 | my $reg = shift @ARGV; |
|---|
| 36 | die "--both expects argument 'regexp'" if not defined $reg; |
|---|
| 37 | $reg_old = qr/$reg/; |
|---|
| 38 | $reg_new = $reg_old; |
|---|
| 39 | $require_both_hits = 1; |
|---|
| 40 | } |
|---|
| 41 | else { |
|---|
| 42 | die "Unknown argument '$arg'"; |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | my $reg_all = qr/./; |
|---|
| 48 | my $reg_none = qr/jkasfgadgsfaf/; |
|---|
| 49 | if (defined $reg_old) { |
|---|
| 50 | if (defined $reg_new) { $require_both_hits = 1; } |
|---|
| 51 | else { $reg_new = $reg_none; } |
|---|
| 52 | } |
|---|
| 53 | elsif (defined $reg_new) { $reg_old = $reg_none; } |
|---|
| 54 | else { |
|---|
| 55 | $reg_old = $reg_all; |
|---|
| 56 | $reg_new = $reg_all; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | my $oldfile = undef; |
|---|
| 60 | my $newfile = undef; |
|---|
| 61 | |
|---|
| 62 | my $newpad = ''; |
|---|
| 63 | my $oldpad = ''; |
|---|
| 64 | |
|---|
| 65 | my $correct = 0; # perform correction? |
|---|
| 66 | my $corr = 0; # correct by number of lines (calc for next hunk) |
|---|
| 67 | my $this_corr = 0; # correct by number of lines (value for current hunk) |
|---|
| 68 | |
|---|
| 69 | sub set_padding_and_correction() { |
|---|
| 70 | $correct = 1; |
|---|
| 71 | $corr = 0; |
|---|
| 72 | |
|---|
| 73 | if (defined $oldfile and defined $newfile) { |
|---|
| 74 | my $oldlen = length($oldfile); |
|---|
| 75 | my $newlen = length($newfile); |
|---|
| 76 | |
|---|
| 77 | $newpad = ''; |
|---|
| 78 | $oldpad = ''; |
|---|
| 79 | |
|---|
| 80 | while ($oldlen<$newlen) { $oldpad .= ' '; $oldlen++; } |
|---|
| 81 | while ($newlen<$oldlen) { $newpad .= ' '; $newlen++; } |
|---|
| 82 | |
|---|
| 83 | if ($oldfile ne $newfile) { $correct = 0; } |
|---|
| 84 | |
|---|
| 85 | print "diff2grep.pl: oldfile='$oldfile'\n"; |
|---|
| 86 | print "diff2grep.pl: newfile='$newfile'\n"; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | sub print_tagged($$) { |
|---|
| 91 | my ($tag,$line) = @_; |
|---|
| 92 | if ($line =~ /^\s+/o) { |
|---|
| 93 | my ($pad,$rest) = ($&,$'); |
|---|
| 94 | my $tag_len = length($tag); |
|---|
| 95 | my $pad_len = length($pad); |
|---|
| 96 | |
|---|
| 97 | if ($pad_len>($tag_len+1)) { |
|---|
| 98 | my $extra_pad = substr($pad,$pad_len-$tag_len-1); |
|---|
| 99 | $line = $extra_pad.$rest; |
|---|
| 100 | } |
|---|
| 101 | else { |
|---|
| 102 | $line = $rest; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | print $tag.' '.$line."\n"; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | my $pwd = `pwd`; chomp($pwd); |
|---|
| 109 | |
|---|
| 110 | print "diff2grep.pl: Entering directory `$pwd'\n"; |
|---|
| 111 | |
|---|
| 112 | my ($o1,$o2,$n1,$n2); |
|---|
| 113 | my $old_hitten = undef; |
|---|
| 114 | |
|---|
| 115 | while (defined ($_ = <STDIN>)) { |
|---|
| 116 | chomp; |
|---|
| 117 | if (/^--- ([^\t]+)\t/) { |
|---|
| 118 | $oldfile = $1; |
|---|
| 119 | set_padding_and_correction(); |
|---|
| 120 | } |
|---|
| 121 | elsif (/^\+\+\+ ([^\t]+)\t/) { |
|---|
| 122 | $newfile = $1; |
|---|
| 123 | set_padding_and_correction(); |
|---|
| 124 | $this_corr = $corr; |
|---|
| 125 | } |
|---|
| 126 | elsif (/^-/) { |
|---|
| 127 | my $rest = $'; |
|---|
| 128 | if ($rest =~ $reg_old) { |
|---|
| 129 | my $hitmsg = "$oldfile:".($o1+$this_corr*$correct).":$oldpad OLD: $rest\n"; |
|---|
| 130 | if ($require_both_hits) { $old_hitten = $hitmsg; } |
|---|
| 131 | else { print $hitmsg; } |
|---|
| 132 | } |
|---|
| 133 | else { |
|---|
| 134 | print_tagged('[OLD]', $rest); |
|---|
| 135 | } |
|---|
| 136 | $corr--; |
|---|
| 137 | $o1++; |
|---|
| 138 | } |
|---|
| 139 | elsif (/^\+/) { |
|---|
| 140 | my $rest = $'; |
|---|
| 141 | if ($rest =~ $reg_new) { |
|---|
| 142 | my $showNew = 1; |
|---|
| 143 | if ($require_both_hits==1) { |
|---|
| 144 | if (defined $old_hitten) { |
|---|
| 145 | print $old_hitten; |
|---|
| 146 | } |
|---|
| 147 | else { |
|---|
| 148 | $showNew = 0; |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | if ($showNew==1) { |
|---|
| 152 | print "$newfile:".($n1).":$newpad NEW: $rest\n"; |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | else { |
|---|
| 156 | print_tagged('[NEW]', $rest); |
|---|
| 157 | } |
|---|
| 158 | $corr++; |
|---|
| 159 | $n1++; |
|---|
| 160 | } |
|---|
| 161 | elsif (/^\@\@ -(.*),(.*) \+(.*),(.*) \@\@$/) { |
|---|
| 162 | ($o1,$o2,$n1,$n2) = ($1,$2,$3,$4); |
|---|
| 163 | print $_."\n"; |
|---|
| 164 | $this_corr = $corr; |
|---|
| 165 | $old_hitten = undef; |
|---|
| 166 | } |
|---|
| 167 | else { |
|---|
| 168 | print $_."\n"; |
|---|
| 169 | $o1++; |
|---|
| 170 | $n1++; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | print "diff2grep.pl: Leaving directory `$pwd'\n"; |
|---|
| 175 | |
|---|