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, Emacs22 and EmacsCVS
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 or later and Git 1.5.0 and up
66
;; If you'are using Emacs 20, try changing this:
68
;; (overlay-put ovl 'face (list :background
69
;; (cdr (assq 'color (cddddr info)))))
73
;; (overlay-put ovl 'face (cons 'background-color
74
;; (cdr (assq 'color (cddddr info)))))
77
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81
(eval-when-compile (require 'cl)) ; to use `push', `pop'
84
(defun git-blame-color-scale (&rest elements)
85
"Given a list, returns a list of triples formed with each
88
a b => bbb bba bab baa abb aba aaa aab"
93
(setq result (cons (format "#%s%s%s" a b c) result)))))
96
;; (git-blame-color-scale "0c" "04" "24" "1c" "2c" "34" "14" "3c") =>
97
;; ("#3c3c3c" "#3c3c14" "#3c3c34" "#3c3c2c" "#3c3c1c" "#3c3c24"
98
;; "#3c3c04" "#3c3c0c" "#3c143c" "#3c1414" "#3c1434" "#3c142c" ...)
100
(defmacro git-blame-random-pop (l)
101
"Select a random element from L and returns it. Also remove
102
selected element from l."
103
;; only works on lists with unique elements
104
`(let ((e (elt ,l (random (length ,l)))))
105
(setq ,l (remove e ,l))
108
(defvar git-blame-dark-colors
109
(git-blame-color-scale "0c" "04" "24" "1c" "2c" "34" "14" "3c")
110
"*List of colors (format #RGB) to use in a dark environment.
112
To check out the list, evaluate (list-colors-display git-blame-dark-colors).")
114
(defvar git-blame-light-colors
115
(git-blame-color-scale "c4" "d4" "cc" "dc" "f4" "e4" "fc" "ec")
116
"*List of colors (format #RGB) to use in a light environment.
118
To check out the list, evaluate (list-colors-display git-blame-light-colors).")
120
(defvar git-blame-colors '()
121
"Colors used by git-blame. The list is built once when activating git-blame
124
(defvar git-blame-ancient-color "dark green"
125
"*Color to be used for ancient commit.")
127
(defvar git-blame-autoupdate t
128
"*Automatically update the blame display while editing")
130
(defvar git-blame-proc nil
131
"The running git-blame process")
132
(make-variable-buffer-local 'git-blame-proc)
134
(defvar git-blame-overlays nil
135
"The git-blame overlays used in the current buffer.")
136
(make-variable-buffer-local 'git-blame-overlays)
138
(defvar git-blame-cache nil
139
"A cache of git-blame information for the current buffer")
140
(make-variable-buffer-local 'git-blame-cache)
142
(defvar git-blame-idle-timer nil
143
"An idle timer that updates the blame")
144
(make-variable-buffer-local 'git-blame-cache)
146
(defvar git-blame-update-queue nil
147
"A queue of update requests")
148
(make-variable-buffer-local 'git-blame-update-queue)
151
(defvar git-blame-file nil)
152
(defvar git-blame-current nil)
154
(defvar git-blame-mode nil)
155
(make-variable-buffer-local 'git-blame-mode)
157
(defvar git-blame-mode-line-string " blame"
158
"String to display on the mode line when git-blame is active.")
160
(or (assq 'git-blame-mode minor-mode-alist)
161
(setq minor-mode-alist
162
(cons '(git-blame-mode git-blame-mode-line-string) minor-mode-alist)))
165
(defun git-blame-mode (&optional arg)
166
"Toggle minor mode for displaying Git blame
168
With prefix ARG, turn the mode on if ARG is positive."
172
(if git-blame-mode (git-blame-mode-off) (git-blame-mode-on)))
173
((> (prefix-numeric-value arg) 0) (git-blame-mode-on))
174
(t (git-blame-mode-off))))
176
(defun git-blame-mode-on ()
177
"Turn on git-blame mode.
179
See also function `git-blame-mode'."
180
(make-local-variable 'git-blame-colors)
181
(if git-blame-autoupdate
182
(add-hook 'after-change-functions 'git-blame-after-change nil t)
183
(remove-hook 'after-change-functions 'git-blame-after-change t))
185
(let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
186
(if (eq bgmode 'dark)
187
(setq git-blame-colors git-blame-dark-colors)
188
(setq git-blame-colors git-blame-light-colors)))
189
(setq git-blame-cache (make-hash-table :test 'equal))
190
(setq git-blame-mode t)
193
(defun git-blame-mode-off ()
194
"Turn off git-blame mode.
196
See also function `git-blame-mode'."
198
(if git-blame-idle-timer (cancel-timer git-blame-idle-timer))
199
(setq git-blame-mode nil))
202
(defun git-reblame ()
203
"Recalculate all blame information in the current buffer"
205
(unless git-blame-mode
206
(error "Git-blame is not active"))
211
(defun git-blame-run (&optional startline endline)
213
;; Should maybe queue up a new run here
214
(message "Already running git blame")
215
(let ((display-buf (current-buffer))
216
(blame-buf (get-buffer-create
217
(concat " git blame for " (buffer-name))))
218
(args '("--incremental" "--contents" "-")))
220
(setq args (append args
221
(list "-L" (format "%d,%d" startline endline)))))
222
(setq args (append args
223
(list (file-name-nondirectory buffer-file-name))))
225
(apply 'start-process
226
"git-blame" blame-buf
229
(with-current-buffer blame-buf
231
(make-local-variable 'git-blame-file)
232
(make-local-variable 'git-blame-current)
233
(setq git-blame-file display-buf)
234
(setq git-blame-current nil))
235
(set-process-filter git-blame-proc 'git-blame-filter)
236
(set-process-sentinel git-blame-proc 'git-blame-sentinel)
237
(process-send-region git-blame-proc (point-min) (point-max))
238
(process-send-eof git-blame-proc))))
240
(defun remove-git-blame-text-properties (start end)
241
(let ((modified (buffer-modified-p))
242
(inhibit-read-only t))
243
(remove-text-properties start end '(point-entered nil))
244
(set-buffer-modified-p modified)))
246
(defun git-blame-cleanup ()
247
"Remove all blame properties"
248
(mapcar 'delete-overlay git-blame-overlays)
249
(setq git-blame-overlays nil)
250
(remove-git-blame-text-properties (point-min) (point-max)))
252
(defun git-blame-update-region (start end)
253
"Rerun blame to get updates between START and END"
254
(let ((overlays (overlays-in start end)))
256
(let ((overlay (pop overlays)))
257
(if (< (overlay-start overlay) start)
258
(setq start (overlay-start overlay)))
259
(if (> (overlay-end overlay) end)
260
(setq end (overlay-end overlay)))
261
(setq git-blame-overlays (delete overlay git-blame-overlays))
262
(delete-overlay overlay))))
263
(remove-git-blame-text-properties start end)
264
;; We can be sure that start and end are at line breaks
265
(git-blame-run (1+ (count-lines (point-min) start))
266
(count-lines (point-min) end)))
268
(defun git-blame-sentinel (proc status)
269
(with-current-buffer (process-buffer proc)
270
(with-current-buffer git-blame-file
271
(setq git-blame-proc nil)
272
(if git-blame-update-queue
273
(git-blame-delayed-update))))
274
;;(kill-buffer (process-buffer proc))
275
;;(message "git blame finished")
278
(defvar in-blame-filter nil)
280
(defun git-blame-filter (proc str)
282
(set-buffer (process-buffer proc))
283
(goto-char (process-mark proc))
284
(insert-before-markers str)
286
(unless in-blame-filter
290
(setq more (git-blame-parse)))))))
292
(defun git-blame-parse ()
293
(cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
294
(let ((hash (match-string 1))
295
(src-line (string-to-number (match-string 2)))
296
(res-line (string-to-number (match-string 3)))
297
(num-lines (string-to-number (match-string 4))))
298
(setq git-blame-current
299
(if (string= hash "0000000000000000000000000000000000000000")
301
(git-blame-new-commit
302
hash src-line res-line num-lines))))
303
(delete-region (point) (match-end 0))
305
((looking-at "filename \\(.+\\)\n")
306
(let ((filename (match-string 1)))
307
(git-blame-add-info "filename" filename))
308
(delete-region (point) (match-end 0))
310
((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
311
(let ((key (match-string 1))
312
(value (match-string 2)))
313
(git-blame-add-info key value))
314
(delete-region (point) (match-end 0))
316
((looking-at "boundary\n")
317
(setq git-blame-current nil)
318
(delete-region (point) (match-end 0))
323
(defun git-blame-new-commit (hash src-line res-line num-lines)
325
(set-buffer git-blame-file)
326
(let ((info (gethash hash git-blame-cache))
327
(inhibit-point-motion-hooks t)
328
(inhibit-modification-hooks t))
330
;; Assign a random color to each new commit info
331
;; Take care not to select the same color multiple times
332
(let ((color (if git-blame-colors
333
(git-blame-random-pop git-blame-colors)
334
git-blame-ancient-color)))
335
(setq info (list hash src-line res-line num-lines
336
(git-describe-commit hash)
337
(cons 'color color))))
338
(puthash hash info git-blame-cache))
340
(while (> num-lines 0)
341
(if (get-text-property (point) 'git-blame)
343
(let* ((start (point))
344
(end (progn (forward-line 1) (point)))
345
(ovl (make-overlay start end)))
346
(push ovl git-blame-overlays)
347
(overlay-put ovl 'git-blame info)
348
(overlay-put ovl 'help-echo hash)
349
(overlay-put ovl 'face (list :background
350
(cdr (assq 'color (nthcdr 5 info)))))
351
;; the point-entered property doesn't seem to work in overlays
352
;;(overlay-put ovl 'point-entered
353
;; `(lambda (x y) (git-blame-identify ,hash)))
354
(let ((modified (buffer-modified-p)))
355
(put-text-property (if (= start 1) start (1- start)) (1- end)
357
`(lambda (x y) (git-blame-identify ,hash)))
358
(set-buffer-modified-p modified))))
359
(setq num-lines (1- num-lines))))))
361
(defun git-blame-add-info (key value)
362
(if git-blame-current
363
(nconc git-blame-current (list (cons (intern key) value)))))
365
(defun git-blame-current-commit ()
366
(let ((info (get-char-property (point) 'git-blame)))
369
(error "No commit info"))))
371
(defun git-describe-commit (hash)
373
(call-process "git" nil t nil
374
"log" "-1" "--pretty=oneline"
376
(buffer-substring (point-min) (1- (point-max)))))
378
(defvar git-blame-last-identification nil)
379
(make-variable-buffer-local 'git-blame-last-identification)
380
(defun git-blame-identify (&optional hash)
382
(let ((info (gethash (or hash (git-blame-current-commit)) git-blame-cache)))
383
(when (and info (not (eq info git-blame-last-identification)))
384
(message "%s" (nth 4 info))
385
(setq git-blame-last-identification info))))
387
;; (defun git-blame-after-save ()
388
;; (when git-blame-mode
389
;; (git-blame-cleanup)
391
;; (add-hook 'after-save-hook 'git-blame-after-save)
393
(defun git-blame-after-change (start end length)
395
(git-blame-enq-update start end)))
397
(defvar git-blame-last-update nil)
398
(make-variable-buffer-local 'git-blame-last-update)
399
(defun git-blame-enq-update (start end)
400
"Mark the region between START and END as needing blame update"
401
;; Try to be smart and avoid multiple callouts for sequential
403
(cond ((and git-blame-last-update
404
(= start (cdr git-blame-last-update)))
405
(setcdr git-blame-last-update end))
406
((and git-blame-last-update
407
(= end (car git-blame-last-update)))
408
(setcar git-blame-last-update start))
410
(setq git-blame-last-update (cons start end))
411
(setq git-blame-update-queue (nconc git-blame-update-queue
412
(list git-blame-last-update)))))
413
(unless (or git-blame-proc git-blame-idle-timer)
414
(setq git-blame-idle-timer
415
(run-with-idle-timer 0.5 nil 'git-blame-delayed-update))))
417
(defun git-blame-delayed-update ()
418
(setq git-blame-idle-timer nil)
419
(if git-blame-update-queue
420
(let ((first (pop git-blame-update-queue))
421
(inhibit-point-motion-hooks t))
422
(git-blame-update-region (car first) (cdr first)))))
426
;;; git-blame.el ends here