| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Test::Git; | 
| 2 |  |  |  |  |  |  | $Test::Git::VERSION = '1.323'; | 
| 3 | 14 |  |  | 14 |  | 1059120 | use strict; | 
|  | 14 |  |  |  |  | 515 |  | 
|  | 14 |  |  |  |  | 618 |  | 
| 4 | 14 |  |  | 14 |  | 114 | use warnings; | 
|  | 14 |  |  |  |  | 64 |  | 
|  | 14 |  |  |  |  | 742 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 14 |  |  | 14 |  | 97 | use Exporter; | 
|  | 14 |  |  |  |  | 60 |  | 
|  | 14 |  |  |  |  | 926 |  | 
| 7 | 14 |  |  | 14 |  | 100 | use Test::Builder; | 
|  | 14 |  |  |  |  | 56 |  | 
|  | 14 |  |  |  |  | 436 |  | 
| 8 | 14 |  |  | 14 |  | 7143 | use Git::Repository;    # 1.15 | 
|  | 14 |  |  |  |  | 42 |  | 
|  | 14 |  |  |  |  | 62 |  | 
| 9 | 14 |  |  | 14 |  | 10363 | use File::Temp qw( tempdir ); | 
|  | 14 |  |  |  |  | 208202 |  | 
|  | 14 |  |  |  |  | 961 |  | 
| 10 | 14 |  |  | 14 |  | 6108 | use File::Spec::Functions qw( catdir ); | 
|  | 14 |  |  |  |  | 12576 |  | 
|  | 14 |  |  |  |  | 904 |  | 
| 11 | 14 |  |  | 14 |  | 98 | use Git::Version::Compare qw( cmp_git ); | 
|  | 14 |  |  |  |  | 29 |  | 
|  | 14 |  |  |  |  | 618 |  | 
| 12 | 14 |  |  | 14 |  | 80 | use Cwd qw( cwd ); | 
|  | 14 |  |  |  |  | 31 |  | 
|  | 14 |  |  |  |  | 513 |  | 
| 13 | 14 |  |  | 14 |  | 75 | use Carp; | 
|  | 14 |  |  |  |  | 34 |  | 
|  | 14 |  |  |  |  | 8408 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our @ISA     = qw( Exporter ); | 
| 16 |  |  |  |  |  |  | our @EXPORT  = qw( has_git test_repository ); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | my $Test = Test::Builder->new(); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub has_git { | 
| 21 | 0 |  |  | 0 | 1 | 0 | my ( $version, @options ) = ( ( grep !ref, @_ )[0], grep ref, @_ ); | 
| 22 | 0 | 0 |  |  |  | 0 | carp sprintf 'has_git() is obsolete, write `use Test::Requires::Git; test_requires_git%s;` instead', | 
| 23 |  |  |  |  |  |  | $version ? " '$version'" : ''; | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | # check some git is present | 
| 26 | 0 | 0 |  |  |  | 0 | $Test->skip_all('Default git binary not found in PATH') | 
| 27 |  |  |  |  |  |  | if !Git::Repository::Command::_is_git('git'); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # check it's at least some minimum version | 
| 30 | 0 |  |  |  |  | 0 | my $git_version = Git::Repository->version(@options); | 
| 31 | 0 | 0 | 0 |  |  | 0 | $Test->skip_all( | 
| 32 |  |  |  |  |  |  | "Test script requires git >= $version (this is only $git_version)") | 
| 33 |  |  |  |  |  |  | if $version && Git::Repository->version_lt( $version, @options ); | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub test_repository { | 
| 37 | 13 |  |  | 13 | 1 | 39312 | my %args = @_; | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | croak "Can't use both 'init' and 'clone' parameters" | 
| 40 | 13 | 0 | 33 |  |  | 166 | if exists $args{init} && exists $args{clone}; | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | # setup some default values | 
| 43 | 13 |  | 50 |  |  | 279 | my $temp = $args{temp} || [ CLEANUP => 1 ];    # File::Temp options | 
| 44 | 13 |  | 50 |  |  | 150 | my $init = $args{init} || [];                  # git init options | 
| 45 | 13 |  | 100 |  |  | 157 | my $opts = $args{git}  || {};                  # Git::Repository options | 
| 46 | 13 |  |  |  |  | 105 | my $safe = { %$opts, fatal => [] };            # ignore 'fatal' settings | 
| 47 | 13 |  |  |  |  | 45 | my $clone = $args{clone};                      # git clone options | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | # version check | 
| 50 | 13 |  |  |  |  | 55 | my $bad_version; | 
| 51 | 13 |  |  |  |  | 267 | my $git_version = Git::Repository->version($safe); | 
| 52 | 13 | 100 |  |  |  | 900 | if ($clone) { | 
|  |  | 50 |  |  |  |  |  | 
| 53 | 2 | 50 | 33 |  |  | 41 | if ( | 
|  |  |  | 33 |  |  |  |  | 
| 54 |  |  |  |  |  |  | cmp_git( $git_version, '1.6.2.rc0' ) < 0 | 
| 55 |  |  |  |  |  |  | || (   cmp_git( '1.7.0.rc1', $git_version ) <= 0 | 
| 56 |  |  |  |  |  |  | && cmp_git( $git_version, '1.7.0.2' ) <= 0 ) | 
| 57 |  |  |  |  |  |  | ) | 
| 58 |  |  |  |  |  |  | { | 
| 59 | 0 |  |  |  |  | 0 | $bad_version = "test_repository( clone => ... ) requires git >= 1.6.2.rc0 and 1.7.0.rc1 < git < 1.7.0.2 (this is $git_version)"; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | elsif ( cmp_git( $git_version, '1.4.0' ) <= 0 ) { | 
| 63 | 0 |  |  |  |  | 0 | $bad_version = "test_repository( init => ... ) requires git >= 1.4.0 (this is only $git_version)"; | 
| 64 |  |  |  |  |  |  | } | 
| 65 | 13 | 50 |  |  |  | 1099 | croak $bad_version if $bad_version; | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | # create a temporary directory to host our repository | 
| 68 | 13 |  |  |  |  | 244 | my $dir = tempdir(@$temp); | 
| 69 | 13 |  |  |  |  | 10025 | my $cwd = { cwd => $dir };    # option to chdir there | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # create the git repository there | 
| 72 | 13 | 50 |  |  |  | 1009 | unshift @$init, | 
| 73 |  |  |  |  |  |  | Git::Repository->version_lt('1.5.0.rc1') ? 'init-db' : 'init'; | 
| 74 | 13 | 100 |  |  |  | 3188 | my @cmd = $clone ? ( clone => @$clone, $dir ) : ( @$init, $cwd ); | 
| 75 | 13 |  |  |  |  | 185 | Git::Repository->run( @cmd, $safe ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | # create the Git::Repository object | 
| 78 | 13 |  |  |  |  | 704 | my $gitdir = Git::Repository->run( qw( rev-parse --git-dir ), $cwd ); | 
| 79 | 13 |  |  |  |  | 870 | return Git::Repository->new( git_dir => catdir( $dir, $gitdir ), $opts ); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | 1; | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | __END__ |