]> code.delx.au - gnu-emacs-elpa/commitdiff
Implement integration with Magit 2.4
authorDmitry Gutov <dgutov@yandex.ru>
Sun, 31 Jan 2016 01:30:14 +0000 (04:30 +0300)
committerDmitry Gutov <dgutov@yandex.ru>
Sun, 31 Jan 2016 01:30:14 +0000 (04:30 +0300)
Closes #60

https://github.com/magit/magit/issues/2491
https://github.com/magit/magit/pull/2530

README.md
diff-hl.el

index ab8c9d81e25031b5a87fcc172033b2651a8892ed..8de2bf0bccd9e50db786e2ef869298cfccd8cf3d 100644 (file)
--- a/README.md
+++ b/README.md
@@ -90,5 +90,11 @@ psvn
 Magit\r
 -----\r
 \r
-If you have a recent enough version installed, it defines\r
-`magit-revert-buffer-hook` (or `magit-not-reverted-hook`), which we use.\r
+If you're using a version before 2.4.0, it defines `magit-revert-buffer-hook`\r
+(or `magit-not-reverted-hook`), which we use.\r
+\r
+When using Magit 2.4 or newer, add this to your init script:\r
+\r
+```lisp\r
+(add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh t)\r
+```\r
index 63c2cde18ba999cbc5949c9c8217f56a53b61f6f..9c6f877bde608aabb2cb5222f466eec08fc69d27 100644 (file)
@@ -488,8 +488,8 @@ in the source file, or the last line of the hunk above it."
         ;; doesn't care about changed VC state.
         ;; https://github.com/magit/magit/issues/603
         (add-hook 'magit-revert-buffer-hook 'diff-hl-update nil t)
-        ;; Magit 2+ doesn't do the above and calls this instead,
-        ;; but only when it doesn't call `revert-buffer':
+        ;; Magit versions 2.0-2.3 don't do the above and call this
+        ;; instead, but only when they dosn't call `revert-buffer':
         (add-hook 'magit-not-reverted-hook 'diff-hl-update nil t)
         (add-hook 'auto-revert-mode-hook 'diff-hl-update nil t)
         (add-hook 'text-scale-mode-hook 'diff-hl-define-bitmaps nil t))
@@ -517,6 +517,32 @@ in the source file, or the last line of the hunk above it."
       (scan diff-hl-command-map)
       (smartrep-define-key diff-hl-mode-map diff-hl-command-prefix smart-keys))))
 
+(declare-function magit-toplevel "magit-git")
+(declare-function magit-unstaged-files "magit-git")
+
+(defun diff-hl-magit-post-refresh ()
+  (let* ((topdir (magit-toplevel))
+         (modified-files
+          (mapcar (lambda (file) (expand-file-name file topdir))
+                  (magit-unstaged-files t)))
+         (unmodified-states '(up-to-date ignored unregistered)))
+    (dolist (buf (buffer-list))
+      (when (and (buffer-local-value 'diff-hl-mode buf)
+                 (not (buffer-modified-p buf))
+                 (file-in-directory-p (buffer-file-name buf) topdir))
+        (with-current-buffer buf
+          (let* ((file buffer-file-name)
+                 (backend (vc-backend file)))
+            (when backend
+              (cond
+               ((member file modified-files)
+                (when (memq (vc-state file) unmodified-states)
+                  (vc-state-refresh file backend))
+                (diff-hl-update))
+               ((not (memq (vc-state file backend) unmodified-states))
+                (vc-state-refresh file backend)
+                (diff-hl-update))))))))))
+
 (defun diff-hl-dir-update ()
   (dolist (pair (if (vc-dir-marked-files)
                     (vc-dir-marked-only-files-and-states)