blib/lib/App/KGB/Change.pm

filter: Statementstmt 82.3% 56 / 68 Branchbran 40.9% 9 / 22 Conditioncond 72.7% 8 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 88.8% 8 / 9 Podpod 100% 3 / 3 totaltotal 73.9% 88 / 119 CC 19 SCAR 22.6
CC
19
Cov
72%
as_string
20.0
detect_common_dir
11.0
new
8.5
CRAP 9.6
« blib/lib/WWW/Shorten/Debli.pm ↑ Summary blib/lib/JSON/RPC/Client/Any.pm »
1 # vim: ts=4:sw=4:et:ai:sts=4
2 #
3 # KGB - an IRC bot helping collaboration
4 # Copyright © 2008 Martina Ferrari
5 # Copyright © 2009 Damyan Ivanov
6 #
7 # This program is free software; you can redistribute it and/or modify it under
8 # the terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 # details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # this program; if not, write to the Free Software Foundation, Inc., 51
19 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package App::KGB::Change;
21  
22 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 21 our $VERSION = 1.22;
26  
27 21 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head1 NAME
30  
31 App::KGB::Change - a single file change
32  
33 =head1 SYNOPSIS
34  
35 my $c = App::KGB::Change->new(
36 { action => "M", prop_change => 1, path => "/there" } );
37  
38 print $c;
39  
40 my $c = App::KGB::Change->new("(M+)/there");
41  
42 =head1 DESCRIPTION
43  
44 B<App::KGB::Change> encapsulates a single path change from a given change set
45 (or commit).
46  
47 B<App::KGB::Change> overloads the "" operator in order to provide a default
48 string representation of changes.
49  
50 =head1 FIELDS
51  
52 =over
53  
54 =item B<action> (B<mandatory>)
55  
56 The action performed on the item. Possible values are:
57  
58 =over
59  
60 =item B<M>
61  
62 The path was modified.
63  
64 =item B<A>
65  
66 The path was added.
67  
68 =item B<D>
69  
70 The path was deleted.
71  
72 =item B<R>
73  
74 The path was replaced.
75  
76 =back
77  
78 =item path (B<mandatory>)
79  
80 The path that was changed.
81  
82 =item prop_change
83  
84 Boolean. Indicated that some properties of the path, not the content were
85 changed.
86  
87 =back
88  
89 =cut
90  
91 21 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
92 21 __PACKAGE__->mk_accessors(qw( action prop_change path ));
93  
94 21 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 =head1 CONSTRUCTOR
97  
98 =head2 new ( { I<initial values> } )
99  
100 More-or-less standard constructor.
101  
102 It can take a hashref with keys all the field names (See L<|FIELDS>).
103  
104 Or, it can take a single string, which is de-composed into components.
105  
106 See L<|SYNOPSIS> for examples.
107  
108 =cut
109  
110 7 ▶sub new {
Subroutine new: called CC=6 SCAR=21.4
Pod documented
111 7 my $class = shift;
112 7 my $self = $class->SUPER::new();
113  
114 7 my $h = shift;
115 7 ▶ if ( ref($h) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h) { } 0 7 7
116 0 ▶ defined( $self->action( delete $h->{action} ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->action(delete $h->{"action"}) or confess("'action' is required") 0 0 0
117 or confess "'action' is required";
118 0 ▶ defined( $self->path( delete $h->{path} ) )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->path(delete $h->{"path"}) or confess("'path' is required") 0 0 0
119 or confess "'path' is required";
120 0 $self->prop_change( delete $h->{prop_change} );
121 }
122 else {
123 7 ▶ my ( $a, $pc, $p ) = $h =~ /^(?:\(([MADR])?(\+)?\))?(.+)$/
Branch: 1 branch
BranchTrueFalseTotal
() = $h =~ /^(?:\(([MADR])?(\+)?\))?(.+)$/ or confess("'${h}' is not recognized as a change string") 7 0 7
124 or confess "'$h' is not recognized as a change string";
125 7 ▶ $self->action( $a //= 'M' );
Condition100%2/2
$a //= "M"
l!l
52
MC/DC100%1/1
$a //= "M"
$a
Truth table100%2/2
A
A= $a
Aresult
11
00
126 7 $self->prop_change( defined $pc );
127 7 $self->path($p);
128 }
129  
130 7 return $self;
131 }
132  
133 =head1 METHODS
134  
135 =over
136  
137 =item as_string()
138  
139 Return a string representation of the change. Used by the "" overload. The resulting string is suitable for feeding the L<|new> constructor if needed.
140  
141 =cut
142  
143 21 ▶use overload '""' => \&as_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 0 ▶sub as_string {
Subroutine as_string: not called CC=4 SCAR=30.0
Pod documented
146 0 my $c = shift;
147 0 my $a = $c->action;
148 0 ▶ my $pc = $c->prop_change ? '+' : '';
Branch: 1 branch
BranchTrueFalseTotal
$c->prop_change ? : 0 0 0
149 0 my $p = $c->path;
150  
151 0 my $text = '';
152  
153 # ignore flags for modifications (unless there is also a property change)
154 0 ▶ $text = "($a$pc)" if $a ne 'M' or $pc;
Branch: 1 branch
BranchTrueFalseTotal
if $a ne "M" or $pc 0 0 0
Condition0%0/3
$a ne "M" or $pc
l!l&&r!l&&!r
000
MC/DC0%0/2
$a ne "M" or $pc
$a ne "M" $pc
Truth table0%0/3
A or B
A= $a ne "M"B= $pc
ABresult
1X1
011
000
155 0 $p =~ s,^/,,; # strip leading slash from paths
156 0 $text .= $p;
157 0 return $text;
158 }
159  
160 =back
161  
162 =head1 CLASS METHODS
163  
164 =over
165  
166 =item detect_common_dir(C<changes>)
167  
168 Given an arrayref of changes (instances of APP::KGB::Change), detects the
169 longest path that is common to all of them. All the changes' paths are trimmed
170 from the common part.
171  
172 Example:
173  
174 foo/b
175 foo/x
176 foo/bar/a
177  
178 would return 'foo' and the paths would be trimmed to
179  
180 b
181 x
182 bar/a
183  
184 =cut
185  
186 3 ▶sub detect_common_dir {
Subroutine detect_common_dir: called CC=11 SCAR=24.0
Pod documented
187 3 my $self = shift;
188 3 my $changes = shift;
189  
190 3 ▶ return '' if @$changes < 2; # common dir concept only meaningful for
Branch: 1 branch
BranchTrueFalseTotal
if @$changes < 2 0 3 3
191 # more than one path
192  
193 3 my %dirs;
194 my %most_dirs;
195 3 for my $c (@$changes) {
196 7 my $path = $c->path;
197  
198 # we need to pretend the paths are absolute, because otherwise
199 # paths like "a" and "." will be treated as being of the same
200 # deepness, while "." is really the parent of "a"
201 # the leading "/" is stripped before further processing
202 7 ▶ $path = "/$path" unless $path =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ m[^/] 7 0 7
203 7 my $dir = dirname($path);
204 7 $dirs{$dir}++;
205 7 while (1) {
206 23 $most_dirs{$dir}++;
207 23 my $ndir = dirname($dir);
208 23 ▶ last if $ndir eq $dir; # reached the root?
Branch: 1 branch
BranchTrueFalseTotal
if $ndir eq $dir 7 16 23
209 16 $dir = $ndir;
210 }
211 }
212  
213 3 my $topdir = '';
214 3 my $max = 0;
215  
216 # we want to print the common root of all the changed files and say
217 # "foo/bar (42 files changed)"
218  
219 3 for my $dirpath ( keys %most_dirs ) {
220 12 ▶ if ( $max < $most_dirs{$dirpath}
Branch: 1 branch
BranchTrueFalseTotal
if ($max < $most_dirs{$dirpath} or $max == $most_dirs{$dirpath} and length $topdir < length $dirpath) 7 5 12
Condition100%6/6
$max < $most_dirs{$dirpath} or $max == $most_dirs{$dirpath} and length $topdir < length $dirpath
l!l&&r!l&&!r
435
$max == $most_dirs{$dirpath} and length $topdir < length $dirpath
!ll&&!rl&&r
233
MC/DC100%3/3
$max < $most_dirs{$dirpath} or $max == $most_dirs{$dirpath} and length $topdir < length $dirpath
$max < $most_dirs{$dirpath} $max == $most_dirs{$dirpath} length $topdir < length $dirpath
Truth table100%4/4
A or B and C
A= $max < $most_dirs{$dirpath}B= $max == $most_dirs{$dirpath}C= length $topdir < length $dirpath
ABCresult
1XX1
0111
00X0
0100
221 or $max == $most_dirs{$dirpath}
222 and length($topdir) < length($dirpath) )
223 {
224 7 $max = $most_dirs{$dirpath};
225 7 $topdir = $dirpath;
226 }
227 }
228  
229 # remove the artificial leading slash
230 3 $topdir =~ s{^/}{};
231  
232 3 for (@$changes) {
233 7 my $p = $_->path;
234 7 ▶ $p =~ s{^/$topdir/?}{}x
Branch: 1 branch
BranchTrueFalseTotal
$p =~ s[^/$topdir/?][]x or $p =~ s[^$topdir/?][] 0 7 7
235 or $p =~ s{^$topdir/?}{};
236 7 $_->path($p);
237 }
238  
239 3 return $topdir;
240 }
241  
242 =back
243  
244 =cut
245  
246 21 1;
« blib/lib/WWW/Shorten/Debli.pm ↑ Summary blib/lib/JSON/RPC/Client/Any.pm »