Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: editing hunks fails (when file has other hunks?)

Tags:

git

Given a file with contents:

foo
bar
baz

foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo

foo
bar
baz

When I edit this file to become

foo
bar1
baz

foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo

foo
bar1
baz

(replaced bars with bar1s)

When I run git add --patch and edit the first hunk

@@ -1,5 +1,5 @@
 foo
-bar
+bar1
 baz

 foo

To become

@@ -1,5 +1,5 @@
 foo
-bar
+bar2
 baz

 foo

(replaced +bar1 with +bar2)

I get the following error

error: patch fragment without header at line 12: @@ -15,5 +15,5 @@ foo
Your edited hunk does not apply. Edit again (saying "no" discards!) [y/n]?

It is not clear to me why this edit would fail.

Git version: 2.17.0

like image 443
Oliver Joseph Ash Avatar asked May 09 '18 17:05

Oliver Joseph Ash


1 Answers

This appears to be a regression in 2.17.0.

I filed this issue on the Git mailing list and the community there kindly helped me identity the issue.

My question: https://public-inbox.org/git/[email protected]/

The answer: https://public-inbox.org/git/[email protected]/

The problem turned out to be that my editor was trimming trailing whitespace from context lines when editing the hunks. In this example it was the space on the empty line between baz and foo.

This appears to be a change in behaviour since Git 2.17.0, as explained in this email: https://public-inbox.org/git/[email protected]/.

like image 101
Oliver Joseph Ash Avatar answered Nov 13 '22 09:11

Oliver Joseph Ash