blib/lib/VCS/LibCVS/Datum/RevisionNumber.pm

filter: Statementstmt 28.8% 32 / 111 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 38.4% 10 / 26 Podpod 100% 12 / 12 totaltotal 25.1% 54 / 215 CC 30 SCAR 47.6
CC
30
Cov
19%
compare
342.0
get_predecessor
20.0
is_import_branch
12.0
CRAP 116.2
« blib/lib/VCS/LibCVS/Command.pm ↑ Summary blib/lib/VCS/LibCVS/RepositoryFile.pm »
1 #
2 # Copyright (c) 2003,2004,2005 Alexander Taler (dissent@0--0.org)
3 #
4 # All rights reserved. This program is free software; you can redistribute it
5 # and/or modify it under the same terms as Perl itself.
6 #
7  
8 package VCS::LibCVS::Datum::RevisionNumber;
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 VCS::LibCVS::Datum::RevisionNumber - A CVS revision number.
16  
17 =head1 SYNOPSIS
18  
19 =head1 DESCRIPTION
20  
21 Represents a CVS revision number, either a branch or regular one.
22  
23 It accepts both regular branch numbers (1.4.2) and magic ones (1.4.0.2). This
24 means that numbers of the form "0.x" are ambiguous. They are treated as
25 revision numbers on the branch "0".
26  
27 The revision number "0" is used for added files, as well as for one of the
28 trunk branches.
29  
30 I use the term "depth" to refer to the number of fields in a revision. I use
31 the terms "ancestor" and "descendant" to refer to a relationship between two
32 revisions, whereby the intervening revisions increase monotonically. Eg. 1.6
33 is an ancestor of 1.19 and of 1.6.4.5, but 1.19 and 1.6.4.5 are neither
34 ancestors nor descendants of each other.
35  
36 =head1 SUPERCLASS
37  
38 VCS::LibCVS::Datum
39  
40 =cut
41  
42 ###############################################################################
43 # Class constants
44 ###############################################################################
45  
46 1 ▶use constant REVISION => '$Header: /cvsroot/libcvs-perl/libcvs-perl/VCS/LibCVS/Datum/RevisionNumber.pm,v 1.16 2005/10/10 12:52:12 dissent Exp $ ';
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 1 ▶use vars ('@ISA');
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 @ISA = ("VCS::LibCVS::Datum");
50  
51 # COMPARE_* constants are documented in compare() routine
52 1 ▶use constant COMPARE_EQUAL => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use constant COMPARE_ANCESTOR => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use constant COMPARE_DESCENDANT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use constant COMPARE_INCOMPARABLE => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use constant COMPARE_POSSIBLE_ANCESTOR => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use constant COMPARE_POSSIBLE_DESCENDANT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 ###############################################################################
60 # Class variables
61 ###############################################################################
62  
63 ###############################################################################
64 # Private variables
65 ###############################################################################
66  
67 # $self->{Number} is the number as a string, with magic branch numbers
68 # converted to regular branch numbers.
69 # $self->{IsBranch} true if it's a branch revision number
70  
71 ###############################################################################
72 # Class routines
73 ###############################################################################
74  
75 =head1 CLASS ROUTINES
76  
77 =head2 B<new()>
78  
79 $rev_num = VCS::LibCVS::Datum::RevisionNumber->new("1.2.4.5")
80 $rev_num = VCS::LibCVS::Datum::RevisionNumber->new("1.2.0.4")
81 $rev_num = VCS::LibCVS::Datum::RevisionNumber->new("1.2.4")
82  
83 =over 4
84  
85 =item return type: VCS::LibCVS::Datum::RevisionNumber
86  
87 =item argument 1 type: scalar string
88  
89 Must be a valid CVS revision number.
90  
91 =back
92  
93 =cut
94  
95 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
96 0 my ($class, $num) = @_;
97 0 my $that = bless {}, $class;
98  
99 # 0 may only appear by itself, or as the first field or the second to last
100 0 ▶ confess "Bad revision number $num"
Branch: 1 branch
BranchTrueFalseTotal
unless $num =~ /^0$|^(0\.)?([1-9][0-9]*\.)*(0\.)?[1-9][0-9]*$/ 0 0 0
101 unless ($num =~ /^0$|^(0\.)?([1-9][0-9]*\.)*(0\.)?[1-9][0-9]*$/);
102  
103 # Convert magic branch numbers to regular ones by removing the 0 in the
104 # second to last place, unless it's the first number.
105 0 $num =~ s/\.0(\.[0-9])*$/$1/;
106 0 $that->{Number} = $num;
107  
108 # It's a branch if there are an odd number of fields
109 0 $that->{IsBranch} = ($that->_depth() % 2);
110  
111 0 return $that;
112 }
113  
114 ###############################################################################
115 # Instance routines
116 ###############################################################################
117  
118 =head1 INSTANCE ROUTINES
119  
120 =head2 B<as_string()>
121  
122 $rev_str = $rev_num->as_string()
123  
124 =over 4
125  
126 =item return type: string scalar
127  
128 =back
129  
130 Returns the revision number as a string.
131  
132 =cut
133  
134 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
Pod documented
135 0 my $self = shift;
136 0 return $self->{Number};
137 }
138  
139 =head2 B<equals()>
140  
141 if ($rev_num1->equals($rev_num2)) {
142  
143 =over 4
144  
145 =item return type: boolean
146  
147 =item argument 1 type: VCS::LibCVS::Datum::RevisionNumber
148  
149 =back
150  
151 Returns true if the revision numbers contain the same information.
152  
153 =cut
154  
155 0 ▶sub equals {
Subroutine equals: not called CC=2 SCAR=17.9
Pod documented
156 0 my $self = shift;
157 0 ▶ return 0 unless $self->SUPER::equals(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->SUPER::equals(@_) 0 0 0
158 0 my $other = shift;
159 0 return $self->{Number} eq $other->{Number};
160 }
161  
162 =head2 B<is_branch()>
163  
164 if ($rev_num->is_branch()) { . . .
165  
166 =over 4
167  
168 =item return type: boolean scalar
169  
170 =back
171  
172 Return true if this is a branch revision number, false otherwise.
173  
174 =cut
175  
176 0 ▶sub is_branch {
Subroutine is_branch: not called CC=1 SCAR=6.9
Pod documented
177 0 my $self = shift;
178 0 return $self->{IsBranch};
179 }
180  
181 =head2 B<is_trunk()>
182  
183 if ($rev_num->is_trunk()) { . . .
184  
185 =over 4
186  
187 =item return type: boolean scalar
188  
189 =back
190  
191 Return true if this is a revision number for the trunk, false otherwise.
192 A trunk revision number is one with only one field, like: "1", "2", . . .
193  
194 =cut
195  
196 0 ▶sub is_trunk {
Subroutine is_trunk: not called CC=1 SCAR=6.9
Pod documented
197 0 my $self = shift;
198 0 return ($self->_depth() == 1);
199 }
200  
201 =head2 B<is_import_branch()>
202  
203 if ($rev_num->is_import_branch()) { . . .
204  
205 =over 4
206  
207 =item return type: boolean scalar
208  
209 =back
210  
211 Return true if this is a branch revision number for an import branch, false
212 otherwise. An import branch is one with an odd revision number, which is
213 assumed to be the result of an import.
214  
215 =cut
216  
217 0 ▶sub is_import_branch {
Subroutine is_import_branch: not called CC=3 SCAR=24.8
Pod documented
218 0 my $self = shift;
219 return ( $self->{IsBranch}
220 0 ▶ && ($self->_depth() >= 3)
Condition0%0/6
$self->{'IsBranch'} && $self->_depth >= 3 && $self->_last_field % 2 == 1
!ll&&!rl&&r
000
$self->{"IsBranch"} and $self->_depth >= 3
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{'IsBranch'} && $self->_depth >= 3 && $self->_last_field % 2 == 1
$self->{"IsBranch"} $self->_depth >= 3 $self->_last_field % 2 == 1
Truth table0%0/4
A and B && C
A= $self->{"IsBranch"}B= $self->_depth >= 3C= $self->_last_field % 2 == 1
ABCresult
0XX0
10X0
1100
1111
221 && ($self->_last_field() % 2 == 1));
222 }
223  
224 =head2 B<branch_of()>
225  
226 $branch_num = $rev_num->branch_of()
227  
228 =over 4
229  
230 =item return type: VCS::LibCVS::Datum::RevisionNumber
231  
232 =back
233  
234 Get the RevisionNumber for the branch on which this revision lives. If it's a
235 branch revision number it throws an exception.
236  
237 =cut
238  
239 0 ▶sub branch_of {
Subroutine branch_of: not called CC=2 SCAR=17.9
Pod documented
240 0 my $self = shift;
241 0 ▶ confess "No branch_of() for a branch RevisionNumber" if $self->is_branch();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_branch 0 0 0
242 0 return $self->_subrevision();
243 }
244  
245 =head2 B<base_of()>
246  
247 $branch_num = $rev_num->base_of()
248  
249 =over 4
250  
251 =item return type: VCS::LibCVS::Datum::RevisionNumber
252  
253 =back
254  
255 Returns the RevisionNumber from which this branch starts. If it's the trunk or
256 a non-branch revision, an exception is thrown.
257  
258 =cut
259  
260 0 ▶sub base_of {
Subroutine base_of: not called CC=3 SCAR=24.8
Pod documented
261 0 my $self = shift;
262 0 ▶ confess "Only branch revisions have a base" unless $self->is_branch();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_branch 0 0 0
263 0 ▶ confess "Main branch has no base" if $self->_depth == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_depth == 1 0 0 0
264 0 return $self->_subrevision();
265 }
266  
267 =head2 B<first_revision_of()>
268  
269 $rev_num = $branch_num->first_revision_of()
270  
271 =over 4
272  
273 =item return type: VCS::LibCVS::Datum::RevisionNumber
274  
275 =back
276  
277 Returns the RevisionNumber of the first revision committed to this branch.
278 This is not the same as the base of the branch, it's the branch number with a
279 .1 appended.
280  
281 =cut
282  
283 0 ▶sub first_revision_of {
Subroutine first_revision_of: not called CC=2 SCAR=17.9
Pod documented
284 0 my $self = shift;
285 0 ▶ confess "Only branches have a first revision" unless $self->is_branch();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_branch 0 0 0
286 0 return VCS::LibCVS::Datum::RevisionNumber->new($self->as_string() . ".1");
287 }
288  
289 =head2 B<get_predecessor()>
290  
291 $p_rev_num = $rev_num->get_predecessor()
292  
293 =over 4
294  
295 =item return type: VCS::LibCVS::Datum::RevisionNumber
296  
297 =back
298  
299 Return the revision number that immediately preceeds this one, it's youngest
300 ancestor. Return undef if there isn't one. Throw an exception if this is a
301 branch revision.
302  
303 =cut
304  
305 0 ▶sub get_predecessor {
Subroutine get_predecessor: not called CC=4 SCAR=30.0
Pod documented
306 0 my $self = shift;
307 0 ▶ if ($self->is_branch()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_branch) 0 0 0
308 0 confess "Branches don't have predecessors.";
309 }
310 0 ▶ if ($self->_last_field != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_last_field != 1) { } 0 0 0
311 0 my $rvstr = $self->_subrevision_string() . "." . ($self->_last_field() - 1);
312 0 return VCS::LibCVS::Datum::RevisionNumber->new($rvstr);
313 } else {
314 0 ▶ if ($self->branch_of()->is_trunk()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->branch_of->is_trunk) { } 0 0 0
315 0 return;
316 } else {
317 0 return $self->branch_of()->base_of();
318 }
319 }
320 }
321  
322 =head2 B<get_successor()>
323  
324 $p_rev_num = $rev_num->get_successor()
325  
326 =over 4
327  
328 =item return type: VCS::LibCVS::Datum::RevisionNumber
329  
330 =back
331  
332 Return the revision number on the same branch that immediately follows this
333 one, it's eldest descendant. Throw an exception if this is a branch revision.
334  
335 =cut
336  
337 0 ▶sub get_successor {
Subroutine get_successor: not called CC=2 SCAR=17.9
Pod documented
338 0 my $self = shift;
339 0 ▶ if ($self->is_branch()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_branch) 0 0 0
340 0 confess "Branches don't have successors.";
341 }
342 0 my $rvstr = $self->_subrevision_string() . "." . ($self->_last_field() + 1);
343 0 return VCS::LibCVS::Datum::RevisionNumber->new($rvstr);
344 }
345  
346 =head2 B<compare()>
347  
348 $diff = $rev_num1->compare($rev_num2)
349  
350 =over 4
351  
352 =item return type: integer, one of VCS::LibCVS::Datum::RevisionNumber::COMPARE_*
353  
354 =item argument 1 type: string or RevisionNumber object
355  
356 =back
357  
358 Compares this revision number with the argument.
359  
360 The meanings of the return values are:
361  
362 =over 4
363  
364 =item COMPARE_EQUAL
365  
366 They are the same revision number.
367  
368 =item COMPARE_ANCESTOR
369  
370 The argument is an ancestor of this.
371  
372 =item COMPARE_DESCENDANT
373  
374 The argument is a descendant of this.
375  
376 =item COMPARE_POSSIBLE_ANCESTOR
377  
378 The argument is possibly an ancestor of this.
379  
380 =item COMPARE_POSSIBLE_DESCENDANT
381  
382 The argument is possibly a descendant of this.
383  
384 =item COMPARE_INCOMPARABLE
385  
386 The argument is neither an ancestor, nor a descendant of this, and they aren't
387 equal.
388  
389 =back
390  
391 Both branch and regular revision numbers can be compared this way. A branch
392 revision number is an ancestor of all revisions on it (except its base
393 revision) and its subbranches. So 1.6.2 is an ancestor of 1.6.2.4, a
394 descendant of 1.6, and incomparable with 1.7.
395  
396 Branches that sprout from the same revision are possibly ancestors or
397 descendants. However, this can't be known because of CVS's lazy branching. In
398 this case, the values returned are COMPARE_POSSIBLE_ANCESTOR and
399 COMPARE_POSSIBLE_DESCENDANT. For example, 1.11.4 is possibly and ancestor of
400 1.11.6.
401  
402 It would be nice if 1.6 were a descendant of 1.6.2, since it's on that branch,
403 but then we'd lose transitivity, because then we'd have 1.6.2 < 1.6, 1.6 < 1.7,
404 but 1.6.2 and 1.7 are incomparable.
405  
406 =cut
407  
408 0 ▶sub compare {
Subroutine compare: not called CC=18 SCAR=58.3
Pod documented
409 0 my ($self, $other) = @_;
410  
411 0 ▶ if (!ref($other)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $other) 0 0 0
412 0 $other = new VCS::LibCVS::Datum::RevisionNumber($other);
413 }
414  
415 # Check revision numbers that are of the same depth.
416 # Since they are of the same depth, they are either both branches, or both
417 # revisions
418 0 ▶ if ($self->_depth() == $other->_depth()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_depth == $other->_depth) 0 0 0
419 # Check for trivial equality
420 0 ▶ if ($self->equals($other)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->equals($other)) 0 0 0
421 0 return COMPARE_EQUAL;
422 }
423 # Branches of the same depth that sprout from different revisions are
424 # incomparable. If they sprout from the same revision they are possibly
425 # related.
426 0 ▶ if ($self->is_branch()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_branch) 0 0 0
427 0 ▶ if (! $self->base_of()->equals($other->base_of())) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->base_of->equals($other->base_of)) { } 0 0 0
428 0 return COMPARE_INCOMPARABLE;
429 } else {
430 0 ▶ return ($other->_last_field > $self->_last_field)
Branch: 1 branch
BranchTrueFalseTotal
$other->_last_field > $self->_last_field ? : 0 0 0
431 ? COMPARE_POSSIBLE_DESCENDANT : COMPARE_POSSIBLE_ANCESTOR;
432 }
433 }
434 # For revisions, check that they are on the same branch, then check their
435 # last field
436 0 ▶ if ($self->branch_of()->equals($other->branch_of)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->branch_of->equals($other->branch_of)) 0 0 0
437 0 ▶ return ($other->_last_field > $self->_last_field)
Branch: 1 branch
BranchTrueFalseTotal
$other->_last_field > $self->_last_field ? : 0 0 0
438 ? COMPARE_DESCENDANT : COMPARE_ANCESTOR;
439 }
440 # They are revisions on different branches
441 0 return COMPARE_INCOMPARABLE;
442 }
443  
444 # The revision numbers are of different depths
445  
446 # Reduce the deep revision to the same depth as the shallow revision, and
447 # then compare them.
448 0 ▶ my $shallow_rev = ($self->_depth > $other->_depth) ? $other : $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->_depth > $other->_depth ? : 0 0 0
449 0 ▶ my $deep_rev = ($self->_depth > $other->_depth) ? $self : $other;
Branch: 1 branch
BranchTrueFalseTotal
$self->_depth > $other->_depth ? : 0 0 0
450 # $less_deep_rev is the ancestor of $deep_rev with the same depth as
451 # $shallow_rev
452 0 my $less_deep_rev = $deep_rev;
453 0 ▶ while ($shallow_rev->_depth() != $less_deep_rev->_depth()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($shallow_rev->_depth != $less_deep_rev->_depth) 0 0 0
454 0 $less_deep_rev = $less_deep_rev->_subrevision();
455 }
456  
457 # Compare $shallow_rev and $less_deep_rev
458 0 my $result = $shallow_rev->compare($less_deep_rev);
459  
460 # Adjust $result to be correct for $shallow_rev->compare($deep_rev)
461 # eg $shallow_rev is 1.6, $deep_rev is 1.6.4.3
462 0 ▶ $result = COMPARE_DESCENDANT if ($result == COMPARE_EQUAL);
Branch: 1 branch
BranchTrueFalseTotal
if $result == 0 0 0 0
463  
464 # eg $shallow_rev is 1.4, $deep_rev is 1.6.4.3
465 0 ▶ $result = COMPARE_DESCENDANT if ($result == COMPARE_DESCENDANT);
Branch: 1 branch
BranchTrueFalseTotal
if $result == 2 0 0 0
466  
467 # eg $shallow_rev is 1.8, $deep_rev is 1.6.4.3
468 0 ▶ $result = COMPARE_INCOMPARABLE if ($result == COMPARE_ANCESTOR);
Branch: 1 branch
BranchTrueFalseTotal
if $result == 1 0 0 0
469  
470 0 ▶ $result = COMPARE_INCOMPARABLE if ($result == COMPARE_INCOMPARABLE);
Branch: 1 branch
BranchTrueFalseTotal
if $result == 3 0 0 0
471  
472 # Adjust $result to be correct for $self->compare($other)
473 # Only COMPARE_DESCENDANT and COMPARE_INCOMPARABLE are possible
474 0 ▶ if (($self->_depth > $other->_depth) && $result == COMPARE_DESCENDANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_depth > $other->_depth and $result == 2) 0 0 0
Condition0%0/3
$self->_depth > $other->_depth and $result == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_depth > $other->_depth and $result == 2
$self->_depth > $other->_depth $result == 2
Truth table0%0/3
A and B
A= $self->_depth > $other->_depthB= $result == 2
ABresult
0X0
100
111
475 0 $result = COMPARE_ANCESTOR;
476 }
477 0 return $result;
478 }
479  
480 ###############################################################################
481 # Private routines
482 ###############################################################################
483  
484 # Create new revision number with one less field, as a string
485 0 ▶sub _subrevision_string {
Subroutine _subrevision_string: not called CC=1 SCAR=6.9
486 0 my $self = shift;
487 0 my ($sub_num) = ($self->{Number} =~ /^(.*)\.[0-9]+$/);
488 0 return $sub_num;
489 }
490  
491 # Create new RevisionNumber with one less field
492 0 ▶sub _subrevision {
Subroutine _subrevision: not called CC=1 SCAR=6.9
493 0 return VCS::LibCVS::Datum::RevisionNumber->new(shift->_subrevision_string());
494 }
495  
496 # Get the depth of the revision, ie. the number of fields.
497 0 ▶sub _depth {
Subroutine _depth: not called CC=1 SCAR=6.9
498 0 my $self = shift;
499 0 my $depth = (my @t = split(/\./, "$self->{Number}"));
500 0 return $depth;
501 }
502  
503 # The last field of this revision number
504 0 ▶sub _last_field {
Subroutine _last_field: not called CC=1 SCAR=6.9
505 0 my $self = shift;
506 0 my ($self_field) = ($self->{Number} =~ /.*\.([0-9]+)/);
507 0 return $self_field;
508 }
509  
510 =head1 SEE ALSO
511  
512 VCS::LibCVS::Datum
513  
514 =cut
515  
516 1 1;
« blib/lib/VCS/LibCVS/Command.pm ↑ Summary blib/lib/VCS/LibCVS/RepositoryFile.pm »