| 1293 |
dpurdie |
1 |
###############################################################################
|
|
|
2 |
# Codestriker: Copyright (c) 2001, 2002, 2003 David Sitsky.
|
|
|
3 |
# All rights reserved.
|
|
|
4 |
# sits@users.sourceforge.net
|
|
|
5 |
#
|
|
|
6 |
# This program is free software; you can redistribute it and modify it under
|
|
|
7 |
# the terms of the GPL.
|
|
|
8 |
|
|
|
9 |
# Parser object for reading CVS diffs. This parser object accepts output
|
|
|
10 |
# from either:
|
|
|
11 |
#
|
|
|
12 |
# cvs diff -uN or
|
|
|
13 |
# cvs rdiff -uN -r TAG1 -r TAG2 MODULENAME
|
|
|
14 |
#
|
|
|
15 |
# The second command actually produces a patch, but it doesn't contain
|
|
|
16 |
# important CVS filename and revision information.
|
|
|
17 |
|
|
|
18 |
package Codestriker::FileParser::CvsUnidiff;
|
|
|
19 |
|
|
|
20 |
use strict;
|
|
|
21 |
use Codestriker::FileParser::UnidiffUtils;
|
|
|
22 |
|
|
|
23 |
# Return the array of filenames, revision number, linenumber, whether its
|
|
|
24 |
# binary or not, and the diff text.
|
|
|
25 |
# Return () if the file can't be parsed, meaning it is in another format.
|
|
|
26 |
sub parse ($$$) {
|
|
|
27 |
my ($type, $fh, $repository) = @_;
|
|
|
28 |
|
|
|
29 |
# Retrieve the repository root, and escape back-slashes in the case of
|
|
|
30 |
# a Windows CVS repository, as it is used in regular expressions.
|
|
|
31 |
my $repository_root =
|
|
|
32 |
(defined $repository) ? $repository->getRoot() : undef;
|
|
|
33 |
if (defined $repository_root) {
|
|
|
34 |
$repository_root =~ s/\\/\\\\/g;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
# Array of results found.
|
|
|
38 |
my @result = ();
|
|
|
39 |
|
|
|
40 |
my $line = <$fh>;
|
|
|
41 |
while (defined($line)) {
|
|
|
42 |
# Values associated with the diff.
|
|
|
43 |
my $revision;
|
|
|
44 |
my $filename = "";
|
|
|
45 |
my $old_linenumber = -1;
|
|
|
46 |
my $new_linenumber = -1;
|
|
|
47 |
my $binary = 0;
|
|
|
48 |
my $repmatch = 0;
|
|
|
49 |
my $diff = "";
|
|
|
50 |
|
|
|
51 |
# CVS diffs may start with a number of unknown files, which start
|
|
|
52 |
# with a ? character. These lines can be skipped, as can blank lines.
|
|
|
53 |
# Note, some review text is formed by concatenating multiple
|
|
|
54 |
# cvs diffs together, so the check is done here. Also, this handles
|
|
|
55 |
# trailing whitespace.
|
|
|
56 |
while (defined($line) && ($line =~ /^\?/o || $line =~ /^\s*$/o)) {
|
|
|
57 |
$line = <$fh>;
|
|
|
58 |
}
|
|
|
59 |
return @result unless defined $line;
|
|
|
60 |
|
|
|
61 |
# For CVS diffs, the start of the diff block is the Index line.
|
|
|
62 |
return () unless defined $line && $line =~ /^Index:/o;
|
|
|
63 |
$line = <$fh>;
|
|
|
64 |
|
|
|
65 |
# The separator line appears next, for diffs, but not rdiffs.
|
|
|
66 |
return @result unless defined $line;
|
|
|
67 |
if ($line =~ /^===================================================================$/o) {
|
|
|
68 |
$line = <$fh>;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
# Now we expect the RCS line, whose filename should include the CVS
|
|
|
72 |
# repository, and if not, it is probably a new file, or it is a
|
|
|
73 |
# cvs rdiff. Make the match for the repository root case insensitive
|
|
|
74 |
# since different Windoze CVS clients (Cygwin + CvsNT) may return different
|
|
|
75 |
# repository roots with different casing.
|
|
|
76 |
return () unless defined $line;
|
|
|
77 |
if (defined $repository_root &&
|
|
|
78 |
$line =~ /^RCS file: $repository_root\/(.*),v$/i) {
|
|
|
79 |
$repmatch = 1;
|
|
|
80 |
$filename = $1;
|
|
|
81 |
$line = <$fh>;
|
|
|
82 |
return () unless defined $line;
|
|
|
83 |
} elsif ($line =~ /^RCS file: (.*)$/o) {
|
|
|
84 |
$filename = $1;
|
|
|
85 |
$line = <$fh>;
|
|
|
86 |
return () unless defined $line;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
# Now we expect the retrieving revision line, unless it is a new or
|
|
|
90 |
# removed file, or a cvs rdiff.
|
|
|
91 |
if ($line =~ /^retrieving revision (.*)$/o) {
|
|
|
92 |
$revision = $1;
|
|
|
93 |
$line = <$fh>;
|
|
|
94 |
return () unless defined $line;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
# If we are doing a diff between two revisions, a second revision
|
|
|
98 |
# line will appear. Don't care what the value of the second
|
|
|
99 |
# revision is.
|
|
|
100 |
if ($line =~ /^retrieving revision (.*)$/o) {
|
|
|
101 |
$line = <$fh>;
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
# For some rdiffs, if there is a binary file which has changed, no
|
|
|
105 |
# other information is posted, so process the next header.
|
|
|
106 |
next if ($line =~ /^Index:/o);
|
|
|
107 |
|
|
|
108 |
# Now read in the diff line, followed by the legend lines. If this is
|
|
|
109 |
# not present, then we know we aren't dealing with a diff file of any
|
|
|
110 |
# kind.
|
|
|
111 |
return () unless $line =~ /^diff/o;
|
|
|
112 |
$line = <$fh>;
|
|
|
113 |
return () unless defined $line;
|
|
|
114 |
|
|
|
115 |
# If the diff is empty (since we may have used the -b flag), continue
|
|
|
116 |
# processing the next diff header back around this loop. Note this is
|
|
|
117 |
# only an issue with cvs diffs. Ordinary diffs just don't include
|
|
|
118 |
# a diff section if it is blank.
|
|
|
119 |
while (defined $line && $line =~ /^\s*$/o) {
|
|
|
120 |
$line = <$fh>;
|
|
|
121 |
}
|
|
|
122 |
next if (! defined $line) || ($line =~ /^Index:/o);
|
|
|
123 |
|
|
|
124 |
# Check for binary files being added, changed or removed.
|
|
|
125 |
if ($line =~ /^Binary files \/dev\/null and (.*) differ$/o) {
|
|
|
126 |
# Binary file has been added.
|
|
|
127 |
$revision = $Codestriker::ADDED_REVISION;
|
|
|
128 |
$filename = $1;
|
|
|
129 |
$binary = 1;
|
|
|
130 |
} elsif ($line =~ /^Binary files (.*) and \/dev\/null differ$/o) {
|
|
|
131 |
# Binary file has been removed.
|
|
|
132 |
$revision = $Codestriker::REMOVED_REVISION;
|
|
|
133 |
$binary = 1;
|
|
|
134 |
} elsif ($line =~ /^Binary files .* and (.*) differ$/o) {
|
|
|
135 |
# Binary file has been modified.
|
|
|
136 |
$binary = 1;
|
|
|
137 |
$filename = $1;
|
|
|
138 |
} elsif ($line =~ /^\-\-\- \/dev\/null/o) {
|
|
|
139 |
# File has been added.
|
|
|
140 |
$revision = $Codestriker::ADDED_REVISION;
|
|
|
141 |
} elsif ($line =~ /^\-\-\- nul/o) {
|
|
|
142 |
# File has been added.
|
|
|
143 |
$revision = $Codestriker::ADDED_REVISION;
|
|
|
144 |
} elsif ($line =~ /^\-\-\- (.*):(\d+\.[\d\.]+)\t/) {
|
|
|
145 |
# This matchs a cvs rdiff file, extract the filename and revision.
|
|
|
146 |
# It is assumed to match the repository specified, although there
|
|
|
147 |
# is no real way of checking.
|
|
|
148 |
$filename = $1;
|
|
|
149 |
$revision = $2;
|
|
|
150 |
$repmatch = 1;
|
|
|
151 |
} elsif (! $line =~ /^\-\-\-/o) {
|
|
|
152 |
return ();
|
|
|
153 |
}
|
|
|
154 |
|
|
|
155 |
# If its a binary file, add the delta to the list.
|
|
|
156 |
if ($binary) {
|
|
|
157 |
my $chunk = {};
|
|
|
158 |
$chunk->{filename} = $filename;
|
|
|
159 |
$chunk->{revision} = $revision;
|
|
|
160 |
$chunk->{old_linenumber} = -1;
|
|
|
161 |
$chunk->{new_linenumber} = -1;
|
|
|
162 |
$chunk->{binary} = 1;
|
|
|
163 |
$chunk->{text} = "";
|
|
|
164 |
$chunk->{description} = "";
|
|
|
165 |
$chunk->{repmatch} = $repmatch;
|
|
|
166 |
push @result, $chunk;
|
|
|
167 |
} else {
|
|
|
168 |
# Now expect the +++ line.
|
|
|
169 |
$line = <$fh>;
|
|
|
170 |
return () unless (defined $line && $line =~ /^\+\+\+/o);
|
|
|
171 |
|
|
|
172 |
# Check if it is a removed file.
|
|
|
173 |
if ($line =~ /^\+\+\+ \/dev\/null/o) {
|
|
|
174 |
# File has been removed.
|
|
|
175 |
$revision = $Codestriker::REMOVED_REVISION;
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
# If it is an added file, and the filename hasn't been extracted
|
|
|
179 |
# (remote diffs), do so now.
|
|
|
180 |
if ($revision eq $Codestriker::ADDED_REVISION &&
|
|
|
181 |
$filename eq "" &&
|
|
|
182 |
$line =~ /^\+\+\+ (.*)\t/) {
|
|
|
183 |
$filename = $1;
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
# Now read in the multiple chunks.
|
|
|
187 |
my @file_diffs = Codestriker::FileParser::UnidiffUtils->
|
|
|
188 |
read_unidiff_text($fh, $filename, $revision, $repmatch);
|
|
|
189 |
|
|
|
190 |
push @result, @file_diffs;
|
|
|
191 |
}
|
|
|
192 |
|
|
|
193 |
# Read the next line.
|
|
|
194 |
$line = <$fh>;
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
# Return the found diff chunks.
|
|
|
198 |
return @result;
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
1;
|
|
|
202 |
|
|
|
203 |
|