blib/lib/VCS/LibCVS/WorkingFile.pm

filter: Statementstmt 26.9% 38 / 141 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 41.3% 12 / 29 Podpod 100% 9 / 9 totaltotal 23.4% 59 / 252 CC 35 SCAR 36.5
CC
35
Cov
18%
get_state
90.0
get_rstate
90.0
get_file_branch
72.0
CRAP 38.7
« blib/lib/VCS/LibCVS/Datum/Entry.pm ↑ Summary blib/lib/VCS/LibCVS/Client.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::WorkingFile;
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::WorkingFile - A file checked out of CVS.
16  
17 =head1 SYNOPSIS
18  
19 =head1 DESCRIPTION
20  
21 This object represents a local copy of a file checked out from CVS.
22  
23 =head1 SUPERCLASS
24  
25 VCS::LibCVS::WorkingFileOrDirectory
26  
27 =cut
28  
29 ###############################################################################
30 # Class constants
31 ###############################################################################
32  
33 1 ▶use constant REVISION => '$Header: /cvsroot/libcvs-perl/libcvs-perl/VCS/LibCVS/WorkingFile.pm,v 1.15 2005/10/10 12:52:11 dissent Exp $ ';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 ▶use vars ('@ISA');
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 @ISA = ("VCS::LibCVS::WorkingFileOrDirectory");
37  
38 # ACTION_* constants are documented in the get_scheduled_action routine
39 1 ▶use constant ACTION_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 ▶use constant ACTION_ADD => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use constant ACTION_REMOVE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # STATE_* constants are documented in the get_state routine
44 1 ▶use constant STATE_UPTODATE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use constant STATE_MODIFIED => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use constant STATE_HADCONFLICTS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶use constant STATE_ABSENT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 ▶use constant STATE_WILLCONFLICT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 ###############################################################################
51 # Class variables
52 ###############################################################################
53  
54 ###############################################################################
55 # Private variables
56 ###############################################################################
57  
58 # $self->{Entry} VCS::LibCVS::Datum::Entry for this file, from the Admin dir
59  
60 ###############################################################################
61 # Class routines
62 ###############################################################################
63  
64 =head1 CLASS ROUTINES
65  
66 =head2 B<new()>
67  
68 $working_file = VCS::LibCVS::WorkingFile->new($filename)
69  
70 =over 4
71  
72 =item return type: VCS::LibCVS::WorkingFile
73  
74 =item argument 1 type: scalar string
75  
76 The name of the file which is under CVS control.
77  
78 =back
79  
80 Creates a new WorkingFile. The filename may be relative or absolute, and is
81 stored as such.
82  
83 It throws an exception if the file is not recorded in the CVS/Entries file.
84  
85 =cut
86  
87 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
88 0 my $class = shift;
89 0 my $that = $class->SUPER::new(@_);
90  
91 0 $that->{Admin} = VCS::LibCVS::Admin->new($that->get_name({no_base => 1}));
92 0 $that->{Entry} = $that->{Admin}->get_Entries()->{$that->get_name({no_dir => 1})};
93  
94 0 ▶ confess ($that->get_name() . " is not managed by CVS") unless $that->{Entry};
Branch: 1 branch
BranchTrueFalseTotal
unless $that->{"Entry"} 0 0 0
95  
96 0 return $that;
97 }
98  
99 ###############################################################################
100 # Instance routines
101 ###############################################################################
102  
103 =head1 INSTANCE ROUTINES
104  
105 =head2 B<get_remote_object()>
106  
107 $r_file = $w_file->get_remote_object()
108  
109 =over 4
110  
111 =item return type: VCS::LibCVS::RepositoryFile
112  
113 Returns the remote CVS file associated with this working file.
114  
115 =back
116  
117 It reads the CVS sandbox administrative directory to get this info.
118  
119 =cut
120  
121 0 ▶sub get_remote_object {
Subroutine get_remote_object: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123  
124 0 my $repo = $self->get_repository();
125 0 my $repo_dir = $self->{Admin}->get_Repository()->{DirectoryName};
126 0 my $name = $self->get_name({no_dir => 1 });
127 0 my $repo_file = File::Spec::Unix->catfile($repo_dir, $name);
128  
129 0 return VCS::LibCVS::RepositoryFile->new($repo, $repo_file);
130 }
131  
132 =head2 B<get_branch()>
133  
134 $branch = $w_file->get_branch()
135  
136 =over 4
137  
138 =item return type: VCS::LibCVS::Branch
139  
140 Returns the branch this local file is on.
141  
142 =back
143  
144 If there is no sticky branch tag, it returns the MAIN branch.
145  
146 =cut
147  
148 0 ▶sub get_branch {
Subroutine get_branch: not called CC=1 SCAR=6.9
Pod documented
149 0 my $self = shift;
150  
151 0 return $self->get_file_branch()->get_branch();
152 }
153  
154 =head2 B<get_file_branch()>
155  
156 $file_branch = $w_file->get_file_branch()
157  
158 =over 4
159  
160 =item return type: VCS::LibCVS::FileBranch
161  
162 Returns the file branch this local file is on.
163  
164 =back
165  
166 The branch is determined by looking at any sticky branch tags, and then at the
167 revision number.
168  
169 If a branch has a sticky non-branch tag, then there can be amibiguity about
170 which branch the revision is on. For instance, revision 1.4 could be the
171 fourth revision on branch 1 (the trunk), or it could be the initial revision of
172 branch 1.4.2. The best way to resolve this ambiguity would be to examine the
173 rest of the files in the repository to determine which branch the non-branch
174 tag lies on. Such an algorithm is not implemented here, instead the parent
175 branch is always assumed. This is important to consider in the common case of
176 base tags for branches.
177  
178 =cut
179  
180 0 ▶sub get_file_branch {
Subroutine get_file_branch: not called CC=8 SCAR=42.8
Pod documented
181 0 my $self = shift;
182  
183 0 my $tag = $self->{Entry}->get_tag();
184  
185 # Quickly handle the common case of a trunk revision.
186 0 my $revision = $self->get_revision_number();
187 0 my $branch_rev = $revision->branch_of();
188 0 ▶ if (!defined($tag) && $branch_rev->is_trunk()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tag and $branch_rev->is_trunk) 0 0 0
Condition0%0/3
not defined $tag and $branch_rev->is_trunk
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $tag and $branch_rev->is_trunk
not defined $tag $branch_rev->is_trunk
Truth table0%0/3
not(A) and B
A= not defined $tagB= $branch_rev->is_trunk
ABresult
0X0
100
111
189 0 return VCS::LibCVS::FileBranch->new($self->get_remote_object(),
190 undef,
191 $branch_rev);
192 }
193  
194 # If there's a sticky tag, it could be a branch tag or a non-branch tag.
195 # However, we can't distinguish them, so we get all the branches from the
196 # server and search for the right one. If it's a non-branch tag, it's
197 # handled just the same as the case of no tag.
198  
199 0 my $r_file = $self->get_remote_object();
200 0 my $all_branches = $r_file->get_branches();
201  
202 # If it's a branch tag, it will match one of the branches.
203 0 ▶ if (defined ($tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag) 0 0 0
204 0 foreach my $branch (@$all_branches) {
205 0 ▶ return $branch if $branch->get_tag()->equals($tag);
Branch: 1 branch
BranchTrueFalseTotal
if $branch->get_tag->equals($tag) 0 0 0
206 }
207 }
208  
209 # Either a non-branch tag, or no tag at all, just use the revision number to
210 # find the right branch.
211 0 foreach my $branch (@$all_branches) {
212 0 ▶ return $branch if $branch->get_revision_number()->equals($branch_rev);
Branch: 1 branch
BranchTrueFalseTotal
if $branch->get_revision_number->equals($branch_rev) 0 0 0
213 }
214  
215 # No named branch could be found, so we return an unnamed one.
216 0 return VCS::LibCVS::FileBranch->new($self->get_remote_object(),
217 undef,
218 $branch_rev);
219 }
220  
221 =head2 B<get_directory_of()>
222  
223 $w_file->get_directory_of()
224  
225 =over 4
226  
227 =item return type: VCS::LibCVS::WorkingDirectory
228  
229 =back
230  
231 Returns the working directory containing this object. If the directory isn't
232 under CVS control, or if the object's specification is relative, and it's
233 parent directory can't be determined, an exception is thrown.
234  
235 =cut
236  
237 0 ▶sub get_directory_of {
Subroutine get_directory_of: not called CC=1 SCAR=6.9
Pod documented
238 0 my $self = shift;
239 0 return VCS::LibCVS::WorkingDirectory->new($self->get_name({no_base => 1}));
240 }
241  
242 =head2 B<get_scheduled_action()>
243  
244 $w_file->get_scheduled_action()
245  
246 =over 4
247  
248 =item return type: integer, one of VCS::LibCVS::WorkingFile::ACTION_*
249  
250 =back
251  
252 Returns any action which has been scheduled on this file. The actions are:
253  
254 ACTION_NONE
255 ACTION_ADD
256 ACTION_REMOVE
257  
258 =cut
259  
260 0 ▶sub get_scheduled_action {
Subroutine get_scheduled_action: not called CC=3 SCAR=24.8
Pod documented
261 0 my $self = shift;
262  
263 0 ▶ return ACTION_ADD if ("$self->{Entry}->{Revision}" eq "0");
Branch: 1 branch
BranchTrueFalseTotal
if qq[$self->{"Entry"}{"Revision"}] eq 0 0 0 0
264 0 ▶ return ACTION_REMOVE if ("$self->{Entry}->{Revision}" =~ /^-/);
Branch: 1 branch
BranchTrueFalseTotal
if qq[$self->{"Entry"}{"Revision"}] =~ /^-/ 0 0 0
265 0 return ACTION_NONE;
266 }
267  
268 =head2 B<get_state()>
269  
270 $w_file->get_state()
271  
272 =over 4
273  
274 =item return type: one of VCS::LibCVS::WorkingFile::STATE_*
275  
276 =back
277  
278 Returns the state of the local file. This is determined with reference to the
279 information in the Admin directory, including any scheduled actions. The
280 possible states are:
281  
282 STATE_UPTODATE
283 STATE_MODIFIED
284 STATE_HADCONFLICTS
285 STATE_ABSENT
286  
287 In the case of a file which has been scheduled for removal, it should not
288 appear in the working directory, so being absent is treated as uptodate. If it
289 is present, it is reported as modified.
290  
291 =cut
292  
293 0 ▶sub get_state {
Subroutine get_state: not called CC=9 SCAR=45.0
Pod documented
294 0 my $self = shift;
295  
296 0 ▶ if ($self->get_scheduled_action() == ACTION_REMOVE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_scheduled_action == 2) 0 0 0
297 0 ▶ return (-e $self->get_name()) ? STATE_MODIFIED : STATE_UPTODATE;
Branch: 1 branch
BranchTrueFalseTotal
-e $self->get_name ? : 0 0 0
298 }
299  
300 0 ▶ if ($self->get_scheduled_action() == ACTION_ADD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_scheduled_action == 1) 0 0 0
301 0 ▶ return (-e $self->get_name()) ? STATE_UPTODATE : STATE_ABSENT;
Branch: 1 branch
BranchTrueFalseTotal
-e $self->get_name ? : 0 0 0
302 }
303  
304 0 ▶ if ($self->get_scheduled_action() == ACTION_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_scheduled_action == 0) 0 0 0
305 0 ▶ return STATE_ABSENT unless (-e $self->get_name());
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->get_name 0 0 0
306 0 ▶ return STATE_HADCONFLICTS if $self->_had_conflicts();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_had_conflicts 0 0 0
307 0 ▶ return ($self->_is_modified()) ? STATE_MODIFIED : STATE_UPTODATE;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_modified ? : 0 0 0
308 }
309 0 confess( "Unexpected Action: " . $self->get_scheduled_action()
310 . ". On File: " . $self->get_name());
311 }
312  
313 =head2 B<get_rstate()>
314  
315 $w_file->get_rstate()
316  
317 =over 4
318  
319 =item return type: one of VCS::LibCVS::WorkingFile::STATE_*
320  
321 =back
322  
323 Returns the state of the repository with respect to the working file info. This
324 is determined with reference to the information in the Admin directory,
325 including any scheduled actions. The possible states are:
326  
327 STATE_UPTODATE
328 STATE_MODIFIED
329 STATE_ABSENT
330 STATE_WILLCONFLICT
331  
332 =cut
333  
334 0 ▶sub get_rstate {
Subroutine get_rstate: not called CC=9 SCAR=45.0
Pod documented
335 0 my $self = shift;
336  
337 # The remote file may not exist, and in fact shouldn't in the case of an add,
338 # so wrap in an eval to catch this.
339 0 my $remote_file;
340 0 eval { $remote_file = $self->get_remote_object(); };
341 0 ▶ if ($@ && ($@ !~ /cvs (server|log): nothing known about/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $@ =~ /cvs (server|log): nothing known about/) 0 0 0
Condition0%0/3
$@ and not $@ =~ /cvs (server|log): nothing known about/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not $@ =~ /cvs (server|log): nothing known about/
$@ not $@ =~ /cvs (server|log): nothing known about/
Truth table0%0/3
A and not(B)
A= $@B= not $@ =~ /cvs (server|log): nothing known about/
ABresult
0X0
100
111
342 0 confess($@);
343 }
344  
345 # For newly added files, the repository state is ok ("U") unless the file is
346 # already present in the repository
347 0 ▶ if ($self->get_scheduled_action() == ACTION_ADD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_scheduled_action == 1) 0 0 0
348 0 ▶ return ($remote_file) ? STATE_MODIFIED : STATE_UPTODATE;
Branch: 1 branch
BranchTrueFalseTotal
$remote_file ? : 0 0 0
349 }
350  
351 # If the remote file doesn't exist, then it's missing.
352 0 ▶ return STATE_ABSENT unless $remote_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $remote_file 0 0 0
353  
354 # For files that are already present in the repository, check if the tip of
355 # the branch is a different revision.
356 0 my $branch = $self->get_file_branch();
357 0 my $tip_rev = $branch->get_tip_revision()->get_revision_number();
358 0 my $cmp = $self->get_revision_number()->compare($tip_rev);
359  
360 0 ▶ return STATE_UPTODATE
Branch: 1 branch
BranchTrueFalseTotal
if $cmp == 0 0 0 0
361 if $cmp == VCS::LibCVS::Datum::RevisionNumber::COMPARE_EQUAL;
362  
363 0 ▶ if ( $cmp == VCS::LibCVS::Datum::RevisionNumber::COMPARE_DESCENDANT ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp == 2) 0 0 0
364 0 ▶ return $self->_will_conflict() ? STATE_WILLCONFLICT : STATE_MODIFIED;
Branch: 1 branch
BranchTrueFalseTotal
$self->_will_conflict ? : 0 0 0
365 }
366  
367 # Any other compare result is an error
368  
369 0 confess ("Unexpected comparison result (" . $cmp . ") for revisions: "
370 . $tip_rev->as_string() . " and "
371 . $self->get_revision_number()->as_string());
372 }
373  
374 =head2 B<get_revision_number()>
375  
376 $w_file->get_revision_number()
377  
378 =over 4
379  
380 =item return type: VCS::LibCVS::Datum::RevisionNumber
381  
382 =back
383  
384 Return the revision number of the local file.
385  
386 When a file was imported and then checked out on the trunk, it will have a
387 revision number of 1.1.1.1, and no sticky tag, and actually reside on the
388 trunk. This routine will hack that case to retun the revision 1.1.
389  
390 =cut
391  
392 0 ▶sub get_revision_number {
Subroutine get_revision_number: not called CC=4 SCAR=30.0
Pod documented
393 0 my $self = shift;
394  
395 0 my $revision = $self->{Entry}->get_revision();
396  
397 # Check for the annoying import revision number.
398 0 ▶ if (!defined $self->{Entry}->get_tag()
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"Entry"}->get_tag and $revision->branch_of->is_import_branch) 0 0 0
Condition0%0/3
not defined $self->{"Entry"}->get_tag and $revision->branch_of->is_import_branch
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $self->{"Entry"}->get_tag and $revision->branch_of->is_import_branch
not defined $self->{"Entry"}->get_tag $revision->branch_of->is_import_branch
Truth table0%0/3
not(A) and B
A= not defined $self->{"Entry"}->get_tagB= $revision->branch_of->is_import_branch
ABresult
0X0
100
111
399 && $revision->branch_of()->is_import_branch()) {
400 0 ▶ while (!$revision->branch_of()->is_trunk()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($revision->branch_of->is_trunk) 0 0 0
401 0 $revision = $revision->branch_of()->base_of();
402 }
403 }
404 0 return $revision;
405 }
406  
407 ###############################################################################
408 # Private routines
409 ###############################################################################
410  
411 # Consult the server to see if this file has been modified.
412 # Internal function for get_state()
413  
414 0 ▶sub _is_modified {
Subroutine _is_modified: not called CC=3 SCAR=24.8
415 0 my $self = shift;
416  
417 # Check file modification time, to save access in many cases
418 0 my $updated_time = $self->{Entry}->get_updated_time();
419 0 ▶ return 0 unless ($self->_changed_since($updated_time));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_changed_since($updated_time) 0 0 0
420  
421 # Create and issue a command to the server
422 0 my $command = VCS::LibCVS::Command->new({}, "status", [], [$self]);
423 0 $command->issue($self->get_repository());
424  
425 # Expect one message of the form: "File: . . . Status: . . ."
426 0 my @st = $command->get_messages(qr/^File: .*Status: .*$/);
427 0 ▶ confess "Bad status message for " . $self->get_name() unless (@st == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless @st == 1 0 0 0
428 0 return $st[0] !~ /Up-to-date/;
429 }
430  
431 # Consult the server to see if this file has modifications which conflict with
432 # modifications in the repository.
433 # Internal function for get_rstate()
434  
435 0 ▶sub _will_conflict {
Subroutine _will_conflict: not called CC=3 SCAR=24.8
436 0 my $self = shift;
437  
438 # Check file modification time, to save access in many cases
439 0 my $updated_time = $self->{Entry}->get_updated_time();
440 0 ▶ return 0 unless ($self->_changed_since($updated_time));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_changed_since($updated_time) 0 0 0
441  
442 # Create and issue a command to the server
443 0 my $command = VCS::LibCVS::Command->new({}, "update", [], [$self]);
444 0 $command->issue($self->get_repository());
445  
446 # To find the conflict, look in the entry line
447 0 my @files = $command->get_files();
448  
449 # If no files were returned, no update is due, so there can't be a conflict.
450 0 ▶ return 0 if (@files == 0);
Branch: 1 branch
BranchTrueFalseTotal
if @files == 0 0 0 0
451  
452 0 my $entry = $files[0]->{Args}[1];
453  
454 0 return $entry->is_conflict();
455 }
456  
457 # Consult CVS Admin files to see if the file had conflicts on merge
458 # Internal function for get_state()
459 # If it's been modified since the conflict time, assume they've been cleaned up
460  
461 # perhaps it could look for the conflict markers in the file
462  
463 0 ▶sub _had_conflicts {
Subroutine _had_conflicts: not called CC=1 SCAR=6.9
464 0 my $self = shift;
465 0 my $conf_time = $self->{Entry}->get_conflict_time();
466  
467 0 return ! $self->_changed_since($conf_time);
468 }
469  
470 # Return true if the file has been changed since the given time
471 # If stat() fails (the file is gone), then return false.
472 0 ▶sub _changed_since {
Subroutine _changed_since: not called CC=2 SCAR=17.9
473 0 my $self = shift;
474 0 my $cmp_time = shift;
475  
476 0 my $mod_time = [ stat($self->get_name()) ]->[9];
477  
478 0 ▶ return 0 if ! defined $mod_time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mod_time 0 0 0
479 0 return ($mod_time > $cmp_time);
480 }
481  
482 # Directory names for reporting to the server.
483 # Routine called in Command.pm, see there for more details.
484 0 ▶sub _get_repo_dirs {
Subroutine _get_repo_dirs: not called CC=1 SCAR=6.9
485 0 my $self = shift;
486 0 my $l_dir = $self->get_name({no_base => 1});
487 0 my $root_repo_dir = $self->{Admin}->get_Root()->{RootDir};
488 0 my $within_repo_dir = $self->{Admin}->get_Repository()->as_string();
489 0 my $r_dir = File::Spec::Unix->catdir($root_repo_dir, $within_repo_dir);
490  
491 0 return [ $l_dir, $r_dir ];
492 }
493  
494 # Return my entry line. Called from Command.pm
495 0 ▶sub _get_entry {
Subroutine _get_entry: not called CC=1 SCAR=6.9
496 0 my $self = shift;
497 0 return $self->{Entry};
498 }
499  
500 # Return my filemode. Called from Command.pm
501 0 ▶sub _get_mode {
Subroutine _get_mode: not called CC=1 SCAR=6.9
502 0 my $self = shift;
503 0 return VCS::LibCVS::Datum::FileMode->new($self->get_name);
504 }
505  
506 # Return my file contents. Called from Command.pm
507 0 ▶sub _get_contents {
Subroutine _get_contents: not called CC=1 SCAR=6.9
508 0 my $self = shift;
509 0 return VCS::LibCVS::Datum::FileContents->new($self->get_name);
510 }
511  
512 =head1 SEE ALSO
513  
514 VCS::LibCVS
515  
516 =cut
517  
518 1 1;
« blib/lib/VCS/LibCVS/Datum/Entry.pm ↑ Summary blib/lib/VCS/LibCVS/Client.pm »