Skip to content

Commit 011fc2e

Browse files
committed
Merge branch 'js/add-i-a-bit-more-tests'
Test coverage update in preparation for further work on "git add -i". * js/add-i-a-bit-more-tests: apply --allow-overlap: fix a corner case git add -p: use non-zero exit code when the diff generation failed t3701: verify that the diff.algorithm config setting is handled t3701: verify the shown messages when nothing can be added t3701: add a test for the different `add -p` prompts t3701: avoid depending on the TTY prerequisite t3701: add a test for advanced split-hunk editing
2 parents d1c0fe8 + b4bbbbd commit 011fc2e

File tree

3 files changed

+97
-11
lines changed

3 files changed

+97
-11
lines changed

apply.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2661,6 +2661,16 @@ static int find_pos(struct apply_state *state,
26612661
unsigned long backwards, forwards, current;
26622662
int backwards_lno, forwards_lno, current_lno;
26632663

2664+
/*
2665+
* When running with --allow-overlap, it is possible that a hunk is
2666+
* seen that pretends to start at the beginning (but no longer does),
2667+
* and that *still* needs to match the end. So trust `match_end` more
2668+
* than `match_beginning`.
2669+
*/
2670+
if (state->allow_overlap && match_beginning && match_end &&
2671+
img->nr - preimage->nr != 0)
2672+
match_beginning = 0;
2673+
26642674
/*
26652675
* If match_beginning or match_end is specified, there is no
26662676
* point starting from a wrong line that will never match and

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: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ diff_cmp () {
2323
test_cmp "$1.filtered" "$2.filtered"
2424
}
2525

26+
# This function uses a trick to manipulate the interactive add to use color:
27+
# the `want_color()` function special-cases the situation where a pager was
28+
# spawned and Git now wants to output colored text: to detect that situation,
29+
# the environment variable `GIT_PAGER_IN_USE` is set. However, color is
30+
# suppressed despite that environment variable if the `TERM` variable
31+
# indicates a dumb terminal, so we set that variable, too.
32+
33+
force_color () {
34+
env GIT_PAGER_IN_USE=true TERM=vt100 "$@"
35+
}
36+
2637
test_expect_success 'setup (initial)' '
2738
echo content >file &&
2839
git add file &&
@@ -94,7 +105,6 @@ test_expect_success 'revert works (commit)' '
94105
grep "unchanged *+3/-0 file" output
95106
'
96107

97-
98108
test_expect_success 'setup expected' '
99109
cat >expected <<-\EOF
100110
EOF
@@ -263,6 +273,35 @@ test_expect_success FILEMODE 'stage mode and hunk' '
263273

264274
# end of tests disabled when filemode is not usable
265275

276+
test_expect_success 'different prompts for mode change/deleted' '
277+
git reset --hard &&
278+
>file &&
279+
>deleted &&
280+
git add --chmod=+x file deleted &&
281+
echo changed >file &&
282+
rm deleted &&
283+
test_write_lines n n n |
284+
git -c core.filemode=true add -p >actual &&
285+
sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
286+
cat >expect <<-\EOF &&
287+
(1/1) Stage deletion [y,n,q,a,d,?]?
288+
(1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
289+
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
290+
EOF
291+
test_cmp expect actual.filtered
292+
'
293+
294+
test_expect_success 'correct message when there is nothing to do' '
295+
git reset --hard &&
296+
git add -p 2>err &&
297+
test_i18ngrep "No changes" err &&
298+
printf "\\0123" >binary &&
299+
git add binary &&
300+
printf "\\0abc" >binary &&
301+
git add -p 2>err &&
302+
test_i18ngrep "Only binary files changed" err
303+
'
304+
266305
test_expect_success 'setup again' '
267306
git reset --hard &&
268307
test_chmod +x file &&
@@ -403,6 +442,28 @@ test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
403442
! grep "^+31" actual
404443
'
405444

445+
test_expect_failure 'edit, adding lines to the first hunk' '
446+
test_write_lines 10 11 20 30 40 50 51 60 >test &&
447+
git reset &&
448+
tr _ " " >patch <<-EOF &&
449+
@@ -1,5 +1,6 @@
450+
_10
451+
+11
452+
+12
453+
_20
454+
+21
455+
+22
456+
_30
457+
EOF
458+
# test sequence is s(plit), e(dit), n(o)
459+
# q n q q is there to make sure we exit at the end.
460+
printf "%s\n" s e n q n q q |
461+
EDITOR=./fake_editor.sh git add -p 2>error &&
462+
test_must_be_empty error &&
463+
git diff --cached >actual &&
464+
grep "^+22" actual
465+
'
466+
406467
test_expect_success 'patch mode ignores unmerged entries' '
407468
git reset --hard &&
408469
test_commit conflict &&
@@ -429,35 +490,48 @@ test_expect_success 'patch mode ignores unmerged entries' '
429490
diff_cmp expected diff
430491
'
431492

432-
test_expect_success TTY 'diffs can be colorized' '
493+
test_expect_success 'diffs can be colorized' '
433494
git reset --hard &&
434495
435496
echo content >test &&
436-
printf y | test_terminal git add -p >output 2>&1 &&
497+
printf y >y &&
498+
force_color git add -p >output 2>&1 <y &&
437499
438500
# We do not want to depend on the exact coloring scheme
439501
# git uses for diffs, so just check that we saw some kind of color.
440502
grep "$(printf "\\033")" output
441503
'
442504

443-
test_expect_success TTY 'diffFilter filters diff' '
505+
test_expect_success 'diffFilter filters diff' '
444506
git reset --hard &&
445507
446508
echo content >test &&
447509
test_config interactive.diffFilter "sed s/^/foo:/" &&
448-
printf y | test_terminal git add -p >output 2>&1 &&
510+
printf y >y &&
511+
force_color git add -p >output 2>&1 <y &&
449512
450513
# avoid depending on the exact coloring or content of the prompts,
451514
# and just make sure we saw our diff prefixed
452515
grep foo:.*content output
453516
'
454517

455-
test_expect_success TTY 'detect bogus diffFilter output' '
518+
test_expect_success 'detect bogus diffFilter output' '
456519
git reset --hard &&
457520
458521
echo content >test &&
459522
test_config interactive.diffFilter "echo too-short" &&
460-
printf y | test_must_fail test_terminal git add -p
523+
printf y >y &&
524+
test_must_fail force_color git add -p <y
525+
'
526+
527+
test_expect_success 'diff.algorithm is passed to `git diff-files`' '
528+
git reset --hard &&
529+
530+
>file &&
531+
git add file &&
532+
echo changed >file &&
533+
test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
534+
test_i18ngrep "error: option diff-algorithm accepts " err
461535
'
462536

463537
test_expect_success 'patch-mode via -i prompts for files' '
@@ -667,7 +741,7 @@ test_expect_success 'show help from add--helper' '
667741
<BOLD;BLUE>What now<RESET>>$SP
668742
Bye.
669743
EOF
670-
test_write_lines h | GIT_PAGER_IN_USE=true TERM=vt100 git add -i >actual.colored &&
744+
test_write_lines h | force_color git add -i >actual.colored &&
671745
test_decode_color <actual.colored >actual &&
672746
test_i18ncmp expect actual
673747
'

0 commit comments

Comments
 (0)