1 ;;; git-blame.el --- Minor mode for incremental blame for Git -*- coding: utf-8 -*-
3 ;; Copyright (C) 2007 David Kågedal
5 ;; Authors: David Kågedal <davidk@lysator.liu.se>
6 ;; Created: 31 Jan 2007
7 ;; Message-ID: <87iren2vqx.fsf@morpheus.local>
9 ;; Keywords: git, version control, release management
11 ;; Compatibility: Emacs21
14 ;; This file is *NOT* part of GNU Emacs.
15 ;; This file is distributed under the same terms as GNU Emacs.
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.
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.
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,
32 ;; http://www.fsf.org/copyleft/gpl.html
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
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:
51 ;; (require 'git-blame)
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
57 ;; (autoload 'git-blame-mode "git-blame"
58 ;; "Minor mode for incremental blame for Git." t)
60 ;; Then first use of `M-x git-blame-mode' would load the package.
64 ;; It requires GNU Emacs 21. If you'are using Emacs 20, try
67 ;; (overlay-put ovl 'face (list :background
68 ;; (cdr (assq 'color (cddddr info)))))
72 ;; (overlay-put ovl 'face (cons 'background-color
73 ;; (cdr (assq 'color (cddddr info)))))
76 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80 (require 'cl
) ; to use `push', `pop'
82 (defun color-scale (l)
91 (push (concat "#" (car r
) (car g
) (car b
)) colors
)
97 (defvar git-blame-dark-colors
98 (color-scale '("0c" "04" "24" "1c" "2c" "34" "14" "3c")))
100 (defvar git-blame-light-colors
101 (color-scale '("c4" "d4" "cc" "dc" "f4" "e4" "fc" "ec")))
103 (defvar git-blame-ancient-color
"dark green")
105 (defvar git-blame-overlays nil
)
106 (defvar git-blame-cache nil
)
108 (defvar git-blame-mode nil
)
109 (make-variable-buffer-local 'git-blame-mode
)
110 (unless (assq 'git-blame-mode minor-mode-alist
)
111 (setq minor-mode-alist
112 (cons (list 'git-blame-mode
" blame")
116 (defun git-blame-mode (&optional arg
)
119 (setq git-blame-mode
(eq arg
1))
120 (setq git-blame-mode
(not git-blame-mode
)))
121 (make-local-variable 'git-blame-overlays
)
122 (make-local-variable 'git-blame-colors
)
123 (make-local-variable 'git-blame-cache
)
127 (let ((bgmode (cdr (assoc 'background-mode
(frame-parameters)))))
128 (if (eq bgmode
'dark
)
129 (setq git-blame-colors git-blame-dark-colors
)
130 (setq git-blame-colors git-blame-light-colors
)))
131 (setq git-blame-cache
(make-hash-table :test
'equal
))
134 (defun git-blame-run ()
135 (let* ((display-buf (current-buffer))
136 (blame-buf (get-buffer-create
137 (concat " git blame for " (buffer-name))))
138 (proc (start-process "git-blame" blame-buf
139 "git" "blame" "--incremental"
140 (file-name-nondirectory buffer-file-name
))))
141 (mapcar 'delete-overlay git-blame-overlays
)
142 (setq git-blame-overlays nil
)
143 (setq git-blame-cache
(make-hash-table :test
'equal
))
144 (with-current-buffer blame-buf
146 (make-local-variable 'git-blame-file
)
147 (make-local-variable 'git-blame-current
)
148 (setq git-blame-file display-buf
)
149 (setq git-blame-current nil
))
150 (set-process-filter proc
'git-blame-filter
)
151 (set-process-sentinel proc
'git-blame-sentinel
)))
153 (defun git-blame-cleanup ()
154 "Remove all blame properties"
155 (mapcar 'delete-overlay git-blame-overlays
)
156 (setq git-blame-overlays nil
)
157 (let ((modified (buffer-modified-p)))
158 (remove-text-properties (point-min) (point-max) '(point-entered nil
))
159 (set-buffer-modified-p modified
)))
161 (defun git-blame-sentinel (proc status
)
162 ;;(kill-buffer (process-buffer proc))
163 (message "git blame finished"))
165 (defvar in-blame-filter nil
)
167 (defun git-blame-filter (proc str
)
169 (set-buffer (process-buffer proc
))
170 (goto-char (process-mark proc
))
171 (insert-before-markers str
)
173 (unless in-blame-filter
177 (setq more
(git-blame-parse)))))))
179 (defun git-blame-parse ()
180 (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
181 (let ((hash (match-string 1))
182 (src-line (string-to-number (match-string 2)))
183 (res-line (string-to-number (match-string 3)))
184 (num-lines (string-to-number (match-string 4))))
185 (setq git-blame-current
186 (if (string= hash
"0000000000000000000000000000000000000000")
188 (git-blame-new-commit
189 hash src-line res-line num-lines
))))
190 (delete-region (point) (match-end 0))
192 ((looking-at "filename \\(.+\\)\n")
193 (let ((filename (match-string 1)))
194 (git-blame-add-info "filename" filename
))
195 (delete-region (point) (match-end 0))
197 ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
198 (let ((key (match-string 1))
199 (value (match-string 2)))
200 (git-blame-add-info key value
))
201 (delete-region (point) (match-end 0))
203 ((looking-at "boundary\n")
204 (setq git-blame-current nil
)
205 (delete-region (point) (match-end 0))
211 (defun git-blame-new-commit (hash src-line res-line num-lines
)
213 (set-buffer git-blame-file
)
214 (let ((info (gethash hash git-blame-cache
))
215 (inhibit-point-motion-hooks t
))
217 (let ((color (pop git-blame-colors
)))
219 (setq color git-blame-ancient-color
))
220 (setq info
(list hash src-line res-line num-lines
221 (git-describe-commit hash
)
222 (cons 'color color
))))
223 (puthash hash info git-blame-cache
))
225 (while (> num-lines
0)
226 (if (get-text-property (point) 'git-blame
)
228 (let* ((start (point))
229 (end (progn (forward-line 1) (point)))
230 (ovl (make-overlay start end
)))
231 (push ovl git-blame-overlays
)
232 (overlay-put ovl
'git-blame info
)
233 (overlay-put ovl
'help-echo hash
)
234 (overlay-put ovl
'face
(list :background
235 (cdr (assq 'color
(nthcdr 5 info
)))))
236 ;; the point-entered property doesn't seem to work in overlays
237 ;;(overlay-put ovl 'point-entered
238 ;; `(lambda (x y) (git-blame-identify ,hash)))
239 (let ((modified (buffer-modified-p)))
240 (put-text-property (if (= start
1) start
(1- start
)) (1- end
)
242 `(lambda (x y
) (git-blame-identify ,hash
)))
243 (set-buffer-modified-p modified
))))
244 (setq num-lines
(1- num-lines
))))))
246 (defun git-blame-add-info (key value
)
247 (if git-blame-current
248 (nconc git-blame-current
(list (cons (intern key
) value
)))))
250 (defun git-blame-current-commit ()
251 (let ((info (get-char-property (point) 'git-blame
)))
254 (error "No commit info"))))
256 (defun git-describe-commit (hash)
258 (call-process "git" nil t nil
259 "log" "-1" "--pretty=oneline"
261 (buffer-substring (point-min) (1- (point-max)))))
263 (defvar git-blame-last-identification nil
)
264 (make-variable-buffer-local 'git-blame-last-identification
)
265 (defun git-blame-identify (&optional hash
)
267 (let ((info (gethash (or hash
(git-blame-current-commit)) git-blame-cache
)))
268 (when (and info
(not (eq info git-blame-last-identification
)))
269 (message "%s" (nth 4 info
))
270 (setq git-blame-last-identification info
))))
274 ;;; git-blame.el ends here