script/snag

filter: Statementstmt 90.9% 60 / 66 Branchbran 65.9% 29 / 44 Conditioncond 54.2% 19 / 35 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 85.7% 6 / 7 Podpod n/a totaltotal 70.4% 119 / 169 CC 1 SCAR 1.3
CC
1
Cov
74%
help
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/File/Value.pm ↑ Summary
1 #!/usr/local/bin/perl
2  
3 17 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 my $VERSION = $File::Value::VERSION;
8  
9 17 ▶use Getopt::Long qw(:config bundling_override);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use File::Value ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 17 my %opt = (
14 force => 0,
15 help => 0,
16 lshigh => 0,
17 lslow => 0,
18 man => 0,
19 mknext => 0,
20 mknextcopy => 0,
21 version => 0,
22 verbose => 0,
23 );
24  
25 # main
26 {
27 17 ▶ GetOptions(\%opt,
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%opt, "force|f", "help|h|?", "lshigh", "lslow", "man", "mknext", "mknextcopy", "version", "verbose|v") or pod2usage(1) 17 0 17
28 'force|f',
29 'help|h|?',
30 'lshigh',
31 'lslow',
32 'man',
33 'mknext',
34 'mknextcopy',
35 'version',
36 'verbose|v',
37 ) or pod2usage(1);
38  
39 help(), exit(0)
40 17 ▶ if $opt{help};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"help"} 0 17 17
41 pod2usage(-exitstatus => 0, -verbose => 2)
42 17 ▶ if $opt{man};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"man"} 0 17 17
43 print "$VERSION\n" and exit(0)
44 17 ▶ if $opt{version};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"version"} 0 17 17
Condition25%1/4
$opt{'version'} && (print("$VERSION\n") && exit 0)
!ll
170
print "$VERSION\n" and exit 0
!ll
00
MC/DC0%0/2
$opt{'version'} && (print("$VERSION\n") && exit 0)
$opt{'version'} print "$VERSION\n"
Truth table33%1/3
A && B
A= $opt{'version'}B= print "$VERSION\n"
ABresult
0X0
100
111
45 pod2usage("$0: --mknext cannot be given with --lshigh or --lslow")
46 17 ▶ if ($opt{lshigh} || $opt{lslow}) && $opt{mknext};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"lshigh"} || $opt{"lslow"} and $opt{"mknext"} 0 17 17
Condition83%5/6
$opt{"lshigh"} || $opt{"lslow"} and $opt{"mknext"}
!ll&&!rl&&r
1520
$opt{"lshigh"} or $opt{"lslow"}
l!l&&r!l&&!r
1115
MC/DC0%0/3
$opt{"lshigh"} || $opt{"lslow"} and $opt{"mknext"}
$opt{"lshigh"} $opt{"lslow"} $opt{"mknext"}
Truth table60%3/5
A or B and C
A= $opt{"lshigh"}B= $opt{"lslow"}C= $opt{"mknext"}
ABCresult
00X0
1X00
0100
1X11
0111
47 17 ▶ help(), exit(1)
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV 0 17 17
48 #pod2usage("$0: no file or directory names given")
49 unless @ARGV;
50  
51 17 foreach my $node (@ARGV) {
52  
53 17 my $as_dir = ($node =~ s,/+$,,); # a dir if ends in '/'
54 17 ▶ my $prnode = $node # print-friendly name
Branch: 1 branch
BranchTrueFalseTotal
$as_dir ? : 3 14 17
55 . ($as_dir ? '/' : ''); # has '/' added back
56  
57 17 my ($n, $msg);
58 17 ▶ if ($opt{lshigh} or $opt{lslow}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opt{'lshigh'} or $opt{'lslow'}) { } 2 15 17
elsif ($opt{'mknext'} or $opt{'mknextcopy'}) { } 10 5 15
Condition100%3/3
$opt{"lshigh"} or $opt{"lslow"}
l!l&&r!l&&!r
1115
MC/DC100%2/2
$opt{"lshigh"} or $opt{"lslow"}
$opt{"lshigh"} $opt{"lslow"}
Truth table100%3/3
A or B
A= $opt{"lshigh"}B= $opt{"lslow"}
ABresult
1X1
011
000
59 # we're only asked to report either or both of
60 # the low version and the high version
61 2 my @nodes;
62 2 $node =~ s/\d+$//;
63 2 ▶ if ($opt{lslow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"lslow"}) 1 1 2
64 1 ($n, $msg) = list_low_version($node);
65 1 ▶ $n == -1 and print(STDERR
Branch: 1 branch
BranchTrueFalseTotal
$n == -1 and print(STDERR "${prnode}: has no numbered versions\n"), exit 2 0 1 1
66 "$prnode: has no numbered versions\n"),
67 exit 2
68 ;
69 1 push @nodes, $msg; # got it, $msg is node
70 }
71 2 ▶ if ($opt{lshigh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"lshigh"}) 1 1 2
72 1 ($n, $msg) = list_high_version($node);
73 1 ▶ $n == -1 and print(STDERR
Branch: 1 branch
BranchTrueFalseTotal
$n == -1 and print(STDERR "${prnode}: has no numbered versions\n"), exit 2 0 1 1
74 "$prnode: has no numbered versions\n"),
75 exit 2
76 ;
77 1 push @nodes, $msg; # got it, $msg is node
78 }
79 print join(" ",
80 2 ▶ grep { (-d $_ and s,$,/,) or $_ } @nodes),
Condition33%2/6
-d $_ && s[$][/] || $_
l!l&&r!l&&!r
020
-d $_ and s[$][/]
!ll&&!rl&&r
200
MC/DC0%0/3
-d $_ && s[$][/] || $_
-d $_ s[$][/] $_
Truth table20%1/5
A and B || C
A= -d $_B= s[$][/]C= $_
ABCresult
11X1
0X11
1011
0X00
1000
81 "\n";
82 2 next;
83 # yyy support "missing" versions ??
84 }
85 ▶ elsif ($opt{mknext} or $opt{mknextcopy}) {
Condition100%3/3
$opt{"mknext"} or $opt{"mknextcopy"}
l!l&&r!l&&!r
735
MC/DC100%2/2
$opt{"mknext"} or $opt{"mknextcopy"}
$opt{"mknext"} $opt{"mknextcopy"}
Truth table100%3/3
A or B
A= $opt{"mknext"}B= $opt{"mknextcopy"}
ABresult
1X1
011
000
86 ($n, $msg) = snag_version($node, {
87 as_dir => $as_dir,
88 no_type_mismatch => ! $opt{force},
89 10 mknextcopy => $opt{mknextcopy}});
90 10 ▶ if ($n == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == -1) 3 7 10
91 3 print STDERR "$prnode: $msg\n";
92 3 exit 2;
93 }
94 # got it: $msg is the node name
95 7 ▶ print "$msg", ($as_dir ? '/' : ''), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$as_dir ? : 0 7 7
96 7 next;
97 }
98 else { # simple snag
99 5 ▶ if ($opt{force} && -e $node) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"force"} and -e $node) 1 4 5
Condition66%2/3
$opt{"force"} and -e $node
!ll&&!rl&&r
401
MC/DC50%1/2
$opt{"force"} and -e $node
$opt{"force"} -e $node
Truth table66%2/3
A and B
A= $opt{"force"}B= -e $node
ABresult
0X0
100
111
100 1 ▶ -d $node and
Branch: 1 branch
BranchTrueFalseTotal
-d $node and rmdir $node || die("${node}: $!") or (unlink $node or die "${node}: $!") 1 0 1
Condition30%3/10
-d $node && (rmdir $node || die("${node}: $!")) || (unlink($node) || die("${node}: $!"))
l!l&&r!l&&!r
100
-d $node and rmdir $node || die("${node}: $!")
!ll&&!rl&&r
001
rmdir $node or die "${node}: $!"
l!l
10
unlink $node or die "${node}: $!"
l!l
00
MC/DC0%0/3
-d $node && (rmdir $node || die("${node}: $!")) || (unlink($node) || die("${node}: $!"))
-d $node rmdir $node unlink $node
Truth table20%1/5
A and B || C
A= -d $nodeB= rmdir $nodeC= unlink $node
ABCresult
11X1
0X11
1011
0X00
1000
101 rmdir($node) || die "$node: $!"
102 or
103 unlink($node) || die "$node: $!"
104 ;
105 }
106 5 ▶ $msg = $as_dir ?
Branch: 1 branch
BranchTrueFalseTotal
$as_dir ? : 2 3 5
107 snag_dir($node) : snag_file($node);
108 5 ▶ if ($msg eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg eq "") 4 1 5
109 4 print "$prnode\n";
110 4 next;
111 }
112 1 ▶ if ($msg eq '1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg eq 1) 0 1 1
113 0 print "$prnode already exists";
114 0 ▶ print ", but as a ", ($as_dir ?
Branch: 2 branches
BranchTrueFalseTotal
if $as_dir != -d $node 0 0 0
$as_dir ? : 0 0 0
115 "file" : "directory")
116 if ($as_dir != -d $node);
117 0 print "\n";
118 0 exit 1;
119 }
120 1 print STDERR "$prnode: $msg\n";
121 1 exit 2;
122 }
123 }
124 13 exit 0;
125 }
126  
127 0 ▶sub help {
Subroutine help: not called CC=1 SCAR=6.9
128 0 print << 'EOI';
129  
130 snag - capture, without clobbering, a file or directory version
131  
132 Basic usage:
133 snag <name> if <name> doesn't exist, create as a file
134 snag <name>/ if <name> doesn't exist, create as a directory
135  
136 Version-aware usage:
137 snag --lshigh <name>[/] list highest existing version of <name>
138 snag --lslow <name>[/] list lowest existing version of <name>
139 snag --mknext <name>[/] create next highest unused version of <name>
140 snag --mknextcopy <name> like --mknext (files only) followed by a copy
141  
142 A version number is just a terminal digit string in <name> (default "1").
143 That string's value becomes the next version number if no numbered versions
144 exist, and its length is the minimum width of the next version number
145 string. See "snag --man" for full documentation.
146  
147 EOI
148 0 return 1;
149 }
150  
151 __END__
« lib/File/Value.pm ↑ Summary