| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::GitGot::Command::fork; | 
| 2 |  |  |  |  |  |  | our $AUTHORITY = 'cpan:GENEHACK'; | 
| 3 |  |  |  |  |  |  | $App::GitGot::Command::fork::VERSION = '1.339'; | 
| 4 |  |  |  |  |  |  | # ABSTRACT: fork a github repo | 
| 5 | 15 |  |  | 15 |  | 9747 | use 5.014; | 
|  | 15 |  |  |  |  | 66 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 15 |  |  | 15 |  | 109 | use autodie; | 
|  | 15 |  |  |  |  | 44 |  | 
|  | 15 |  |  |  |  | 320 |  | 
| 8 | 15 |  |  | 15 |  | 90525 | use Class::Load       'try_load_class'; | 
|  | 15 |  |  |  |  | 41 |  | 
|  | 15 |  |  |  |  | 1202 |  | 
| 9 | 15 |  |  | 15 |  | 140 | use Cwd; | 
|  | 15 |  |  |  |  | 35 |  | 
|  | 15 |  |  |  |  | 1100 |  | 
| 10 | 15 |  |  | 15 |  | 120 | use File::HomeDir::Tiny (); | 
|  | 15 |  |  |  |  | 38 |  | 
|  | 15 |  |  |  |  | 316 |  | 
| 11 | 15 |  |  | 15 |  | 106 | use Path::Tiny; | 
|  | 15 |  |  |  |  | 45 |  | 
|  | 15 |  |  |  |  | 1018 |  | 
| 12 | 15 |  |  | 15 |  | 109 | use Types::Standard -types; | 
|  | 15 |  |  |  |  | 35 |  | 
|  | 15 |  |  |  |  | 274 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 15 |  |  | 15 |  | 72323 | use App::GitGot -command; | 
|  | 15 |  |  |  |  | 43 |  | 
|  | 15 |  |  |  |  | 583 |  | 
| 15 | 15 |  |  | 15 |  | 6525 | use App::GitGot::Repo::Git; | 
|  | 15 |  |  |  |  | 46 |  | 
|  | 15 |  |  |  |  | 713 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 15 |  |  | 15 |  | 108 | use Moo; | 
|  | 15 |  |  |  |  | 41 |  | 
|  | 15 |  |  |  |  | 117 |  | 
| 18 |  |  |  |  |  |  | extends 'App::GitGot::Command'; | 
| 19 | 15 |  |  | 15 |  | 8582 | use namespace::autoclean; | 
|  | 15 |  |  |  |  | 48 |  | 
|  | 15 |  |  |  |  | 148 |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | sub options { | 
| 22 | 4 |  |  | 4 | 0 | 413 | my( $class , $app ) = @_; | 
| 23 |  |  |  |  |  |  | return ( | 
| 24 | 4 |  |  |  |  | 48 | [ 'noclone|n'     => 'If set, do not check out a local working copy of the forked repo' ] , | 
| 25 |  |  |  |  |  |  | [ 'noremoteadd|N' => 'If set, do not add the forked repo as the "upstream" repo in the new working copy' ] , | 
| 26 |  |  |  |  |  |  | ); | 
| 27 |  |  |  |  |  |  | } | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | sub _execute { | 
| 30 | 4 |  |  | 4 |  | 12 | my( $self, $opt, $args ) = @_; | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 4 | 50 | 0 |  |  | 19 | try_load_class('Net::GitHub') or | 
| 33 |  |  |  |  |  |  | say "Sorry, Net::GitHub is required for 'got fork'. Please install it." | 
| 34 |  |  |  |  |  |  | and exit(1); | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 4 | 100 | 33 |  |  | 336 | my $github_url = shift @$args | 
| 37 |  |  |  |  |  |  | or say STDERR "ERROR: Need the URL of a repo to fork!" and exit(1); | 
| 38 |  |  |  |  |  |  |  | 
| 39 | 3 |  |  |  |  | 11 | my( $owner , $repo_name ) = _parse_github_url( $github_url ); | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 1 |  |  |  |  | 7 | my %gh_args = _parse_github_identity(); | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 1 | 50 |  |  |  | 37 | say "Forking '$owner/$repo_name'..." unless $self->quiet; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 1 |  |  |  |  | 60 | my $resp = Net::GitHub->new( %gh_args )->repos->create_fork( $owner , $repo_name ); | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 1 |  |  |  |  | 5627 | my $path = cwd() . "/$repo_name"; | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | my $new_repo = App::GitGot::Repo::Git->new({ entry => { | 
| 50 |  |  |  |  |  |  | name => $repo_name , | 
| 51 |  |  |  |  |  |  | path => $path , | 
| 52 |  |  |  |  |  |  | repo => $resp->{ssh_url} , | 
| 53 | 1 |  |  |  |  | 131 | type => 'git' , | 
| 54 |  |  |  |  |  |  | }}); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 1 | 50 |  |  |  | 150 | if ( ! $self->opt->noclone ) { | 
| 57 | 0 | 0 |  |  |  | 0 | say "Cloning into $path" unless $self->quiet; | 
| 58 | 0 |  |  |  |  | 0 | $new_repo->clone( $resp->{ssh_url} ); | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 | 0 |  |  |  | 0 | if ( ! $self->opt->noremoteadd ) { | 
| 61 | 0 | 0 |  |  |  | 0 | say "Adding '$github_url' as remote 'upstream'..." | 
| 62 |  |  |  |  |  |  | unless $self->quiet; | 
| 63 | 0 |  |  |  |  | 0 | $new_repo->remote( add => upstream => $github_url ); | 
| 64 |  |  |  |  |  |  | } | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 1 |  |  |  |  | 65 | $self->add_repo( $new_repo ); | 
| 68 | 1 |  |  |  |  | 154 | $self->write_config; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | sub _parse_github_identity { | 
| 72 | 1 |  |  | 1 |  | 25 | my $file = path( File::HomeDir::Tiny::home() , '.github-identity' ); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 1 | 50 | 0 |  |  | 76 | $file->exists or | 
| 75 |  |  |  |  |  |  | say STDERR "ERROR: Can't find $file" and exit(1); | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 1 |  |  |  |  | 36 | my @lines = $file->lines; | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 1 |  |  |  |  | 240 | my %config; | 
| 80 | 1 |  |  |  |  | 5 | foreach ( @lines ) { | 
| 81 | 2 |  |  |  |  | 5 | chomp; | 
| 82 | 2 | 50 |  |  |  | 7 | next unless $_; | 
| 83 | 2 |  |  |  |  | 13 | my( $k , $v ) = split /\s/; | 
| 84 | 2 |  |  |  |  | 10 | $config{$k} = $v; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 1 | 50 | 33 |  |  | 19 | if ( defined $config{access_token} ) { | 
|  |  | 50 |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | return ( access_token => $config{access_token} ) | 
| 89 | 0 |  |  |  |  | 0 | } | 
| 90 |  |  |  |  |  |  | elsif ( defined $config{pass} and defined $config{user} ) { | 
| 91 |  |  |  |  |  |  | return ( login => $config{user} , pass => $config{pass} ) | 
| 92 | 1 |  |  |  |  | 10 | } | 
| 93 |  |  |  |  |  |  | else { | 
| 94 | 0 | 0 |  |  |  | 0 | say STDERR "Couldn't parse password or access_token info from ~/.github-identity" | 
| 95 |  |  |  |  |  |  | and exit(1); | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | sub _parse_github_url { | 
| 100 | 3 |  |  | 3 |  | 9 | my $url = shift; | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 3 | 100 | 33 |  |  | 44 | my( $owner , $repo ) = $url =~ m|/github.com/([^/]+)/([^/]+?)(?:\.git)?$| | 
| 103 |  |  |  |  |  |  | or say STDERR "ERROR: Can't parse '$url'.\nURL needs to be of the form 'github.com/OWNER/REPO'.\n" | 
| 104 |  |  |  |  |  |  | and exit(1); | 
| 105 |  |  |  |  |  |  |  | 
| 106 | 1 |  |  |  |  | 5 | return( $owner , $repo ); | 
| 107 |  |  |  |  |  |  | } | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | 1; | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | __END__ |