| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 36 | 1 | ▶ | die "cavil-cli needs the 'git' command for --respect-gitignore, but it was not found in your PATH\n" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 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
| |||||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 14 | return md5_file($out); | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 7 | 1; | |||||||||