Subversion Repositories DevTools

Rev

Rev 1295 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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 patch diffs in basic form.
10
 
11
package Codestriker::FileParser::PatchBasicDiff;
12
 
13
use strict;
14
use Codestriker::FileParser::BasicDiffUtils;
15
 
16
# Return the array of filenames, revision number, linenumber and the diff text.
17
# Return () if the file can't be parsed, meaning it is in another format.
18
sub parse ($$) {
19
    my ($type, $fh, $uploaded_filename) = @_;
20
 
21
    # Array of results found.
22
    my @result = ();
23
 
24
    # The current revision and filename being processed.
25
    my $revision = $Codestriker::PATCH_REVISION;
26
    my $filename = $uploaded_filename;
27
 
28
    # Ignore any whitespace at the start of the file.
29
    my $line = <$fh>;
30
    while (defined($line)) {
31
 
32
	# Values associated with the diff.
33
	my $old_linenumber = -1;
34
	my $new_linenumber = -1;
35
	my $binary = 0;
36
 
37
	# Skip any heading or trailing whitespace contained in the review
38
	# text.
39
	while (defined($line) && $line =~ /^\s*$/) {
40
	    $line = <$fh>;
41
	}
42
	return @result unless defined $line;
43
 
44
	# For basic diffs, the diff line may appear first, but is optional,
45
	# depending on how the diff was generated.  In any case, the line
46
	# is ignored.
47
	if (defined $line && $line =~ /^(diff.*)$/o) {
48
	    # This isn't right, but it provides some information at least on
49
	    # the derivative file.
50
	    $filename = $1;
51
 
52
	    # Read the next line.
53
	    $line = <$fh>;
54
	}
55
	return () unless defined $line;
56
 
57
        # Need to check for binary file differences.
58
        # Unfortunately, when you provide the "-N" argument to diff,
59
        # it doesn't indicate new files or removed files properly.  Without
60
        # the -N argument, it then indicates "Only in ...".
61
        if ($line =~ /^Binary files .* and (.*) differ$/ ||
62
	    $line =~ /^Files .* and (.*) differ$/) {
63
            $filename = $1;
64
            $binary = 1;
65
        } elsif ($line =~ /^Only in (.*): (.*)$/) {
66
            $filename = "$1/$2";
67
            $binary = 1;
68
	}
69
 
70
	if ($binary == 0) {
71
	    my $chunk =
72
		Codestriker::FileParser::BasicDiffUtils->read_diff_text(
73
		       $fh, $line, $filename, $revision, 0);
74
	    return () unless defined $chunk;
75
	    push @result, $chunk;
76
	} else {
77
	    # Binary file changed.
78
	    my $chunk = {};
79
	    $chunk->{filename} = $filename;
80
	    $chunk->{revision} = $revision;
81
	    $chunk->{old_linenumber} = -1;
82
	    $chunk->{new_linenumber} = -1;
83
	    $chunk->{binary} = 1;
84
	    $chunk->{text} = "";
85
	    $chunk->{description} = "";
86
	    $chunk->{repmatch} = 0;
87
	    push @result, $chunk;
88
	}
89
 
90
	# Read the next line.
91
	$line = <$fh>;
92
    }
93
 
94
    # Return the found diff chunks.
95
    return @result;
96
}
97
 
98
1;
99
 
100