File Coverage

lib/App/PRT/Collector/GitDirectory.pm
Criterion Covered Total %
statement 33 33 100.0
branch 12 12 100.0
condition n/a
subroutine 7 7 100.0
pod 0 4 0.0
total 52 56 92.8


line stmt bran cond sub pod time code
1             package App::PRT::Collector::GitDirectory;
2 2     2   2467 use strict;
  2         4  
  2         59  
3 2     2   12 use warnings;
  2         4  
  2         48  
4 2     2   10 use Path::Class;
  2         5  
  2         851  
5              
6             # find git directory from specified directory path
7             # returns:
8             # directory path (when found)
9             # undef (when not found)
10             sub find_git_root_directory {
11 8     8 0 11754 my ($class, $directory) = @_;
12              
13 8 100       169 die "$directory does not exist" unless -d $directory;
14              
15 7         79 my $current = dir($directory);
16 7         982 while (1) {
17 16 100       648 if (-d $current->subdir('.git')) {
18 3         404 return $current->stringify;
19             }
20              
21 13 100       1098 if ($current eq $current->parent) {
22             # not found
23 4         383 return;
24             }
25 9         1192 $current = $current->parent;
26             }
27             }
28              
29             sub new {
30 6     6 0 68818 my ($class, $directory) = @_;
31              
32 6 100       76 die 'directory requird' unless defined $directory;
33              
34 5 100       147 die "directory $directory does not exist" unless -d $directory;
35              
36 4         87 bless {
37             directory => $directory,
38             }, $class;
39             }
40              
41             sub collect {
42 3     3 0 242 my ($self) = @_;
43 3         19 my $git_directory = dir($self->directory)->subdir('.git');
44              
45 3         894 my $output = `git --no-pager --git-dir $git_directory ls-files --full-name 2> /dev/null`;
46              
47 3 100       14561 if ($?) {
48             # when success, exit status is 0
49 1         19 die "directory @{[ $self->directory ]} seems not a git repository";
  1         31  
50             }
51              
52 2         59 my $dir = dir($self->directory);
53 2         439 [ map { $dir->file($_)->stringify } split /\n/, $output ];
  7         882  
54             }
55              
56             sub directory {
57 8     8 0 1271 my ($self) = @_;
58              
59 8         302 $self->{directory};
60             }
61              
62             1;