CLI/Archive.pm

filter: Statementstmt 94.8% 37 / 39 Branchbran 75.0% 12 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 1 totaltotal 88.5% 54 / 61 CC 10 SCAR 20.4
CC
10
Cov
89%
build
10.3
BEGIN
1.0
BEGIN
1.0
CRAP 7.7
« CLI/Util.pm ↑ Summary CLI/Client.pm »
1 # SPDX-FileCopyrightText: SUSE LLC
2 # SPDX-License-Identifier: GPL-2.0-or-later
3  
4 package Cavil::CLI::Archive;
5 7 ▶use Mojo::Base -base, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use Cavil::CLI::Util qw(have_tool md5_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Mojo::File qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 7 has 'dir';
11 5 ▶has excludes => sub { [] }; # extra tar exclude patterns (from --exclude-path and .cavilignore)
Subroutine __ANON__: called CC=1 SCAR=0.0
12 7 has respect_gitignore => 0;
13  
14 # Reproducible archive: identical content must produce identical bytes, so the same tree re-checked hashes the
15 # same and Cavil dedups it instead of opening a second review. Normalize the entry order and the metadata that
16 # otherwise varies between checkouts (mtimes, uid/gid), and use the GNU format to avoid pax headers that would
17 # smuggle in atime/ctime. GNU tar's own gzip is deterministic once these are set.
18 7 my @REPRODUCIBLE = qw(--format=gnu --sort=name --mtime=@0 --owner=0 --group=0 --numeric-owner);
19  
20 # Pack the working tree into a gzip tarball and return its MD5. The tree is taken as it sits on disk, including
21 # vendored subcomponents (node_modules and the like) that .gitignore usually hides but a full legal review must
22 # cover; only .git and explicit excludes are dropped. --respect-gitignore is opt-in for the leaner case.
23 16 ▶sub build ($self, $out) {
Subroutine build: called CC=10 SCAR=23.4
Pod undocumented
24 16 my $dir = path($self->dir);
25 16 ▶ die "Not a directory: $dir\n" unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 15 16
26 15 ▶ die "cavil-cli needs the 'tar' command, but it was not found in your PATH\n" unless have_tool('tar');
Branch: 1 branch
BranchTrueFalseTotal
unless have_tool("tar") 1 14 15
27  
28 14 my @cmd = ('tar', @REPRODUCIBLE, '-czf', "$out", '--exclude=.git');
29 14 my $ignore = $dir->child('.cavilignore');
30 14 ▶ push @cmd, "--exclude-from=$ignore" if -f $ignore;
Branch: 1 branch
BranchTrueFalseTotal
if -f $ignore 5 9 14
31 14 push @cmd, "--exclude=$_" for @{$self->excludes};
32  
33 # Honour .gitignore through git itself: tar's own ignore handling does not understand full gitignore syntax
34 # (a trailing-slash directory pattern, for one), so it would silently keep files git means to hide.
35 14 ▶ if ($self->respect_gitignore) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->respect_gitignore) { } 1 13 14
36 1 ▶ die "cavil-cli needs the 'git' command for --respect-gitignore, but it was not found in your PATH\n"
Branch: 1 branch
BranchTrueFalseTotal
unless have_tool("git") 0 1 1
37 unless have_tool('git');
38 1 my $q = quotemeta "$dir";
39 1 my @files = split /\0/, `git -C $q ls-files -z --cached --others --exclude-standard`;
40 1 ▶ die "--respect-gitignore needs a git repository in $dir\n" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 1 1
41 1 my $list = path("$out.files");
42 1 $list->spew(join "\0", @files);
43 1 push @cmd, '-C', "$dir", '--null', "--files-from=$list";
44 1 my $status = system(@cmd);
45 1 $list->remove;
46 0 ▶ die "Failed to package $dir (tar exited @{[$status >> 8]})\n" if $status != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $status != 0 0 1 1
47 }
48 else {
49 13 push @cmd, '-C', "$dir", '.';
50 0 ▶ die "Failed to package $dir (tar exited @{[$? >> 8]})\n" if system(@cmd) != 0;
Branch: 1 branch
BranchTrueFalseTotal
if system(@cmd) != 0 0 13 13
51 }
52  
53 14 return md5_file($out);
54 }
55  
56 7 1;
« CLI/Util.pm ↑ Summary CLI/Client.pm »