blib/lib/File/Versions.pm

filter: Statementstmt 85.0% 68 / 80 Branchbran 56.6% 17 / 30 Conditioncond 41.6% 5 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 92.3% 12 / 13 Podpod 25.0% 2 / 8 totaltotal 70.0% 105 / 150 CC 21 SCAR 17.9
CC
21
Cov
74%
backup_name
12.3
make_backup
7.1
default_backup
6.0
CRAP 6.0
↑ Summary
1 package File::Versions;
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 require Exporter;
5 1 our @ISA = qw(Exporter);
6 1 our @EXPORT_OK = qw/backup_name make_backup/;
7 1 our %EXPORT_TAGS = ('all' => \@EXPORT_OK);
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use List::Util qw/max/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.10';
12  
13 # Get the type of version control. Not exported.
14  
15 sub get_version_control
16 5 ▶{
Subroutine get_version_control: called CC=2 SCAR=7.6
Pod undocumented
17 5 my $vc = $ENV{VERSION_CONTROL};
18 5 ▶ if (! $vc) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($vc) 0 5 5
19 0 $vc = 'numbered';
20 }
21 5 return $vc;
22 }
23  
24 sub get_file_max_version_number
25 3 ▶{
Subroutine get_file_max_version_number: called CC=4 SCAR=13.9
Pod undocumented
26 3 my ($file) = @_;
27  
28 # The list of files which look like backups of this file.
29  
30 3 my @backup_files;
31  
32 # The version numbers of the files.
33  
34 my @version_numbers;
35  
36 # Get a list of candidate files using "glob".
37  
38 1 ▶ @backup_files = <$file.~*~>;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 3 for my $backup_file (@backup_files) {
41 1 ▶ if ($backup_file =~ /^$file.~(\d+)~$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($backup_file =~ /^$file.~(\d+)~$/) 1 0 1
42 1 my $version_number = $1;
43 1 push @version_numbers, $version_number;
44 }
45 }
46 3 my $max;
47 3 ▶ if (@version_numbers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@version_numbers) 1 2 3
48 1 $max = max @version_numbers;
49 }
50 3 return $max;
51 }
52  
53 # Look at the files in the current directory and find the next
54 # possible file. Not exported.
55  
56 sub find_next_numbered
57 3 ▶{
Subroutine find_next_numbered: called CC=3 SCAR=11.1
Pod undocumented
58 3 my ($file) = @_;
59  
60 3 my $max_version_number = get_file_max_version_number ($file);
61 3 my $next = 1;
62 3 ▶ if ($max_version_number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_version_number) 1 2 3
63 1 $next = $max_version_number + 1;
64 }
65 3 my $next_file = "$file.~$next~";
66  
67 # Test that this file really does not exist.
68  
69 3 ▶ if (-f $next_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $next_file) 0 3 3
70 0 die "There is a bug in this program. A file exists which is not supposed to.";
71 }
72 3 return $next_file;
73 }
74  
75 # Find out what to use for the value of the suffix for simple backups.
76  
77 sub simple_backup_suffix
78 2 ▶{
Subroutine simple_backup_suffix: called CC=2 SCAR=7.0
Pod undocumented
79 2 my $suffix;
80 2 $suffix = $ENV{SIMPLE_BACKUP_SUFFIX};
81 2 ▶ if (! $suffix) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($suffix) 2 0 2
82 2 $suffix = '~';
83 }
84 2 return $suffix;
85 }
86  
87 # Make a simple backup of the file, copy it to a file with the same
88 # name plus the extension '~' or the value of SIMPLE_BACKUP_SUFFIX.
89  
90 sub simple_backup
91 2 ▶{
Subroutine simple_backup: called CC=1 SCAR=0.0
Pod undocumented
92 2 my ($file) = @_;
93 2 my $suffix = simple_backup_suffix ();
94 2 my $backup = "$file$suffix";
95 2 return $backup;
96 }
97  
98 # Make numbered backups of files that already have them, otherwise
99 # simple backups.
100  
101 sub default_backup
102 0 ▶{
Subroutine default_backup: not called CC=2 SCAR=17.9
Pod undocumented
103 0 my ($file) = @_;
104 0 my $backup;
105 0 my $max_version_number = get_file_max_version_number ($file);
106 0 ▶ if ($max_version_number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_version_number) { } 0 0 0
107 0 $backup = find_next_numbered ($file);
108 }
109 else {
110 0 $backup = simple_backup ($file);
111 }
112 }
113  
114  
115 sub backup_name
116 6 ▶{
Subroutine backup_name: called CC=9 SCAR=25.1
Pod documented
117 6 my ($file) = @_;
118  
119 6 my $backup_file;
120  
121 6 ▶ if (! -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $file) { } 1 5 6
122 1 $backup_file = $file;
123 }
124 else {
125 5 my $version_control = get_version_control ();
126  
127 5 ▶ if (! $version_control ||
Branch: 1 branch
BranchTrueFalseTotal
if (not $version_control or $version_control eq "existing" or $version_control eq "nil") 0 5 5
Condition33%2/6
not $version_control or $version_control eq "existing" or $version_control eq "nil"
l!l&&r!l&&!r
005
not $version_control or $version_control eq "existing"
l!l&&r!l&&!r
005
MC/DC0%0/3
not $version_control or $version_control eq "existing" or $version_control eq "nil"
not $version_control $version_control eq "existing" $version_control eq "nil"
Truth table25%1/4
not(A) or B or C
A= not $version_controlB= $version_control eq "existing"C= $version_control eq "nil"
ABCresult
1XX1
01X1
0011
0000
128 $version_control eq 'existing' ||
129 $version_control eq 'nil') {
130 0 $backup_file = default_backup
131 }
132 5 ▶ if ($version_control eq 'numbered' ||
Branch: 2 branches
BranchTrueFalseTotal
if ($version_control eq 'numbered' or $version_control eq 't') { } 3 2 5
elsif ($version_control eq 'simple' or $version_control eq 'never') { } 2 0 2
Condition66%2/3
$version_control eq "numbered" or $version_control eq "t"
l!l&&r!l&&!r
302
MC/DC50%1/2
$version_control eq "numbered" or $version_control eq "t"
$version_control eq "numbered" $version_control eq "t"
Truth table66%2/3
A or B
A= $version_control eq "numbered"B= $version_control eq "t"
ABresult
1X1
011
000
133 $version_control eq 't') {
134 3 $backup_file = find_next_numbered ($file);
135 }
136 ▶ elsif ($version_control eq 'simple' ||
Condition33%1/3
$version_control eq "simple" or $version_control eq "never"
l!l&&r!l&&!r
200
MC/DC0%0/2
$version_control eq "simple" or $version_control eq "never"
$version_control eq "simple" $version_control eq "never"
Truth table33%1/3
A or B
A= $version_control eq "simple"B= $version_control eq "never"
ABresult
1X1
011
000
137 $version_control eq 'never') {
138 2 $backup_file = simple_backup ($file);
139 }
140 else {
141 0 croak __PACKAGE__, ": I don't know how to do the type of version control '$version_control' in your environment.\n";
142 }
143 }
144 6 return $backup_file;
145 }
146  
147 sub make_backup
148 3 ▶{
Subroutine make_backup: called CC=5 SCAR=19.6
Pod documented
149 3 my ($file) = @_;
150 3 ▶ if (! -f $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 3 3
151 0 croak "Asked to make a backup of a file '$file' which does not exist";
152 }
153 3 my $backup_file = backup_name ($file);
154 3 ▶ if (-f $backup_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $backup_file) 0 3 3
155 0 ▶ unlink $backup_file or croak "unlink $backup_file failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $backup_file or croak("unlink $backup_file failed: $!") 0 0 0
156 }
157 3 ▶ rename $file, $backup_file or croak "rename $file, $backup_file failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $file, $backup_file or croak("rename $file, $backup_file failed: $!") 3 0 3
158 3 return $backup_file;
159 }
160  
161 1 1;
162  
↑ Summary