git-autofixup

filter: Statementstmt 82.6% 385 / 466 Branchbran 66.2% 147 / 222 Conditioncond 76.9% 30 / 39 MC/DCmcdc 62.5% 15 / 24 Subroutinesub 92.6% 38 / 41 Podpod n/a totaltotal 77.6% 615 / 792 CC 136 SCAR 29.2
CC
136
Cov
77%
print_hunk_blamediff
132.0
main
36.2
fixup_targets_from_adjacent_context
16.0
CRAP 18.6
↑ Summary blib/lib/App/Git/Autofixup.pm »
1 #!/usr/bin/perl
2 package Autofixup;
3 3 ▶use 5.008004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Getopt::Long qw(:config bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 our $VERSION = 0.005000;
15  
16 3 my $VERBOSE;
17 my @GIT_OPTIONS;
18  
19 # Strictness levels.
20 3 my @STRICTNESS_LEVELS = (
21 my $CONTEXT = 0,
22 my $ADJACENT = 1,
23 my $SURROUNDED = 2,
24 );
25  
26 3 my $usage =<<'END';
27 usage: git-autofixup [<options>] [<revision>] [--] [<path>...]
28  
29 -h show usage
30 --help show manpage
31 --version show version
32 -v, --verbose increase verbosity (use up to 2 times)
33 -c N, --context N set number of diff context lines (default 3)
34 -e, --exit-code use more detailed exit codes (see --help)
35 -s N, --strict N set strictness (default 0)
36 Assign a hunk to fixup a topic branch commit if:
37  
38 0: either only one topic branch commit is blamed in the hunk context or
39 blocks of added lines are adjacent to exactly one topic branch commit.
40 Removing upstream lines is allowed for this level.
41 1: blocks of added lines are adjacent to exactly one topic branch commit
42 2: blocks of added lines are surrounded by exactly one topic branch commit
43  
44 Regardless of strictness level, removed lines are correlated with the
45 commit they're blamed on, and all the blocks of changed lines in a hunk
46 must be correlated with the same topic branch commit in order to be
47 assigned to it. See the --help for more details.
48 -g ARG, --gitopt ARG
49 Specify option for git. Can be used multiple times. Deprecated in favor of
50 GIT_CONFIG_{COUNT,KEY,VALUE} environment variables; see `git help config`.
51 END
52  
53 # Parse hunks out of `git diff` output. Return an array of hunk hashrefs.
54 54 ▶sub parse_hunks {
Subroutine parse_hunks: called CC=10 SCAR=23.0
55 54 my $fh = shift;
56 54 my ($file_a, $file_b);
57 54 my @hunks;
58 54 my $line;
59 54 ▶ while ($line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 301 54 355
60 313 ▶ if ($line =~ /^--- (.*)/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($line =~ /^--- (.*)/) { } 61 252 313
elsif ($line =~ /^\+\+\+ (.*)/) { } 61 191 252
elsif ($line =~ /^\@@ -(\d+)(?:,(\d+))? \+\d+(?:,\d+)? \@@/) { } 63 128 191
61 61 $file_a = dequote_diff_filename($1);
62 } elsif ($line =~ /^\+\+\+ (.*)/) {
63 61 $file_b = dequote_diff_filename($1);
64 } elsif ($line =~ /^@@ -(\d+)(?:,(\d+))? \+\d+(?:,\d+)? @@/) {
65 63 my $header = $line;
66  
67 63 ▶ next if $file_a ne $file_b; # Ignore creations and deletions.
Branch: 1 branch
BranchTrueFalseTotal
if $file_a ne $file_b 3 60 63
68  
69 60 my $lines = [];
70 60 while (1) {
71 218 $line = <$fh>;
72 218 ▶ if (!defined($line) || $line =~ /^[^ +\\-]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $line or $line =~ /^[^ +\\-]/) 60 158 218
Condition100%3/3
not defined $line or $line =~ /^[^ +\\-]/
l!l&&r!l&&!r
4812158
MC/DC100%2/2
not defined $line or $line =~ /^[^ +\\-]/
not defined $line $line =~ /^[^ +\\-]/
Truth table100%3/3
not(A) or B
A= not defined $lineB= $line =~ /^[^ +\\-]/
ABresult
1X1
011
000
73 60 last;
74 }
75 158 push @{$lines}, $line;
76 }
77  
78 60 ▶ push(@hunks, {
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 22 38 60
79 file => $file_a,
80 start => $1,
81 count => defined($2) ? $2 : 1,
82 header => $header,
83 lines => $lines,
84 });
85 # The next line after a hunk could be a header for the next commit
86 # or hunk.
87 60 ▶ redo if defined $line;
Branch: 1 branch
BranchTrueFalseTotal
if defined $line 12 48 60
88 }
89 }
90 54 return @hunks;
91 }
92  
93 # Dequote and unescape filenames that appear in diff output.
94 #
95 # If the filename is otherwise "normal" but contains spaces it's followed by a
96 # trailing tab, and if it contains uncommon control characters or non-ASCII
97 # characters, then the filename gets surrounded in double-quotes and non-ASCII
98 # characters get replaced with octal escape sequences.
99 #
100 # For details about exactly what gets quoted, see the sq_lookup array in
101 # git/quote.c.
102 #
103 # Assume --no-prefix or similar has been used and the diff doesn't include
104 # src/dst prefixes.
105 122 ▶sub dequote_diff_filename {
Subroutine dequote_diff_filename: called CC=2 SCAR=6.9
106 122 $_ = shift;
107 122 s/\t$//m; # Remove trailing tab.
108 122 ▶ if (startswith($_, '"')) {
Branch: 1 branch
BranchTrueFalseTotal
if (startswith($_, "\"")) 4 118 122
109 4 s/^"|"$//gm; # Remove surrounding quotes.
110 # Replace octal and control character escapes.
111 4 s/\\((?:\d{3})|(?:[abtnvfr"\\]))/"qq(\\$1)"/eeg;
112 }
113 122 return $_;
114 }
115  
116 307 ▶sub git_cmd {
Subroutine git_cmd: called CC=1 SCAR=0.0
117 307 return ('git', @GIT_OPTIONS, @_);
118 }
119  
120 # With a linear git history there'll be a single merge base that's easy to
121 # refer to with @{upstream}, but during an interactive rebase we need to get
122 # the "current" branch from the rebase metadata.
123 #
124 # Unusual cases:
125 #
126 # While there can be multiple merge bases if there have been criss-cross
127 # merges, there'll still be a single fork point unless the relevant reflog
128 # entries have already been garbage-collected.
129 #
130 # When multiple upstreams are configured via `branch.<name>.merge` in git's
131 # config the most correct approach is probably to find the fork-point for each
132 # merge value and return those. But it seems unlikely that someone is doing
133 # octopus merges and using git-autofixup, so we're not handling that specially
134 # currently.
135 8 ▶sub find_merge_bases {
Subroutine find_merge_bases: called CC=5 SCAR=16.1
136 8 my $upstream = '@{upstream}';
137  
138 # If an interactive rebase is in progress, derive the upstream from the
139 # rebase meatadata.
140 8 my $gitdir = git_dir();
141 8 ▶ if (-e "$gitdir/rebase-merge") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$gitdir/rebase-merge") 2 6 8
142 2 my $branch = slurp("$gitdir/rebase-merge/head-name");
143 2 chomp $branch;
144 2 $branch =~ s#^refs/heads/##;
145 2 $upstream = "$branch\@{upstream}";
146 }
147  
148 # `git merge-base` will fail if there's no tracking branch. In that case
149 # redirect stderr and communicate failure by returning an empty list. Also,
150 # with the --fork-point option, no merge bases are returned if the relevant
151 # reflog entries have been GC'd, so fall back to normal merge-bases.
152 8 my @merge_bases = ();
153 8 my ($out, $err, $exit_code) = capture(qw(git merge-base --all --fork-point), $upstream, 'HEAD');
154 8 ▶ if ($exit_code == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code == 0) { } 6 2 8
155 6 @merge_bases = map {chomp; $_} split(/\n/, $out);
156 } else {
157 2 my ($out, $err, $exit_code) = capture(qw(git merge-base --all), $upstream, 'HEAD');
158 2 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 0 2 2
159 0 die "git merge-base: $err";
160 }
161 2 @merge_bases = map {chomp; $_} split("\n", $out);
162 }
163  
164 8 ▶ return wantarray ? @merge_bases : \@merge_bases;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 3 8
165 }
166  
167 11 ▶sub git_dir {
Subroutine git_dir: called CC=2 SCAR=7.9
168 11 my ($out, $err, $exit_code) = capture(qw(git rev-parse --git-dir));
169 11 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 0 11 11
170 0 warn "git rev-parse --git-dir: $err\n";
171 0 die "Can't find repo's git dir\n";
172 }
173 11 chomp $out;
174 11 return $out;
175 }
176  
177 54 ▶sub toplevel_dir {
Subroutine toplevel_dir: called CC=2 SCAR=7.9
178 54 my ($out, $err, $exit_code) = capture(qw(git rev-parse --show-toplevel));
179 54 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 0 54 54
180 0 warn "git rev-parse --show-toplevel: $err\n";
181 0 die "Can't find repo's toplevel dir\n";
182 }
183 54 chomp $out;
184 54 return $out;
185 }
186  
187 # Run the given command, capture stdout and stderr, and return an array of
188 # (stdout, stderr, exit_code).
189 168 ▶sub capture {
Subroutine capture: called CC=5 SCAR=16.7
190 168 ▶ open(my $out_fh, '>', undef) or die "create stdout tempfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">", undef or die "create stdout tempfile: $!" 168 0 168
191 168 ▶ open(my $err_fh, '>', undef) or die "create stderr tempfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $err_fh, ">", undef or die "create stderr tempfile: $!" 168 0 168
192 168 my $pid = open3(my $in_fh, $out_fh, $err_fh, @_);
193 168 waitpid $pid, 0;
194 168 ▶ if ($? & 127) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? & 127) 0 168 168
195 0 my $signal = $? & 127;
196 0 die "capture: child died with signal $signal; exiting";
197 }
198 168 my $exit_code = $? >> 8;
199 168 local $/; # slurp
200 168 my $stdout = readline $out_fh;
201 168 my $stderr = readline $err_fh;
202 168 my @array = ($stdout, $stderr, $exit_code);
203 168 ▶ return wantarray ? @array : \@array;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 165 3 168
204 }
205  
206 # Return a description of what $? means.
207 3 ▶sub child_error_desc {
Subroutine child_error_desc: called CC=3 SCAR=13.3
208 3 my $err = shift;
209 3 ▶ if ($err == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err == -1) { } 0 3 3
elsif ($err & 127) { } 0 3 3
210 0 return "failed to execute: $!";
211 } elsif ($err & 127) {
212 0 return "died with signal " . ($err & 127);
213 } else {
214 3 return "exited with " . ($err >> 8);
215 }
216 }
217  
218 2 ▶sub slurp {
Subroutine slurp: called CC=2 SCAR=7.0
219 2 my $filename = shift;
220 2 ▶ open my $fh, '<', $filename or die "slurp $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "slurp ${filename}: $!" 2 0 2
221 2 local $/;
222 2 my $content = readline $fh;
223 2 return $content;
224 }
225  
226 54 ▶sub summary_for_commits {
Subroutine summary_for_commits: called CC=3 SCAR=11.0
227 54 my @upstreams = @_;
228 54 my %commits;
229 54 my $negative = join(" ", map {"^$_"} @upstreams);
230 54 my @lines = qx(git log --no-merges --format=%H:%s HEAD $negative);
231 54 ▶ die "git log: " . child_error_desc($?) if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 54 54
232 54 for (@lines) {
233 64 chomp;
234 64 my ($sha, $msg) = split ':', $_, 2;
235 64 $commits{$sha} = $msg;
236 }
237 54 return \%commits;
238 }
239  
240 # Return targets of fixup!/squash! commits.
241 54 ▶sub sha_aliases {
Subroutine sha_aliases: called CC=7 SCAR=20.6
242 54 my $summary_for = shift;
243 54 my %aliases;
244 54 my @targets = keys(%{$summary_for});
245 54 for my $sha (@targets) {
246 64 my $summary = $summary_for->{$sha};
247 64 ▶ next if $summary !~ /^(?:fixup|squash)! (.*)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $summary =~ /^(?:fixup|squash)! (.*)/ 61 3 64
248 3 my $prefix = $1;
249 3 ▶ if ($prefix =~ /^(?:(?:fixup|squash)! ){2}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prefix =~ /^(?:(?:fixup|squash)! ){2}/) 0 3 3
250 0 die "fixup commits for fixup commits aren't supported: $sha";
251 }
252 3 my @matches = grep {startswith($summary_for->{$_}, $prefix)} @targets;
253 3 ▶ if (@matches > 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@matches > 1) { } 0 3 3
elsif (@matches == 0) { } 0 3 3
elsif (@matches == 1) { } 3 0 3
254 0 die "ambiguous fixup commit target: multiple commit summaries start with: $prefix\n";
255 } elsif (@matches == 0) {
256 0 die "no fixup target in topic branch: $sha\n";
257 } elsif (@matches == 1) {
258 3 $aliases{$sha} = $matches[0];
259 }
260 }
261 54 return \%aliases;
262 }
263  
264 # Given hunk, blame, and commit data, return the SHA for the commit the hunk
265 # should fixup, or undef if an appropriate commit isn't found.
266 60 ▶sub fixup_sha {
Subroutine fixup_sha: called CC=11 SCAR=24.6
267 60 my $args = shift;
268 60 my $hunk = $args->{hunk};
269 60 my $blame = $args->{blame};
270 60 my $summary_for = $args->{summary_for};
271 60 my $strict = $args->{strict};
272 60 ▶ if (grep {!defined} ($hunk, $blame, $summary_for, $strict)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_;} $hunk, $blame, $summary_for, $strict) 0 60 60
273 0 croak 'missing argument';
274 }
275  
276 60 my @targets;
277 60 ▶ if ($args->{strict} == $CONTEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'strict'} == $CONTEXT) { } 41 19 60
278 41 @targets = fixup_targets_from_all_context($args);
279 41 my @topic_targets = grep {defined $summary_for->{$_}} @targets;
280 41 ▶ if (@topic_targets > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@topic_targets > 1) 3 38 41
281 # The context assignment is ambiguous, but an adjacency assignment
282 # might not be.
283 3 @targets = fixup_targets_from_adjacent_context($args);
284 }
285 } else {
286 19 @targets = fixup_targets_from_adjacent_context($args);
287 }
288  
289 60 my $upstream_is_blamed = grep {!defined $summary_for->{$_}} @targets;
290 60 my @topic_targets = grep {defined $summary_for->{$_}} @targets;
291 60 ▶ if ($strict && $upstream_is_blamed) {
Branch: 3 branches
BranchTrueFalseTotal
if ($strict and $upstream_is_blamed) { } 4 56 60
elsif (@topic_targets > 1) { } 0 56 56
elsif (@topic_targets == 0) { } 5 51 56
Condition100%3/3
$strict and $upstream_is_blamed
!ll&&!rl&&r
41154
MC/DC100%2/2
$strict and $upstream_is_blamed
$strict $upstream_is_blamed
Truth table100%3/3
A and B
A= $strictB= $upstream_is_blamed
ABresult
0X0
100
111
292 4 ▶ $VERBOSE && print hunk_desc($hunk), " changes lines blamed on upstream\n";
Branch: 1 branch
BranchTrueFalseTotal
$VERBOSE and print hunk_desc($hunk), " changes lines blamed on upstream\n" 0 4 4
293 4 return;
294 } elsif (@topic_targets > 1) {
295 0 ▶ $VERBOSE && print hunk_desc($hunk), " has multiple targets\n";
Branch: 1 branch
BranchTrueFalseTotal
$VERBOSE and print hunk_desc($hunk), " has multiple targets\n" 0 0 0
296 0 return;
297 } elsif (@topic_targets == 0) {
298 5 ▶ $VERBOSE && print hunk_desc($hunk), " has no targets\n";
Branch: 1 branch
BranchTrueFalseTotal
$VERBOSE and print hunk_desc($hunk), " has no targets\n" 0 5 5
299 5 return;
300 }
301 51 return $topic_targets[0];
302 }
303  
304 0 ▶sub hunk_desc {
Subroutine hunk_desc: not called CC=1 SCAR=6.9
305 0 my $hunk = shift;
306 return join " ", (
307 $hunk->{file},
308 0 $hunk->{header} =~ /(@@[^@]*@@)/,
309 );
310 }
311  
312 41 ▶sub fixup_targets_from_all_context {
Subroutine fixup_targets_from_all_context: called CC=3 SCAR=11.1
313 41 my $args = shift;
314 41 my ($hunk, $blame, $summary_for) = @{$args}{qw(hunk blame summary_for)};
315 41 ▶ croak 'missing argument' if grep {!defined} ($hunk, $blame, $summary_for);
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} $hunk, $blame, $summary_for 0 41 41
316  
317 41 my @targets = uniq(map {$_->{sha}} values(%{$blame}));
318 41 ▶ return wantarray ? @targets : \@targets;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 41 0 41
319 }
320  
321 113 ▶sub uniq {
Subroutine uniq: called CC=1 SCAR=0.0
322 113 my %seen;
323 113 return grep {!$seen{$_}++} @_;
324 }
325  
326 22 ▶sub fixup_targets_from_adjacent_context {
Subroutine fixup_targets_from_adjacent_context: called CC=16 SCAR=27.8
327 22 my $args = shift;
328 22 my $hunk = $args->{hunk};
329 22 my $blame = $args->{blame};
330 22 my $summary_for = $args->{summary_for};
331 22 my $strict = $args->{strict};
332 22 ▶ if (grep {!defined} ($hunk, $blame, $summary_for, $strict)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_;} $hunk, $blame, $summary_for, $strict) 0 22 22
333 0 croak 'missing argument';
334 }
335  
336 22 my $blame_indexes = blame_indexes($hunk);
337  
338 22 my %blamed;
339 22 my $diff = $hunk->{lines};
340 22 ▶ for (my $di = 0; $di < @{$diff}; $di++) { # diff index
Branch: 1 branch
BranchTrueFalseTotal
if ($di < @{$diff;}) 56 22 78
341 56 my $bi = $blame_indexes->[$di];
342 56 my $line = $diff->[$di];
343 56 ▶ if (startswith($line, '-')) {
Branch: 2 branches
BranchTrueFalseTotal
if (startswith($line, '-')) { } 17 39 56
elsif (startswith($line, '+')) { } 18 21 39
344 17 my $sha = $blame->{$bi}{sha};
345 17 $blamed{$sha} = 1;
346 } elsif (startswith($line, '+')) {
347 18 my @lines;
348 18 ▶ if ($di > 0 && defined $blame->{$bi-1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($di > 0 and defined $$blame{$bi - 1}) 17 1 18
Condition66%2/3
$di > 0 and defined $$blame{$bi - 1}
!ll&&!rl&&r
1017
MC/DC50%1/2
$di > 0 and defined $$blame{$bi - 1}
$di > 0 defined $$blame{$bi - 1}
Truth table66%2/3
A and B
A= $di > 0B= defined $$blame{$bi - 1}
ABresult
0X0
100
111
349 17 push @lines, $bi-1;
350 }
351 18 ▶ if (defined $blame->{$bi}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $blame->{$bi}) 5 13 18
352 5 push @lines, $bi;
353 }
354 18 my @adjacent_shas = uniq(map {$_->{sha}} @{$blame}{@lines});
355 18 my @target_shas = grep {defined $summary_for->{$_}} @adjacent_shas;
356 # Note that lines at the beginning or end of a file can be
357 # "surrounded" by a single line.
358 18 ▶ my $is_surrounded = @target_shas > 0
Condition100%6/6
@target_shas > 0 && @target_shas == @adjacent_shas && $target_shas[0] eq $target_shas[-1]
!ll&&!rl&&r
4113
@target_shas > 0 and @target_shas == @adjacent_shas
!ll&&!rl&&r
2214
MC/DC100%3/3
@target_shas > 0 && @target_shas == @adjacent_shas && $target_shas[0] eq $target_shas[-1]
@target_shas > 0 @target_shas == @adjacent_shas $target_shas[0] eq $target_shas[-1]
Truth table100%4/4
A and B && C
A= @target_shas > 0B= @target_shas == @adjacent_shasC= $target_shas[0] eq $target_shas[-1]
ABCresult
0XX0
10X0
1100
1111
359 && @target_shas == @adjacent_shas
360 && $target_shas[0] eq $target_shas[-1];
361 18 my $is_adjacent = @target_shas == 1;
362 18 ▶ if ($is_surrounded || ($strict < $SURROUNDED && $is_adjacent)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_surrounded or $strict < $SURROUNDED and $is_adjacent) 14 4 18
Condition100%6/6
$is_surrounded or $strict < $SURROUNDED and $is_adjacent
l!l&&r!l&&!r
1314
$strict < $SURROUNDED and $is_adjacent
!ll&&!rl&&r
221
MC/DC100%3/3
$is_surrounded or $strict < $SURROUNDED and $is_adjacent
$is_surrounded $strict < $SURROUNDED $is_adjacent
Truth table100%4/4
A or B and C
A= $is_surroundedB= $strict < $SURROUNDEDC= $is_adjacent
ABCresult
1XX1
0111
00X0
0100
363 14 $blamed{$target_shas[0]} = 1;
364 }
365 18 ▶ while ($di < @$diff-1 && startswith($diff->[$di+1], '+')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($di < @$diff - 1 and startswith($$diff[$di + 1], "+")) 4 18 22
Condition100%3/3
$di < @$diff - 1 and startswith($$diff[$di + 1], "+")
!ll&&!rl&&r
1354
MC/DC100%2/2
$di < @$diff - 1 and startswith($$diff[$di + 1], "+")
$di < @$diff - 1 startswith($$diff[$di + 1], "+")
Truth table100%3/3
A and B
A= $di < @$diff - 1B= startswith($$diff[$di + 1], "+")
ABresult
0X0
100
111
366 4 $di++;
367 }
368 }
369 }
370 22 my @targets = keys %blamed;
371 22 ▶ return wantarray ? @targets : \@targets;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 22 0 22
372 }
373  
374 1187 ▶sub startswith {
Subroutine startswith: called CC=1 SCAR=0.0
375 1187 my ($haystack, $needle) = @_;
376 1187 return index($haystack, $needle, 0) == 0;
377 }
378  
379 # Map lines in a hunk's diff to the corresponding `git blame HEAD` output.
380 22 ▶sub blame_indexes {
Subroutine blame_indexes: called CC=4 SCAR=13.9
381 22 my $hunk = shift;
382 22 my @indexes;
383 22 my $bi = $hunk->{start};
384 22 ▶ for (my $di = 0; $di < @{$hunk->{lines}}; $di++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($di < @{$hunk->{"lines"};}) 60 22 82
385 60 push @indexes, $bi;
386 60 my $first = substr($hunk->{lines}[$di], 0, 1);
387 60 ▶ if ($first eq '-' or $first eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "-" or $first eq " ") 38 22 60
Condition100%3/3
$first eq "-" or $first eq " "
l!l&&r!l&&!r
172122
MC/DC100%2/2
$first eq "-" or $first eq " "
$first eq "-" $first eq " "
Truth table100%3/3
A or B
A= $first eq "-"B= $first eq " "
ABresult
1X1
011
000
388 38 $bi++;
389 }
390 # Don't increment $bi for added lines.
391 }
392 22 return \@indexes;
393 }
394  
395 0 ▶sub print_hunk_blamediff {
Subroutine print_hunk_blamediff: not called CC=11 SCAR=48.8
396 0 my $args = shift;
397 0 my $fh = $args->{fh};
398 0 my $hunk = $args->{hunk};
399 0 my $summary_for = $args->{summary_for};
400 0 my $blame = $args->{blame};
401 0 my $blame_indexes = $args->{blame_indexes};
402 0 ▶ if (grep {!defined} ($fh, $hunk, $summary_for, $blame, $blame_indexes)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_;} $fh, $hunk, $summary_for, $blame, $blame_indexes) 0 0 0
403 0 croak 'missing argument';
404 }
405  
406 0 my $format = "%-8.8s|%4.4s|%-30.30s|%-30.30s\n";
407 0 ▶ for (my $i = 0; $i < @{$hunk->{lines}}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$hunk->{"lines"};}) 0 0 0
408 0 my $line_r = $hunk->{lines}[$i];
409 0 my $bi = $blame_indexes->[$i];
410 0 ▶ my $sha = defined $blame->{$bi} ? $blame->{$bi}{sha} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $blame->{$bi} ? : 0 0 0
411  
412 0 ▶ my $display_sha = defined($sha) ? $sha : q{};
Branch: 1 branch
BranchTrueFalseTotal
defined $sha ? : 0 0 0
413 0 my $display_bi = $bi;
414 0 ▶ if (startswith($line_r, '+')) {
Branch: 1 branch
BranchTrueFalseTotal
if (startswith($line_r, "+")) 0 0 0
415 0 $display_sha = q{}; # For added lines.
416 0 $display_bi = q{};
417 }
418 0 ▶ if (defined($sha) && !defined($summary_for->{$sha})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sha and not defined $summary_for->{$sha}) 0 0 0
Condition0%0/3
defined $sha and not defined $summary_for->{$sha}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sha and not defined $summary_for->{$sha}
defined $sha not defined $summary_for->{$sha}
Truth table0%0/3
A and not(B)
A= defined $shaB= not defined $summary_for->{$sha}
ABresult
0X0
100
111
419 # For lines from before the given upstream revision.
420 0 $display_sha = '^';
421 }
422  
423 0 my $line_l = '';
424 0 ▶ if (defined $blame->{$bi} && !startswith($line_r, '+')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $blame->{$bi} and not startswith($line_r, "+")) 0 0 0
Condition0%0/3
defined $blame->{$bi} and not startswith($line_r, "+")
!ll&&!rl&&r
000
MC/DC0%0/2
defined $blame->{$bi} and not startswith($line_r, "+")
defined $blame->{$bi} not startswith($line_r, "+")
Truth table0%0/3
A and not(B)
A= defined $blame->{$bi}B= not startswith($line_r, "+")
ABresult
0X0
100
111
425 0 $line_l = $blame->{$bi}{text};
426 }
427  
428 0 for ($line_l, $line_r) {
429 # For the table to line up, tabs need to be converted to a string of fixed width.
430 0 s/\t/^I/g;
431 # Remove trailing newlines and carriage returns. If more trailing
432 # whitespace is removed, that's fine.
433 0 $_ = rtrim($_);
434 }
435  
436 0 printf {$fh} $format, $display_sha, $display_bi, $line_l, $line_r;
437 }
438 0 print {$fh} "\n";
439 0 return;
440 }
441  
442 0 ▶sub rtrim {
Subroutine rtrim: not called CC=1 SCAR=6.9
443 0 my $s = shift;
444 0 $s =~ s/\s+\z//;
445 0 return $s;
446 }
447  
448 60 ▶sub blame {
Subroutine blame: called CC=8 SCAR=20.8
449 60 my ($hunk, $alias_for, $grafts_file) = @_;
450 60 ▶ if ($hunk->{count} == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hunk->{"count"} == 0) 1 59 60
451 1 return {};
452 }
453 59 my @cmd = git_cmd(
454 'blame', '--porcelain',
455 '-L' => "$hunk->{start},+$hunk->{count}",
456 '-S' => $grafts_file, 'HEAD',
457 '--', "$hunk->{file}");
458 59 my %blame;
459 59 my ($sha, $line_num);
460 59 ▶ open(my $fh, '-|', @cmd) or die "run git blame: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @cmd or die "run git blame: $!\n" 59 0 59
461 59 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 955 59 1014
462 955 ▶ if ($line =~ /^([0-9a-f]{40}) \d+ (\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([0-9a-f]{40}) \d+ (\d+)/) 96 859 955
463 96 ($sha, $line_num) = ($1, $2);
464 }
465 955 ▶ if (startswith($line, "\t")) {
Branch: 1 branch
BranchTrueFalseTotal
if (startswith($line, "\t")) 96 859 955
466 96 ▶ if (defined $alias_for->{$sha}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alias_for->{$sha}) 3 93 96
467 3 $sha = $alias_for->{$sha};
468 }
469 96 $blame{$line_num} = {sha => $sha, text => substr($line, 1)};
470 }
471 }
472 59 ▶ close($fh) or die "git blame: " . child_error_desc($?) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die 'git blame: ' . child_error_desc($?) . "\n" 59 0 59
473 59 return \%blame;
474 }
475  
476 54 ▶sub diff_hunks {
Subroutine diff_hunks: called CC=6 SCAR=18.1
477 54 my $num_context_lines = shift;
478 54 my @files = @_;
479 54 my @cmd = git_cmd(qw(diff --no-prefix --no-ext-diff --ignore-submodules), "-U$num_context_lines");
480 54 ▶ if (is_index_dirty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_index_dirty()) 3 51 54
481 3 push @cmd, "--cached";
482 }
483 54 ▶ if (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 6 48 54
484 6 push @cmd, '--', @files;
485 }
486 54 ▶ open(my $fh, '-|', @cmd) or die "run git diff: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", @cmd or die "run git diff: $!" 54 0 54
487 54 my @hunks = parse_hunks($fh, keep_lines => 1);
488 54 ▶ close($fh) or die "git diff: " . child_error_desc($?) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die 'git diff: ' . child_error_desc($?) . "\n" 54 0 54
489 54 ▶ return wantarray ? @hunks : \@hunks;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 54 54
490 }
491  
492 43 ▶sub commit_fixup {
Subroutine commit_fixup: called CC=5 SCAR=16.6
493 43 my ($sha, $hunks) = @_;
494 43 ▶ open my $fh, '|-', git_cmd(qw(apply --unidiff-zero --cached -)) or die "git apply: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "|-", git_cmd(("apply", "--unidiff-zero", "--cached", "-")) or die "git apply: $!\n" 43 0 43
495 43 for my $hunk (@{$hunks}) {
496 51 print({$fh}
497 "--- a/$hunk->{file}\n",
498 "+++ b/$hunk->{file}\n",
499 $hunk->{header},
500 51 @{$hunk->{lines}},
501 );
502 }
503 43 ▶ close $fh or die "git apply: " . child_error_desc($?) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die 'git apply: ' . child_error_desc($?) . "\n" 40 3 43
504 40 my (undef, $err, $exit_code) = capture(git_cmd('commit', "--fixup=$sha"));
505 40 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 0 40 40
506 0 warn "git commit: $err\n";
507 0 die "git commit exited with $exit_code\n";
508 }
509 40 return;
510 }
511  
512 108 ▶sub is_index_dirty {
Subroutine is_index_dirty: called CC=1 SCAR=0.0
513 108 return system(git_cmd(qw(diff-index --cached HEAD --quiet))) != 0;
514 }
515  
516 54 ▶sub fixup_hunks_by_sha {
Subroutine fixup_hunks_by_sha: called CC=7 SCAR=20.2
517 54 my $args = shift;
518 54 my $hunks = $args->{hunks};
519 54 my $blame_for = $args->{blame_for};
520 54 my $summary_for = $args->{summary_for};
521 54 my $strict = $args->{strict};
522 54 ▶ if (grep {!defined} ($hunks, $blame_for, $summary_for, $strict)) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not defined $_;} $hunks, $blame_for, $summary_for, $strict) 0 54 54
523 0 croak 'missing argument';
524 }
525  
526 54 my %hunks_for;
527 54 for my $hunk (@{$hunks}) {
528 60 my $blame = $blame_for->{$hunk};
529 60 my $sha = fixup_sha({
530 hunk => $hunk,
531 blame => $blame,
532 summary_for => $summary_for,
533 strict => $strict,
534 });
535 60 ▶ if ($sha && $VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sha and $VERBOSE) 0 60 60
Condition66%2/3
$sha and $VERBOSE
!ll&&!rl&&r
9510
MC/DC0%0/2
$sha and $VERBOSE
$sha $VERBOSE
Truth table66%2/3
A and B
A= $shaB= $VERBOSE
ABresult
0X0
100
111
536 printf "%s fixes %s %s\n",
537 hunk_desc($hunk),
538 substr($sha, 0, 8),
539 0 $summary_for->{$sha};
540 }
541 60 ▶ if ($VERBOSE > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($VERBOSE > 1) 0 60 60
542 0 print_hunk_blamediff({
543 fh => *STDOUT,
544 hunk => $hunk,
545 summary_for => $summary_for,
546 blame => $blame,
547 blame_indexes => blame_indexes($hunk)
548 });
549 }
550 60 ▶ next if !$sha;
Branch: 1 branch
BranchTrueFalseTotal
unless $sha 9 51 60
551 51 push @{$hunks_for{$sha}}, $hunk;
552 }
553 54 return \%hunks_for;
554 }
555  
556 # Return SHAs in some consistent order.
557 #
558 # Currently they're ordered by how early their assigned hunks appear in the
559 # diff output. This assumes $hunks is in the order it was parsed from the diff.
560 # This ordering seems nice since it'd be similar to the order a human would
561 # make commits in if they were working their way down the diff.
562 54 ▶sub ordered_shas {
Subroutine ordered_shas: called CC=3 SCAR=11.0
563 54 my $hunks = shift;
564 54 my $sha_for = shift;
565 54 my @ordered = ();
566 54 for my $hunk (@{$hunks}) {
567 60 ▶ if (defined $sha_for->{$hunk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sha_for->{$hunk}) 51 9 60
568 51 push @ordered, $sha_for->{$hunk};
569 }
570 }
571 54 return uniq(@ordered);
572 }
573  
574 # Reverse the sha->hunks hashef and return a hunk->sha hashref.
575 54 ▶sub sha_for_hunk_map {
Subroutine sha_for_hunk_map: called CC=4 SCAR=14.0
576 54 my $hunks_for = shift;
577 54 my %sha_for;
578 54 for my $sha (keys %{$hunks_for}) {
579 43 for my $hunk (@{$hunks_for->{$sha}}) {
580 51 ▶ if (defined $sha_for{$hunk}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sha_for{$hunk}) 0 51 51
581 0 die "multiple SHAs for hunk"; # This should never happen.
582 }
583 51 $sha_for{$hunk} = $sha;
584 }
585 }
586 54 return \%sha_for;
587 }
588  
589 47 ▶sub exit_code {
Subroutine exit_code: called CC=6 SCAR=18.0
590 47 my ($hunks, $hunks_for) = @_;
591 47 my $hunk_count = scalar @{$hunks};
592  
593 47 my $assigned_hunk_count = 0;
594 47 for (values %{$hunks_for}) {
595 36 $assigned_hunk_count += @{$_};
596 }
597  
598 47 my $rc;
599 47 ▶ if ($hunk_count == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($hunk_count == 0) { } 6 41 47
elsif ($assigned_hunk_count == 0) { } 7 34 41
elsif ($assigned_hunk_count < $hunk_count) { } 1 33 34
elsif ($hunk_count == $assigned_hunk_count) { } 33 0 33
600 6 $rc = 3; # no hunks to assign
601 } elsif ($assigned_hunk_count == 0) {
602 7 $rc = 2; # hunks exist, but none assigned
603 } elsif ($assigned_hunk_count < $hunk_count) {
604 1 $rc = 1; # not all hunks assigned
605 } elsif ($hunk_count == $assigned_hunk_count) {
606 33 $rc = 0; # all hunks assigned
607 } else {
608 0 die "unexpected conditions when choosing exit code";
609 }
610 47 return $rc;
611 }
612  
613 # Create a temporary index so we can craft commits with already-staged hunks.
614 # Return a File::Temp object so the caller has control over its lifetime.
615 3 ▶sub create_temp_index {
Subroutine create_temp_index: called CC=7 SCAR=20.3
616 3 my $old_index = shift;
617 3 my $tempfile = File::Temp->new(
618 TEMPLATE => 'git-autofixup_index.XXXXXX',
619 DIR => File::Spec->tmpdir());
620 # The index ought to be equivalent to HEAD. The fastest way to create it
621 # is to start with the current index, and subtract the changes since HEAD.
622 3 ▶ if (not defined($old_index)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $old_index) 3 0 3
623 3 my $gitdir = git_dir();
624 3 $old_index = "$gitdir/index";
625 }
626 3 ▶ close $tempfile or die "close temp index: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $tempfile or die "close temp index: $!" 3 0 3
627 3 ▶ copy($old_index, $tempfile->filename()) or die "Can't copy Git index '$old_index' to '$tempfile': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
copy($old_index, $tempfile->filename) or die "Can't copy Git index '${old_index}' to '${tempfile}': $!\n" 3 0 3
628  
629 3 $ENV{GIT_INDEX_FILE} = $tempfile->filename();
630  
631 # Remove any staged changes from the new index - we want to turn them into fixup commits.
632 3 my $index_changes = qx(git diff-index --patch --no-prefix --no-ext-diff --ignore-submodules --cached HEAD);
633 3 ▶ die "git diff-index: " . child_error_desc($?) if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 3 3
634 3 ▶ open my $fh, '|-', git_cmd(qw(apply -p0 --cached --whitespace=nowarn --reverse -)) or die "run git apply: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "|-", git_cmd(("apply", "-p0", "--cached", "--whitespace=nowarn", "--reverse", "-")) or die "run git apply: $!\n" 3 0 3
635 3 print $fh $index_changes;
636 3 ▶ close $fh or die "git apply: " . child_error_desc($?) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die 'git apply: ' . child_error_desc($?) . "\n" 3 0 3
637  
638 3 return $tempfile;
639 }
640  
641 # Create a grafts file for `git blame -S` that basically says the upstream
642 # commit doesn't have any parents, resulting in blame only searching back as
643 # far back as the upstream commit.
644 54 ▶sub create_grafts_file {
Subroutine create_grafts_file: called CC=4 SCAR=14.1
645 54 my @upstreams = @_;
646 54 my $grafts_file = File::Temp->new(
647 TEMPLATE => 'git-autofixup_grafts.XXXXXX',
648 DIR => File::Spec->tmpdir());
649 54 ▶ open(my $fh, '>', $grafts_file) or die "Can't open $grafts_file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $grafts_file or die "Can't open ${grafts_file}: $!\n" 54 0 54
650 54 for (@upstreams) {
651 55 print $fh $_, "\n";
652 }
653 54 ▶ close($fh) or die "close grafts file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "close grafts file: $!\n" 54 0 54
654 54 return $grafts_file;
655 }
656  
657 50 ▶sub rev_parse {
Subroutine rev_parse: called CC=2 SCAR=7.6
658 50 my $rev = shift;
659 50 my ($out, $err, $exit_code) = capture(qw(git rev-parse --verify --end-of-options), $rev);
660 50 ▶ if ($exit_code != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 0 50 50
661 0 warn "git rev-parse: $err\n";
662 0 die "Can't resolve given revision\n";
663 }
664 50 chomp $out;
665 50 return $out;
666 }
667  
668 54 ▶sub main {
Subroutine main: called CC=23 SCAR=35.9
669 54 $VERBOSE = 0;
670 54 my $help;
671 my $man;
672 54 my $show_version;
673 54 my $strict = $CONTEXT;
674 54 my $num_context_lines = 3;
675 54 my $dryrun;
676 my $use_detailed_exit_codes;
677  
678 54 my $raw_upstream;
679 54 my @files = ();
680  
681 54 my $found_option_terminator = 0;
682 54 my @new_argv = ();
683 54 for my $arg (@ARGV) {
684 168 ▶ if ($found_option_terminator) {
Branch: 2 branches
BranchTrueFalseTotal
if ($found_option_terminator) { } 6 162 168
elsif ($arg eq '--') { } 5 157 162
685 6 push @files, $arg;
686 } elsif ($arg eq '--') {
687 5 $found_option_terminator = 1;
688 } else {
689 157 push @new_argv, $arg;
690 }
691 }
692 54 @ARGV = @new_argv;
693  
694 54 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("h", \$help, "help", \$man, "version", \$show_version, "verbose|v+", \$VERBOSE, "strict|s=i", \$strict, "context|c=i", \$num_context_lines, "dryrun|n", \$dryrun, "gitopt|g=s", \@GIT_OPTIONS, "exit-code", \$use_detailed_exit_codes) or return 1 54 0 54
695 'h' => \$help,
696 'help' => \$man,
697 'version' => \$show_version,
698 'verbose|v+' => \$VERBOSE,
699 'strict|s=i' => \$strict,
700 'context|c=i' => \$num_context_lines,
701 'dryrun|n' => \$dryrun,
702 'gitopt|g=s' => \@GIT_OPTIONS,
703 'exit-code' => \$use_detailed_exit_codes,
704 ) or return 1;
705  
706 54 ▶ if (@ARGV) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 50 4 54
707 50 $raw_upstream = shift @ARGV;
708 50 @files = (@ARGV, @files);
709 }
710 54 ▶ if ($help) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 0 54 54
711 0 print $usage;
712 0 return 0;
713 }
714 54 ▶ if ($show_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($show_version) 0 54 54
715 0 print "$VERSION\n";
716 0 return 0;
717 }
718 54 ▶ if ($man) {
Branch: 1 branch
BranchTrueFalseTotal
if ($man) 0 54 54
719 0 eval {
720 0 require Pod::Usage;
721 };
722 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
723 0 die <<'EOF';
724 Pod::Usage unavailable for formatting the manual. The manual can be found at
725 the end of the git-autofixup script.
726 EOF
727 }
728 0 Pod::Usage::pod2usage(-exitval => 0, -verbose => 2);
729 }
730 54 ▶ if (@GIT_OPTIONS) {
Branch: 1 branch
BranchTrueFalseTotal
if (@GIT_OPTIONS) 0 54 54
731 0 warn <<'EOF';
732 --gitopt|-g is deprecated and will be removed in a future release. Please use
733 the GIT_CONFIG_{COUNT,KEY,VALUE} environment variables instead; see `git help
734 config`.
735 EOF
736 }
737  
738 # "upstream" revisions as 40 byte SHA1 hex hashes.
739 54 my @upstreams = ();
740  
741 54 ▶ if ($raw_upstream) {
Branch: 1 branch
BranchTrueFalseTotal
if ($raw_upstream) { } 50 4 54
742 50 my $upstream = rev_parse("${raw_upstream}^{commit}");
743 50 push @upstreams, $upstream;
744 } else {
745 4 @upstreams = find_merge_bases();
746 4 ▶ if (!@upstreams) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@upstreams) 0 4 4
747 0 die "Can't find tracking branch. Please specify a revision.\n";
748 }
749 }
750  
751 54 ▶ if ($num_context_lines < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_context_lines < 0) 0 54 54
752 0 die "number of context lines must be zero or greater\n";
753 }
754  
755 54 ▶ if (!grep { $strict == $_ } @STRICTNESS_LEVELS) {
Branch: 2 branches
BranchTrueFalseTotal
if (not grep({$strict == $_;} @STRICTNESS_LEVELS)) { } 0 54 54
elsif ($strict > 0 and $num_context_lines == 0) { } 0 89 89
756 0 die "invalid strictness level: $strict\n";
757 ▶ } elsif ($strict > 0 && $num_context_lines == 0) {
Condition66%2/3
$strict > 0 and $num_context_lines == 0
!ll&&!rl&&r
35190
MC/DC0%0/2
$strict > 0 and $num_context_lines == 0
$strict > 0 $num_context_lines == 0
Truth table66%2/3
A and B
A= $strict > 0B= $num_context_lines == 0
ABresult
0X0
100
111
758 0 die "strict hunk assignment requires context\n";
759 }
760  
761  
762 54 my $toplevel = toplevel_dir();
763 54 ▶ chdir $toplevel or die "cd to toplevel: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $toplevel or die "cd to toplevel: $!\n" 54 0 54
764  
765 54 my $hunks = diff_hunks($num_context_lines, @files);
766 54 my $summary_for = summary_for_commits(@upstreams);
767 54 my $alias_for = sha_aliases($summary_for);
768 54 my $grafts_file = create_grafts_file(@upstreams);
769 54 my %blame_for = map {$_ => blame($_, $alias_for, $grafts_file)} @{$hunks};
770 54 my $hunks_for = fixup_hunks_by_sha({
771 hunks => $hunks,
772 blame_for => \%blame_for,
773 summary_for => $summary_for,
774 strict => $strict,
775 });
776 54 my @ordered_shas = ordered_shas($hunks, sha_for_hunk_map($hunks_for));
777  
778 54 ▶ if ($dryrun) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dryrun) 0 54 54
779 0 ▶ if ($use_detailed_exit_codes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_detailed_exit_codes) 0 0 0
780 0 return exit_code($hunks, $hunks_for);
781 }
782 0 return 0;
783 }
784  
785 54 my $old_index = $ENV{GIT_INDEX_FILE};
786 54 local $ENV{GIT_INDEX_FILE}; # Throw away changes between main() calls.
787 54 ▶ if (is_index_dirty()) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_index_dirty()) 3 51 54
788 # Limit the tempfile's lifetime to the execution of main().
789 3 my $tempfile = create_temp_index($old_index);
790 }
791  
792 54 for my $sha (@ordered_shas) {
793 43 my $fixup_hunks = $hunks_for->{$sha};
794 43 commit_fixup($sha, $fixup_hunks);
795 }
796 51 ▶ if ($use_detailed_exit_codes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_detailed_exit_codes) 47 4 51
797 47 return exit_code($hunks, $hunks_for);
798 }
799 4 return 0;
800 }
801  
802 3 ▶if (!caller()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 0 3 3
803 0 exit main();
804 }
805 3 1;
806  
807 __END__
↑ Summary blib/lib/App/Git/Autofixup.pm »