From 2ad882ac82cbb03674699f09c98bdb5e7481843b Mon Sep 17 00:00:00 2001 From: dickmao Date: Sat, 22 Oct 2022 08:33:53 -0400 Subject: [PATCH] Fallout 307ad21 * lisp/vc/vc-git.el (vc-git-working-revision): For better or worse, 307ad21 dupes vc-git--symbolic-ref into this function. Throw the sha1 onto the retval as a hack to recover sha1. (vc-git-mode-line-string): Recover the sha1 for John Q. User. * test/lisp/vc/vc-git-tests.el (vc-git-test--mock-repo): (vc-git-test-detached-head): Test the failure when vc-refresh-state goes after a detached HEAD branch. --- lisp/vc/vc-git.el | 26 +++++++++----------------- test/lisp/vc/vc-git-tests.el | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/lisp/vc/vc-git.el b/lisp/vc/vc-git.el index 3c6afec0378..8682770635d 100644 --- a/lisp/vc/vc-git.el +++ b/lisp/vc/vc-git.el @@ -374,31 +374,23 @@ vc-git-state (defun vc-git-working-revision (_file) "Git-specific version of `vc-working-revision'." (let* ((process-file-side-effects nil) - (commit (vc-git--rev-parse "HEAD" t))) - (or (vc-git-symbolic-commit commit) commit))) - -(defun vc-git--symbolic-ref (file) - (or - (vc-file-getprop file 'vc-git-symbolic-ref) - (let* (process-file-side-effects - (str (vc-git--run-command-string nil "symbolic-ref" "HEAD"))) - (vc-file-setprop file 'vc-git-symbolic-ref - (if str - (if (string-match "^\\(refs/heads/\\)?\\(.+\\)$" str) - (match-string 2 str) - str)))))) + (commit (vc-git--rev-parse "HEAD" t)) + (name (vc-git-symbolic-commit commit))) + (or (prog1 name + (add-text-properties 0 (length name) `(commit ,commit) name)) + commit))) (defun vc-git-mode-line-string (file) "Return a string for `vc-mode-line' to put in the mode line for FILE." (let* ((rev (vc-working-revision file 'Git)) - (disp-rev (or (vc-git--symbolic-ref file) - (and rev (substring rev 0 7)))) (def-ml (vc-default-mode-line-string 'Git file)) (help-echo (get-text-property 0 'help-echo def-ml)) (face (get-text-property 0 'face def-ml))) - (propertize (concat (substring def-ml 0 4) disp-rev) + (propertize (concat (substring def-ml 0 4) rev) 'face face - 'help-echo (concat help-echo "\nCurrent revision: " rev)))) + 'help-echo (concat help-echo "\nCurrent revision: " + (when rev + (or (get-text-property 0 'commit rev) rev)))))) (cl-defstruct (vc-git-extra-fileinfo (:copier nil) diff --git a/test/lisp/vc/vc-git-tests.el b/test/lisp/vc/vc-git-tests.el index dc9641ed46b..c46e1c55942 100644 --- a/test/lisp/vc/vc-git-tests.el +++ b/test/lisp/vc/vc-git-tests.el @@ -56,6 +56,31 @@ vc-git-test-program-version-invalid-leading-dot "git version .2.30.1.5" "0")) +(defmacro vc-git-test--mock-repo (&rest body) + (declare (indent defun)) + `(let* ((dir (make-temp-file "vc-git-tests" t)) + (default-directory dir)) + (unwind-protect + (progn + (vc-git-create-repo) + (vc-git-command nil 0 nil "config" "--add" "user.name" "frou") + ,@body) + (delete-directory dir t)))) + +(ert-deftest vc-git-test-detached-head () + (skip-unless (executable-find vc-git-program)) + (require 'log-edit) + (vc-git-test--mock-repo + (with-temp-file "foo") + (condition-case err + (progn + (vc-git-register (split-string "foo")) + (vc-git-checkin (split-string "foo") "No-Verify: yes +his fooness") + (vc-git-checkout nil (vc-git--rev-parse "HEAD" t))) + (error (signal (car err) (with-current-buffer "*vc*" (buffer-string))))) + (find-file-noselect "foo"))) + (defun vc-git-test--run-program-version-test (mock-version-string expected-output) (cl-letf* (((symbol-function 'vc-git--run-command-string) -- 2.36.1