]> code.delx.au - gnu-emacs-elpa/commitdiff
Add diff-hl-amend-mode
authorDmitry Gutov <dgutov@yandex.ru>
Tue, 3 Sep 2013 16:14:20 +0000 (19:14 +0300)
committerDmitry Gutov <dgutov@yandex.ru>
Tue, 3 Sep 2013 16:14:59 +0000 (19:14 +0300)
Closes #11

diff-hl-amend.el [new file with mode: 0644]
diff-hl.el

diff --git a/diff-hl-amend.el b/diff-hl-amend.el
new file mode 100644 (file)
index 0000000..f3a151b
--- /dev/null
@@ -0,0 +1,63 @@
+;; Author:   Dmitry Gutov <dgutov@yandex.ru>
+;; URL:      https://github.com/dgutov/diff-hl
+
+;; This file is not part of GNU Emacs.
+
+;; This file is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; This file is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;; Toggle in the current buffer with `M-x diff-hl-amend-mode'.
+;; Toggle in all buffers with `M-x global-diff-hl-amend-mode'.
+
+;;; Code:
+
+;;;###autoload
+(define-minor-mode diff-hl-amend-mode
+  "Show changes against the second-last revision in `diff-hl-mode'.
+Most useful with VCSes that support rewriting local commits, and
+'amending' the most recent one in particular.
+Currently only supports Git, Mercurial and Bazaar."
+  :lighter " Amend"
+  (if diff-hl-amend-mode
+      (progn
+        (if vc-mode
+            (diff-hl-amend-set)
+          (add-hook 'find-file-hook 'diff-hl-amend-setup nil t))
+        (add-hook 'after-revert-hook 'diff-hl-amend-setup nil t))
+    (remove-hook 'find-file-hook 'diff-hl-amend-setup t)
+    (remove-hook 'after-revert-hook 'diff-hl-amend-setup t)
+    (setq-local diff-hl-reference-revision nil))
+  (when diff-hl-mode
+    (diff-hl-update)))
+
+(defun diff-hl-amend-setup ()
+  (let ((backend (vc-backend buffer-file-name)))
+    (when backend
+      (setq-local diff-hl-reference-revision
+                  (cl-case backend
+                    (Git
+                     "HEAD^")
+                    (Hg
+                     "-2")
+                    (Bzr
+                     "revno:-2"))))))
+
+;;;###autoload
+(define-globalized-minor-mode global-diff-hl-amend-mode diff-hl-amend-mode
+  turn-on-diff-hl-amend-mode)
+
+(defun turn-on-diff-hl-amend-mode ()
+  "Turn on `diff-hl-amend-mode' in a buffer if appropriate."
+  (and buffer-file-name (diff-hl-amend-mode 1)))
index 2872e378cab38c4ada98f4d3c59a938f01e79de3..506ae31ad774b7cc245ec8341f933ab01e775aa7 100644 (file)
@@ -87,6 +87,9 @@
   :group 'diff-hl\r
   :type 'boolean)\r
 \r
+(defvar diff-hl-reference-revision nil\r
+  "Revision to diff against.  nil means the most recent one.")\r
+\r
 (defun diff-hl-define-bitmaps ()\r
   (let* ((scale (if (and (boundp 'text-scale-mode-amount)\r
                          (numberp text-scale-mode-amount))\r
          ((or (eq state 'edited)\r
               (and (eq state 'up-to-date)\r
                    ;; VC state is stale in after-revert-hook.\r
-                   revert-buffer-in-progress-p))\r
+                   (or revert-buffer-in-progress-p\r
+                       ;; Diffing against an older revision.\r
+                       diff-hl-reference-revision)))\r
           (let* ((buf-name " *diff-hl* ")\r
+                 diff-auto-refine-mode\r
                  res)\r
             (diff-hl-with-diff-switches\r
-             (vc-call-backend backend 'diff (list file) nil nil buf-name))\r
+             (vc-call-backend backend 'diff (list file)\r
+                              diff-hl-reference-revision nil\r
+                              buf-name))\r
             (with-current-buffer buf-name\r
               (goto-char (point-min))\r
               (unless (eobp)\r
   (vc-buffer-sync)\r
   (let* ((line (line-number-at-pos))\r
          (buffer (current-buffer)))\r
-    (vc-diff-internal t (vc-deduce-fileset) nil nil t)\r
+    (vc-diff-internal t (vc-deduce-fileset) diff-hl-reference-revision nil t)\r
     (vc-exec-after `(if (< (line-number-at-pos (point-max)) 3)\r
                         (with-current-buffer ,buffer (diff-hl-remove-overlays))\r
                       (diff-hl-diff-skip-to ,line)\r
@@ -279,7 +287,8 @@ in the source file, or the last line of the hunk above it."
         (fileset (vc-deduce-fileset)))\r
     (unwind-protect\r
         (progn\r
-          (vc-diff-internal nil fileset nil nil nil diff-buffer)\r
+          (vc-diff-internal nil fileset diff-hl-reference-revision nil\r
+                            nil diff-buffer)\r
           (vc-exec-after\r
            `(let (beg-line end-line)\r
               (when (eobp)\r