Skip to content

Commit 27c8cee

Browse files
committed
git add -p: use non-zero exit code when the diff generation failed
The first thing `git add -p` does is to generate a diff. If this diff cannot be generated, `git add -p` should not continue as if nothing happened, but instead fail. What we *actually* do here is much broader: we now verify for *every* `run_cmd_pipe()` call that the spawned process actually succeeded. Note that we have to change two callers in this patch, as we need to store the spawned process' output in a local variable, which means that the callers can no longer decide whether to interpret the `return <$fh>` in array or in scalar context. This bug was noticed while writing a test case for the diff.algorithm feature, and we let that test case double as a regression test for this fixed bug, too. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 3f219fd commit 27c8cee

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

git-add--interactive.perl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ sub run_cmd_pipe {
177177
} else {
178178
my $fh = undef;
179179
open($fh, '-|', @_) or die;
180-
return <$fh>;
180+
my @out = <$fh>;
181+
close $fh || die "Cannot close @_ ($!)";
182+
return @out;
181183
}
182184
}
183185

@@ -224,7 +226,7 @@ sub list_untracked {
224226
sub get_empty_tree {
225227
return $empty_tree if defined $empty_tree;
226228

227-
$empty_tree = run_cmd_pipe(qw(git hash-object -t tree /dev/null));
229+
($empty_tree) = run_cmd_pipe(qw(git hash-object -t tree /dev/null));
228230
chomp $empty_tree;
229231
return $empty_tree;
230232
}
@@ -1127,7 +1129,7 @@ sub edit_hunk_manually {
11271129
EOF2
11281130
close $fh;
11291131

1130-
chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
1132+
chomp(my ($editor) = run_cmd_pipe(qw(git var GIT_EDITOR)));
11311133
system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
11321134

11331135
if ($? != 0) {

t/t3701-add-interactive.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ test_expect_success 'diff.algorithm is passed to `git diff-files`' '
530530
>file &&
531531
git add file &&
532532
echo changed >file &&
533-
git -c diff.algorithm=bogus add -p 2>err &&
533+
test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
534534
test_i18ngrep "error: option diff-algorithm accepts " err
535535
'
536536

0 commit comments

Comments
 (0)