| 1 | package Statocles::Deploy::Git; | ||||||||||||||||||||||||||||
| 2 | 3 | our $VERSION = '0.099'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Deploy a site to a Git repository | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Statocles::Base 'Class'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | extends 'Statocles::Deploy::File'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Git::Repository; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | #pod =attr path | ||||||||||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||||||||||
| 12 | #pod The path to deploy to. Must be the root of the Git repository, or a directory | ||||||||||||||||||||||||||||
| 13 | #pod inside of the Git repository. | ||||||||||||||||||||||||||||
| 14 | #pod | ||||||||||||||||||||||||||||
| 15 | #pod =attr branch | ||||||||||||||||||||||||||||
| 16 | #pod | ||||||||||||||||||||||||||||
| 17 | #pod The Git branch to deploy to. Defaults to "main". If you're building a Github Pages | ||||||||||||||||||||||||||||
| 18 | #pod site for a project, you probably want to use the "gh-pages" branch. | ||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||
| 20 | #pod =cut | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has branch => ( | ||||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||
| 24 | isa => Str, | ||||||||||||||||||||||||||||
| 25 | 4 | ▶ | default => sub { 'main' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 3 | ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | #pod =attr remote | ||||||||||||||||||||||||||||
| 29 | #pod | ||||||||||||||||||||||||||||
| 30 | #pod The name of the remote to deploy to. Defaults to 'origin'. | ||||||||||||||||||||||||||||
| 31 | #pod | ||||||||||||||||||||||||||||
| 32 | #pod =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | has remote => ( | ||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||
| 36 | isa => Str, | ||||||||||||||||||||||||||||
| 37 | 7 | ▶ | default => sub { 'origin' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 3 | ); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | #pod =method deploy | ||||||||||||||||||||||||||||
| 41 | #pod | ||||||||||||||||||||||||||||
| 42 | #pod my @paths = $deploy->deploy( $source_path, %options ); | ||||||||||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||||||||||
| 44 | #pod Deploy the site, copying from the given source path. | ||||||||||||||||||||||||||||
| 45 | #pod | ||||||||||||||||||||||||||||
| 46 | #pod Possible options are: | ||||||||||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||||||||||
| 48 | #pod =over 4 | ||||||||||||||||||||||||||||
| 49 | #pod | ||||||||||||||||||||||||||||
| 50 | #pod =item clean | ||||||||||||||||||||||||||||
| 51 | #pod | ||||||||||||||||||||||||||||
| 52 | #pod Remove all the current contents of the deploy directory before copying the | ||||||||||||||||||||||||||||
| 53 | #pod new content. | ||||||||||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||||||||||
| 55 | #pod =item message | ||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||
| 57 | #pod An optional commit message to use. Defaults to a generic message. | ||||||||||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||||||||||
| 59 | #pod =back | ||||||||||||||||||||||||||||
| 60 | #pod | ||||||||||||||||||||||||||||
| 61 | #pod =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 14 | ▶ | around 'deploy' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||
| 64 | 14 | my ( $orig, $self, $source_path, %options ) = @_; | |||||||||||||||||||||||||||
| 65 | 14 | $source_path = Path->coercion->( $source_path ); | |||||||||||||||||||||||||||
| 66 | 14 | my $deploy_dir = $self->path; | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # Find the repository root | ||||||||||||||||||||||||||||
| 69 | 14 | my $root = Path::Tiny->new( "$deploy_dir" ); # clone | |||||||||||||||||||||||||||
| 70 | 14 | ▶ | until ( $root->child( '.git' )->exists || $root->is_rootdir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$root->child(".git")->exists or $root->is_rootdir
MC/DC100%2/2
$root->child(".git")->exists or $root->is_rootdir
$root->child(".git")->exists
$root->is_rootdir
Truth table100%3/3
A or B
A= $root->child(".git")->existsB= $root->is_rootdir
| |||||||||||||||||||||||||||||
| 71 | 3 | $root = $root->parent; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 14 | ▶ | if ( !$root->child( '.git' )->exists ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 1 | die qq{Deploy path "$deploy_dir" is not in a git repository\n}; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 13 | my $rel_path = $deploy_dir->relative( $root ); | |||||||||||||||||||||||||||
| 77 | #; say "Relative: $rel_path"; | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 13 | my $git = Git::Repository->new( work_tree => "$root" ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 13 | my $current_branch = _git_current_branch( $git ); | |||||||||||||||||||||||||||
| 82 | 13 | ▶ | if ( !$current_branch ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | die qq{Repository has no branches. Please create a commit before deploying\n}; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # Switch to the right branch | ||||||||||||||||||||||||||||
| 87 | 12 | ▶ | if ( !_git_has_branch( $git, $self->branch ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | #; say "Creating new branch: " . $self->branch; | ||||||||||||||||||||||||||||
| 89 | # Create a new, orphan branch | ||||||||||||||||||||||||||||
| 90 | # Orphan branches were introduced in git 1.7.2 | ||||||||||||||||||||||||||||
| 91 | 4 | $self->site->log->info( sprintf 'Creating deploy branch "%s"', $self->branch ); | |||||||||||||||||||||||||||
| 92 | 4 | $self->_run( $git, checkout => '--orphan', $self->branch ); | |||||||||||||||||||||||||||
| 93 | 4 | $self->_run( $git, 'rm', '-r', '-f', '.' ); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||||||||
| 96 | #; say "Switching branches to " . $self->branch; | ||||||||||||||||||||||||||||
| 97 | 8 | $self->_run( $git, checkout => $self->branch ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 12 | ▶ | if ( $options{ clean } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 2 | ▶ | if ( $current_branch eq $self->branch ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | die "--clean on the same branch as deploy will destroy all content. Stopping.\n"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 1 | $self->site->log->info( sprintf 'Cleaning old content in branch "%s"', $self->branch ); | |||||||||||||||||||||||||||
| 105 | 1 | $self->_run( $git, 'rm', '-r', '-f', '.' ); | |||||||||||||||||||||||||||
| 106 | 1 | delete $options{ clean }; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # Copy the files | ||||||||||||||||||||||||||||
| 110 | 11 | $self->$orig( $source_path, %options ); | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # Check to see which files were changed | ||||||||||||||||||||||||||||
| 113 | # --porcelain was added in 1.7.0 | ||||||||||||||||||||||||||||
| 114 | 11 | my @status_lines = $git->run( | |||||||||||||||||||||||||||
| 115 | status => '--porcelain', '--ignore-submodules', '--untracked-files', | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 11 | my %in_status; | |||||||||||||||||||||||||||
| 119 | 11 | for my $line ( @status_lines ) { | |||||||||||||||||||||||||||
| 120 | 34 | my ( $status, $path ) = $line =~ /^\s*(\S+)\s+(.+)$/; | |||||||||||||||||||||||||||
| 121 | 34 | $in_status{ $path } = $status; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | #; use Data::Dumper; | ||||||||||||||||||||||||||||
| 125 | #; say Dumper \%in_status; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Commit the files | ||||||||||||||||||||||||||||
| 128 | 27 | my @files = map { $_->[0] } | |||||||||||||||||||||||||||
| 129 | 31 | grep { $source_path->child( $_->[1] )->exists } | |||||||||||||||||||||||||||
| 130 | 11 | map { [ $_, Path::Tiny->new( $_ )->relative( $rel_path ) ] } | |||||||||||||||||||||||||||
| 131 | keys %in_status; | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | #; say "Files to commit: " . join "; ", @files; | ||||||||||||||||||||||||||||
| 134 | 11 | ▶ | if ( @files ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 9 | $self->site->log->info( sprintf 'Deploying %d changed files', scalar @files ); | |||||||||||||||||||||||||||
| 136 | 9 | $self->_run( $git, add => @files ); | |||||||||||||||||||||||||||
| 137 | 9 | ▶ | $self->_run( $git, commit => -m => $options{message} || "Site update" ); | ||||||||||||||||||||||||||
|
Condition100%2/2
$options{'message'} || 'Site update'
MC/DC100%1/1
$options{'message'} || 'Site update'
$options{'message'}
Truth table100%2/2
A
A= $options{'message'}
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | else { | ||||||||||||||||||||||||||||
| 140 | 2 | $self->site->log->warn( 'No files changed' ); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 11 | ▶ | if ( _git_has_remote( $git, $self->remote ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 9 | $self->_run( $git, push => $self->remote => $self->branch ); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | else { | ||||||||||||||||||||||||||||
| 147 | 2 | $self->site->log->warn( | |||||||||||||||||||||||||||
| 148 | sprintf 'Git remote "%s" does not exist. Not pushing.', $self->remote, | ||||||||||||||||||||||||||||
| 149 | ); | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # Tidy up | ||||||||||||||||||||||||||||
| 153 | 11 | $self->_run( $git, checkout => $current_branch ); | |||||||||||||||||||||||||||
| 154 | 3 | }; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Run the given git command on the given git repository, logging the | ||||||||||||||||||||||||||||
| 157 | # command for those running in debug mode | ||||||||||||||||||||||||||||
| 158 | 55 | ▶ | sub _run { | ||||||||||||||||||||||||||
|
Subroutine
_run: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 55 | my ( $self, $git, @args ) = @_; | |||||||||||||||||||||||||||
| 160 | 55 | $self->site->log->debug( "Running git command: " . join " ", @args ); | |||||||||||||||||||||||||||
| 161 | 55 | return _git_run( $git, @args ); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 171 | ▶ | sub _git_run { | ||||||||||||||||||||||||||
|
Subroutine
_git_run: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 165 | 171 | my ( $git, @args ) = @_; | |||||||||||||||||||||||||||
| 166 | 171 | my $cmdline = join " ", 'git', @args; | |||||||||||||||||||||||||||
| 167 | 171 | my $cmd = $git->command( @args ); | |||||||||||||||||||||||||||
| 168 | 171 | ▶ | my $stdout = join( "\n", readline( $cmd->stdout ) ) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
join("\n", readline $cmd->stdout) // ""
MC/DC0%0/1
join("\n", readline $cmd->stdout) // ""
join("\n", readline $cmd->stdout)
Truth table50%1/2
A
A= join("\n", readline $cmd->stdout)
| |||||||||||||||||||||||||||||
| 169 | 171 | ▶ | my $stderr = join( "\n", readline( $cmd->stderr ) ) // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
join("\n", readline $cmd->stderr) // ""
MC/DC0%0/1
join("\n", readline $cmd->stderr) // ""
join("\n", readline $cmd->stderr)
Truth table50%1/2
A
A= join("\n", readline $cmd->stderr)
| |||||||||||||||||||||||||||||
| 170 | 171 | $cmd->close; | |||||||||||||||||||||||||||
| 171 | 171 | my $exit = $cmd->exit; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 171 | ▶ | if ( $exit ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | die "git $args[0] exited with $exit\n\n-- CMD --\n$cmdline\n\n-- STDOUT --\n$stdout\n\n-- STDERR --\n$stderr\n"; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 171 | return $cmd->exit; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 13 | ▶ | sub _git_current_branch { | ||||||||||||||||||||||||||
|
Subroutine
_git_current_branch: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 181 | 13 | my ( $git ) = @_; | |||||||||||||||||||||||||||
| 182 | 13 | my @branches = map { s/^\*\s+//; $_ } grep { /^\*/ } $git->run( 'branch' ); | |||||||||||||||||||||||||||
| 183 | 13 | return $branches[0]; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 12 | ▶ | sub _git_has_branch { | ||||||||||||||||||||||||||
|
Subroutine
_git_has_branch: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 187 | 12 | my ( $git, $branch ) = @_; | |||||||||||||||||||||||||||
| 188 | 12 | return !!grep { $_ eq $branch } map { s/^[\*\s]\s+//; $_ } $git->run( 'branch' ); | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 11 | ▶ | sub _git_has_remote { | ||||||||||||||||||||||||||
|
Subroutine
_git_has_remote: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 192 | 11 | my ( $git, $remote ) = @_; | |||||||||||||||||||||||||||
| 193 | 11 | return !!grep { $_ eq $remote } map { s/^[\*\s]\s+//; $_ } $git->run( 'remote' ); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 2 | ▶ | sub _git_version { | ||||||||||||||||||||||||||
|
Subroutine
_git_version: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 197 | 2 | my $output = `git --version`; | |||||||||||||||||||||||||||
| 198 | 2 | my ( $git_version ) = $output =~ /git version (\d+[.]\d+[.]\d+)/; | |||||||||||||||||||||||||||
| 199 | 2 | ▶ | return unless $git_version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 2 | my $v = sprintf '%i.%03i%03i', split /[.]/, $git_version; | |||||||||||||||||||||||||||
| 201 | 2 | return $v; | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 3 | 1; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | __END__ | ||||||||||||||||||||||||||||