| 1 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | package Git::Helpers; | ||||||||||||
| 5 | 1 | our $VERSION = '1.000000'; | |||||||||||
| 6 | 1 | ▶ | use Carp qw( croak ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use Capture::Tiny 'capture_stderr'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use File::pushd qw( pushd ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 1 | ▶ | use Git::Sub; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 1 | use Sub::Exporter -setup => { | |||||||||||
| 11 | exports => [ | ||||||||||||
| 12 | 'checkout_root', | ||||||||||||
| 13 | 'current_branch_name', | ||||||||||||
| 14 | 'https_remote_url', | ||||||||||||
| 15 | 'ignored_files', | ||||||||||||
| 16 | 'is_inside_work_tree', | ||||||||||||
| 17 | 'remote_url', | ||||||||||||
| 18 | ] | ||||||||||||
| 19 | 1 | ▶ | }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 1 | ▶ | use Try::Tiny qw( catch try ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 1 | ▶ | use URI (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 1 | ▶ | use URI::FromHash qw( uri ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 1 | ▶ | use URI::Heuristic qw(uf_uristr); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 1 | ▶ | use URI::git (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | |||||||||||||
| 26 | sub checkout_root { | ||||||||||||
| 27 | 3 | ▶ | my $dir = shift; | ||||||||||
|
Subroutine
checkout_root: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 28 | |||||||||||||
| 29 | 3 | my ( $new_dir, $root ); | |||||||||||
| 30 | 3 | ▶ | $new_dir = pushd($dir) if $dir; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 3 | ▶ | $dir ||= '.'; | ||||||||||
|
Condition100%2/2
$dir ||= "."
MC/DC100%1/1
$dir ||= "."
$dir
Truth table100%2/2
A
A= $dir
| |||||||||||||
| 32 | |||||||||||||
| 33 | # the exception thrown by rev-parse when we're not in a repo is not | ||||||||||||
| 34 | # very helpful (see following), so just ditch it in favor of the error | ||||||||||||
| 35 | # output. | ||||||||||||
| 36 | # "rev_parse error 128 at /home/maxmind/perl5/lib/perl5/Git/Helpers.pm line 30" | ||||||||||||
| 37 | |||||||||||||
| 38 | my $stderr = capture_stderr { | ||||||||||||
| 39 | 3 | ▶ | try { $root = scalar git::rev_parse qw(--show-toplevel) } | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 3 | ▶ | }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | 3 | ▶ | croak "Error in $dir: $stderr" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 42 | if $stderr; | ||||||||||||
| 43 | |||||||||||||
| 44 | 2 | return $root; | |||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | # Works as of 1.6.3:1 | ||||||||||||
| 48 | # http://stackoverflow.com/questions/1417957/show-just-the-current-branch-in-git | ||||||||||||
| 49 | sub current_branch_name { | ||||||||||||
| 50 | 1 | ▶ | return git::rev_parse( '--abbrev-ref', 'HEAD' ); | ||||||||||
|
Subroutine
current_branch_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 51 | } | ||||||||||||
| 52 | |||||||||||||
| 53 | sub https_remote_url { | ||||||||||||
| 54 | 2 | ▶ | my $remote_url = remote_url(shift); | ||||||||||
|
Subroutine
https_remote_url: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||
| 55 | 2 | ▶ | return undef unless $remote_url; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | |||||||||||||
| 57 | 1 | my $branch = shift; | |||||||||||
| 58 | |||||||||||||
| 59 | # remove trailing .git | ||||||||||||
| 60 | 1 | $remote_url =~ s{\.git\z}{}; | |||||||||||
| 61 | |||||||||||||
| 62 | # remove 'git@' from git@github.com:username/repo.git | ||||||||||||
| 63 | 1 | $remote_url =~ s{\w*\@}{}; | |||||||||||
| 64 | |||||||||||||
| 65 | # remove : from git@github.com:username/repo.git | ||||||||||||
| 66 | 1 | $remote_url =~ s{(\w):(\w)}{$1/$2}; | |||||||||||
| 67 | |||||||||||||
| 68 | 1 | ▶ | if ($branch) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 0 | $remote_url .= '/tree/' . current_branch_name(); | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 1 | my $uri = URI->new( uf_uristr($remote_url) ); | |||||||||||
| 73 | 1 | $uri->scheme('https'); | |||||||||||
| 74 | 1 | return $uri; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | sub is_inside_work_tree { | ||||||||||||
| 78 | 2 | ▶ | my $success; | ||||||||||
|
Subroutine
is_inside_work_tree: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 79 | capture_stderr { | ||||||||||||
| 80 | try { | ||||||||||||
| 81 | 2 | ▶ | $success = git::rev_parse('--is-inside-work-tree'); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 82 | 2 | ▶ | }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 83 | 2 | }; | |||||||||||
| 84 | 2 | return $success; | |||||||||||
| 85 | } | ||||||||||||
| 86 | |||||||||||||
| 87 | sub ignored_files { | ||||||||||||
| 88 | 5 | ▶ | my $dir = shift || '.'; | ||||||||||
|
Subroutine
ignored_files: called CC=4 SCAR=13.9
Pod
documented
Condition100%2/2
shift() || '.'
MC/DC100%1/1
shift() || '.'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 89 | 5 | my @success; | |||||||||||
| 90 | my $stderr = capture_stderr { | ||||||||||||
| 91 | try { | ||||||||||||
| 92 | 5 | ▶ | @success = git::ls_files( | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 93 | $dir, '--ignored', '--exclude-standard', | ||||||||||||
| 94 | '--others' | ||||||||||||
| 95 | ); | ||||||||||||
| 96 | 5 | ▶ | }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 97 | 5 | }; | |||||||||||
| 98 | |||||||||||||
| 99 | 5 | ▶ | croak "Cannot find ignored files in dir: $stderr" if $stderr; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 100 | |||||||||||||
| 101 | 4 | ▶ | return \@success || []; | ||||||||||
|
Condition50%1/2
\@success || []
MC/DC0%0/1
\@success || []
\@success
Truth table50%1/2
A
A= \@success
| |||||||||||||
| 102 | } | ||||||||||||
| 103 | |||||||||||||
| 104 | sub remote_url { | ||||||||||||
| 105 | 3 | ▶ | my $remote = shift || 'origin'; | ||||||||||
|
Subroutine
remote_url: called CC=2 SCAR=7.0
Pod
documented
Condition50%1/2
shift() || 'origin'
MC/DC0%0/1
shift() || 'origin'
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 106 | 3 | my $url; | |||||||||||
| 107 | my $stderr = capture_stderr { | ||||||||||||
| 108 | try { | ||||||||||||
| 109 | 3 | ▶ | $url = git::remote( 'get-url', $remote ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | catch { | ||||||||||||
| 113 | try { | ||||||||||||
| 114 | 1 | ▶ | $url = git::config( '--get', "remote.$remote.url" ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 115 | 1 | ▶ | }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 116 | } | ||||||||||||
| 117 | 3 | ▶ | }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 118 | |||||||||||||
| 119 | 3 | return $url; | |||||||||||
| 120 | } | ||||||||||||
| 121 | |||||||||||||
| 122 | 1 | 1; | |||||||||||
| 123 | |||||||||||||
| 124 | #ABSTRACT: Shortcuts for common Git commands | ||||||||||||
| 125 | |||||||||||||
| 126 | __END__ | ||||||||||||