From 67084d2292d93f582370503df74c68c0e95bd209 Mon Sep 17 00:00:00 2001 From: Jim Porter Date: Thu, 14 Dec 2023 11:31:27 -0800 Subject: [PATCH] Abbreviate the VC revision in vc-annotate's buffer name * lisp/vc/vc-hooks.el (vc-short-revision): New function... * lisp/vc/vc-annotate.el (vc-annotate): ... call it. * lisp/vc/vc-git.el (vc-git-use-short-revisions): New option. (vc-git-short-revision): New function. (vc-git--rev-parse): Add SHORT argument. * etc/NEWS: Announce this change (bug#67062). --- etc/NEWS | 6 ++++++ lisp/vc/vc-annotate.el | 2 +- lisp/vc/vc-git.el | 20 +++++++++++++++++--- lisp/vc/vc-hooks.el | 8 ++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index 1ff2f8a149f..a2dcdfc901a 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -457,6 +457,12 @@ With this value only the revision number is displayed on the mode-line. *** Obsolete command 'vc-switch-backend' re-added as 'vc-change-backend'. The command was previously obsoleted and unbound in Emacs 28. +--- +*** 'vc-annotate' can now abbreviate the revision in the buffer name. +VC backends with a 'vc-BACKEND-short-revision' function can show the +revision in a shorter form, and 'vc-annotate' will use this form in +its buffer name. Currently, the Git backend supports this. + ** Diff mode +++ diff --git a/lisp/vc/vc-annotate.el b/lisp/vc/vc-annotate.el index de6c3adbbdb..a63b8fe3ec2 100644 --- a/lisp/vc/vc-annotate.el +++ b/lisp/vc/vc-annotate.el @@ -397,7 +397,7 @@ vc-annotate (save-current-buffer (vc-ensure-vc-buffer) (list buffer-file-name - (let ((def (vc-working-revision buffer-file-name))) + (let ((def (vc-short-revision buffer-file-name))) (if (null current-prefix-arg) def (vc-read-revision (format-prompt "Annotate from revision" def) diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el index 2e057ecfaa7..f8675620568 100644 --- a/lisp/vc/vc-git.el +++ b/lisp/vc/vc-git.el @@ -111,6 +111,12 @@ vc-git :version "24.1" :group 'vc) +(defcustom vc-git-use-short-revisions t + "If non-nil, use short Git revisions when requested. +If nil, always use full Git revisions." + :type 'boolean + :version "30.1") + (defcustom vc-git-diff-switches t "String or list of strings specifying switches for Git diff under VC. If nil, use the value of `vc-diff-switches'. If t, use no switches." @@ -403,6 +409,11 @@ vc-git-working-revision (let (process-file-side-effects) (vc-git--rev-parse "HEAD"))) +(defun vc-git-short-revision (_file) + "Return the working revision in abbreviated form." + (let (process-file-side-effects) + (vc-git--rev-parse "HEAD" 'short))) + (defun vc-git--symbolic-ref (file) (or (vc-file-getprop file 'vc-git-symbolic-ref) @@ -1832,11 +1843,14 @@ vc-git-previous-revision ;; does not (and cannot) quote. (vc-git--rev-parse (concat rev "~1")))) -(defun vc-git--rev-parse (rev) +(defun vc-git--rev-parse (rev &optional short) (with-temp-buffer (and - (vc-git--out-ok "rev-parse" rev) - (buffer-substring-no-properties (point-min) (+ (point-min) 40))))) + (apply #'vc-git--out-ok "rev-parse" + (append (when short '("--short")) ;; FIXME + (list rev))) + (goto-char (point-min)) + (buffer-substring-no-properties (point) (pos-eol))))) (defun vc-git-next-revision (file rev) "Git-specific version of `vc-next-revision'." diff --git a/lisp/vc/vc-hooks.el b/lisp/vc/vc-hooks.el index 8451128286b..0f36e2bc7ae 100644 --- a/lisp/vc/vc-hooks.el +++ b/lisp/vc/vc-hooks.el @@ -506,6 +506,14 @@ vc-working-revision (vc-call-backend backend 'working-revision file)))))) +(defun vc-short-revision (file &optional backend) + "Return the repository version for FILE in human-readable form. +If FILE is not registered, this function always returns nil." + (setq backend (or backend (vc-backend file))) + (if (vc-find-backend-function backend 'short-revision) + (vc-call-backend backend 'short-revision file) + (vc-working-revision file backend))) + (defun vc-default-registered (backend file) "Check if FILE is registered in BACKEND using vc-BACKEND-master-templates." (let ((sym (vc-make-backend-sym backend 'master-templates))) -- 2.25.1