Skip to contentSkip to conversation

Git add -p “Your edited hunk does not apply. Edit again”

PATCH Superseded Tanky Woo 5 participants 17 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore

Tanky Woo

author
#0 lore raw

I asked this question in stackoverflow a few days ago, but get no answer.

edit-again

---

An example, the original file:

test:
make clean
nosetests

clean:
rm -rf htmlcov
rm -rf build dist
coverage erase
The file after changed:

HTMLCOV="htmlcov/"

test:
make clean
nosetests

clean:
coverage erase
rm -rf htmlcov
rm -rf build dist

covhtml:

       make clean
       nosetests
       coverage html
       cd ${HTMLCOV} && python -m SimpleHTTPServer

This is the diff:

mf +9 -1
index b9ca06e..6316e17 100644
@@ -1,8 +1,16 @@
+HTMLCOV="htmlcov/"            # Hunk 1
+
 test:
        make clean
        nosetests
 
 clean:
+       coverage erase         # Hunk 2
        rm -rf htmlcov
        rm -rf build dist
-       coverage erase         # Hunk 3
+
+covhtml:
+       make clean
+       nosetests
+       coverage html
+       cd ${HTMLCOV} && python -m SimpleHTTPServer

I use git add -p, want only to add:

+ coverage erase
rm -rf htmlcov
rm -rf build dist
- coverage erase
So I first use s to split hunks, and n to ignore first add, then use y to add second hunk, the third hunk
is:

- coverage erase
+
+covhtml:

+       make clean
+       nosetests
+       coverage html
+       cd ${HTMLCOV} && python -m SimpleHTTPServer

So I use e to manually edit the hunk, I delete all the + lines, it seems:

# Manual hunk edit mode -- see bottom for a quick guide @@ -6,3 +9,8 @@
rm -rf htmlcov
rm -rf build dist
- coverage erase
# ---
# To remove '-' lines, make them ' ' lines (context). # To remove '+' lines, delete them.
# Lines starting with # will be removed.
#

# ....
But when I saved, it says:

@@ -6,3 +9,8 @@
rm -rf htmlcov
rm -rf build dist
- coverage erase
+
+covhtml:

+       make clean
+       nosetests
+       coverage html
+       cd ${HTMLCOV} && python -m SimpleHTTPServer

Stage this hunk [y,n,q,a,d,/,K,g,e,?]? e
error: patch failed: mf:1
error: mf: patch does not apply
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]? And then I change to @@ from-file-range to-file-range @@ part to:

@@ -6,3 +9,2 @@
There is still the same problem.

If I first add only the second Hunk,
and then I run git add -p xxx again, and edit the hunk, there is no problem. Why?

#1 lore raw
Quoted 8 lines · Tanky Woo
(quoted diff) +6 -1
-       coverage erase
+
+covhtml:
+       make clean
+       nosetests
+       coverage html
+       cd ${HTMLCOV} && python -m SimpleHTTPServer

So I use e to manually edit the hunk, I delete all the + lines, it seems:

I can reproduce on git.git's master. Actually, you don't even have to edit the patch, just save and exit, and you get the same error message. So it's clearly a bug.

I can get the same kind of bug with "stash -p", without even using the 'e' command, by doing "split" and then answer n, y. On a simpler example with two hunks:

$ git stash -p
bar.txt +2 -0
index 35fbd83..1d3fda3 100644
@@ -1,4 +1,6 @@
 aaa
+added line 1
 bbb
+added line 2
 ccc
 ddd

Stash this hunk [y,n,q,a,d,/,s,e,?]? s
Split into 2 hunks.
@@ -1,2 +1,3 @@
aaa
+added line 1
bbb
Stash this hunk [y,n,q,a,d,/,j,J,g,e,?]? n
@@ -2,3 +3,4 @@
bbb
+added line 2
ccc
ddd
Stash this hunk [y,n,q,a,d,/,K,g,e,?]? y

Saved working directory and index state WIP on master: 1cad001 abcd
error: patch failed: bar.txt:1
error: bar.txt: patch does not apply
Cannot remove worktree changes

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#2 lore raw

cover demonstrate add -p and stash -p failures.

Playing a bit with add -p and stash -p, I can confirm the bug reported by Tanky. This series just adds failing tests, but I couldn't debug it.

I've exhausted my Git time budget for now, so if someone wants to take over and fix the bugs, feel free to do it!

Matthieu Moy (4):
add -p: demonstrate failure when running 'edit' after a split
t3904-stash-patch: fix test description
t3904-stash-patch: factor PERL prereq at the top of the file stash -p: demonstrate failure of split with mixed y/n

 t/t3701-add-interactive.sh | 25 +++++++++++++++++++++++++
 t/t3904-stash-patch.sh     | 46 +++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 64 insertions(+), 7 deletions(-)
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#3 lore raw

PATCH 1/4 add -p: demonstrate failure when running 'edit' after a split

The test passes if one replaces the 'e' command with a 'y' command in the 'add -p' session.

---
 t/t3701-add-interactive.sh | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
t/t3701-add-interactive.sh +25 -0
index 24ddd8a..b48a75c 100755
@@ -335,6 +335,31 @@ test_expect_success 'split hunk "add -p (edit)"' '
 	! grep "^+15" actual
 '
 
+test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
+	cat >test <<-\EOF &&
+	5
+	10
+	20
+	21
+	30
+	31
+	40
+	50
+	60
+	EOF
+	git reset &&
+	# test sequence is s(plit), n(o), y(es), e(dit)
+	# q n q q is there to make sure we exit at the end.
+	for a in s n y e   q n q q
+	do
+		echo $a
+	done |
+	EDITOR=: git add -p 2>error &&
+	test_must_be_empty error &&
+	git diff >actual &&
+	! grep "^+31" actual
+'
+
 test_expect_success 'patch mode ignores unmerged entries' '
 	git reset --hard &&
 	test_commit conflict &&
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#5 lore raw

PATCH 2/4 t3904-stash-patch: fix test description

In reply to Matthieu Moy

The old description is rather clearly a wrong cut-and-paste from t2016-checkout-patch.sh.

---
 t/t3904-stash-patch.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t3904-stash-patch.sh +1 -1
index 70655c1..9a59683 100755
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-test_description='git checkout --patch'
+test_description='stash -p'
 . ./lib-patch-mode.sh
 
 test_expect_success PERL 'setup' '
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#6 lore raw

PATCH 3/4 t3904-stash-patch: factor PERL prereq at the top of the file

In reply to Matthieu Moy
---
 t/t3904-stash-patch.sh | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
t/t3904-stash-patch.sh +12 -6
index 9a59683..0f8f47f 100755
@@ -3,7 +3,13 @@
 test_description='stash -p'
 . ./lib-patch-mode.sh
 
-test_expect_success PERL 'setup' '
+if ! test_have_prereq PERL
+then
+	skip_all='skipping stash -p tests, perl not available'
+	test_done
+fi
+
+test_expect_success 'setup' '
 	mkdir dir &&
 	echo parent > dir/foo &&
 	echo dummy > bar &&
@@ -20,7 +26,7 @@ test_expect_success PERL 'setup' '
 
 # note: order of files with unstaged changes: HEAD bar dir/foo
 
-test_expect_success PERL 'saying "n" does nothing' '
+test_expect_success 'saying "n" does nothing' '
 	set_state HEAD HEADfile_work HEADfile_index &&
 	set_state dir/foo work index &&
 	(echo n; echo n; echo n) | test_must_fail git stash save -p &&
@@ -29,7 +35,7 @@ test_expect_success PERL 'saying "n" does nothing' '
 	verify_state dir/foo work index
 '
 
-test_expect_success PERL 'git stash -p' '
+test_expect_success 'git stash -p' '
 	(echo y; echo n; echo y) | git stash save -p &&
 	verify_state HEAD committed HEADfile_index &&
 	verify_saved_state bar &&
@@ -41,7 +47,7 @@ test_expect_success PERL 'git stash -p' '
 	verify_state dir/foo work head
 '
 
-test_expect_success PERL 'git stash -p --no-keep-index' '
+test_expect_success 'git stash -p --no-keep-index' '
 	set_state HEAD HEADfile_work HEADfile_index &&
 	set_state bar bar_work bar_index &&
 	set_state dir/foo work index &&
@@ -56,7 +62,7 @@ test_expect_success PERL 'git stash -p --no-keep-index' '
 	verify_state dir/foo work index
 '
 
-test_expect_success PERL 'git stash --no-keep-index -p' '
+test_expect_success 'git stash --no-keep-index -p' '
 	set_state HEAD HEADfile_work HEADfile_index &&
 	set_state bar bar_work bar_index &&
 	set_state dir/foo work index &&
@@ -71,7 +77,7 @@ test_expect_success PERL 'git stash --no-keep-index -p' '
 	verify_state dir/foo work index
 '
 
-test_expect_success PERL 'none of this moved HEAD' '
+test_expect_success 'none of this moved HEAD' '
 	verify_saved_head
 '
 
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#7 lore raw

PATCH 4/4 stash -p: demonstrate failure of split with mixed y/n

In reply to Matthieu Moy
---
 t/t3904-stash-patch.sh | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
t/t3904-stash-patch.sh +26 -0
index 0f8f47f..6f053ff 100755
@@ -81,4 +81,30 @@ test_expect_success 'none of this moved HEAD' '
 	verify_saved_head
 '
 
+test_expect_failure 'stash -p with split hunk' '
+	git reset --hard &&
+	cat >test <<-\EOF &&
+	aaa
+	bbb
+	ccc
+	EOF
+	git add test &&
+	git commit -m "initial" &&
+	cat >test <<-\EOF &&
+	aaa
+	added line 1
+	bbb
+	added line 2
+	ccc
+	EOF
+	for a in s n y q
+	do
+		echo $a
+	done |
+	test_might_fail git stash -p 2>error &&
+	! test_must_be_empty error &&
+	grep "added line 1" test &&
+	! grep "added line 2" test
+'
+
 test_done
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#9 lore raw

Re: [PATCH 0/4] demonstrate add -p and stash -p failures.

In reply to Matthieu Moy
Quoted 5 lines · Matthieu Moy

Playing a bit with add -p and stash -p, I can confirm the bug reported by Tanky. This series just adds failing tests, but I couldn't debug it.

I've exhausted my Git time budget for now, so if someone wants to take over and fix the bugs, feel free to do it!

I do not have time to dig this either for now, but I suspect this "split a hunk into two overlapping hunks" issue may be related to the caveat in the very original:

http://thread.gmane.org/gmane.comp.version-control.git/76650/focus=87197

where I seem to have said that the proposed change would make "add -p" less robust than the original code (even without 'e'dit):

Junio C Hamano wrote:
>
> I recall that the original "add--interactive" carefully counted > numbers in hunks it reassembles (as it can let you split and then > you can choose to use both parts, which requires it to merge > overlapping hunks back), but if you are going to use --recount > anyway, perhaps we can discard that logic? It may make the patch > application less robust, though. I dunno.

It seems that we accepted that series saying "let's see what happens" at the end, and I suspect we are seeing its consequences ;-).

#17 lore raw
In reply to a message not in the archive
Quoted 7 lines · Tanky Woo

Stash this hunk [y,n,q,a,d,/,j,J,g,e,?]? y

(quoted diff) +0 -0
@@ -2,3 +3,4 @@

 bbb
+added line 2
 ccc
 ddd
Stash this hunk [y,n,q,a,d,/,K,g,e,?]? y

My version does n, y, not y, y. The problem is the mix of stashed/not stashed hunks. See my other message for a reproducible test, it does fail on Git master.

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
#4 lore raw

Re: [PATCH 1/4] add -p: demonstrate failure when running 'edit' after a split

In reply to Matthieu Moy
Quoted 23 lines · Matthieu Moy

The test passes if one replaces the 'e' command with a 'y' command in the 'add -p' session.

---

t/t3701-add-interactive.sh +9 -0
index 24ddd8a..b48a75c 100755
@@ -335,6 +335,31 @@ test_expect_success 'split hunk "add -p (edit)"' '
        ! grep "^+15" actual
 '
 
+test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
+       cat >test <<-\EOF &&
+       5
+       10
+       20
+       21
+       30
+       31
+       40

t/t3701-add-interactive.sh test_expect_success 'split hunk "add -p (edit)"' ' · line 356

Quoted 10 lines
t/t3701-add-interactive.sh +10 -0
+       50
+       60
+       EOF
+       git reset &&
+       # test sequence is s(plit), n(o), y(es), e(dit)
+       # q n q q is there to make sure we exit at the end.
+       for a in s n y e   q n q q
+       do
+               echo $a
+       done |

Simplified:

printf '%s\n' s n y e q n q q |

Quoted 11 lines
t/t3701-add-interactive.sh +6 -1
+       EDITOR=: git add -p 2>error &&
+       test_must_be_empty error &&
+       git diff >actual &&
+       ! grep "^+31" actual
+'
+
 test_expect_success 'patch mode ignores unmerged entries' '
        git reset --hard &&
        test_commit conflict &&
--

2.4.0.rc1.42.g9642cc6

#8 lore raw

Re: [PATCH 4/4] stash -p: demonstrate failure of split with mixed y/n

In reply to Matthieu Moy
Quoted 20 lines · Matthieu Moy

---

t/t3904-stash-patch.sh +10 -0
index 0f8f47f..6f053ff 100755
@@ -81,4 +81,30 @@ test_expect_success 'none of this moved HEAD' '
        verify_saved_head
 '
 
+test_expect_failure 'stash -p with split hunk' '
+       git reset --hard &&
+       cat >test <<-\EOF &&
+       aaa
+       bbb
+       ccc
+       EOF
+       git add test &&
+       git commit -m "initial" &&
+       cat >test <<-\EOF &&

t/t3904-stash-patch.sh test_expect_success 'none of this moved HEAD' ' · line 103

Quoted 10 lines
t/t3904-stash-patch.sh +10 -0
+       aaa
+       added line 1
+       bbb
+       added line 2
+       ccc
+       EOF
+       for a in s n y q
+       do
+               echo $a
+       done |

Simplified:

printf '%s\n' s n y q |

Quoted 9 lines
t/t3904-stash-patch.sh +6 -1
+       test_might_fail git stash -p 2>error &&
+       ! test_must_be_empty error &&
+       grep "added line 1" test &&
+       ! grep "added line 2" test
+'
+
 test_done
--

2.4.0.rc1.42.g9642cc6

#10 lore raw

v2 cover demonstrate add -p and stash -p failures

In reply to Matthieu Moy

Change since v2: simplify the code (for consistancy, I also changed the code I copy/pasted from in a separate patch).

Still out of Git time budget though :-(.

Matthieu Moy (5):
t3701-add-interactive: simplify code
add -p: demonstrate failure when running 'edit' after a split
t3904-stash-patch: fix test description
t3904-stash-patch: factor PERL prereq at the top of the file stash -p: demonstrate failure of split with mixed y/n

 t/t3701-add-interactive.sh | 27 +++++++++++++++++++++++----
 t/t3904-stash-patch.sh     | 43 ++++++++++++++++++++++++++++++++++++-------
 2 files changed, 59 insertions(+), 11 deletions(-)
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#11 lore raw

PATCH v2 1/5 t3701-add-interactive: simplify code

---
 t/t3701-add-interactive.sh | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
t/t3701-add-interactive.sh +1 -4
index 24ddd8a..b63b9d4 100755
@@ -326,10 +326,7 @@ test_expect_success 'split hunk "add -p (edit)"' '
 	# 2. Correct version applies the (not)edited version, and asks
 	#    about the next hunk, against which we say q and program
 	#    exits.
-	for a in s e     q n q q
-	do
-		echo $a
-	done |
+	printf "%s\n" s e     q n q q |
 	EDITOR=: git add -p &&
 	git diff >actual &&
 	! grep "^+15" actual
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#12 lore raw

PATCH v2 2/5 add -p: demonstrate failure when running 'edit' after a split

In reply to Matthieu Moy

The test passes if one replaces the 'e' command with a 'y' command in the 'add -p' session.

---
 t/t3701-add-interactive.sh | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
t/t3701-add-interactive.sh +22 -0
index b63b9d4..deae948 100755
@@ -332,6 +332,28 @@ test_expect_success 'split hunk "add -p (edit)"' '
 	! grep "^+15" actual
 '
 
+test_expect_failure 'split hunk "add -p (no, yes, edit)"' '
+	cat >test <<-\EOF &&
+	5
+	10
+	20
+	21
+	30
+	31
+	40
+	50
+	60
+	EOF
+	git reset &&
+	# test sequence is s(plit), n(o), y(es), e(dit)
+	# q n q q is there to make sure we exit at the end.
+	printf "%s\n" s n y e   q n q q |
+	EDITOR=: git add -p 2>error &&
+	test_must_be_empty error &&
+	git diff >actual &&
+	! grep "^+31" actual
+'
+
 test_expect_success 'patch mode ignores unmerged entries' '
 	git reset --hard &&
 	test_commit conflict &&
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#13 lore raw

PATCH v2 3/5 t3904-stash-patch: fix test description

In reply to Matthieu Moy

The old description is rather clearly a wrong cut-and-paste from t2016-checkout-patch.sh.

---
 t/t3904-stash-patch.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t3904-stash-patch.sh +1 -1
index 70655c1..9a59683 100755
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-test_description='git checkout --patch'
+test_description='stash -p'
 . ./lib-patch-mode.sh
 
 test_expect_success PERL 'setup' '
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#14 lore raw

PATCH v2 4/5 t3904-stash-patch: factor PERL prereq at the top of the file

In reply to Matthieu Moy
---
 t/t3904-stash-patch.sh | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
t/t3904-stash-patch.sh +12 -6
index 9a59683..0f8f47f 100755
@@ -3,7 +3,13 @@
 test_description='stash -p'
 . ./lib-patch-mode.sh
 
-test_expect_success PERL 'setup' '
+if ! test_have_prereq PERL
+then
+	skip_all='skipping stash -p tests, perl not available'
+	test_done
+fi
+
+test_expect_success 'setup' '
 	mkdir dir &&
 	echo parent > dir/foo &&
 	echo dummy > bar &&
@@ -20,7 +26,7 @@ test_expect_success PERL 'setup' '
 
 # note: order of files with unstaged changes: HEAD bar dir/foo
 
-test_expect_success PERL 'saying "n" does nothing' '
+test_expect_success 'saying "n" does nothing' '
 	set_state HEAD HEADfile_work HEADfile_index &&
 	set_state dir/foo work index &&
 	(echo n; echo n; echo n) | test_must_fail git stash save -p &&
@@ -29,7 +35,7 @@ test_expect_success PERL 'saying "n" does nothing' '
 	verify_state dir/foo work index
 '
 
-test_expect_success PERL 'git stash -p' '
+test_expect_success 'git stash -p' '
 	(echo y; echo n; echo y) | git stash save -p &&
 	verify_state HEAD committed HEADfile_index &&
 	verify_saved_state bar &&
@@ -41,7 +47,7 @@ test_expect_success PERL 'git stash -p' '
 	verify_state dir/foo work head
 '
 
-test_expect_success PERL 'git stash -p --no-keep-index' '
+test_expect_success 'git stash -p --no-keep-index' '
 	set_state HEAD HEADfile_work HEADfile_index &&
 	set_state bar bar_work bar_index &&
 	set_state dir/foo work index &&
@@ -56,7 +62,7 @@ test_expect_success PERL 'git stash -p --no-keep-index' '
 	verify_state dir/foo work index
 '
 
-test_expect_success PERL 'git stash --no-keep-index -p' '
+test_expect_success 'git stash --no-keep-index -p' '
 	set_state HEAD HEADfile_work HEADfile_index &&
 	set_state bar bar_work bar_index &&
 	set_state dir/foo work index &&
@@ -71,7 +77,7 @@ test_expect_success PERL 'git stash --no-keep-index -p' '
 	verify_state dir/foo work index
 '
 
-test_expect_success PERL 'none of this moved HEAD' '
+test_expect_success 'none of this moved HEAD' '
 	verify_saved_head
 '
 
Signature · 1 line
2.4.0.rc1.42.g9642cc6
#15 lore raw

PATCH v2 5/5 stash -p: demonstrate failure of split with mixed y/n

In reply to Matthieu Moy
---
 t/t3904-stash-patch.sh | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
t/t3904-stash-patch.sh +23 -0
index 0f8f47f..38e7300 100755
@@ -81,4 +81,27 @@ test_expect_success 'none of this moved HEAD' '
 	verify_saved_head
 '
 
+test_expect_failure 'stash -p with split hunk' '
+	git reset --hard &&
+	cat >test <<-\EOF &&
+	aaa
+	bbb
+	ccc
+	EOF
+	git add test &&
+	git commit -m "initial" &&
+	cat >test <<-\EOF &&
+	aaa
+	added line 1
+	bbb
+	added line 2
+	ccc
+	EOF
+	printf "%s\n" s n y q |
+	test_might_fail git stash -p 2>error &&
+	! test_must_be_empty error &&
+	grep "added line 1" test &&
+	! grep "added line 2" test
+'
+
 test_done
Signature · 1 line
2.4.0.rc1.42.g9642cc6