source: tags/ms_ra2q1/SOURCE_TOOLS/diff2grep.pl

Last change on this file was 16763, checked in by westram, 6 years ago
  • Property svn:executable set to *
File size: 4.8 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6my $reg_old = undef;
7my $reg_new = undef;
8
9my $inv_old = 0;
10my $inv_new = 0;
11
12my $require_both_hits = 0;
13my $require_any_hit   = 0;
14
15my $args = scalar(@ARGV);
16if ($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
61my $reg_all  = qr/./;
62my $reg_none = qr/jkasfgadgsfaf/;
63if (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}
71elsif (defined $reg_new) { $reg_old = $reg_none; }
72else {
73  $reg_old = $reg_all;
74  $reg_new = $reg_all;
75}
76
77my $oldfile = undef;
78my $newfile = undef;
79
80my $newpad = '';
81my $oldpad = '';
82
83my $correct   = 0; # perform correction?
84my $corr      = 0; # correct by number of lines (calc for next hunk)
85my $this_corr = 0; # correct by number of lines (value for current hunk)
86
87sub 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
108sub 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
126sub matches($\$$) {
127  my ($rest,$regx_r,$inv) = @_;
128  if ($rest =~ $$regx_r) {
129    return $inv ? 0 : 1;
130  }
131  return $inv ? 1 : 0;
132}
133
134my $pwd = `pwd`; chomp($pwd);
135
136print "diff2grep.pl: Entering directory `$pwd'\n";
137
138my ($o1,$o2,$n1,$n2);
139my $old_hitten = undef;
140
141while (defined ($_ = <STDIN>)) {
142  chomp;
143  if (/^--- ([^\t]+)\t/) {
144    $oldfile = $1;
145    set_padding_and_correction();
146  }
147  elsif (/^\+\+\+ ([^\t]+)\t/) {
148    $newfile = $1;
149    set_padding_and_correction();
150    $this_corr = $corr;
151  }
152  elsif (/^-/) {
153    my $rest = $';
154    if (matches($rest, $reg_old, $inv_old)) {
155      my $hitmsg = "$oldfile:".($o1+$this_corr*$correct).":$oldpad OLD: $rest\n";
156      if ($require_both_hits) { $old_hitten = $hitmsg; }
157      else { print $hitmsg; }
158    }
159    else {
160      print_tagged('[OLD]', $rest);
161    }
162    $corr--;
163    $o1++;
164  }
165  elsif (/^\+/) {
166    my $rest = $';
167    if (matches($rest, $reg_new, $inv_new)) {
168      my $showNew = 1;
169      if ($require_both_hits==1) {
170        if (defined $old_hitten) {
171          print $old_hitten;
172        }
173        else {
174          $showNew = 0;
175        }
176      }
177      if ($showNew==1) {
178        print "$newfile:".($n1).":$newpad NEW: $rest\n";
179      }
180    }
181    else {
182      print_tagged('[NEW]', $rest);
183    }
184    $corr++;
185    $n1++;
186  }
187  elsif (/^\@\@ -(.*),(.*) \+(.*),(.*) \@\@$/) {
188    ($o1,$o2,$n1,$n2) = ($1,$2,$3,$4);
189    print $_."\n";
190    $this_corr = $corr;
191    $old_hitten = undef;
192  }
193  else {
194    print $_."\n";
195    $o1++;
196    $n1++;
197    $old_hitten = undef;
198  }
199}
200
201print "diff2grep.pl: Leaving directory `$pwd'\n";
202
Note: See TracBrowser for help on using the repository browser.