blib/lib/Dist/Zilla/Plugin/Git/GatherDir.pm

filter: Statementstmt 79.4% 58 / 73 Branchbran 50.0% 14 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 8 / 10 Podpod n/a totaltotal 68.9% 80 / 116 CC 18 SCAR 25.9
CC
18
Cov
69%
_homedir
30.0
__ANON__
20.0
__ANON__
11.1
CRAP 13.3
« blib/lib/Dist/Zilla/Plugin/Git/Init.pm ↑ Summary Makefile_git.PL »
1 #
2 # This file is part of Dist-Zilla-Plugin-Git
3 #
4 # This software is copyright (c) 2009 by Jerome Quelin.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Dist::Zilla::Plugin::Git::GatherDir;
10 # ABSTRACT: Gather all tracked files in a Git working directory
11  
12 2 our $VERSION = '2.052';
13  
14 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 extends 'Dist::Zilla::Plugin::GatherDir' => { -version => 4.200016 }; # exclude_match
16  
17 #pod =head1 SYNOPSIS
18 #pod
19 #pod In your F<dist.ini>:
20 #pod
21 #pod [Git::GatherDir]
22 #pod root = . ; this is the default
23 #pod prefix = ; this is the default
24 #pod include_dotfiles = 0 ; this is the default
25 #pod include_untracked = 0 ; this is the default
26 #pod exclude_filename = dir/skip ; there is no default
27 #pod exclude_match = ^local_ ; there is no default
28 #pod
29 #pod =head1 DESCRIPTION
30 #pod
31 #pod This is a trivial variant of the L<GatherDir|Dist::Zilla::Plugin::GatherDir>
32 #pod plugin. It looks in the directory named in the L</root> attribute and adds all
33 #pod the Git tracked files it finds there (as determined by C<git ls-files>). If the
34 #pod root begins with a tilde, the tilde portion is passed through C<glob()> first.
35 #pod
36 #pod Most users just need:
37 #pod
38 #pod [Git::GatherDir]
39 #pod
40 #pod ...and this will pick up all tracked files from the current directory into the
41 #pod dist. You can use it multiple times, as you can any other plugin, by providing
42 #pod a plugin name. For example, if you want to include external specification
43 #pod files into a subdir of your dist, you might write:
44 #pod
45 #pod [Git::GatherDir]
46 #pod ; this plugin needs no config and gathers most of your files
47 #pod
48 #pod [Git::GatherDir / SpecFiles]
49 #pod ; this plugin gets all tracked files in the root dir and adds them under ./spec
50 #pod root = ~/projects/my-project/spec
51 #pod prefix = spec
52 #pod
53 #pod =cut
54  
55 2 ▶use List::Util 1.45 qw(uniq any);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use Types::Standard 'Bool';
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 #pod =head1 ATTRIBUTES
61 #pod
62 #pod This plugin inherits attributes from L<Dist::Zilla::Plugin::GatherDir>, with the following
63 #pod modifications:
64 #pod
65 #pod =attr root
66 #pod
67 #pod See L<Dist::Zilla::Plugin::GatherDir/root>.
68 #pod It does not need to be the root of a
69 #pod Git repository, but it must be inside a repository.
70 #pod
71 #pod =attr include_untracked
72 #pod
73 #pod By default, files not tracked by Git will not be gathered. If this is
74 #pod set to a true value, then untracked files not covered by a Git ignore
75 #pod pattern (i.e. those reported by C<git ls-files -o --exclude-standard>)
76 #pod are also gathered (and you'll probably want to use
77 #pod L<Git::Check|Dist::Zilla::Plugin::Git::Check> to ensure all files are
78 #pod checked in before a release).
79 #pod
80 #pod C<include_untracked> requires at least Git 1.5.4, but you should
81 #pod probably not use it if your Git is older than 1.6.5.2. Versions
82 #pod before that would not list files matched by your F<.gitignore>, even
83 #pod if they were already being tracked by Git (which means they will not
84 #pod be gathered, even though they should be). Whether that is a problem
85 #pod depends on the contents of your exclude files (including the global
86 #pod one, if any).
87 #pod
88 #pod =attr follow_symlinks
89 #pod
90 #pod Git::GatherDir does not honor GatherDir's
91 #pod L<follow_symlinks|Dist::Zilla::Plugin::GatherDir/follow_symlinks>
92 #pod option. While the attribute exists (because Git::GatherDir is a
93 #pod subclass), setting it has no effect.
94 #pod
95 #pod Directories that are symlinks will not be gathered. Instead, you'll
96 #pod get a message saying C<WARNING: %s is symlink to directory, skipping it>.
97 #pod To suppress the warning, add that directory to C<exclude_filename> or
98 #pod C<exclude_match>. To gather the files in the symlinked directory, use
99 #pod a second instance of GatherDir or Git::GatherDir with appropriate
100 #pod C<root> and C<prefix> options.
101 #pod
102 #pod Files which are symlinks are always gathered.
103 #pod
104 #pod =cut
105  
106 2 has include_untracked => (
107 is => 'ro',
108 isa => Bool,
109 default => 0,
110 );
111  
112 around dump_config => sub
113 0 ▶{
Subroutine __ANON__: not called CC=4 SCAR=30.0
114 0 my $orig = shift;
115 0 my $self = shift;
116  
117 0 my $config = $self->$orig;
118  
119 0 ▶ $config->{+__PACKAGE__} = {
Branch: 2 branches
BranchTrueFalseTotal
$self->include_untracked ? : 0 0 0
builtin::blessed($self) ne __PACKAGE__ ? : 0 0 0
120 include_untracked => $self->include_untracked ? 1 : 0,
121 blessed($self) ne __PACKAGE__ ? ( version => $VERSION ) : (),
122 };
123  
124 $self->log('WARNING: unused config variable "follow_symlinks"')
125 0 ▶ if $config->{'Dist::Zilla::Plugin::GatherDir'}{follow_symlinks};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"Dist::Zilla::Plugin::GatherDir"}{"follow_symlinks"} 0 0 0
126  
127 0 delete @{$config->{'Dist::Zilla::Plugin::GatherDir'}}{qw(follow_symlinks)};
128  
129 0 return $config;
130 2 };
131  
132 10 ▶override gather_files => sub {
Subroutine __ANON__: called CC=11 SCAR=24.1
133 10 my ($self) = @_;
134  
135 10 require Git::Wrapper;
136 10 require Path::Tiny;
137  
138 10 my $root = '' . $self->root;
139  
140 # Convert ~ portion to real directory:
141 0 $root =~ s{^(~[^\\/]*)([\\/])}{$self->_homedir($1) . $2}e;
142  
143 10 $root = Path::Tiny::path($root)->absolute($self->zilla->root);
144  
145 # Prepare to gather files
146 10 my $git = Git::Wrapper->new($root->stringify);
147  
148 10 my @opts;
149 10 ▶ @opts = qw(--cached --others --exclude-standard) if $self->include_untracked;
Branch: 1 branch
BranchTrueFalseTotal
if $self->include_untracked 3 7 10
150  
151 10 my $exclude_regex = qr/\000/;
152 $exclude_regex = qr/$exclude_regex|$_/
153 10 for (@{ $self->exclude_match });
154  
155 10 my %is_excluded = map +($_ => 1), @{ $self->exclude_filename };
156  
157 10 my $prefix = $self->prefix;
158  
159 # Loop over files reported by git ls-files
160 10 for my $filename (uniq $git->ls_files(@opts)) {
161 # $file is a Path::Tiny relative to $root
162 45 my $file = Path::Tiny::path($filename);
163  
164 45 $self->log_debug("considering $file");
165  
166 # Exclusion tests
167 45 ▶ unless ($self->include_dotfiles) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->include_dotfiles) 32 13 45
168 32 ▶ next if grep /^\./, split q{/}, $file->stringify;
Branch: 1 branch
BranchTrueFalseTotal
if grep /^\./, split(m[/], $file->stringify, 0) 9 23 32
169 }
170  
171 36 ▶ next if $file =~ $exclude_regex;
Branch: 1 branch
BranchTrueFalseTotal
if $file =~ /$exclude_regex/ 0 36 36
172 36 ▶ next if $is_excluded{ $file };
Branch: 1 branch
BranchTrueFalseTotal
if $is_excluded{$file} 1 35 36
173  
174 # skip if any directory in the path matches a 'prune_directory' regex.
175 35 my @dirs = split /\//, $file->dirname;
176 3 ▶ next if any {
Subroutine __ANON__: called CC=1 SCAR=0.0
177 3 my $pd = $_;
178 3 ▶ any { $_ =~ $pd } @dirs;
Subroutine __ANON__: called CC=1 SCAR=0.0
179 }
180 35 ▶ @{ $self->prune_directory };
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { my $pd = $_; &any(sub { $_ =~ /$pd/; } , @dirs); } , @{$self->prune_directory;}) 1 34 35
181  
182 # DZil can't gather directory symlinks
183 34 my $path = $root->child($file);
184  
185 34 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) 0 34 34
186 0 $self->log("WARNING: $file is symlink to directory, skipping it");
187 0 next;
188 }
189  
190 # Gather the file
191 34 my $fileobj = $self->_file_from_filename($path->stringify);
192  
193 34 ▶ $file = Path::Tiny::path($prefix, $file) if length $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if length $prefix 1 33 34
194  
195 34 $fileobj->name($file->stringify);
196 34 $self->add_file($fileobj);
197 34 $self->log_debug("gathered $file");
198 }
199  
200 10 return;
201 2 };
202  
203 0 ▶sub _homedir {
Subroutine _homedir: not called CC=5 SCAR=34.0
204 0 my ($self, $tilde_dir) = @_;
205  
206 # other architectures have no issue with globbing any kind of ~user path
207 0 ▶ return (glob($tilde_dir))[0] if $^O ne 'Win32';
Branch: 1 branch
BranchTrueFalseTotal
if $^O ne "Win32" 0 0 0
208  
209 # on Win32, we can use the environment variable(s) for the current user
210 0 ▶ return "$]" < '5.016' ? $ENV{HOME} || $ENV{USERPROFILE} : (glob('~'))[0]
Branch: 2 branches
BranchTrueFalseTotal
if $tilde_dir eq "~" 0 0 0
"$]" < '5.016' ? : 0 0 0
Condition0%0/3
$ENV{"HOME"} or $ENV{"USERPROFILE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{"HOME"} or $ENV{"USERPROFILE"}
$ENV{"HOME"} $ENV{"USERPROFILE"}
Truth table0%0/3
A or B
A= $ENV{"HOME"}B= $ENV{"USERPROFILE"}
ABresult
1X1
011
000
211 if $tilde_dir eq '~';
212  
213 # but otherwise, we can't support this at all, on any version.
214 0 $self->log_fatal('expanding "' . $tilde_dir . '" on Win32 not supported');
215 }
216  
217 2 __PACKAGE__->meta->make_immutable;
218 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 2 1;
220  
221 __END__
« blib/lib/Dist/Zilla/Plugin/Git/Init.pm ↑ Summary Makefile_git.PL »