| 1 | # -*- Mode: cperl; cperl-indent-level: 4 -*- | ||
| 2 | |||
| 3 | package SVN::Web::Diff; | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use base 'SVN::Web::action'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | use Encode (); | ||
| 11 | use SVN::Core; | ||
| 12 | use SVN::Ra; | ||
| 13 | use SVN::Client; | ||
| 14 | use SVN::Web::DiffParser; | ||
| 15 | use SVN::Web::X; | ||
| 16 | use List::Util qw(max min); | ||
| 17 | |||
| 18 | our $VERSION = 0.62; | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | SVN::Web::Diff - SVN::Web action to show differences between file revisions | ||
| 23 | |||
| 24 | =head1 SYNOPSIS | ||
| 25 | |||
| 26 | In F<config.yaml> | ||
| 27 | |||
| 28 | actions: | ||
| 29 | ... | ||
| 30 | diff: | ||
| 31 | class: SVN::Web::Diff | ||
| 32 | ... | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | Returns the difference between two revisions of the same file. | ||
| 37 | |||
| 38 | =head1 CONFIGURATION | ||
| 39 | |||
| 40 | The following configuration options may be specified in F<config.yaml>. | ||
| 41 | |||
| 42 | =over | ||
| 43 | |||
| 44 | =item max_diff_size | ||
| 45 | |||
| 46 | If showing the diff (see C<show_diff>), this determines the maximum size | ||
| 47 | of the diff that will be shown. If the size of the generated diff (in | ||
| 48 | bytes) is larger than this figure then it is not shown. | ||
| 49 | |||
| 50 | Defaults to 200,000 bytes. | ||
| 51 | |||
| 52 | =back | ||
| 53 | |||
| 54 | =head1 OPTIONS | ||
| 55 | |||
| 56 | =over 8 | ||
| 57 | |||
| 58 | =item rev1 | ||
| 59 | |||
| 60 | The first revision of the file to compare. | ||
| 61 | |||
| 62 | =item rev2 | ||
| 63 | |||
| 64 | The second revision of the file to compare. | ||
| 65 | |||
| 66 | =item revs | ||
| 67 | |||
| 68 | A list of two or more revisions. If present, the smallest number in | ||
| 69 | the list is assigned to C<rev1> (overriding any given C<rev1> value) and the | ||
| 70 | largest number in the list is assigned to C<rev2> (overriding any given | ||
| 71 | C<rev2> value). | ||
| 72 | |||
| 73 | In other words: | ||
| 74 | |||
| 75 | ...?rev1=5;rev2=10 | ||
| 76 | |||
| 77 | is equal to: | ||
| 78 | |||
| 79 | ...?revs=10;revs=5 | ||
| 80 | |||
| 81 | This supports the "diff between arbitrary revisions" functionality. | ||
| 82 | |||
| 83 | =item mime | ||
| 84 | |||
| 85 | The desired output format. The default is C<html> for a diff marked | ||
| 86 | up in HTML. The other allowed value is C<text>, for a plain text | ||
| 87 | unified diff. | ||
| 88 | |||
| 89 | =back | ||
| 90 | |||
| 91 | =head1 TEMPLATE VARIABLES | ||
| 92 | |||
| 93 | =over 8 | ||
| 94 | |||
| 95 | =item at_head | ||
| 96 | |||
| 97 | Boolean, indicating whether or not we're currently diffing against the | ||
| 98 | youngest revision of this file. | ||
| 99 | |||
| 100 | =item context | ||
| 101 | |||
| 102 | Always C<file>. | ||
| 103 | |||
| 104 | =item rev1 | ||
| 105 | |||
| 106 | The first revision of the file to compare. Corresponds with the C<rev1> | ||
| 107 | parameter, either set explicitly, or extracted from C<revs>. | ||
| 108 | |||
| 109 | =item rev2 | ||
| 110 | |||
| 111 | The second revision of the file to compare. Corresponds with the C<rev2> | ||
| 112 | parameter, either set explicitly, or extracted from C<revs>. | ||
| 113 | |||
| 114 | =item diff | ||
| 115 | |||
| 116 | An L<SVN::Web::DiffParser> object that contains the text of the diff. | ||
| 117 | Call the object's methods to format the diff. | ||
| 118 | |||
| 119 | =item diff_size | ||
| 120 | |||
| 121 | The size of the generated diff (before parsing). | ||
| 122 | |||
| 123 | =item max_diff_size | ||
| 124 | |||
| 125 | The configured maximum diff size. | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =head1 EXCEPTIONS | ||
| 130 | |||
| 131 | =over 4 | ||
| 132 | |||
| 133 | =item (cannot diff nodes of different types: %1 %2 %3) | ||
| 134 | |||
| 135 | The given path has different node types at the different revisions. | ||
| 136 | This probably means a file was added, deleted, and then re-added as a | ||
| 137 | directory at a later date (or vice-versa). | ||
| 138 | |||
| 139 | =item (path %1 is a directory at rev %2) | ||
| 140 | |||
| 141 | The user has tried to diff two directories. This is not currently | ||
| 142 | supported. | ||
| 143 | |||
| 144 | =item (path %1 does not exist in revision %2) | ||
| 145 | |||
| 146 | The given path is not present in the repository at the given revision. | ||
| 147 | |||
| 148 | =item (two revisions must be provided) | ||
| 149 | |||
| 150 | No revisions were given to diff against. | ||
| 151 | |||
| 152 | =item (rev1 and rev2 must be different) | ||
| 153 | |||
| 154 | Either only one revision number was given, or several were given, but | ||
| 155 | they're the same number. | ||
| 156 | |||
| 157 | =back | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | my %default_opts = ( max_diff_size => 200_000, ); | ||
| 162 | |||
| 163 | sub cache_key { | ||
| 164 | my $self = shift; | ||
| 165 | |||
| 166 | my ( $rev1, $rev2 ) = $self->_check_params(); | ||
| 167 | my $path = $self->{path}; | ||
| 168 | my $mime = $self->{cgi}->param('mime') || 'text/html'; | ||
| 169 | |||
| 170 | return "$rev1:$rev2:$mime:$path"; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub run { | ||
| 174 | my $self = shift; | ||
| 175 | |||
| 176 | $self->{opts} = { %default_opts, %{ $self->{opts} } }; | ||
| 177 | |||
| 178 | my ( $rev1, $rev2 ) = $self->_check_params(); | ||
| 179 | |||
| 180 | my $ctx = $self->{repos}{client}; | ||
| 181 | my $ra = $self->{repos}{ra}; | ||
| 182 | my $uri = $self->{repos}{uri}; | ||
| 183 | $uri .= '/'.$self->rpath if $self->rpath; | ||
| 184 | |||
| 185 | my ( undef, undef, undef, $at_head ) = $self->get_revs(); | ||
| 186 | |||
| 187 | my $mime = $self->{cgi}->param('mime') || 'text/html'; | ||
| 188 | |||
| 189 | my %types = ( | ||
| 190 | $rev1 => $ra->check_path( $self->rpath, $rev1 ), | ||
| 191 | $rev2 => $ra->check_path( $self->rpath, $rev2 ) | ||
| 192 | ); | ||
| 193 | |||
| 194 | SVN::Web::X->throw( | ||
| 195 | error => '(cannot diff nodes of different types: %1 %2 %3)', | ||
| 196 | vars => [ $self->rpath, $rev1, $rev2 ] | ||
| 197 | ) if $types{$rev1} != $types{$rev2}; | ||
| 198 | |||
| 199 | foreach my $rev ( $rev1, $rev2 ) { | ||
| 200 | SVN::Web::X->throw( | ||
| 201 | error => '(path %1 does not exist in revision %2)', | ||
| 202 | vars => [ $self->rpath, $rev ] | ||
| 203 | ) if $types{$rev} == $SVN::Node::none; | ||
| 204 | |||
| 205 | SVN::Web::X->throw( | ||
| 206 | error => '(path %1 is a directory at rev %2)', | ||
| 207 | vars => [ $self->rpath, $rev ] | ||
| 208 | ) if $types{$rev} == $SVN::Node::dir; | ||
| 209 | } | ||
| 210 | |||
| 211 | my $style; | ||
| 212 | $mime eq 'text/html' and $style = 'Text::Diff::HTML'; | ||
| 213 | $mime eq 'text/plain' and $style = 'Unified'; | ||
| 214 | |||
| 215 | if ( $mime eq 'text/html' ) { | ||
| 216 | my $out = Encode::decode('utf8',$self->svn_get_diff($uri, $rev1, $uri, $rev2, 0)); | ||
| 217 | my $diff; | ||
| 218 | my $diff_size = length($out); | ||
| 219 | my $max_diff_size = $self->{opts}{max_diff_size} || 0; | ||
| 220 | if ( $diff_size <= $max_diff_size ) { | ||
| 221 | $diff = SVN::Web::DiffParser->new($out); | ||
| 222 | } | ||
| 223 | |||
| 224 | return { | ||
| 225 | template => 'diff', | ||
| 226 | data => { | ||
| 227 | context => 'file', | ||
| 228 | rev1 => $rev1, | ||
| 229 | rev2 => $rev2, | ||
| 230 | diff => $diff, | ||
| 231 | diff_size => $diff_size, | ||
| 232 | max_diff_size => $max_diff_size, | ||
| 233 | at_head => $at_head, | ||
| 234 | } | ||
| 235 | }; | ||
| 236 | } | ||
| 237 | else { | ||
| 238 | return { | ||
| 239 | mimetype => $mime, | ||
| 240 | body => $self->svn_get_diff($uri, $rev1, $uri, $rev2, 0), | ||
| 241 | }; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | |||
| 245 | sub _check_params { | ||
| 246 | my $self = shift; | ||
| 247 | |||
| 248 | my $rev1 = $self->{cgi}->param('rev1'); | ||
| 249 | my $rev2 = $self->{cgi}->param('rev2'); | ||
| 250 | my @revs = $self->{cgi}->param('revs'); | ||
| 251 | |||
| 252 | if (@revs) { | ||
| 253 | $rev1 = min(@revs); | ||
| 254 | $rev2 = max(@revs); | ||
| 255 | } | ||
| 256 | |||
| 257 | SVN::Web::X->throw( | ||
| 258 | error => '(two revisions must be provided)', | ||
| 259 | vars => [] | ||
| 260 | ) | ||
| 261 | unless defined $rev1 | ||
| 262 | and defined $rev2; | ||
| 263 | |||
| 264 | SVN::Web::X->throw( | ||
| 265 | error => '(rev1 and rev2 must be different)', | ||
| 266 | vars => [] | ||
| 267 | ) if @revs and @revs < 2; | ||
| 268 | |||
| 269 | SVN::Web::X->throw( | ||
| 270 | error => '(rev1 and rev2 must be different)', | ||
| 271 | vars => [] | ||
| 272 | ) if $rev1 == $rev2; | ||
| 273 | |||
| 274 | return ( $rev1, $rev2 ); | ||
| 275 | } | ||
| 276 | |||
| 277 | # Make sure that a path exists in a revision | ||
| 278 | sub _check_path { | ||
| 279 | my $self = shift; | ||
| 280 | my $path = shift; | ||
| 281 | my $rev = shift; | ||
| 282 | |||
| 283 | my $ra = $self->{repos}{ra}; | ||
| 284 | |||
| 285 | if ( $ra->check_path( $self->rpath($path), $rev ) == $SVN::Node::none ) { | ||
| 286 | SVN::Web::X->throw( | ||
| 287 | error => '(path %1 does not exist in revision %2)', | ||
| 288 | vars => [ $path, $rev ], | ||
| 289 | ); | ||
| 290 | } | ||
| 291 | } | ||
| 292 | |||
| 293 | 1; | ||
| 294 | |||
| 295 | =head1 COPYRIGHT | ||
| 296 | |||
| 297 | Copyright 2003-2004 by Chia-liang Kao C<< <clkao@clkao.org> >>. | ||
| 298 | |||
| 299 | Copyright 2005-2007 by Nik Clayton C<< <nik@FreeBSD.org> >>. | ||
| 300 | |||
| 301 | This program is free software; you can redistribute it and/or modify it | ||
| 302 | under the same terms as Perl itself. | ||
| 303 | |||
| 304 | See L<http://www.perl.com/perl/misc/Artistic.html> | ||
| 305 | |||
| 306 | =cut | ||