Skip to content

Commit 8a14049

Browse files
committed
Fix diff-hl-resolved-reference-revision for Hg and Bzr with buffer-local diff-hl-reference-revision.
**Problem**: `diff-hl-resolved-reference-revision` fails to work correctly when `diff-hl-reference-revision` is set as a buffer-local variable. Example: ```emacs-lisp (setq-local diff-hl-reference-revision "test-rev") ``` **Cause**: When the implementation for backends like Hg (and similarly Bzr) uses `with-temp-buffer`, buffer-local variables from the original buffer context are not accessible. Consequently, `diff-hl-reference-revision` evaluates to `nil` within the temporary buffer. For 'Hg as an exmaple, ```emacs-lisp (with-temp-buffer (vc-hg-command (current-buffer) 0 nil "identify" "-r" diff-hl-reference-revision "-i") ...) ``` This leads to an incorrect command being executed: * **Expected hg command**: `hg --config ui.report_untrusted=0 identify -r test-rev -i .` * **Actual hg command**: `hg --config ui.report_untrusted=0 identify -r -i .` **Fix**: To resolve this, the value of `diff-hl-reference-revision` is now copied into a non-buffer-local variable (e.g., `original-diff-hl-reference-revision`) *before* entering the `with-temp-buffer` block. This non-buffer-local variable is then used within the temporary buffer context, ensuring the correct revision is always passed to the underlying version control command. **Tests**: I have tested it in my hg repo. Also added a test in `diff-hl-test.el`.
1 parent c1cecb2 commit 8a14049

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

diff-hl.el

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,19 +1442,25 @@ 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 `diff-hl-reference-revision`
1446+
;; inside the `with-temp-buffer`.
1447+
(let ((original-diff-hl-reference-revision diff-hl-reference-revision))
1448+
(with-temp-buffer
1449+
(vc-hg-command (current-buffer) 0 nil
1450+
"identify" "-r" original-diff-hl-reference-revision
1451+
"-i")
1452+
(goto-char (point-min))
1453+
(buffer-substring-no-properties (point) (line-end-position)))))
14511454
((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))))
1455+
;; Preserve the potentially buffer-local `diff-hl-reference-revision`
1456+
;; inside the `with-temp-buffer`.
1457+
(let ((original-diff-hl-reference-revision diff-hl-reference-revision))
1458+
(with-temp-buffer
1459+
(vc-bzr-command (current-buffer) 0 nil
1460+
"log" "--log-format=template" "--template-str='{revno}'"
1461+
"-r" original-diff-hl-reference-revision)
1462+
(goto-char (point-min))
1463+
(buffer-substring-no-properties (point) (line-end-position)))))
14581464
(t
14591465
diff-hl-reference-revision)))
14601466

test/diff-hl-test.el

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,20 @@
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+
(condition-case err
230+
(progn
231+
(diff-hl-resolved-reference-revision 'Hg)
232+
(ert-fail "Expected an error to be signaled but none was."))
233+
;; We don't have a hg repo, but we can use the error message to verify the
234+
;; underlying command.
235+
(error
236+
(should (string-match-p
237+
(regexp-quote "identify -r test-rev -i .")
238+
(error-message-string err)))))))
239+
226240
(provide 'diff-hl-test)
227241

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

0 commit comments

Comments
 (0)