Helpers.pm

filter: Statementstmt 98.6% 75 / 76 Branchbran 90.0% 9 / 10 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 27 / 27 Podpod 100% 6 / 6 totaltotal 95.4% 125 / 131 CC 10 SCAR 6.4
CC
10
Cov
96%
ignored_files
4.0
checkout_root
4.0
https_remote_url
3.0
CRAP 1.9
↑ Summary Helpers/CPAN.pm »
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.000001';
6  
7 1 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Capture::Tiny qw( capture_stderr );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use File::pushd qw( pushd );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Git::Sub;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 exports => [
13 'checkout_root',
14 'current_branch_name',
15 'https_remote_url',
16 'ignored_files',
17 'is_inside_work_tree',
18 'remote_url',
19 ]
20 1 };
21 1 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use 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 3 ▶sub checkout_root {
Subroutine checkout_root: called CC=4 SCAR=13.9
Pod documented
27 3 my $dir = shift;
28  
29 3 my ( $new_dir, $root );
30 3 ▶ $new_dir = pushd($dir) if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 2 1 3
31 3 ▶ $dir ||= '.';
Condition100%2/2
$dir ||= "."
l!l
21
MC/DC100%1/1
$dir ||= "."
$dir
Truth table100%2/2
A
A= $dir
Aresult
11
00
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 3 ▶ my $stderr = capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
39 3 ▶ try { $root = scalar git::rev_parse qw(--show-toplevel) }
Subroutine __ANON__: called CC=1 SCAR=0.0
40 3 };
41 3 ▶ croak "Error in $dir: $stderr"
Branch: 1 branch
BranchTrueFalseTotal
if $stderr 1 2 3
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 1 ▶sub current_branch_name {
Subroutine current_branch_name: called CC=1 SCAR=0.0
Pod documented
50 1 return git::rev_parse( '--abbrev-ref', 'HEAD' );
51 }
52  
53 2 ▶sub https_remote_url {
Subroutine https_remote_url: called CC=3 SCAR=11.1
Pod documented
54 2 my $remote_url = remote_url(shift);
55 2 ▶ return undef unless $remote_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $remote_url 1 1 2
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
BranchTrueFalseTotal
if ($branch) 0 1 1
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 2 ▶sub is_inside_work_tree {
Subroutine is_inside_work_tree: called CC=1 SCAR=0.0
Pod documented
78 2 my $success;
79 2 ▶ capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
80 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 2 $success = git::rev_parse('--is-inside-work-tree');
82 2 };
83 2 };
84 2 return $success;
85 }
86  
87 5 ▶sub ignored_files {
Subroutine ignored_files: called CC=4 SCAR=13.9
Pod documented
88 5 ▶ my $dir = shift || '.';
Condition100%2/2
shift() || '.'
l!l
23
MC/DC100%1/1
shift() || '.'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
89 5 my @success;
90 5 ▶ my $stderr = capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
91 5 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
92 5 @success = git::ls_files(
93 $dir, '--ignored', '--exclude-standard',
94 '--others'
95 );
96 5 };
97 5 };
98  
99 5 ▶ croak "Cannot find ignored files in dir: $stderr" if $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr 1 4 5
100  
101 4 ▶ return \@success || [];
Condition50%1/2
\@success || []
l!l
40
MC/DC0%0/1
\@success || []
\@success
Truth table50%1/2
A
A= \@success
Aresult
11
00
102 }
103  
104 3 ▶sub remote_url {
Subroutine remote_url: called CC=2 SCAR=7.0
Pod documented
105 3 ▶ my $remote = shift || 'origin';
Condition50%1/2
shift() || 'origin'
l!l
30
MC/DC0%0/1
shift() || 'origin'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
106 3 my $url;
107 3 ▶ my $stderr = capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
108 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
109 3 $url = git::remote( 'get-url', $remote );
110 }
111  
112 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
114 1 $url = git::config( '--get', "remote.$remote.url" );
115 1 };
116 }
117 3 };
118  
119 3 return $url;
120 }
121  
122 1 1;
123  
124 #ABSTRACT: Shortcuts for common Git commands
125  
126 __END__
↑ Summary Helpers/CPAN.pm »