1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | my $reg_old = undef; |
---|
7 | my $reg_new = undef; |
---|
8 | |
---|
9 | my $inv_old = 0; |
---|
10 | my $inv_new = 0; |
---|
11 | |
---|
12 | my $require_both_hits = 0; |
---|
13 | my $require_any_hit = 0; |
---|
14 | |
---|
15 | my $args = scalar(@ARGV); |
---|
16 | if ($args>0) { |
---|
17 | while (scalar(@ARGV)) { |
---|
18 | my $arg = shift @ARGV; |
---|
19 | if ($arg eq '--help' or $arg eq '-h') { |
---|
20 | print "Usage: diff2grep.pl [switches]\n"; |
---|
21 | print "Converts a patch into grep-alike output\n"; |
---|
22 | print "switches:\n"; |
---|
23 | print " --old regexp only list old lines matching regexp\n"; |
---|
24 | print " --new regexp only list new lines matching regexp\n"; |
---|
25 | print " --both regexp only list if old AND new lines match regexp\n"; |
---|
26 | print " --any regexp list both if old OR new lines match regexp\n"; |
---|
27 | print "\n"; |
---|
28 | print "If --old and --new are specified, both have to match!\n"; |
---|
29 | print "(to avoid this behavior, specify '--any .' before them)"; |
---|
30 | print "\n"; |
---|
31 | print "If regexp starts with '~' => expect it to mismatch\n"; |
---|
32 | exit(0); |
---|
33 | } |
---|
34 | elsif ($arg eq '--old') { |
---|
35 | my $reg = shift @ARGV; |
---|
36 | die "--old expects argument 'regexp'" if not defined $reg; |
---|
37 | if ($reg =~ '^~') { $reg = $'; $inv_old = 1; } |
---|
38 | $reg_old = qr/$reg/; |
---|
39 | } |
---|
40 | elsif ($arg eq '--new') { |
---|
41 | my $reg = shift @ARGV; |
---|
42 | die "--new expects argument 'regexp'" if not defined $reg; |
---|
43 | if ($reg =~ '^~') { $reg = $'; $inv_new = 1; } |
---|
44 | $reg_new = qr/$reg/; |
---|
45 | } |
---|
46 | elsif ($arg eq '--both' or $arg eq '--any') { |
---|
47 | my $reg = shift @ARGV; |
---|
48 | die "$arg expects argument 'regexp'" if not defined $reg; |
---|
49 | if ($reg =~ '^~') { $reg = $'; $inv_old = 1; $inv_new = 1; } |
---|
50 | $reg_old = qr/$reg/; |
---|
51 | $reg_new = $reg_old; |
---|
52 | $require_both_hits = $arg eq '--both' ? 1 : 0; |
---|
53 | $require_any_hit = 1-$require_both_hits; |
---|
54 | } |
---|
55 | else { |
---|
56 | die "Unknown argument '$arg'"; |
---|
57 | } |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | my $reg_all = qr/./; |
---|
62 | my $reg_none = qr/jkasfgadgsfaf/; |
---|
63 | if (defined $reg_old) { |
---|
64 | if (defined $reg_new) { |
---|
65 | if ($require_any_hit==0) { |
---|
66 | $require_both_hits = 1; |
---|
67 | } |
---|
68 | } |
---|
69 | else { $reg_new = $reg_none; } |
---|
70 | } |
---|
71 | elsif (defined $reg_new) { $reg_old = $reg_none; } |
---|
72 | else { |
---|
73 | $reg_old = $reg_all; |
---|
74 | $reg_new = $reg_all; |
---|
75 | } |
---|
76 | |
---|
77 | my $oldfile = undef; |
---|
78 | my $newfile = undef; |
---|
79 | |
---|
80 | my $newpad = ''; |
---|
81 | my $oldpad = ''; |
---|
82 | |
---|
83 | my $correct = 0; # perform correction? |
---|
84 | my $corr = 0; # correct by number of lines (calc for next hunk) |
---|
85 | my $this_corr = 0; # correct by number of lines (value for current hunk) |
---|
86 | |
---|
87 | sub set_padding_and_correction() { |
---|
88 | $correct = 1; |
---|
89 | $corr = 0; |
---|
90 | |
---|
91 | if (defined $oldfile and defined $newfile) { |
---|
92 | my $oldlen = length($oldfile); |
---|
93 | my $newlen = length($newfile); |
---|
94 | |
---|
95 | $newpad = ''; |
---|
96 | $oldpad = ''; |
---|
97 | |
---|
98 | while ($oldlen<$newlen) { $oldpad .= ' '; $oldlen++; } |
---|
99 | while ($newlen<$oldlen) { $newpad .= ' '; $newlen++; } |
---|
100 | |
---|
101 | if ($oldfile ne $newfile) { $correct = 0; } |
---|
102 | |
---|
103 | print "diff2grep.pl: oldfile='$oldfile'\n"; |
---|
104 | print "diff2grep.pl: newfile='$newfile'\n"; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | sub print_tagged($$) { |
---|
109 | my ($tag,$line) = @_; |
---|
110 | if ($line =~ /^\s+/o) { |
---|
111 | my ($pad,$rest) = ($&,$'); |
---|
112 | my $tag_len = length($tag); |
---|
113 | my $pad_len = length($pad); |
---|
114 | |
---|
115 | if ($pad_len>($tag_len+1)) { |
---|
116 | my $extra_pad = substr($pad,$pad_len-$tag_len-1); |
---|
117 | $line = $extra_pad.$rest; |
---|
118 | } |
---|
119 | else { |
---|
120 | $line = $rest; |
---|
121 | } |
---|
122 | } |
---|
123 | print $tag.' '.$line."\n"; |
---|
124 | } |
---|
125 | |
---|
126 | sub correctABfile($) { |
---|
127 | # automatically remove a/ b/ prefixes from filenames |
---|
128 | my ($file) = @_; |
---|
129 | if ($file =~ /^[ab]\//o) { |
---|
130 | my $withoutAB = $'; |
---|
131 | if (not -e $file and -e $withoutAB) { |
---|
132 | return $withoutAB; |
---|
133 | } |
---|
134 | } |
---|
135 | return $file; |
---|
136 | } |
---|
137 | |
---|
138 | sub matches($\$$) { |
---|
139 | my ($rest,$regx_r,$inv) = @_; |
---|
140 | if ($rest =~ $$regx_r) { |
---|
141 | return $inv ? 0 : 1; |
---|
142 | } |
---|
143 | return $inv ? 1 : 0; |
---|
144 | } |
---|
145 | |
---|
146 | my $pwd = `pwd`; chomp($pwd); |
---|
147 | |
---|
148 | print "diff2grep.pl: Entering directory `$pwd'\n"; |
---|
149 | |
---|
150 | my ($o1,$o2,$n1,$n2); |
---|
151 | my $old_hitten = undef; |
---|
152 | |
---|
153 | while (defined ($_ = <STDIN>)) { |
---|
154 | chomp; |
---|
155 | if (/^---\s([^\t]+)(\t|$)/o) { |
---|
156 | $oldfile = correctABfile($1); |
---|
157 | set_padding_and_correction(); |
---|
158 | } |
---|
159 | elsif (/^\+\+\+\s([^\t]+)(\t|$)/o) { |
---|
160 | $newfile = correctABfile($1); |
---|
161 | set_padding_and_correction(); |
---|
162 | $this_corr = $corr; |
---|
163 | } |
---|
164 | elsif (/^-/o) { |
---|
165 | my $rest = $'; |
---|
166 | if (matches($rest, $reg_old, $inv_old)) { |
---|
167 | my $hitmsg = "$oldfile:".($o1+$this_corr*$correct).":$oldpad OLD: $rest\n"; |
---|
168 | if ($require_both_hits) { $old_hitten = $hitmsg; } |
---|
169 | else { print $hitmsg; } |
---|
170 | } |
---|
171 | else { |
---|
172 | print_tagged('[OLD]', $rest); |
---|
173 | } |
---|
174 | $corr--; |
---|
175 | $o1++; |
---|
176 | } |
---|
177 | elsif (/^\+/o) { |
---|
178 | my $rest = $'; |
---|
179 | if (matches($rest, $reg_new, $inv_new)) { |
---|
180 | my $showNew = 1; |
---|
181 | if ($require_both_hits==1) { |
---|
182 | if (defined $old_hitten) { |
---|
183 | print $old_hitten; |
---|
184 | } |
---|
185 | else { |
---|
186 | $showNew = 0; |
---|
187 | } |
---|
188 | } |
---|
189 | if ($showNew==1) { |
---|
190 | print "$newfile:".($n1).":$newpad NEW: $rest\n"; |
---|
191 | } |
---|
192 | } |
---|
193 | else { |
---|
194 | print_tagged('[NEW]', $rest); |
---|
195 | } |
---|
196 | $corr++; |
---|
197 | $n1++; |
---|
198 | } |
---|
199 | elsif (/^\@\@\s-(.*),(.*)\s\+(.*),(.*)\s\@\@([\t\s]|$)/o) { |
---|
200 | ($o1,$o2,$n1,$n2) = ($1,$2,$3,$4); |
---|
201 | print $_."\n"; |
---|
202 | $this_corr = $corr; |
---|
203 | $old_hitten = undef; |
---|
204 | } |
---|
205 | else { |
---|
206 | print $_."\n"; |
---|
207 | $o1++; |
---|
208 | $n1++; |
---|
209 | $old_hitten = undef; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | print "diff2grep.pl: Leaving directory `$pwd'\n"; |
---|
214 | |
---|