File Coverage

lib/App/gh/Command/Update.pm
Criterion Covered Total %
statement 15 36 41.6
branch 0 8 0.0
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 20 51 39.2


line stmt bran cond sub pod time code
1             package App::gh::Command::Update;
2 1     1   1745 use warnings;
  1         2  
  1         37  
3 1     1   5 use strict;
  1         2  
  1         37  
4 1     1   5 use base qw(App::gh::Command);
  1         2  
  1         189  
5 1     1   8 use App::gh;
  1         2  
  1         43  
6 1         655 use App::gh::Utils qw(
7             info
8             error
9             build_git_remote_command
10             git_current_branch
11 1     1   6 );
  1         2  
12              
13             =head1 NAME
14              
15             App::gh::Command::Update - remote update --prune
16              
17             =head1 DESCRIPTION
18              
19             Simply run git remote update --prune , git pull --all , then push back to
20             writable remotes.
21              
22             =cut
23              
24             sub run {
25 0     0 0   my $self = shift;
26 0           my @remotes = @_;
27              
28 0 0         unless ( -d ".git" ) {
29 0           die "Not a repository";
30             }
31              
32 0           info "Running remote update with prune";
33 0           my @cmds = build_git_remote_command('update',{ prune => 1 });
34 0 0         system(@cmds) == 0
35             or die error "system @cmds failed: $?";
36              
37 0           my $diff = qx(git diff);
38 0           chomp($diff);
39 0 0         die error("Can not update, you have uncommitted changes.") if $diff;
40              
41 0           my $current_head = git_current_branch;
42 0 0         if( @remotes ) {
43 0           for my $remote (@remotes) {
44 0           info "Pull and rebase from $remote/$current_head...";
45 0           qx{git pull --rebase $remote $current_head};
46             }
47             }
48             else {
49 0           my @lines = split /\n/,qx{ git remote -v | grep '(fetch)'};
50 0           for my $line ( @lines ) {
51 0           my ( $remote, $uri, $type) = ($line =~ m{^(\w+)\s+(\S+)\s+\((\w+)\)} );
52 0           info "Pull and rebase from $remote ...";
53 0           qx{git pull --rebase $remote $current_head};
54              
55             # if( $uri =~ /^git\@github\.com/ ) {
56             # info "Pushing changes to $remote : $uri";
57             # qx{ git push $remote };
58             # }
59             }
60             }
61 0           info "Done";
62             }
63              
64             1;