lib/Git/Helpers.pm

filter: Statementstmt 100% 79 / 79 Branchbran 100% 18 / 18 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 26 / 26 Podpod 100% 6 / 6 totaltotal 97.1% 137 / 141 CC 14 SCAR 10.1
CC
14
Cov
98%
https_remote_url
7.0
ignored_files
4.0
checkout_root
4.0
CRAP 2.7
↑ Summary blib/lib/Git/Helpers/CPAN.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Git::Helpers;
5 2 our $VERSION = '1.000002';
6  
7 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Capture::Tiny qw( capture_stderr );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::pushd qw( pushd );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Git::Sub;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶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 2 };
21 2 ▶use Try::Tiny qw( catch try );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use URI::git ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 ▶sub checkout_root {
Subroutine checkout_root: called CC=4 SCAR=13.9
Pod documented
26 3 my $dir = shift;
27  
28 3 my ( $new_dir, $root );
29 3 ▶ $new_dir = pushd($dir) if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 2 1 3
30 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
31  
32 # the exception thrown by rev-parse when we're not in a repo is not
33 # very helpful (see following), so just ditch it in favor of the error
34 # output.
35 # "rev_parse error 128 at /home/maxmind/perl5/lib/perl5/Git/Helpers.pm line 30"
36  
37 3 ▶ my $stderr = capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 3 ▶ try { $root = scalar git::rev_parse qw(--show-toplevel) }
Subroutine __ANON__: called CC=1 SCAR=0.0
39 3 };
40 3 ▶ croak "Error in $dir: $stderr"
Branch: 1 branch
BranchTrueFalseTotal
if $stderr 1 2 3
41 if $stderr;
42  
43 2 return $root;
44 }
45  
46 # Works as of 1.6.3:1
47 # http://stackoverflow.com/questions/1417957/show-just-the-current-branch-in-git
48 3 ▶sub current_branch_name {
Subroutine current_branch_name: called CC=1 SCAR=0.0
Pod documented
49 3 return git::rev_parse( '--abbrev-ref', 'HEAD' );
50 }
51  
52 16 ▶sub https_remote_url {
Subroutine https_remote_url: called CC=7 SCAR=19.5
Pod documented
53 16 my $remote_url = remote_url(shift);
54 16 ▶ return undef unless $remote_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $remote_url 1 15 16
55  
56 15 my $branch = shift;
57  
58 # remove trailing .git
59 15 $remote_url =~ s{\.git\z}{};
60  
61 15 my $uri;
62 15 ▶ if ( $remote_url =~ m{\A[a-z][a-z0-9+.-]*://}i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($remote_url =~ m[\A[a-z][a-z0-9+.-]*://]i) { } 7 8 15
elsif ($remote_url =~ m[\A(?:[^\@/]+@)?([^/:]+):/*(.+)\z]) { } 5 3 8
63 7 $uri = URI->new($remote_url);
64 7 ▶ return undef if $uri->scheme eq 'file';
Branch: 1 branch
BranchTrueFalseTotal
if $uri->scheme eq "file" 1 6 7
65  
66 # an ssh or git port is not the web port
67 6 my $keep_port = $uri->scheme =~ m{\Ahttps?\z};
68 6 $uri->scheme('https');
69 6 $uri->userinfo(undef);
70 6 ▶ $uri->port(undef) unless $keep_port;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep_port 2 4 6
71 }
72 elsif ( $remote_url =~ m{\A(?:[^@/]+@)?([^/:]+):/*(.+)\z} ) {
73 5 $uri = URI->new("https://$1/$2");
74 }
75 else {
76 3 return undef;
77 }
78  
79 11 ▶ if ($branch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branch) 2 9 11
80 2 $uri->path( $uri->path . '/tree/' . current_branch_name() );
81 }
82  
83 11 return $uri;
84 }
85  
86 2 ▶sub is_inside_work_tree {
Subroutine is_inside_work_tree: called CC=1 SCAR=0.0
Pod documented
87 2 my $success;
88 2 ▶ capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
90 2 $success = git::rev_parse('--is-inside-work-tree');
91 2 };
92 2 };
93 2 return $success;
94 }
95  
96 5 ▶sub ignored_files {
Subroutine ignored_files: called CC=4 SCAR=13.9
Pod documented
97 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
98 5 my @success;
99 5 ▶ my $stderr = capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 5 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 5 @success = git::ls_files(
102 $dir, '--ignored', '--exclude-standard',
103 '--others'
104 );
105 5 };
106 5 };
107  
108 5 ▶ croak "Cannot find ignored files in dir: $stderr" if $stderr;
Branch: 1 branch
BranchTrueFalseTotal
if $stderr 1 4 5
109  
110 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
111 }
112  
113 17 ▶sub remote_url {
Subroutine remote_url: called CC=2 SCAR=7.0
Pod documented
114 17 ▶ my $remote = shift || 'origin';
Condition50%1/2
shift() || 'origin'
l!l
170
MC/DC0%0/1
shift() || 'origin'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
115 17 my $url;
116 17 ▶ capture_stderr {
Subroutine __ANON__: called CC=1 SCAR=0.0
117 17 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
118 17 $url = git::remote( 'get-url', $remote );
119 }
120  
121 1 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 1 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 1 $url = git::config( '--get', "remote.$remote.url" );
124 1 };
125 }
126 17 };
127  
128 17 return $url;
129 }
130  
131 2 1;
132  
133 #ABSTRACT: Shortcuts for common Git commands
134  
135 __END__
↑ Summary blib/lib/Git/Helpers/CPAN.pm »