GNU bug report logs - #77529
31.0.50; [Feature Request][VC] How can I get the current revision of the buffer opened by =vc-find-revision=?

Previous Next

Package: emacs;

Reported by: jixiuf <jixiuf <at> qq.com>

Date: Fri, 4 Apr 2025 04:26:01 UTC

Severity: wishlist

Found in version 31.0.50

To reply to this bug, email your comments to 77529 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#77529; Package emacs. (Fri, 04 Apr 2025 04:26:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to jixiuf <jixiuf <at> qq.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 04 Apr 2025 04:26:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: jixiuf <jixiuf <at> qq.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 31.0.50; [Feature Request][VC] How can I get the current revision
 of the buffer opened by =vc-find-revision=?
Date: Fri, 04 Apr 2025 12:09:47 +0800
How can I retrieve the current revision when I'm in a buffer opened by
=vc-find-revision= ?

I'm attempting to write two commands: =vc-next-revision= and
=vc-prev-revision=, similar to =magit-blob-next= and
=magit-blob-previous=.

Currently, I obtain the revision from the buffer name (which might
support Git), but I don't consider this to be the best approach. Should
we add a local variable called =vc-revision=, similar to
=vc-parent-buffer=?

Alternatively, should Emacs implement =vc-next-revision= and
=vc-prev-revision= directly in =vc.el=?

----------------------------------

;;;###autoload
(defun vc-next-revision ()
  "Visit the next revision which modified the current file."
  (interactive)
  (let* ((buffname (buffer-name))
         (prev-buffer (current-buffer))
         (parent-buffer vc-parent-buffer)
         (fileset-arg (vc-deduce-fileset nil t))
         (backend (car fileset-arg))
         (filename (car (cadr fileset-arg)))
         rev next)
    (when parent-buffer
      ;; FIXME: support other bankend for find rev?
      (when (string-match "^\\([^~]+?\\)\\(?:\\.~\\([^~]+\\)~\\)?$" buffname)
        (setq rev (match-string 2 buffname)))
      (setq next (vc-call-backend backend 'next-revision
                                  filename rev))
      (kill-buffer prev-buffer)
      (if next
          (switch-to-buffer (vc-find-revision filename next))
        (find-file filename)
        (user-error "vc timemachine: You have reached the end of time")))))
;;;###autoload
(defun vct-prev-revision ()
  "Visit the prev revision which modified the current file."
  (interactive)
  (let* ((buffname (buffer-name))
         (cur-buffer (current-buffer))
         (parent-buffer vc-parent-buffer)
         (fileset-arg (vc-deduce-fileset nil t))
         (backend (car fileset-arg))
         (filename (car (cadr fileset-arg)))
         rev prev)
    ;; FIXME: support other bankend for find rev?
    (when (string-match "^\\([^~]+?\\)\\(?:\\.~\\([^~]+\\)~\\)?$" buffname)
      (setq rev (match-string 2 buffname)))
    (if rev
        (setq prev (vc-call-backend backend 'previous-revision
                                    filename rev))
      (setq prev (vc-short-revision filename)))
    (if prev
        (progn  (switch-to-buffer (vc-find-revision filename prev))
                (when parent-buffer (kill-buffer cur-buffer)))
      (user-error "vc timemachine: You have reached the beginning of time"))))






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#77529; Package emacs. (Fri, 04 Apr 2025 11:04:02 GMT) Full text and rfc822 format available.

Message #8 received at 77529 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: jixiuf <jixiuf <at> qq.com>
Cc: 77529 <at> debbugs.gnu.org
Subject: Re: bug#77529: 31.0.50;
 [Feature Request][VC] How can I get the current revision of the
 buffer opened by =vc-find-revision=?
Date: Fri, 04 Apr 2025 14:02:50 +0300
> Date: Fri, 04 Apr 2025 12:09:47 +0800
> From:  jixiuf via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <bug-gnu-emacs <at> gnu.org>
> 
> How can I retrieve the current revision when I'm in a buffer opened by
> =vc-find-revision= ?
> 
> I'm attempting to write two commands: =vc-next-revision= and
> =vc-prev-revision=, similar to =magit-blob-next= and
> =magit-blob-previous=.

Emacs already has vc-git-next-revision, so if you need this only
for Git, the function is already there, you just need to wrap it with
a command.

As for vc-prev-revision, isn't that just SHA^, where SHA is the hash
of the revision used to call vc-prev-revision?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#77529; Package emacs. (Sat, 05 Apr 2025 07:42:02 GMT) Full text and rfc822 format available.

Message #11 received at 77529 <at> debbugs.gnu.org (full text, mbox):

From: jixiuf <jixiuf <at> qq.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 77529 <at> debbugs.gnu.org
Subject: Re: bug#77529: 31.0.50; [Feature Request][VC] How can I get the
 current revision of the buffer opened by =vc-find-revision=?
Date: Sat, 05 Apr 2025 15:41:28 +0800
Eli Zaretskii <eliz <at> gnu.org> writes:

>
> Emacs already has vc-git-next-revision, so if you need this only
> for Git, the function is already there, you just need to wrap it with
> a command.
>
> As for vc-prev-revision, isn't that just SHA^, where SHA is the hash
> of the revision used to call vc-prev-revision?

But how can I get the SHA of the current revision when I am in a buffer
opened by =vc-find-revision=?

Getting the SHA from the buffer name is not easy, as shown in the demo
below:

- 7f855b5297b..: Eli Zaretskii 2023-01-07 ; Fix description of etc/DOC  
- (origin/emacs-29)e9341119fe4..: Eli Zaretskii 2023-01-07 ; Fix documentation of etc/DOC  
- cae528457cb..: Eli Zaretskii 2023-01-01 ; Add 2023 to copyright years.  

(vc-git-previous-revision "doc/lispref/help.texi" "7f855b5297b")  
=> "remotes/origin/emacs-29"  

(vc-git-next-revision "doc/lispref/help.texi" "cae528457cb")  
=> "remotes/origin/emacs-29"  

(vc-find-revision "doc/lispref/help.texi" "remotes/origin/emacs-29")  
=> buffer-name: help.texi.~remotes_origin_emacs-29~  

As you can see, the SHA is "remotes_origin_emacs-29" (obtained from the
buffer name). I don’t know the true branch/tag name of
"remotes_origin_emacs-29"; it could be "remotes/origin/emacs-29" or
"remotes_origin_emacs-29."





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#77529; Package emacs. (Sat, 05 Apr 2025 07:57:01 GMT) Full text and rfc822 format available.

Message #14 received at 77529 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: jixiuf <jixiuf <at> qq.com>
Cc: 77529 <at> debbugs.gnu.org
Subject: Re: bug#77529: 31.0.50; [Feature Request][VC] How can I get the
 current revision of the buffer opened by =vc-find-revision=?
Date: Sat, 05 Apr 2025 10:55:44 +0300
> From: jixiuf <jixiuf <at> qq.com>
> Cc: 77529 <at> debbugs.gnu.org
> Date: Sat, 05 Apr 2025 15:41:28 +0800
> 
> Eli Zaretskii <eliz <at> gnu.org> writes:
> 
> >
> > Emacs already has vc-git-next-revision, so if you need this only
> > for Git, the function is already there, you just need to wrap it with
> > a command.
> >
> > As for vc-prev-revision, isn't that just SHA^, where SHA is the hash
> > of the revision used to call vc-prev-revision?
> 
> But how can I get the SHA of the current revision when I am in a buffer
> opened by =vc-find-revision=?

That's the argument with which vc-find-revision was called, right?  So
the only change we might need is to record the revision in some
buffer-local variable, if we don't already do that (don't we show the
revision of the mode line?).




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#77529; Package emacs. (Sat, 05 Apr 2025 09:54:01 GMT) Full text and rfc822 format available.

Message #17 received at 77529 <at> debbugs.gnu.org (full text, mbox):

From: jixiuf <jixiuf <at> qq.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 77529 <at> debbugs.gnu.org
Subject: Re: bug#77529: 31.0.50; [Feature Request][VC] How can I get the
 current revision of the buffer opened by =vc-find-revision=?
Date: Sat, 05 Apr 2025 17:53:25 +0800
[Message part 1 (text/plain, inline)]
Eli Zaretskii <eliz <at> gnu.org> writes:

>> But how can I get the SHA of the current revision when I am in a buffer
>> opened by =vc-find-revision=?
>
> That's the argument with which vc-find-revision was called, right?  So
> the only change we might need is to record the revision in some
> buffer-local variable, if we don't already do that (don't we show the
> revision of the mode line?).
Yes a buffer-local.

the 'revision' on the mode line is part of buffer name, not always a
revision.

(vc-find-revision "doc/lispref/help.texi" "remotes/origin/emacs-29"
'Git)

[20250405-174822.jpg (image/jpeg, inline)]

This bug report was last modified today.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.