7daa4e69931d400b25f65d4b725c1363085b6339
   1;;; git-blame.el --- Minor mode for incremental blame for Git  -*- coding: utf-8 -*-
   2;;
   3;; Copyright (C) 2007  David Kågedal
   4;;
   5;; Authors:    David Kågedal <davidk@lysator.liu.se>
   6;; Created:    31 Jan 2007
   7;; Message-ID: <87iren2vqx.fsf@morpheus.local>
   8;; License:    GPL
   9;; Keywords:   git, version control, release management
  10;;
  11;; Compatibility: Emacs21
  12
  13
  14;; This file is *NOT* part of GNU Emacs.
  15;; This file is distributed under the same terms as GNU Emacs.
  16
  17;; This program is free software; you can redistribute it and/or
  18;; modify it under the terms of the GNU General Public License as
  19;; published by the Free Software Foundation; either version 2 of
  20;; the License, or (at your option) any later version.
  21
  22;; This program is distributed in the hope that it will be
  23;; useful, but WITHOUT ANY WARRANTY; without even the implied
  24;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  25;; PURPOSE.  See the GNU General Public License for more details.
  26
  27;; You should have received a copy of the GNU General Public
  28;; License along with this program; if not, write to the Free
  29;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  30;; MA 02111-1307 USA
  31
  32;; http://www.fsf.org/copyleft/gpl.html
  33
  34
  35;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36;;
  37;;; Commentary:
  38;;
  39;; Here is an Emacs implementation of incremental git-blame.  When you
  40;; turn it on while viewing a file, the editor buffer will be updated by
  41;; setting the background of individual lines to a color that reflects
  42;; which commit it comes from.  And when you move around the buffer, a
  43;; one-line summary will be shown in the echo area.
  44
  45;;; Installation:
  46;;
  47;; To use this package, put it somewhere in `load-path' (or add
  48;; directory with git-blame.el to `load-path'), and add the following
  49;; line to your .emacs:
  50;;
  51;;    (require 'git-blame)
  52;;
  53;; If you do not want to load this package before it is necessary, you
  54;; can make use of the `autoload' feature, e.g. by adding to your .emacs
  55;; the following lines
  56;;
  57;;    (autoload 'git-blame-mode "git-blame"
  58;;              "Minor mode for incremental blame for Git." t)
  59;;
  60;; Then first use of `M-x git-blame-mode' would load the package.
  61
  62;;; Compatibility:
  63;;
  64;; It requires GNU Emacs 21.  If you'are using Emacs 20, try
  65;; changing this:
  66;;
  67;;            (overlay-put ovl 'face (list :background
  68;;                                         (cdr (assq 'color (cddddr info)))))
  69;;
  70;; to
  71;;
  72;;            (overlay-put ovl 'face (cons 'background-color
  73;;                                         (cdr (assq 'color (cddddr info)))))
  74
  75
  76;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  77;;
  78;;; Code:
  79
  80(require 'cl)                         ; to use `push', `pop'
  81
  82(defun color-scale (l)
  83  (let* ((colors ())
  84         r g b)
  85    (setq r l)
  86    (while r
  87      (setq g l)
  88      (while g
  89        (setq b l)
  90        (while b
  91          (push (concat "#" (car r) (car g) (car b)) colors)
  92          (pop b))
  93        (pop g))
  94      (pop r))
  95    colors))
  96
  97(defvar git-blame-dark-colors
  98  (color-scale '("0c" "04" "24" "1c" "2c" "34" "14" "3c")))
  99
 100(defvar git-blame-light-colors
 101  (color-scale '("c4" "d4" "cc" "dc" "f4" "e4" "fc" "ec")))
 102
 103(defvar git-blame-ancient-color "dark green")
 104
 105(defvar git-blame-proc nil
 106  "The running git-blame process")
 107(make-variable-buffer-local 'git-blame-proc)
 108
 109(defvar git-blame-overlays nil
 110  "The git-blame overlays used in the current buffer.")
 111(make-variable-buffer-local 'git-blame-overlays)
 112
 113(defvar git-blame-cache nil
 114  "A cache of git-blame information for the current buffer")
 115(make-variable-buffer-local 'git-blame-cache)
 116
 117(defvar git-blame-mode nil)
 118(make-variable-buffer-local 'git-blame-mode)
 119(unless (assq 'git-blame-mode minor-mode-alist)
 120  (setq minor-mode-alist
 121        (cons (list 'git-blame-mode " blame")
 122              minor-mode-alist)))
 123
 124;;;###autoload
 125(defun git-blame-mode (&optional arg)
 126  "Minor mode for displaying Git blame"
 127  (interactive "P")
 128  (if arg
 129      (setq git-blame-mode (eq arg 1))
 130    (setq git-blame-mode (not git-blame-mode)))
 131  (make-local-variable 'git-blame-colors)
 132  (git-blame-cleanup)
 133  (if git-blame-mode
 134      (progn
 135        (let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
 136          (if (eq bgmode 'dark)
 137              (setq git-blame-colors git-blame-dark-colors)
 138            (setq git-blame-colors git-blame-light-colors)))
 139        (setq git-blame-cache (make-hash-table :test 'equal))
 140        (git-blame-run))))
 141
 142;;;###autoload
 143(defun git-reblame ()
 144  "Recalculate all blame information in the current buffer"
 145  (unless git-blame-mode
 146    (error "git-blame is not active"))
 147  (interactive)
 148  (git-blame-cleanup)
 149  (git-blame-run))
 150
 151(defun git-blame-run ()
 152  (if git-blame-proc
 153      ;; Should maybe queue up a new run here
 154      (message "Already running git blame")
 155    (let ((display-buf (current-buffer))
 156          (blame-buf (get-buffer-create
 157                      (concat " git blame for " (buffer-name)))))
 158      (setq git-blame-proc
 159            (start-process "git-blame" blame-buf
 160                           "git" "blame"
 161                           "--incremental" "--contents" "-"
 162                           (file-name-nondirectory buffer-file-name)))
 163      (with-current-buffer blame-buf
 164        (erase-buffer)
 165        (make-local-variable 'git-blame-file)
 166        (make-local-variable 'git-blame-current)
 167        (setq git-blame-file display-buf)
 168        (setq git-blame-current nil))
 169      (set-process-filter git-blame-proc 'git-blame-filter)
 170      (set-process-sentinel git-blame-proc 'git-blame-sentinel)
 171      (process-send-region git-blame-proc (point-min) (point-max))
 172      (process-send-eof git-blame-proc))))
 173
 174(defun remove-git-blame-text-properties (start end)
 175  (let ((modified (buffer-modified-p))
 176        (inhibit-read-only t))
 177    (remove-text-properties start end '(point-entered nil))
 178    (set-buffer-modified-p modified)))
 179
 180(defun git-blame-cleanup ()
 181  "Remove all blame properties"
 182    (mapcar 'delete-overlay git-blame-overlays)
 183    (setq git-blame-overlays nil)
 184    (remove-git-blame-text-properties (point-min) (point-max)))
 185
 186(defun git-blame-sentinel (proc status)
 187  (with-current-buffer (process-buffer proc)
 188    (with-current-buffer git-blame-file
 189      (setq git-blame-proc nil)))
 190  ;;(kill-buffer (process-buffer proc))
 191  ;;(message "git blame finished")
 192  )
 193
 194(defvar in-blame-filter nil)
 195
 196(defun git-blame-filter (proc str)
 197  (save-excursion
 198    (set-buffer (process-buffer proc))
 199    (goto-char (process-mark proc))
 200    (insert-before-markers str)
 201    (goto-char 0)
 202    (unless in-blame-filter
 203      (let ((more t)
 204            (in-blame-filter t))
 205        (while more
 206          (setq more (git-blame-parse)))))))
 207
 208(defun git-blame-parse ()
 209  (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
 210         (let ((hash (match-string 1))
 211               (src-line (string-to-number (match-string 2)))
 212               (res-line (string-to-number (match-string 3)))
 213               (num-lines (string-to-number (match-string 4))))
 214           (setq git-blame-current
 215                 (if (string= hash "0000000000000000000000000000000000000000")
 216                     nil
 217                   (git-blame-new-commit
 218                    hash src-line res-line num-lines))))
 219         (delete-region (point) (match-end 0))
 220         t)
 221        ((looking-at "filename \\(.+\\)\n")
 222         (let ((filename (match-string 1)))
 223           (git-blame-add-info "filename" filename))
 224         (delete-region (point) (match-end 0))
 225         t)
 226        ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
 227         (let ((key (match-string 1))
 228               (value (match-string 2)))
 229           (git-blame-add-info key value))
 230         (delete-region (point) (match-end 0))
 231         t)
 232        ((looking-at "boundary\n")
 233         (setq git-blame-current nil)
 234         (delete-region (point) (match-end 0))
 235         t)
 236        (t
 237         nil)))
 238
 239
 240(defun git-blame-new-commit (hash src-line res-line num-lines)
 241  (save-excursion
 242    (set-buffer git-blame-file)
 243    (let ((info (gethash hash git-blame-cache))
 244          (inhibit-point-motion-hooks t))
 245      (when (not info)
 246        (let ((color (pop git-blame-colors)))
 247          (unless color
 248            (setq color git-blame-ancient-color))
 249          (setq info (list hash src-line res-line num-lines
 250                           (git-describe-commit hash)
 251                           (cons 'color color))))
 252        (puthash hash info git-blame-cache))
 253      (goto-line res-line)
 254      (while (> num-lines 0)
 255        (if (get-text-property (point) 'git-blame)
 256            (forward-line)
 257          (let* ((start (point))
 258                 (end (progn (forward-line 1) (point)))
 259                 (ovl (make-overlay start end)))
 260            (push ovl git-blame-overlays)
 261            (overlay-put ovl 'git-blame info)
 262            (overlay-put ovl 'help-echo hash)
 263            (overlay-put ovl 'face (list :background
 264                                         (cdr (assq 'color (nthcdr 5 info)))))
 265            ;; the point-entered property doesn't seem to work in overlays
 266            ;;(overlay-put ovl 'point-entered
 267            ;;             `(lambda (x y) (git-blame-identify ,hash)))
 268            (let ((modified (buffer-modified-p)))
 269              (put-text-property (if (= start 1) start (1- start)) (1- end)
 270                                 'point-entered
 271                                 `(lambda (x y) (git-blame-identify ,hash)))
 272              (set-buffer-modified-p modified))))
 273        (setq num-lines (1- num-lines))))))
 274
 275(defun git-blame-add-info (key value)
 276  (if git-blame-current
 277      (nconc git-blame-current (list (cons (intern key) value)))))
 278
 279(defun git-blame-current-commit ()
 280  (let ((info (get-char-property (point) 'git-blame)))
 281    (if info
 282        (car info)
 283      (error "No commit info"))))
 284
 285(defun git-describe-commit (hash)
 286  (with-temp-buffer
 287    (call-process "git" nil t nil
 288                  "log" "-1" "--pretty=oneline"
 289                  hash)
 290    (buffer-substring (point-min) (1- (point-max)))))
 291
 292(defvar git-blame-last-identification nil)
 293(make-variable-buffer-local 'git-blame-last-identification)
 294(defun git-blame-identify (&optional hash)
 295  (interactive)
 296  (let ((info (gethash (or hash (git-blame-current-commit)) git-blame-cache)))
 297    (when (and info (not (eq info git-blame-last-identification)))
 298      (message "%s" (nth 4 info))
 299      (setq git-blame-last-identification info))))
 300
 301(provide 'git-blame)
 302
 303;;; git-blame.el ends here