1#!/usr/bin/env perl
2use strict;
3use warnings;
4
5my $args = join ' ' => @ARGV;
6
7my $afile = <<'EOT';
8diff --git a/a.file b/a.file
9index a7175683..7646fa7b 100644
10--- a/a.file
11+++ b/a.file
12@@ garbage @@
13 package A;
14 sub sub1 {
15-    my ($self) = @_;
16+    my $self = shift;
17     ...
18 }
19
20 sub sub2 {
21     ...
22 }
23
24+my @foo = ("X", "Y");
25
26- sub sub3 { ... }
27
28-my @foo = ("X", "Y");
29
30  1;
31EOT
32
33my $bfile = <<'EOT';
34diff --git a/b.file b/b.file
35index a7175683..7646fa7b 100644
36--- a/b.file
37+++ b/b.file
38@@ garbage @@
39 package B;
40
41+our $global = "yes";
42
43 sub sub1 {
44-    my ($self) = @_;
45+    my $self = shift;
46     ...
47 }
48
49 sub sub2 {
50     ...
51 }
52
53  1;
54EOT
55
56my $cfile = <<'EOT';
57diff --git a/c.file b/c.file
58index a7175683..7646fa7b 100644
59--- a/c.file
60+++ b/c.file
61@@ garbage @@
62 package C;
63
64 sub sub1 {
65-    my ($self) = @_;
66+    my $self = shift;
67     ...
68 }
69
70 sub sub2 {
71     ...
72 }
73
74  1;
75EOT
76
77my %out = (
78    'rev-parse HEAD'              => [0, "4570988f2c2bd26a1691a82766d5bf5c7524bcea\n"],
79    'rev-parse --short HEAD'      => [0, "4570988\n"],
80    'status -s'                   => [0, " M lib/App/Yath/Plugin/Git.pm\n"],
81    'rev-parse --abbrev-ref HEAD' => [0, "my.branch.foo\n"],
82
83    'merge-base --is-ancestor HEAD master' => [1, ""],
84    'diff HEAD --name-only'                => [0, ""],
85    'diff -U1000000 -W --minimal HEAD'     => [0, ""],
86
87    'merge-base --is-ancestor HEAD^ master' => [1, ""],
88    'diff HEAD^ --name-only'                => [0, "a.file\n"],
89    'diff -U1000000 -W --minimal HEAD^'     => [0, $afile],
90
91    'merge-base --is-ancestor HEAD^^ master' => [1, ""],
92    'diff HEAD^^ --name-only'                => [0, "a.file\nb.file\n"],
93    'diff -U1000000 -W --minimal HEAD^^'     => [0, $afile . $bfile],
94
95    'merge-base --is-ancestor HEAD^^^ master' => [0, ""],
96    'diff HEAD^^^ --name-only'                => [0, "a.file\nb.file\nc.file\n"],
97    'diff -U1000000 -W --minimal HEAD^^^'     => [0, $afile . $bfile . $cfile],
98);
99
100if (my $res = $out{$args}) {
101    my ($exit, $text) = @$res;
102    print $text;
103    exit $exit;
104}
105
106print STDERR "Invalid args: $args\n";
107exit 1;
108
109__END__
110
111diff --git a/lib/App/Yath/Plugin/Git.pm b/lib/App/Yath/Plugin/Git.pm
112index a7175683..7646fa7b 100644
113--- a/lib/App/Yath/Plugin/Git.pm
114+++ b/lib/App/Yath/Plugin/Git.pm
115@@ -1,170 +1,218 @@
116 package App::Yath::Plugin::Git;
117 use strict;
118 use warnings;
119
120 our $VERSION = '1.000045';
121
122 use IPC::Cmd qw/can_run/;
123 use Test2::Harness::Util::IPC qw/run_cmd/;
124 use parent 'App::Yath::Plugin';
125
126 use App::Yath::Options;
127
128 option_group {prefix => 'git', category => "Git Options"} => sub {
129     option change_base => (
130         type => 's',
131         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
132the branch/commit specified as the change base.",
133         long_examples  => [" master", " HEAD^", " df22abe4"],
134     );
135 };
136
137 my $GIT_CMD = can_run('git');
138 sub git_cmd { $ENV{GIT_COMMAND} || $GIT_CMD }
139
140 sub git_output {
141     my $class = shift;
142     my (@args) = @_;
143
144     my $cmd = $class->git_cmd or return;
145
146     my ($rh, $wh, $irh, $iwh);
147     pipe($rh, $wh) or die "No pipe: $!";
148     pipe($irh, $iwh) or die "No pipe: $!";
149     my $pid = run_cmd(stderr => $iwh, stdout => $wh, command => [$cmd, @args]);
150-    waitpid($pid, 0);
151-    return if $?;
152
153     close($wh);
154     close($iwh);
155+
156+    waitpid($pid, 0);
157+    if($?) {
158+        print STDERR <$irh>;
159+        return;
160+    }
161+
162     close($irh);
163
164     return <$rh>;
165 }
166
167 sub inject_run_data {
168     my $class  = shift;
169     my %params = @_;
170
171     my $meta   = $params{meta};
172     my $fields = $params{fields};
173
174     my $long_sha  = $ENV{GIT_LONG_SHA};
175     my $short_sha = $ENV{GIT_SHORT_SHA};
176     my $status    = $ENV{GIT_STATUS};
177     my $branch    = $ENV{GIT_BRANCH};
178
179