blib/lib/SVN/Notify/HTML/ColorDiff.pm

filter: Statementstmt 98.8% 86 / 87 Branchbran 92.0% 46 / 50 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 96.7% 148 / 153 SCAR 33.7
CC
29
Cov
97%
output_diff
29.1
BEGIN
1.0
BEGIN
1.0
CRAP 29.0
« blib/lib/SVN/Notify/HTML.pm ↑ Summary Build »
1 package SVN::Notify::HTML::ColorDiff;
2  
3 134 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 134 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 134 ▶use SVN::Notify::HTML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 134 $SVN::Notify::HTML::ColorDiff::VERSION = '2.86';
8 134 @SVN::Notify::HTML::ColorDiff::ISA = qw(SVN::Notify::HTML);
9  
10 =head1 Name
11  
12 SVN::Notify::HTML::ColorDiff - Subversion activity HTML notification with colorized diff
13  
14 =head1 Synopsis
15  
16 Use F<svnnotify> in F<post-commit>:
17  
18 svnnotify --repos-path "$1" --revision "$2" \
19 --to developers@example.com --handler HTML::ColorDiff [options]
20  
21 Use the class in a custom script:
22  
23 use SVN::Notify::HTML::ColorDiff;
24  
25 my $notifier = SVN::Notify::HTML::ColorDiff->new(%params);
26 $notifier->prepare;
27 $notifier->execute;
28  
29 =head1 Description
30  
31 This subclass of L<SVN::Notify::HTML|SVN::Notify::HTML> sends HTML formatted
32 email messages for Subversion activity, and if the C<with_diff> parameter is
33 specified (but not C<attach_diff>), then a pretty colorized version of the
34 diff will be included, rather than the plain text diff output by
35 SVN::Notify::HTML.
36  
37 =head1 Usage
38  
39 To use SVN::Notify::HTML::ColorDiff, simply follow the
40 L<instructions|SVN::Notify/Usage> in SVN::Notify, but when using F<svnnotify>,
41 specify C<--handler HTML::ColorDiff>.
42  
43 =cut
44  
45 ##############################################################################
46  
47 =head1 Instance Interface
48  
49 =head2 Instance Methods
50  
51 =head3 output_css
52  
53 $notifier->output_css($file_handle);
54  
55 This method starts outputs the CSS for the HTML message.
56 SVN::Notify::HTML::ColorDiff adds extra CSS to its output so that it can
57 nicely style the diff.
58  
59 =cut
60  
61 # We use _css() so that ColorDiff can override it and the filters then applied
62 # only one to all of the CSS.
63  
64 ##############################################################################
65  
66 =head3 output_diff
67  
68 $notifier->output_diff($out_file_handle, $diff_file_handle);
69  
70 Reads the diff data from C<$diff_file_handle> and prints it to
71 C<$out_file_handle> for inclusion in the notification message. The diff is
72 output with nice colorized HTML markup. Each line of the diff file is escaped
73 by C<HTML::Entities::encode_entities()>.
74  
75 If there are any C<diff> filters, this method will do no HTML formatting, but
76 redispatch to L<SVN::Notify::output_diff|SVN::Notify/"output_diff">. See
77 L<Writing Output Filters|SVN::Notify/"Writing Output Filters"> for details on
78 filters.
79  
80 =cut
81  
82 134 my %types = (
83 Modified => 'modfile',
84 Added => 'addfile',
85 Deleted => 'delfile',
86 Copied => 'copfile',
87 );
88  
89 sub output_diff {
90 145 ▶ my ($self, $out, $diff) = @_;
Subroutine output_diff: called CC=29 SCAR=33.7
Pod documented
91 145 ▶ if ( $self->filters_for('diff') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filters_for("diff")) 14 131 145
92 14 return $self->SUPER::output_diff($out, $diff);
93 }
94 131 ▶ $self->_dbpnt( "Outputting colorized HTML diff") if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 131 131
95  
96 131 my $in_div;
97 131 my $in_span = '';
98 131 print $out qq{</div>\n<div id="patch">\n<h3>Diff</h3>\n};
99 131 my ($length, %seen) = 0;
100 131 my $max = $self->max_diff_length;
101  
102 131 ▶ while (my $line = <$diff>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $diff)) 3271 123 3394
103 3271 $line =~ s/[\n\r]+$//;
104 3271 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 217 3054 3271
105 3054 ▶ if ( $max && ( $length += length $line ) >= $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max and ($length += length $line) >= $max) { } 8 3046 3054
Condition100%3/3
$max and ($length += length $line) >= $max
!ll&&!rl&&r
29261208
MC/DC100%2/2
$max and ($length += length $line) >= $max
$max ($length += length $line) >= $max
Truth table100%3/3
A and B
A= $maxB= ($length += length $line) >= $max
ABresult
0X0
100
111
106 8 ▶ print $out "</$in_span>" if $in_span;
Branch: 1 branch
BranchTrueFalseTotal
if $in_span 8 0 8
107 8 print $out qq{<span class="lines">\@\@ Diff output truncated at $max characters. \@\@\n</span>};
108 8 $in_span = '';
109 8 last;
110 } else {
111 3046 ▶ if ($line =~ /^(Modified|Added|Deleted|Copied): (.*)/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ /^(Modified|Added|Deleted|Copied): (.*)/) { } 250 2796 3046
elsif ($line =~ /^Property changes on: (.*)/ and not $seen{$1}) { } 31 2765 2796
elsif ($line =~ /^\@\@/) { } 300 2465 2765
elsif ($line =~ /^([-+])/) { } 576 1889 2465
112 250 my $class = $types{my $action = $1};
113 250 ++$seen{$2};
114 250 my $file = encode_entities($2, '<>&"');
115 250 (my $id = $file) =~ s/[^\w_]//g;
116  
117 250 ▶ print $out "</$in_span>" if $in_span;
Branch: 1 branch
BranchTrueFalseTotal
if $in_span 119 131 250
118 250 ▶ print $out "</span></pre></div>\n" if $in_div;
Branch: 1 branch
BranchTrueFalseTotal
if $in_div 119 131 250
119  
120 # Dump line, but check it's content.
121 250 ▶ if (<$diff> !~ /^=/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (readline($diff) =~ /^=/) 35 215 250
122 # Looks like they used --no-diff-added or --no-diff-deleted.
123 35 ($in_span, $in_div) = '';
124 35 print $out qq{<a id="$id"></a>\n<div class="$class">},
125 qq{<h4>$action: $file</h4></div>\n};
126 35 next;
127 }
128  
129 # Get the revision numbers.
130 215 my $before = <$diff>;
131 215 $before =~ s/[\n\r]+$//;
132  
133 215 ▶ if ($before =~ /^\(Binary files differ\)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before =~ /^\(Binary files differ\)/) 1 214 215
134 # Just output the whole file div.
135 1 print $out qq{<a id="$id"></a>\n<div class="binary"><h4>},
136 qq{$action: $file</h4>\n<pre class="diff"><span>\n},
137 qq{<span class="cx">$before\n</span></span></pre></div>\n};
138 1 ($in_span, $in_div) = '';
139 1 next;
140 }
141  
142 214 my ($rev1) = $before =~ /\(rev (\d+)\)$/;
143 214 my $after = <$diff>;
144 214 $after =~ s/[\n\r]+$//;
145 214 my ($rev2) = $after =~ /\(rev (\d+)\)$/;
146  
147 # Output the headers.
148 214 print $out qq{<a id="$id"></a>\n<div class="$class"><h4>$action: $file},
149 " ($rev1 => $rev2)</h4>\n";
150 214 print $out qq{<pre class="diff"><span>\n<span class="info">};
151 214 $in_div = 1;
152 214 print $out encode_entities($_, '<>&"'), "\n" for ($before, $after);
153 214 print $out "</span>";
154 214 $in_span = '';
155 ▶ } elsif ($line =~ /^Property changes on: (.*)/ && !$seen{$1}) {
Condition100%3/3
$line =~ /^Property changes on: (.*)/ and not $seen{$1}
!ll&&!rl&&r
27481731
MC/DC100%2/2
$line =~ /^Property changes on: (.*)/ and not $seen{$1}
$line =~ /^Property changes on: (.*)/ not $seen{$1}
Truth table100%3/3
A and not(B)
A= $line =~ /^Property changes on: (.*)/B= not $seen{$1}
ABresult
0X0
100
111
156 # It's just property changes.
157 31 my $file = encode_entities($1, '<>&"');
158 31 (my $id = $file) =~ s/[^\w_]//g;
159 # Dump line.
160 31 <$diff>;
161  
162 # Output the headers.
163 31 ▶ print $out "</$in_span>" if $in_span;
Branch: 1 branch
BranchTrueFalseTotal
if $in_span 30 1 31
164 31 ▶ print $out "</span></pre></div>\n" if $in_div;
Branch: 1 branch
BranchTrueFalseTotal
if $in_div 30 1 31
165 31 print $out qq{<a id="$id"></a>\n<div class="propset">},
166 qq{<h4>Property changes: $file</h4>\n<pre class="diff"><span>\n};
167 31 $in_div = 1;
168 31 $in_span = '';
169 } elsif ($line =~ /^\@\@/) {
170 300 ▶ print $out "</$in_span>" if $in_span;
Branch: 1 branch
BranchTrueFalseTotal
if $in_span 86 214 300
171 300 print $out (
172 qq{<span class="lines">},
173 encode_entities($line, '<>&"'),
174 "\n</span>",
175 );
176 300 $in_span = '';
177 } elsif ($line =~ /^([-+])/) {
178 576 ▶ my $type = $1 eq '+' ? 'ins' : 'del';
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '+' ? : 447 129 576
179 576 ▶ if ($in_span eq $type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_span eq $type) { } 170 406 576
180 170 print $out encode_entities($line, '<>&"'), "\n";
181 } else {
182 406 ▶ print $out "</$in_span>" if $in_span;
Branch: 1 branch
BranchTrueFalseTotal
if $in_span 403 3 406
183 406 print $out (
184 qq{<$type>},
185 encode_entities($line, '<>&"'),
186 "\n",
187 );
188 406 $in_span = $type;
189 }
190 } else {
191 1889 ▶ if ($in_span eq 'cx') {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_span eq 'cx') { } 0 1889 1889
192 0 print $out encode_entities($line, '<>&"'), "\n";
193 } else {
194 1889 ▶ print $out "</$in_span>" if $in_span;
Branch: 1 branch
BranchTrueFalseTotal
if $in_span 1561 328 1889
195 1889 print $out (
196 qq{<span class="cx">},
197 encode_entities($line, '<>&"'),
198 "\n",
199 );
200 1889 $in_span = 'span';
201 }
202 }
203 }
204 }
205 131 ▶ print $out "</$in_span>" if $in_span;
Branch: 1 branch
BranchTrueFalseTotal
if $in_span 88 43 131
206 131 ▶ print $out "</span></pre>\n</div>\n" if $in_div;
Branch: 1 branch
BranchTrueFalseTotal
if $in_div 96 35 131
207 131 print $out "</div>\n";
208  
209 131 ▶ close $diff or warn "Child process exited: $?\n";
Branch: 1 branch
BranchTrueFalseTotal
close $diff or warn "Child process exited: $?\n" 0 131 131
210 131 return $self;
211 }
212  
213 ##############################################################################
214  
215 sub _css {
216 394 ▶ my $css = shift->SUPER::_css;
Subroutine _css: called CC=1 SCAR=0.0
217 394 push @$css,
218 qq(#patch h4 {font-family: verdana,arial,helvetica,sans-serif;),
219 qq(font-size:10pt;padding:8px;background:#369;color:#fff;),
220 qq(margin:0;}\n),
221 qq(#patch .propset h4, #patch .binary h4 {margin:0;}\n),
222 qq(#patch pre {padding:0;line-height:1.2em;margin:0;}\n),
223 qq(#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;),
224 qq(overflow:auto;}\n),
225 qq(#patch .propset .diff, #patch .binary .diff {padding:10px 0;}\n),
226 qq(#patch span {display:block;padding:0 10px;}\n),
227 qq(#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, ),
228 qq(#patch .binary, #patch .copfile {border:1px solid #ccc;),
229 qq(margin:10px 0;}\n),
230 qq(#patch ins {background:#dfd;text-decoration:none;display:block;),
231 qq(padding:0 10px;}\n),
232 qq(#patch del {background:#fdd;text-decoration:none;display:block;),
233 qq(padding:0 10px;}\n),
234 qq(#patch .lines, .info {color:#888;background:#fff;}\n);
235 394 return $css;
236 }
237  
238 134 1;
239 __END__
« blib/lib/SVN/Notify/HTML.pm ↑ Summary Build »