Subject: bug#26345: [PATCH] Handle renamed files when cycling through revisions

From: Sean Whitton <[email-masked]>
Date: Tue, 08 Jul 2025 11:30:40 +0100

Hello,

On Mon 07 Jul 2025 at 11:09pm +02, Jakub Ječmínek via "Bug reports for GNU Emacs, the Swiss army knife of text editors" wrote:

> On 7/7/25 10:34, Sean Whitton wrote:
>> That's okay, I think the only thing is to check that the backend
>> implements file-name-changes using vc-find-backend-function.  Then so
>> long as the general approach makes sense and works for Git it's fine.
>
> I've updated the patch based on your suggestions. You're right, `vc-annotate'
> is indeed a better place for this change.

Jakub, I'm attaching a version of your patch with a rewritten commit
message to more closely match our conventions, and to avoid overlong
lines of code.  Please take a look.

>>> Dmitry Gutov <[email-masked]> writes:
>>>
>>>> On 05/07/2025 19:42, Sean Whitton wrote:
>>>> I think (?) the file name might have changed in some later revision - so
>>>> it won't be returned by vc-git-file-name-changes for that specific REV,
>>>> but still might not match the current name.
>>>
>>> I'm not sure.
>> Seems like this needs to be investigated before we can proceed.
> I've tested various scenarios and haven't encountered any issues with the
> current implementation. It would be great if you could test the change as
> well, in case there are edge cases I missed.

I think that we need to see an argument in terms of the definition of
the file-name-changes backend function that demonstrates that your
approach will always work:

;;   Return the list of pairs with changes in file names in REV.  When
;;   a file was added, it should be a cons with nil car.  When
;;   deleted, a cons with nil cdr.  When copied or renamed, a cons
;;   with the source name as car and destination name as cdr.

-- 
Sean Whitton
>From 4a4110b2ed540f3f6c58c3d8a52d965891c8523e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jakub=20Je=C4=8Dm=C3=ADnek?= <[email-masked]>
Date: Mon, 7 Jul 2025 22:32:57 +0200
Subject: [PATCH] vc-annotate: Handle renamed files when changing revision

* lisp/vc/vc-annotate.el (vc-annotate--file-name-change): New
function.
(vc-annotate): Call it on the FILE argument to the
'annotate-command' backend function (bug#26345).
---
 lisp/vc/vc-annotate.el | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/lisp/vc/vc-annotate.el b/lisp/vc/vc-annotate.el
index 4f72faacaaf..19f7d1fefc9 100644
--- a/lisp/vc/vc-annotate.el
+++ b/lisp/vc/vc-annotate.el
@@ -459,7 +459,8 @@ vc-annotate
             (setq coding-system-for-read
                   (coding-system-change-eol-conversion coding-system-for-read
                                                        'dos)))
-        (vc-call-backend backend 'annotate-command file
+        (vc-call-backend backend 'annotate-command
+                         (vc-annotate--file-name-change file rev backend)
                          (get-buffer temp-buffer-name) rev)
         ;; we must setup the mode first, and then set our local
         ;; variables before the show-function is called at the exit of
@@ -798,6 +799,16 @@ vc-annotate-goto-line
       (message "Annotations were for revision %s; line numbers may be incorrect"
 	       rev)))))
 
+(defun vc-annotate--file-name-change (file rev backend)
+  "Return the name of FILE at revision REV using BACKEND.
+If the file name has changed in the given revision, return the new name;
+otherwise, return FILE unchanged."
+  (if (vc-find-backend-function backend 'file-name-changes)
+      (or (cdr (assoc file
+                      (vc-call-backend backend 'file-name-changes rev)))
+          file)
+    file))
+
 (provide 'vc-annotate)
 
 ;;; vc-annotate.el ends here
-- 
2.47.2