Skip to content

Commit 36aeb30

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 d6b95ad commit 36aeb30

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
@@ -163,7 +163,9 @@ sub run_cmd_pipe {
163163
} else {
164164
my $fh = undef;
165165
open($fh, '-|', @_) or die;
166-
return <$fh>;
166+
my @out = <$fh>;
167+
close $fh || die "Cannot close @_ ($!)";
168+
return @out;
167169
}
168170
}
169171

@@ -210,7 +212,7 @@ sub list_untracked {
210212
sub get_empty_tree {
211213
return $empty_tree if defined $empty_tree;
212214

213-
$empty_tree = run_cmd_pipe(qw(git hash-object -t tree /dev/null));
215+
($empty_tree) = run_cmd_pipe(qw(git hash-object -t tree /dev/null));
214216
chomp $empty_tree;
215217
return $empty_tree;
216218
}
@@ -1103,7 +1105,7 @@ sub edit_hunk_manually {
11031105
EOF2
11041106
close $fh;
11051107

1106-
chomp(my $editor = run_cmd_pipe(qw(git var GIT_EDITOR)));
1108+
chomp(my ($editor) = run_cmd_pipe(qw(git var GIT_EDITOR)));
11071109
system('sh', '-c', $editor.' "$@"', $editor, $hunkfile);
11081110

11091111
if ($? != 0) {

t/t3701-add-interactive.sh

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

0 commit comments

Comments
 (0)