| 1 | package GitHub::WebHook::Clone; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use v5.10; | ||
| 5 | |||
| 6 | use parent 'GitHub::WebHook::Run'; | ||
| 7 | |||
| 8 | our $VERSION = '0.10'; | ||
| 9 | |||
| 10 | sub new { | ||
| 11 | my ($class, %config) = @_; | ||
| 12 | |||
| 13 | my $branch = $config{branch} || 'master'; | ||
| 14 | my $work_tree = $config{work_tree} // die "missing work_tree parameter"; | ||
| 15 | my $git_dir = $config{git_dir} // "$work_tree/.git"; | ||
| 16 | |||
| 17 | my @git = $git_dir eq "$work_tree/.git" | ||
| 18 | ? ('git') | ||
| 19 | : ('git',"--git-dir=$git_dir"); | ||
| 20 | |||
| 21 | bless { | ||
| 22 | cmd => sub { | ||
| 23 | my ($payload, $event, $delivery, $log) = @_; | ||
| 24 | my $origin = $_[0]->{repository}->{clone_url} or die 'missing clone_url'; | ||
| 25 | if ( -d $git_dir ) { | ||
| 26 | chdir $work_tree; | ||
| 27 | return [@git,'pull',$origin,$branch]; | ||
| 28 | } else { | ||
| 29 | return [@git,'clone',$origin,'-b',$branch,$work_tree]; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | }, $class; | ||
| 33 | } | ||
| 34 | |||
| 35 | 1; | ||
| 36 | __END__ |