File Coverage

lib/App/gh/Command/Pull.pm
Criterion Covered Total %
statement 15 52 28.8
branch 0 12 0.0
condition 0 4 0.0
subroutine 5 7 71.4
pod 0 2 0.0
total 20 77 25.9


line stmt bran cond sub pod time code
1             package App::gh::Command::Pull;
2 1     1   1214 use warnings;
  1         2  
  1         32  
3 1     1   5 use strict;
  1         1  
  1         34  
4 1     1   5 use base qw(App::gh::Command);
  1         2  
  1         81  
5 1     1   6 use App::gh;
  1         1  
  1         23  
6 1     1   6 use App::gh::Utils;
  1         19  
  1         568  
7              
8             =head1 NAME
9              
10             App::gh::Command::Pull - pull changes from other forks.
11              
12             =head1 USAGE
13              
14             First you show up all fork network:
15              
16             $ gh network
17              
18             Then you can pull changes from one:
19              
20             $ gh pull [id] (from branch) (to branch)
21              
22             For example:
23              
24             $ gh pull gugod
25              
26             This will create a gugod-master branch:
27              
28             =head1 OPTIONS
29              
30             --branch
31              
32             create tracked branch for fork.
33              
34             --merge
35              
36             merge changes from fork.
37              
38             =head3 Github Steps:
39              
40             Step 1: Check out a new branch to test the changes — run this from your project directory
41              
42             git checkout -b chocolateboy-optional_dep_and_warnings_fixes master
43              
44             Step 2: Bring in chocolateboy's changes and test
45              
46             git pull http://github.com/chocolateboy/App-gh.git optional_dep_and_warnings_fixes
47              
48             Step 3: Merge the changes and update the server
49              
50             git checkout master
51             git merge chocolateboy-optional_dep_and_warnings_fixes
52             git push origin master
53              
54             =cut
55              
56             sub options { (
57 0     0 0   "m|merge" => "merge",
58             "b|branch" => "branch",
59             "verbose" => "verbose",
60             "ssh" => "protocol_ssh", # git@github.com:c9s/repo.git
61             "http" => "protocol_http", # http://github.com/c9s/repo.git
62             "https" => "protocol_https", # https://github.com/c9s/repo.git
63             "git|ro" => "protocol_git" # git://github.com/c9s/repo.git
64             ) }
65              
66             sub run {
67 0     0 0   my ( $self, $acc, $from_branch, $to_branch ) = @_;
68              
69 0 0         unless ( $acc ) {
70 0           print "Usage: \n";
71 0           print " gh pull [userid] (from branch) (to branch)\n\n";
72              
73              
74              
75 0           my $origin_url = qx( git remote -v | grep origin | grep fetch );
76 0           my ($userid,$repo) = ($origin_url =~ m{:(\w+)/(.*?)\.git$});
77              
78 0           print "Available forks:\n";
79              
80             # XXX: refactor this ... XD
81 0           my $objs = App::gh->api->request(GET => qq(repos/show/$userid/$repo/network));
82 0           my $networks = $objs->{network};
83 0           for my $net ( @$networks ) {
84 0           print sprintf( "% 20s - watchers(%d) forks(%d)\n"
85             , $net->{owner} . '/' . $net->{name}
86             , $net->{watchers}
87             , $net->{forks}
88             );
89             }
90 0           print "\n";
91 0           return;
92             }
93              
94 0   0       $from_branch ||= 'master';
95 0   0       $to_branch ||= 'master';
96              
97 0 0         if( qx(git diff) ) {
98 0           die "Your repository is dirty!\n";
99             }
100              
101 0 0         die "git config not found." if ! -e ".git/config" ;
102              
103 0           my $remote_name = $acc;
104 0           my $fork_branch_name = "$acc-$from_branch";
105 0           my $current_repo = $self->get_current_repo();
106 0           my $fork_uri = $self->gen_uri( $acc , $current_repo );
107              
108 0 0         unless( qx(git remote | grep $remote_name ) ) {
109 0           print "Adding remote [$remote_name] for [$fork_uri]\n";
110 0           qx(git remote add $remote_name $fork_uri);
111             }
112              
113 0           print "Fetching $acc ...\n";
114 0           print qx(git fetch $acc);
115              
116              
117 0 0         if( $self->{merge} ) {
118 0           print "Checking out $to_branch ...\n";
119 0           qx(git checkout $to_branch);
120              
121 0           print "Merging changes from [$acc/$from_branch] to $to_branch\n";
122 0           print qx(git merge $acc/$from_branch);
123             }
124              
125 0 0         if( $self->{branch} ) {
126 0           print "Creating track branch for $acc/$from_branch\n";
127 0           print qx(git checkout --track -b $fork_branch_name $acc/$from_branch);
128             }
129              
130 0           print "Done\n";
131             }
132              
133             1;