Skip to content

Commit 9861c94

Browse files
authored
Merge pull request #248 from whhone/master
Fix `diff-hl-resolved-reference-revision` for Hg and Bzr with buffer-local `diff-hl-reference-revision`
2 parents c1cecb2 + 07eb3e1 commit 9861c94

2 files changed

Lines changed: 43 additions & 14 deletions

File tree

diff-hl.el

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,21 +1442,35 @@ CONTEXT-LINES is the size of the unified diff context, defaults to 0."
14421442
((eq backend 'Git)
14431443
(vc-git--rev-parse diff-hl-reference-revision))
14441444
((eq backend 'Hg)
1445-
(with-temp-buffer
1446-
(vc-hg-command (current-buffer) 0 nil
1447-
"identify" "-r" diff-hl-reference-revision
1448-
"-i")
1449-
(goto-char (point-min))
1450-
(buffer-substring-no-properties (point) (line-end-position))))
1445+
;; Preserve the potentially buffer-local variables (e.g.,
1446+
;; `diff-hl-reference-revision`, `vc-hg-program`) by not switching buffer
1447+
;; until the vc-hg-command is done.
1448+
(let ((temp-buffer (generate-new-buffer " *temp*")))
1449+
(unwind-protect
1450+
(progn
1451+
(vc-hg-command temp-buffer 0 nil
1452+
"identify" "-r" diff-hl-reference-revision "-i")
1453+
(with-current-buffer temp-buffer
1454+
(goto-char (point-min))
1455+
(buffer-substring-no-properties (point) (line-end-position))))
1456+
(kill-buffer temp-buffer))))
14511457
((eq backend 'Bzr)
1452-
(with-temp-buffer
1453-
(vc-bzr-command (current-buffer) 0 nil
1454-
"log" "--log-format=template" "--template-str='{revno}'"
1455-
"-r" diff-hl-reference-revision)
1456-
(goto-char (point-min))
1457-
(buffer-substring-no-properties (point) (line-end-position))))
1458-
(t
1459-
diff-hl-reference-revision)))
1458+
;; Preserve the potentially buffer-local variables (e.g.,
1459+
;; `diff-hl-reference-revision`, `vc-bzr-program`) by not switching buffer
1460+
;; until the vc-bzr-command is done.
1461+
(let ((temp-buffer (generate-new-buffer " *temp*")))
1462+
(unwind-protect
1463+
(progn
1464+
(vc-bzr-command temp-buffer 0 nil
1465+
"log" "--log-format=template"
1466+
"--template-str='{revno}'"
1467+
"-r" diff-hl-reference-revision)
1468+
(with-current-buffer temp-buffer
1469+
(goto-char (point-min))
1470+
(buffer-substring-no-properties (point) (line-end-position))))
1471+
(kill-buffer temp-buffer))))
1472+
(t
1473+
diff-hl-reference-revision)))
14601474

14611475
;; TODO: Cache based on .git/index's mtime, maybe.
14621476
(defun diff-hl-git-index-object-name (file)

test/diff-hl-test.el

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@
223223
Diff finished."
224224
(buffer-substring (point) (point-max))))))))
225225

226+
(diff-hl-deftest diff-hl-resolved-reference-revision-buffer-local-hg ()
227+
(diff-hl-test-in-source
228+
(setq-local diff-hl-reference-revision "test-rev")
229+
(setq-local vc-hg-program "chg")
230+
(condition-case err
231+
(progn
232+
(diff-hl-resolved-reference-revision 'Hg)
233+
(ert-fail "Expected an error to be signaled but none was."))
234+
;; We don't have a hg repo, but we can use the error message to verify the
235+
;; underlying command.
236+
(error
237+
(should (string-match-p
238+
"chg .*identify -r test-rev -i"
239+
(error-message-string err)))))))
240+
226241
(provide 'diff-hl-test)
227242

228243
;;; diff-hl-test.el ends here

0 commit comments

Comments
 (0)