File Coverage

blib/lib/App/GHPT/WorkSubmitter/ChangedFilesFactory.pm
Criterion Covered Total %
statement 53 64 82.8
branch 4 8 50.0
condition 7 20 35.0
subroutine 10 11 90.9
pod n/a
total 74 103 71.8


line stmt bran cond sub pod time code
1              
2             use App::GHPT::Wrapper::OurMoose;
3 1     1   8  
  1         3  
  1         12  
4             our $VERSION = '2.000000';
5              
6             use IPC::Run3 qw( run3 );
7 1     1   11 use App::GHPT::Types qw( ArrayRef HashRef Str );
  1         2  
  1         72  
8 1     1   6 use App::GHPT::WorkSubmitter::ChangedFiles;
  1         2  
  1         15  
9 1     1   5611  
  1         4  
  1         1305  
10             has changed_files_class => (
11             is => 'ro',
12             isa => Str,
13             default => 'App::GHPT::WorkSubmitter::ChangedFiles',
14             );
15              
16             has merge_to_branch_name => (
17             is => 'ro',
18             isa => Str,
19             required => 1,
20             );
21              
22             has current_branch_name => (
23             is => 'ro',
24             isa => Str,
25             lazy => 1,
26             builder => '_build_current_branch_name',
27             );
28              
29             my $branch;
30              
31 2     2   5 my @command = (
32             'git',
33 2         15 'rev-parse',
34             '--abbrev-ref',
35             'HEAD',
36             );
37              
38             run3 \@command, \undef, \$branch, \my $error;
39             if ( $error || $? ) {
40 2         19 die join q{ }, 'Problem running git rev-parse:', @command, $error, $?;
41 2 50 33     17910 }
42 0         0  
43             chomp $branch;
44             return $branch;
45 2         19 }
46 2         168  
47             has merge_base => (
48             is => 'ro',
49             isa => Str,
50             lazy => 1,
51             builder => '_build_merge_base',
52             );
53              
54             my $merge_base;
55              
56 2     2   4 my @command = (
  2         10  
  2         4  
57 2         7 'git',
58             'merge-base',
59 2         66 $self->current_branch_name,
60             $self->merge_to_branch_name,
61             );
62              
63             run3 \@command, \undef, \$merge_base, \my $error;
64             if ( $error || $? ) {
65             die join q{ }, 'Problem running git merge-base:', @command, $error,
66 2         21 $?;
67 2 50 33     18264 }
68 0         0  
69             chomp $merge_base;
70             return $merge_base;
71             }
72 2         18  
73 2         191 has _git_diff_file_list => (
74             is => 'ro',
75             isa => HashRef [ ArrayRef [Str] ],
76             lazy => 1,
77             builder => '_build_git_diff_file_list',
78             );
79              
80             my %return_value;
81              
82             my @command = (
83 2     2   7 'git',
  2         4  
  2         4  
84 2         3 'diff',
85             '--name-status',
86 2         69 $self->merge_base,
87             $self->current_branch_name,
88             );
89              
90             run3 \@command, \undef, sub ($line) {
91             chomp $line;
92             my ( $code, $filename ) = split /\s+/, $line, 2;
93             $return_value{$code} ||= [];
94 0     0   0 push $return_value{$code}->@*, $filename;
  0         0  
  0         0  
95 0         0 }, \my $error;
96 0         0  
97 0   0     0 if ( $error || $? ) {
98 0         0 die join q{ }, 'Problem running git diff:', @command, $error, $?;
99 2         44 }
100              
101 2 50 33     19633 return \%return_value;
102 0         0 }
103              
104             has _all_files => (
105 2         208 is => 'ro',
106             isa => ArrayRef,
107             lazy => 1,
108             builder => '_build_all_files',
109             );
110              
111             my @files;
112              
113             my @command = (
114             'git',
115 2     2   6 'ls-tree',
  2         11  
  2         4  
116 2         11 '-r',
117             'HEAD:',
118 2         21 );
119              
120             run3 \@command, \undef, sub ($line) {
121             chomp $line;
122             my ( undef, undef, undef, $filename ) = split /\s+/, $line, 4;
123             push @files, $filename;
124             }, \my $error;
125 2     2   8  
  2         16974  
  2         14  
126 2         15 if ( $error || $? ) {
127 2         42 die join q{ }, 'Problem running git ls-tree:', @command,
128 2         31 $error, $?;
129 2         30 }
130              
131 2 50 33     209 return \@files;
132 0         0 }
133              
134             has changed_files => (
135             is => 'ro',
136 2         185 isa => 'App::GHPT::WorkSubmitter::ChangedFiles',
137             lazy => 1,
138             builder => '_build_changed_files',
139             );
140              
141             return $self->changed_files_class->new(
142             added_files => $self->_git_diff_file_list->{A} || [],
143             modified_files => $self->_git_diff_file_list->{M} || [],
144             deleted_files => $self->_git_diff_file_list->{D} || [],
145             all_files => $self->_all_files,
146 2     2   6 );
  2         4  
  2         4  
147             }
148              
149             __PACKAGE__->meta->make_immutable;
150 2   50     84  
      50        
      50        
151             1;
152              
153             # ABSTRACT: Work out what files have changed in the git branch
154              
155              
156             =pod
157              
158             =encoding UTF-8
159              
160             =head1 NAME
161              
162             App::GHPT::WorkSubmitter::ChangedFilesFactory - Work out what files have changed in the git branch
163              
164             =head1 VERSION
165              
166             version 2.000000
167              
168             =head1 SYNOPSIS
169              
170             =head1 DESCRIPTION
171              
172             Builds a L<App::GHPT::WorkSubmitter::ChangedFiles> from the git repo. Only concerns
173             itself with things that have been committed, doesn't care about what's in the
174             working directory at all.
175              
176             Used by L<App::GHPT::WorkSubmitter::AskPullRequestQuestions>.
177              
178             =for test_synopsis use v5.20;
179              
180             my $factory = App::GHPT::WorkSubmitter::ChangedFilesFactory->new(
181             merge_to_branch_name => 'main',
182             );
183              
184             my $changed_files = $factory->changed_files;
185              
186             say 'The files that were added or modified since branching are:';
187             say for $changed_files->changed_files;
188              
189             =head1 SUPPORT
190              
191             Bugs may be submitted through L<https://github.com/maxmind/App-GHPT/issues>.
192              
193             =head1 AUTHORS
194              
195             =over 4
196              
197             =item *
198              
199             Mark Fowler <mark@twoshortplanks.com>
200              
201             =item *
202              
203             Dave Rolsky <autarch@urth.org>
204              
205             =back
206              
207             =head1 COPYRIGHT AND LICENSE
208              
209             This software is Copyright (c) 2022 by MaxMind, Inc.
210              
211             This is free software, licensed under:
212              
213             The Artistic License 2.0 (GPL Compatible)
214              
215             =cut