Subject: Re: 30.0.50; `vc-print-log´ does not erase buffer when called from *vc-change-log* buffer, at least for CVS logs

From: Jens Schmidt <[email-masked]>
Date: Fri, 9 Jun 2023 00:44:39 +0200

Here come five patches for this issue, based on emacs-29:

0001-Always-erase-log-buffer-before-calling-vc-print-log.patch
0002-Always-erase-log-buffer-before-calling-vc-print-log.patch
0003-Always-erase-log-buffer-before-calling-vc-print-log.patch

   Name should be self-explaining, patches uncritical.

0004-Fix-documentation-bug-and-remove-obsolete-fixmes.patch

   `vc-deduce-fileset' uses `with-current-buffer' to protect the current
   buffer, which has not been reflected in comments.

0005-Avoid-setting-circular-vc-parent-buffer.patch

   This one fixes the issue related to VC parent buffer described in the
   previous update.  The "local" change seems to be logical (a buffer
   should not be the VC parent buffer of itself), but I'm not quite sure
   about any adverse "global" consequences.

From 9e3cb8038ea5bea3e2f129f0271a0cf3ec948159 Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Thu, 8 Jun 2023 23:36:46 +0200
Subject: [PATCH 1/5] Always erase log buffer before calling `vc-*-print-log'

When calling `vc-print-log' from buffer *vc-change-log* itself for
backends SCCS, RCS, and CVS, the log information is inserted or
appended to the *vc-change-log* buffer again without previously
erasing it.
* lisp/vc/vc-sccs.el (vc-sccs-print-log): Add call to
`vc-setup-buffer'. (Bug#63949)
---
 lisp/vc/vc-sccs.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/vc/vc-sccs.el b/lisp/vc/vc-sccs.el
index 03e9d12b76f..e5430753a64 100644
--- a/lisp/vc/vc-sccs.el
+++ b/lisp/vc/vc-sccs.el
@@ -310,6 +310,8 @@ vc-sccs-print-log
   "Print commit log associated with FILES into specified BUFFER.
 Remaining arguments are ignored."
   (setq files (vc-expand-dirs files 'SCCS))
+  (save-current-buffer
+    (vc-setup-buffer buffer))
   (vc-sccs-do-command buffer 0 "prs" (mapcar #'vc-master-name files))
   (when limit 'limit-unsupported))
 
-- 
2.30.2

From 4cb5eaa2166103d645fa77972b456dd6a4fad282 Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Thu, 8 Jun 2023 23:37:45 +0200
Subject: [PATCH 2/5] Always erase log buffer before calling `vc-*-print-log'

When calling `vc-print-log' from buffer *vc-change-log* itself for
backends SCCS, RCS, and CVS, the log information is inserted or
appended to the *vc-change-log* buffer again without previously
erasing it.
* lisp/vc/vc-cvs.el (vc-cvs-print-log): Add call to
`vc-setup-buffer'. (Bug#63949)
---
 lisp/vc/vc-cvs.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/vc/vc-cvs.el b/lisp/vc/vc-cvs.el
index 6e0246ea762..cf74ac31154 100644
--- a/lisp/vc/vc-cvs.el
+++ b/lisp/vc/vc-cvs.el
@@ -550,6 +550,8 @@ vc-cvs-modify-change-comment
 (defun vc-cvs-print-log (files buffer &optional _shortlog _start-revision limit)
   "Print commit log associated with FILES into specified BUFFER.
 Remaining arguments are ignored."
+  (save-current-buffer
+    (vc-setup-buffer buffer))
   ;; It's just the catenation of the individual logs.
   (vc-cvs-command
    buffer
-- 
2.30.2

From 4eb1a951229d96026ca58f965c652ab8e8392812 Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Thu, 8 Jun 2023 23:38:45 +0200
Subject: [PATCH 3/5] Always erase log buffer before calling `vc-*-print-log'

When calling `vc-print-log' from buffer *vc-change-log* itself for
backends SCCS, RCS, and CVS, the log information is inserted or
appended to the *vc-change-log* buffer again without previously
erasing it.
* lisp/vc/vc-rcs.el (vc-rcs-print-log): Add call to
`vc-setup-buffer'. (Bug#63949)
---
 lisp/vc/vc-rcs.el | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lisp/vc/vc-rcs.el b/lisp/vc/vc-rcs.el
index c2112b76ad3..05846a7de35 100644
--- a/lisp/vc/vc-rcs.el
+++ b/lisp/vc/vc-rcs.el
@@ -548,6 +548,8 @@ vc-rcs-print-log
 Remaining arguments are ignored.
 If FILE is a directory the operation is applied to all registered
 files beneath it."
+  (save-current-buffer
+    (vc-setup-buffer buffer))
   (vc-do-command (or buffer "*vc*") 0 "rlog"
                  (mapcar #'vc-master-name (vc-expand-dirs files 'RCS)))
   (with-current-buffer (or buffer "*vc*")
-- 
2.30.2

From 8f62cd7bbe336ba0668afb72cbbbd8c46c15376a Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Thu, 8 Jun 2023 23:46:33 +0200
Subject: [PATCH 4/5] Fix documentation bug and remove obsolete fixmes

Function `vc-deduce-fileset' preserves current buffer since commit
d494833d47968fcd97ba549654a259d6fb6c2eee.  Change documentation
accordingly.
* lisp/vc/vc.el (vc-deduce-fileset): Change doc string.
(vc-deduce-fileset-1): Remove now-obsolete fixme comment.  (Bug#63949)
---
 lisp/vc/vc.el | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index 1144a23f317..6b99eba9d3b 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -1120,7 +1120,7 @@ vc-deduce-fileset
 `vc-checkout-model'.  Otherwise, these 3 members may be omitted from
 the returned list.
 
-BEWARE: this function may change the current buffer."
+This function preserves the current buffer."
   (with-current-buffer (or (buffer-base-buffer) (current-buffer))
     (vc-deduce-fileset-1 not-state-changing
                          allow-unregistered
@@ -1151,7 +1151,7 @@ vc-deduce-fileset-1
 				  (or (derived-mode-p 'vc-dir-mode)
 				      (derived-mode-p 'dired-mode)
 				      (derived-mode-p 'diff-mode)))))
-      (progn                  ;FIXME: Why not `with-current-buffer'? --Stef.
+      (progn
 	(set-buffer vc-parent-buffer)
 	(vc-deduce-fileset-1 not-state-changing allow-unregistered state-model-only-files)))
      ((and (not buffer-file-name)
-- 
2.30.2

From 9f3fc16ac6eb2fbf6c73aef5e47569dba2cb5d80 Mon Sep 17 00:00:00 2001
From: Jens Schmidt <[email-masked]>
Date: Fri, 9 Jun 2023 00:24:28 +0200
Subject: [PATCH 5/5] Avoid setting circular `vc-parent-buffer'

Otherwise, calling e.g. `vc-print-log' from buffer *vc-change-log*
sets `vc-parent-buffer' to itself.
* lisp/vc/vc-dispatcher.el (vc-setup-buffer): Avoid setting circular
`vc-parent-buffer'.  (Bug#63949)
---
 lisp/vc/vc-dispatcher.el | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/lisp/vc/vc-dispatcher.el b/lisp/vc/vc-dispatcher.el
index fd5f655a0f6..ac4d3a78afd 100644
--- a/lisp/vc/vc-dispatcher.el
+++ b/lisp/vc/vc-dispatcher.el
@@ -186,10 +186,13 @@ vc-setup-buffer
       ;; want any of its output to appear from now on.
       (when oldproc (delete-process oldproc)))
     (kill-all-local-variables)
-    (setq-local vc-parent-buffer camefrom)
-    (setq-local vc-parent-buffer-name
-                (concat " from " (buffer-name camefrom)))
-    (setq default-directory olddir)
+    ;; Do not set the VC parent buffer and related variables to
+    ;; ourselves.  (Bug#63949)
+    (unless (equal (current-buffer) camefrom)
+      (setq-local vc-parent-buffer camefrom)
+      (setq-local vc-parent-buffer-name
+                  (concat " from " (buffer-name camefrom)))
+      (setq default-directory olddir))
     (let ((buffer-undo-list t)
           (inhibit-read-only t))
       (erase-buffer))))
-- 
2.30.2