Subject: bug#43297: 27.1; corrupts patch when diff-update-on-the-fly is set to nil

From: Daniel Martín <[email-masked]>
Date: Wed, 21 Dec 2022 11:28:54 +0100

Mark H Weaver <[email-masked]> writes:

> I don't know if this behavior is new in 27.1, but that's the only
> version I've tried this on.
>
> When I set 'diff-update-on-the-fly' to nil, I find that some patches are
> consistently corrupted when I save them while in diff-mode.  See below
> for an example patch that demonstrates this behavior.
>
> To reproduce:
>
> * Save the example patch below to "test.patch", and make another copy
>   in "test2.patch".
>
> * Launch "emacs -nw -q"  (I used Emacs 27.1)
>
> * Evaluate (setq diff-update-on-the-fly nil)
>
> * Find file "test2.patch".
>
> * Add a character anywhere in the buffer and then delete it, such that
>   the buffer is now in a "modified" state.
>
> * Save the file.
>
> * Run "diff -u test.patch test2.patch" in the shell.
>
> * Here's the result that I see:
>
> --- test.patch
> +++ test2.patch
> @@ -39,7 +39,7 @@
>   
>   	var elements = document.getElementsByTagName("style");
>   	for(var i = 0; i < elements.length; i++){
> -@@ -41,16 +48,20 @@ function reveal_css(){
> +@@ -41,17 +48,20 @@ function reveal_css(){
>   
>   		}
>   	}
>
>
> Emacs corrupts the line count of the final hunk, making it one larger
> than it should be (17 instead of 16).  This breaks the patch, such that
> attempts to apply it will fail.

I've tried to reproduce this bug in an Emacs build from the current
master branch and I couldn't.  Could you try a recent build of Emacs and
confirm if it's also fixed for you?  Thanks.