| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::gh::Utils; | 
| 2 | 3 |  |  | 3 |  | 28539 | use warnings; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 109 |  | 
| 3 | 3 |  |  | 3 |  | 16 | use strict; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 108 |  | 
| 4 | 3 |  |  | 3 |  | 17 | use base qw(Exporter); | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 346 |  | 
| 5 | 3 |  |  | 3 |  | 3984 | use Term::ANSIColor; | 
|  | 3 |  |  |  |  | 35736 |  | 
|  | 3 |  |  |  |  | 386 |  | 
| 6 | 3 |  |  | 3 |  | 1235 | use URI; | 
|  | 3 |  |  |  |  | 7451 |  | 
|  | 3 |  |  |  |  | 232 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 3 |  |  | 3 |  | 18 | use constant debug => $ENV{DEBUG}; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 5569 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our @EXPORT = qw(_debug | 
| 12 |  |  |  |  |  |  | info | 
| 13 |  |  |  |  |  |  | error | 
| 14 |  |  |  |  |  |  | notice | 
| 15 |  |  |  |  |  |  | get_github_auth print_list | 
| 16 |  |  |  |  |  |  | ); | 
| 17 |  |  |  |  |  |  | our @EXPORT_OK = qw( | 
| 18 |  |  |  |  |  |  | generate_repo_uri | 
| 19 |  |  |  |  |  |  | git_current_branch | 
| 20 |  |  |  |  |  |  | run_git_fetch | 
| 21 |  |  |  |  |  |  | build_git_clone_command | 
| 22 |  |  |  |  |  |  | build_git_fetch_command | 
| 23 |  |  |  |  |  |  | build_git_remote_command | 
| 24 |  |  |  |  |  |  | dialog_yes_default | 
| 25 |  |  |  |  |  |  | ); | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub build_git_fetch_command; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # XXX: move this to logger....... orz | 
| 30 |  |  |  |  |  |  | sub _debug { | 
| 31 | 0 |  |  | 0 |  | 0 | print STDERR @_,"\n" if debug; | 
| 32 |  |  |  |  |  |  | } | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | sub prop_line { | 
| 35 | 0 |  |  | 0 | 0 | 0 | my ( $label, $value ) = @_; | 
| 36 | 0 |  |  |  |  | 0 | printf "%15s: %s\n", $label, $value; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub print_repo_info { | 
| 40 | 0 |  |  | 0 | 0 | 0 | my ( $class, $ret ) = @_; | 
| 41 | 0 |  |  |  |  | 0 | prop_line "Name" , $ret->{name}; | 
| 42 | 0 |  |  |  |  | 0 | prop_line "Description" , $ret->{description}; | 
| 43 | 0 |  |  |  |  | 0 | prop_line "Owner" , $ret->{owner}; | 
| 44 | 0 |  |  |  |  | 0 | prop_line "URL"   , $ret->{url}; | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 0 |  |  |  |  | 0 | prop_line "Watchers"   , $ret->{watchers}; | 
| 47 | 0 |  |  |  |  | 0 | prop_line "Forks"      , $ret->{forks}; | 
| 48 | 0 |  |  |  |  | 0 | prop_line "Open Issues"     , $ret->{open_issues}; | 
| 49 | 0 |  |  |  |  | 0 | prop_line "Created at" , $ret->{created_at}; | 
| 50 | 0 |  | 0 |  |  | 0 | prop_line "Pushed at"  , $ret->{pushed_at} || "never"; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 0 | 0 |  |  |  | 0 | prop_line "Parent"     , $ret->{parent} if( $ret->{parent} ); | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 0 | 0 |  |  |  | 0 | print ' ' x 15 . "* Is private\n"    if $ret->{private}; | 
| 55 | 0 | 0 |  |  |  | 0 | print ' ' x 15 . "* Has downloads\n" if $ret->{has_downloads}; | 
| 56 | 0 | 0 |  |  |  | 0 | print ' ' x 15 . "* Has issues\n"    if $ret->{has_issues}; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub print_list { | 
| 60 | 0 |  |  | 0 | 0 | 0 | my @lines = @_; | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 0 |  |  |  |  | 0 | my $column_w = 0; | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 0 | 0 |  |  |  | 0 | map { | 
| 65 | 0 |  |  |  |  | 0 | $column_w = length($_->[0]) if length($_->[0]) > $column_w ; | 
| 66 |  |  |  |  |  |  | } @lines; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 |  |  |  |  | 0 | my $screen_width = 92; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 |  |  |  |  | 0 | for my $arg ( @lines ) { | 
| 71 | 0 |  |  |  |  | 0 | my $title = shift @$arg; | 
| 72 | 0 |  |  |  |  | 0 | my $padding = int($column_w) - length( $title ); | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 0 | 0 | 0 |  |  | 0 | if ( $ENV{WRAP} && ( $column_w + 3 + length( join(" ",@$arg)) ) > $screen_width ) { | 
| 75 |  |  |  |  |  |  | # wrap description | 
| 76 | 0 |  |  |  |  | 0 | my $string = | 
| 77 |  |  |  |  |  |  | color('bold') . | 
| 78 |  |  |  |  |  |  | $title . | 
| 79 |  |  |  |  |  |  | color('reset') . | 
| 80 |  |  |  |  |  |  | " " x $padding . " - " . join(" ",@$arg) . "\n"; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  | 0 | $string =~ s/\n//g; | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  | 0 | my $cnt = 0; | 
| 85 | 0 |  |  |  |  | 0 | my $firstline = 1; | 
| 86 | 0 |  |  |  |  | 0 | my $tab = 4; | 
| 87 | 0 |  |  |  |  | 0 | my $wrapped = 0; | 
| 88 | 0 |  |  |  |  | 0 | while( $string =~ /(.)/g ) { | 
| 89 | 0 |  |  |  |  | 0 | $cnt++; | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 0 |  |  |  |  | 0 | my $c = $1; | 
| 92 | 0 |  |  |  |  | 0 | print $c; | 
| 93 |  |  |  |  |  |  |  | 
| 94 | 0 | 0 | 0 |  |  | 0 | if( $c =~ /[ \,]/ && $firstline && $cnt > $screen_width ) { | 
|  |  | 0 | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 95 | 0 |  |  |  |  | 0 | print "\n" . " " x ($column_w + 3 + $tab ); | 
| 96 | 0 |  |  |  |  | 0 | $firstline = 0; | 
| 97 | 0 |  |  |  |  | 0 | $cnt = 0; | 
| 98 | 0 |  |  |  |  | 0 | $wrapped = 1; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  | elsif( $c =~ /[ \,]/ && ! $firstline && $cnt > ($screen_width - $column_w) ) { | 
| 101 | 0 |  |  |  |  | 0 | print "\n" . " " x ($column_w + 3 + $tab ); | 
| 102 | 0 |  |  |  |  | 0 | $cnt = 0; | 
| 103 | 0 |  |  |  |  | 0 | $wrapped = 1; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  | } | 
| 106 | 0 |  |  |  |  | 0 | print "\n"; | 
| 107 | 0 | 0 |  |  |  | 0 | print "\n" if $wrapped; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | else { | 
| 110 | 0 |  |  |  |  | 0 | print color 'bold'; | 
| 111 | 0 |  |  |  |  | 0 | print $title; | 
| 112 | 0 |  |  |  |  | 0 | print color 'reset'; | 
| 113 | 0 |  |  |  |  | 0 | print " " x $padding; | 
| 114 | 0 |  |  |  |  | 0 | print " - "; | 
| 115 | 0 | 0 |  |  |  | 0 | $$arg[0] = ' ' unless $$arg[0]; | 
| 116 | 0 |  |  |  |  | 0 | print join " " , @$arg; | 
| 117 | 0 |  |  |  |  | 0 | print "\n"; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | } | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub error { | 
| 126 | 0 |  |  | 0 | 0 | 0 | my @msg = @_; | 
| 127 | 0 |  |  |  |  | 0 | print STDERR color 'red'; | 
| 128 | 0 |  |  |  |  | 0 | print STDERR join("\n", @msg), "\n"; | 
| 129 | 0 |  |  |  |  | 0 | print STDERR color 'reset'; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | sub info { | 
| 133 | 0 |  |  | 0 | 0 | 0 | my @msg = @_; | 
| 134 | 0 |  |  |  |  | 0 | print STDERR color 'green'; | 
| 135 | 0 |  |  |  |  | 0 | print STDERR join("\n", @msg), "\n"; | 
| 136 | 0 |  |  |  |  | 0 | print STDERR color 'reset'; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | sub notice { | 
| 140 | 0 |  |  | 0 | 0 | 0 | my @msg = @_; | 
| 141 | 0 |  |  |  |  | 0 | print STDERR color 'bold yellow'; | 
| 142 | 0 |  |  |  |  | 0 | print STDERR join("\n", @msg), "\n"; | 
| 143 | 0 |  |  |  |  | 0 | print STDERR color 'reset'; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | # | 
| 148 |  |  |  |  |  |  | # @param string $remote git remote name | 
| 149 |  |  |  |  |  |  | # @param hashref $options | 
| 150 |  |  |  |  |  |  | # @return string command output | 
| 151 |  |  |  |  |  |  | sub run_git_fetch { | 
| 152 | 0 |  |  | 0 | 0 | 0 | my @command = build_git_fetch_command @_; | 
| 153 | 0 |  |  |  |  | 0 | my $cmd = join ' ' , @command; | 
| 154 | 0 |  |  |  |  | 0 | my $result = qx($cmd); | 
| 155 | 0 |  |  |  |  | 0 | return $result; | 
| 156 |  |  |  |  |  |  | } | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | # | 
| 160 |  |  |  |  |  |  | # @param string $remote Git remote name | 
| 161 |  |  |  |  |  |  | # @param hashref $options | 
| 162 |  |  |  |  |  |  | # @return array command list | 
| 163 |  |  |  |  |  |  | sub build_git_fetch_command { | 
| 164 | 0 |  |  | 0 | 0 | 0 | my ($remote,$options) = (undef,{}); | 
| 165 | 0 | 0 |  |  |  | 0 | $remote = shift if ref($_[0]) ne 'HASH'; | 
| 166 | 0 | 0 |  |  |  | 0 | $options = shift if ref($_[0]) eq 'HASH'; | 
| 167 | 0 |  |  |  |  | 0 | my @command = qw(git fetch); | 
| 168 | 0 | 0 |  |  |  | 0 | push @command, $remote      if $remote; | 
| 169 | 0 | 0 |  |  |  | 0 | push @command, '--all'      if $options->{all}; | 
| 170 | 0 | 0 |  |  |  | 0 | push @command, '--multiple' if $options->{multiple}; | 
| 171 | 0 | 0 |  |  |  | 0 | push @command, '--tags'     if $options->{tags}; | 
| 172 | 0 | 0 |  |  |  | 0 | push @command, '--quiet'    if $options->{quiet}; | 
| 173 | 0 | 0 |  |  |  | 0 | push @command, '--verbose'  if $options->{verbose}; | 
| 174 | 0 | 0 | 0 |  |  | 0 | push @command, '--recurse-submodules=' | 
| 175 |  |  |  |  |  |  | . ($options->{submodules} || 'yes') | 
| 176 |  |  |  |  |  |  | if $options->{submodules}; | 
| 177 | 0 |  |  |  |  | 0 | return @command; | 
| 178 |  |  |  |  |  |  | } | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | sub build_git_remote_command { | 
| 181 | 2 |  |  | 2 | 0 | 1812 | my ($subcommand,@args,$options); | 
| 182 | 2 | 50 |  |  |  | 7 | $subcommand = shift if ! ref $subcommand; | 
| 183 |  |  |  |  |  |  |  | 
| 184 | 2 |  | 66 |  |  | 19 | push @args, shift(@_) while $_[0] && ! ref $_[0]; | 
| 185 | 2 | 50 |  |  |  | 6 | $options    = shift if ref $_[0] eq 'HASH'; | 
| 186 | 2 |  | 50 |  |  | 5 | $options    ||= {}; | 
| 187 |  |  |  |  |  |  |  | 
| 188 | 2 |  |  |  |  | 5 | my @command = qw(git remote); | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 2 | 50 |  |  |  | 5 | push @command, '--verbose' if $options->{verbose}; | 
| 191 | 2 | 50 |  |  |  | 4 | push @command, $subcommand if $subcommand; | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | # git remote update | 
| 194 | 2 | 100 |  |  |  | 10 | if( $subcommand =~ /update/ ) { | 
|  |  | 50 |  |  |  |  |  | 
| 195 | 1 | 50 |  |  |  | 3 | push @command, '--prune' if $options->{prune}; | 
| 196 |  |  |  |  |  |  | } | 
| 197 |  |  |  |  |  |  | elsif( $subcommand =~ /prune/ ) { | 
| 198 | 1 | 50 |  |  |  | 4 | push @command, '--dry-run' if $options->{dry_run}; | 
| 199 |  |  |  |  |  |  | } | 
| 200 | 2 | 100 |  |  |  | 4 | push @command, @args if @args; | 
| 201 | 2 |  |  |  |  | 8 | return @command; | 
| 202 |  |  |  |  |  |  | } | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | # | 
| 206 |  |  |  |  |  |  | # @param string $uri | 
| 207 |  |  |  |  |  |  | # @param hashref $options default { } | 
| 208 |  |  |  |  |  |  | # @return array command list | 
| 209 |  |  |  |  |  |  | sub build_git_clone_command { | 
| 210 | 0 |  |  | 0 | 0 |  | my $uri = shift;; | 
| 211 | 0 |  | 0 |  |  |  | my $options = shift || {}; | 
| 212 | 0 |  |  |  |  |  | my @command = qw(git clone); | 
| 213 | 0 | 0 |  |  |  |  | push @command, '--bare'                         if $options->{bare}; | 
| 214 | 0 | 0 |  |  |  |  | push @command, '--branch=' . $options->{branch} if $options->{branch}; | 
| 215 | 0 | 0 |  |  |  |  | push @command, '--quiet'                        if $options->{quiet}; | 
| 216 | 0 | 0 |  |  |  |  | push @command, '--mirror'                       if $options->{mirror}; | 
| 217 | 0 | 0 |  |  |  |  | push @command, '--recursive'                    if $options->{recursive}; | 
| 218 | 0 | 0 |  |  |  |  | push @command, '--origin=' . $options->{origin} if $options->{origin}; | 
| 219 | 0 | 0 |  |  |  |  | push @command, '--verbose' if $options->{verbose}; | 
| 220 | 0 |  |  |  |  |  | push @command, $uri; | 
| 221 | 0 |  |  |  |  |  | return @command; | 
| 222 |  |  |  |  |  |  | } | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | sub git_current_branch { | 
| 225 | 0 |  |  | 0 | 0 |  | my $ref = qx(git rev-parse --abbrev-ref HEAD); | 
| 226 | 0 |  |  |  |  |  | chomp($ref); | 
| 227 | 0 |  |  |  |  |  | return $ref; | 
| 228 |  |  |  |  |  |  | } | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  | # | 
| 231 |  |  |  |  |  |  | # @param string $user | 
| 232 |  |  |  |  |  |  | # @param string $repo | 
| 233 |  |  |  |  |  |  | # @param hashref $options | 
| 234 |  |  |  |  |  |  | # return string GitHub Clone URI | 
| 235 |  |  |  |  |  |  | sub generate_repo_uri { | 
| 236 | 0 |  |  | 0 | 0 |  | my ($user,$repo,$options) = @_; | 
| 237 |  |  |  |  |  |  |  | 
| 238 | 0 | 0 |  |  |  |  | $options->{protocol_ssh} = 1 | 
| 239 |  |  |  |  |  |  | if App::gh->config->github_id eq $user; | 
| 240 |  |  |  |  |  |  |  | 
| 241 | 0 | 0 | 0 |  |  |  | if( $options->{protocol_git} ) { | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
|  |  | 0 |  |  |  |  |  | 
| 242 | 0 |  |  |  |  |  | return sprintf( 'git://github.com/%s/%s.git', $user, $repo ); | 
| 243 |  |  |  |  |  |  | } | 
| 244 |  |  |  |  |  |  | elsif( $options->{protocol_ssh} || | 
| 245 |  |  |  |  |  |  | $options->is_mine($user, $repo) ) { | 
| 246 | 0 |  |  |  |  |  | return sprintf( 'git@github.com:%s/%s.git', $user, $repo ); | 
| 247 |  |  |  |  |  |  | } | 
| 248 |  |  |  |  |  |  | elsif( $options->{protocol_http} ) { | 
| 249 | 0 |  |  |  |  |  | return sprintf( 'http://github.com/%s/%s.git', $user , $repo ); | 
| 250 |  |  |  |  |  |  | } | 
| 251 |  |  |  |  |  |  | elsif( $options->{protocol_https}) { | 
| 252 | 0 |  |  |  |  |  | return sprintf( 'https://github.com/%s/%s.git', $user , $repo ); | 
| 253 |  |  |  |  |  |  | } | 
| 254 | 0 |  |  |  |  |  | return sprintf( 'git://github.com/%s/%s.git', $user, $repo ); | 
| 255 |  |  |  |  |  |  | } | 
| 256 |  |  |  |  |  |  |  | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | # | 
| 259 |  |  |  |  |  |  | # @param string $msg | 
| 260 |  |  |  |  |  |  | # @return boolean | 
| 261 |  |  |  |  |  |  | sub dialog_yes_default { | 
| 262 | 0 |  |  | 0 | 0 |  | my $msg = shift; | 
| 263 | 0 |  |  |  |  |  | local $|; | 
| 264 | 0 |  |  |  |  |  | print STDERR $msg; | 
| 265 | 0 |  |  |  |  |  | print STDERR ' (Y/n) '; | 
| 266 |  |  |  |  |  |  |  | 
| 267 | 0 |  |  |  |  |  | my $a = ; | 
| 268 | 0 |  |  |  |  |  | chomp $a; | 
| 269 | 0 | 0 |  |  |  |  | if($a =~ /n/) { | 
| 270 | 0 |  |  |  |  |  | return 0; | 
| 271 |  |  |  |  |  |  | } | 
| 272 | 0 | 0 |  |  |  |  | return 1 if $a =~ /y/; | 
| 273 | 0 |  |  |  |  |  | return 1; # default to Y | 
| 274 |  |  |  |  |  |  | } | 
| 275 |  |  |  |  |  |  |  | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | 1; |