Debian Codegraph @

lib/App/Yath/Plugin/Git.pm

raw
1 package App::Yath::Plugin::Git;
2 use strict;
3 use warnings;
4
5 our $VERSION = '1.000179';
6
7 use IPC::Cmd qw/can_run/;
8 use Test2::Harness::Util::IPC qw/run_cmd/;
9 use parent 'App::Yath::Plugin';
10
11 use App::Yath::Options;
12
13 option_group {prefix => 'git', category => "Git Options"} => sub {
14 option change_base => (
15 type => 's',
16 description => "Find files changed by all commits in the current branch from most recent stopping when a commit is found that is also present in the history of the branch/commit specified as the change base.",
17 long_examples => [" master", " HEAD^", " df22abe4"],
18 );
19 };
20
21 my $GIT_CMD = can_run('git');
22 sub git_cmd { $ENV{GIT_COMMAND} || $GIT_CMD }
23
24 sub git_output {
25 my $class = shift;
26 my (@args) = @_;
27
28 my $cmd = $class->git_cmd or return sub {()};
29
30 my ($rh, $wh, $irh, $iwh);
31 pipe($rh, $wh) or die "No pipe: $!";
32 pipe($irh, $iwh) or die "No pipe: $!";
33 my $pid = run_cmd(stderr => $iwh, stdout => $wh, command => [$cmd, @args]);
34
35 close($wh);
36 close($iwh);
37
38 $rh->blocking(1);
39 $irh->blocking(0);
40
41 my $waited = 0;
42 return sub {
43 my $line = <$rh>;
44 return $line if defined $line;
45
46 unless ($waited++) {
47 local $?;
48 waitpid($pid, 0);
49 print STDERR <$irh> if $?;
50 close($irh);
51
52 # Try again
53 $line = <$rh>;
54 return $line if defined $line;
55 }
56
57 close($rh);
58 return;
59 };
60 }
61
62 sub inject_run_data {
63 my $class = shift;
64 my %params = @_;
65
66 my $meta = $params{meta};
67 my $fields = $params{fields};
68
69 my $long_sha = $ENV{GIT_LONG_SHA};
70 my $short_sha = $ENV{GIT_SHORT_SHA};
71 my $status = $ENV{GIT_STATUS};
72 my $branch = $ENV{GIT_BRANCH};
73
74 my @sets = (
75 [\$long_sha, 'rev-parse', 'HEAD'],
76 [\$short_sha, 'rev-parse', '--short', 'HEAD'],
77 [\$status, 'status', '-s'],
78 [\$branch, 'rev-parse', '--abbrev-ref', 'HEAD'],
79 );
80
81 for my $set (@sets) {
82 my ($var, @args) = @$set;
83 next if $$var; # Already set
84 my $output = $class->git_output(@args);
85
86 my @lines;
87 while (my $line = $output->()) {
88 push @lines => $line;
89 }
90
91 chomp($$var = join "\n" => @lines);
92 }
93
94 return unless $long_sha;
95
96 $meta->{git}->{sha} = $long_sha;
97 $meta->{git}->{status} = $status if $status;
98
99 if ($branch) {
100 $meta->{git}->{branch} = $branch;
101
102 my $short = length($branch) > 20 ? substr($branch, 0, 20) : $branch;
103
104 push @$fields => {name => 'git', details => $short, raw => $branch, data => $meta->{git}};
105 }
106 else {
107 $short_sha ||= substr($long_sha, 0, 16);
108 push @$fields => {name => 'git', details => $short_sha, raw => $long_sha, data => $meta->{git}};
109 }
110
111 return;
112 }
113
114 sub changed_diff {
115 my $class = shift;
116 my ($settings) = @_;
117
118 $class->_changed_diff($settings->git->change_base);
119 }
120
121 sub _changed_diff {
122 my $class = shift;
123 my ($base) = @_;
124
125 my $cmd = $class->git_cmd or return;
126
127 my $from = 'HEAD';
128
129 if ($base) {
130 $from .= "^" while system($cmd => 'merge-base', '--is-ancestor', $from, $base);
131 return $class->_diff_from($from);
132 }
133
134 my @files = $class->_diff_from($from);
135 return @files if @files;
136
137 return $class->_diff_from("${from}^");
138 }
139
140 sub _diff_from {
141 my $class = shift;
142 my ($from) = @_;
143 my $cmd = $class->git_cmd or return;
144
145 return (line_sub => $class->git_output('diff', '-U1000000', '-W', '--minimal', $from));
146 }
147
148 1;
149
150 __END__
151
152 =pod
153
154 =encoding UTF-8
155
156 =head1 NAME
157
158 App::Yath::Plugin::Git - Plugin to attach git data to a test run.
159
160 =head1 DESCRIPTION
161
162 This plugin will attach git data to your test logs if any is available.
163
164 =head1 SYNOPSIS
165
166 $ yath test -pGit ...
167
168 =head1 READING THE DATA
169
170 The data is attached to the 'run' entry in the log file. This can be seen
171 directly in the json data. The data is also easily accessible with
172 L<Test2::Harness::UI>.
173
174 The data will include the long sha, short sha, branch name, and a brief status.
175
176 =head1 PROVIDED OPTIONS
177
178 =head2 COMMAND OPTIONS
179
180 =head3 Git Options
181
182 =over 4
183
184 =item --git-change-base master
185
186 =item --git-change-base HEAD^
187
188 =item --git-change-base df22abe4
189
190 =item --no-git-change-base
191
192 Find files changed by all commits in the current branch from most recent stopping when a commit is found that is also present in the history of the branch/commit specified as the change base.
193
194
195 =back
196
197 =head1 SOURCE
198
199 The source code repository for Test2-Harness can be found at
200 F<http://github.com/Test-More/Test2-Harness/>.
201
202 =head1 MAINTAINERS
203
204 =over 4
205
206 =item Chad Granum E<lt>[email protected]<gt>
207
208 =back
209
210 =head1 AUTHORS
211
212 =over 4
213
214 =item Chad Granum E<lt>[email protected]<gt>
215
216 =back
217
218 =head1 COPYRIGHT
219
220 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
221
222 This program is free software; you can redistribute it and/or
223 modify it under the same terms as Perl itself.
224
225 See F<http://dev.perl.org/licenses/>
226
227 =cut