| 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 |
# Utility object for reading unidiffs.
|
|
|
10 |
|
|
|
11 |
package Codestriker::FileParser::UnidiffUtils;
|
|
|
12 |
|
|
|
13 |
use strict;
|
|
|
14 |
|
|
|
15 |
# Read from the file the diff chunks, and return them.
|
|
|
16 |
sub read_unidiff_text($$$$) {
|
|
|
17 |
my ($type, $fh, $filename, $revision, $repmatch) = @_;
|
|
|
18 |
|
|
|
19 |
# List of diff chunks read.
|
|
|
20 |
my @result = ();
|
|
|
21 |
|
|
|
22 |
my $lastpos = tell $fh;
|
|
|
23 |
my $line = <$fh>;
|
|
|
24 |
while (defined $line &&
|
|
|
25 |
$line =~ /^\@\@ \-(\d+)(\,)?(\d+)? \+(\d+)(\,)?(\d+)? \@\@(.*)$/) {
|
|
|
26 |
my $old_linenumber = $1;
|
|
|
27 |
my $number_old_lines = 1;
|
|
|
28 |
$number_old_lines = $3 if defined $3;
|
|
|
29 |
my $new_linenumber = $4;
|
|
|
30 |
my $number_new_lines = 1;
|
|
|
31 |
$number_new_lines = $6 if defined $6;
|
|
|
32 |
my $function_name = $7;
|
|
|
33 |
my $num_matched_old_lines = 0;
|
|
|
34 |
my $num_matched_new_lines = 0;
|
|
|
35 |
|
|
|
36 |
if (length($function_name) > 1) {
|
|
|
37 |
$function_name =~ s/^ //;
|
|
|
38 |
}
|
|
|
39 |
else {
|
|
|
40 |
$function_name = "";
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
# Now read in the diff text until finished.
|
|
|
44 |
my $diff = "";
|
|
|
45 |
$line = <$fh>;
|
|
|
46 |
while (defined $line) {
|
|
|
47 |
# Skip lines line "\ No newline at end of file".
|
|
|
48 |
if ($line !~ /^[\\]/o) {
|
|
|
49 |
|
|
|
50 |
# Check if the diff block with the trailing context has been
|
|
|
51 |
# read. Note Perforce diffs can contain empty lines.
|
|
|
52 |
if ($num_matched_old_lines >= $number_old_lines &&
|
|
|
53 |
$num_matched_new_lines >= $number_new_lines) {
|
|
|
54 |
last unless $line =~ /^\s*$/o;
|
|
|
55 |
}
|
|
|
56 |
else {
|
|
|
57 |
if ($line =~ /^\-/o) {
|
|
|
58 |
$num_matched_old_lines++;
|
|
|
59 |
} elsif ($line =~ /^\+/o) {
|
|
|
60 |
$num_matched_new_lines++;
|
|
|
61 |
} elsif ($line =~ /^ /o || $line =~ /^$/o) {
|
|
|
62 |
$num_matched_old_lines++;
|
|
|
63 |
$num_matched_new_lines++;
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
# Add the line to the diff chunk.
|
|
|
68 |
$diff .= $line;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
$lastpos = tell $fh;
|
|
|
72 |
$line = <$fh>;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
my $chunk = {};
|
|
|
76 |
$chunk->{filename} = $filename;
|
|
|
77 |
$chunk->{revision} = $revision;
|
|
|
78 |
$chunk->{old_linenumber} = $old_linenumber;
|
|
|
79 |
$chunk->{new_linenumber} = $new_linenumber;
|
|
|
80 |
$chunk->{binary} = 0;
|
|
|
81 |
$chunk->{text} = $diff;
|
|
|
82 |
$chunk->{description} = $function_name;
|
|
|
83 |
$chunk->{repmatch} = $repmatch;
|
|
|
84 |
push @result, $chunk;
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
# Restore the file point back to the start of the last unmatched line.
|
|
|
88 |
seek $fh, $lastpos, 0;
|
|
|
89 |
|
|
|
90 |
# Return the diff chunks found.
|
|
|
91 |
return @result;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
1;
|