Subject: Re: bug#72556: 29.1.90; vc-diff does not undo hunk in end of source file

From: Tomas Nordin <[email-masked]>
Date: Sat, 01 Mar 2025 21:21:39 +0000

Tomas Nordin <[email-masked]> writes:

[...]

> NEW: if non-nil, hunk is already applied
> OLD: if non-nil, hunk is not applied

Those notes might be correct for the normal case but when REVERSE is
non-nil, the notion of NEW and OLD is reversed.

[...]

So I added a check to only satisfy the NEW *and* OLD case when REVERSE is
not given.  What do you think?  (Also aligned the leading whitespace
with original code).  In my manual tests it seems to work.  I learned
about the splitting of hunks and it doesn't seem broken.

>From 2dbac66263c3b8406bc2e6bbc5bce0e168ca7fd9 Mon Sep 17 00:00:00 2001
From: Tomas Nordin <[email-masked]>
Date: Sat, 1 Mar 2025 14:50:36 +0100
Subject: [PATCH] Fix detection of hunk already applied

* lisp/vc/diff-mode.el (diff-find-source-location):
Consider the case when there is no diff context above or below
edited lines.  (bug#72556)
---
 lisp/vc/diff-mode.el | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/lisp/vc/diff-mode.el b/lisp/vc/diff-mode.el
index 16e696b6609..c585038d24d 100644
--- a/lisp/vc/diff-mode.el
+++ b/lisp/vc/diff-mode.el
@@ -1996,9 +1996,11 @@ diff-find-source-location
         (goto-char (point-min)) (forward-line (1- (string-to-number line)))
 	(let* ((orig-pos (point))
 	       (switched nil)
-	       ;; FIXME: Check for case where both OLD and NEW are found.
-	       (pos (or (diff-find-text (car old))
-			(progn (setq switched t) (diff-find-text (car new)))
+	       (maybe-old (diff-find-text (car old)))
+	       (maybe-new (diff-find-text (car new)))
+	       (pos (or (and maybe-new maybe-old (null reverse) (setq switched t) maybe-new)
+			maybe-old
+			(progn (setq switched t) maybe-new)
 			(progn (setq switched nil)
 			       (condition-case nil
 				   (diff-find-approx-text (car old))
-- 
2.39.5