git-blame.el: blame unsaved changes
[git.git] / contrib / emacs / git-blame.el
blobf1839647bcc07679a63bcc8092241dd01647b70b
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
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,
30 ;; MA 02111-1307 USA
32 ;; http://www.fsf.org/copyleft/gpl.html
35 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37 ;;; Commentary:
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.
45 ;;; Installation:
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.
62 ;;; Compatibility:
64 ;; It requires GNU Emacs 21. If you'are using Emacs 20, try
65 ;; changing this:
67 ;; (overlay-put ovl 'face (list :background
68 ;; (cdr (assq 'color (cddddr info)))))
70 ;; to
72 ;; (overlay-put ovl 'face (cons 'background-color
73 ;; (cdr (assq 'color (cddddr info)))))
76 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78 ;;; Code:
80 (require 'cl) ; to use `push', `pop'
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))
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-proc nil
106 "The running git-blame process")
107 (make-variable-buffer-local 'git-blame-proc)
109 (defvar git-blame-overlays nil
110 "The git-blame overlays used in the current buffer.")
111 (make-variable-buffer-local 'git-blame-overlays)
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)
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)))
124 ;;;###autoload
125 (defun git-blame-mode (&optional arg)
126 (interactive "P")
127 (if arg
128 (setq git-blame-mode (eq arg 1))
129 (setq git-blame-mode (not git-blame-mode)))
130 (make-local-variable 'git-blame-colors)
131 (git-blame-cleanup)
132 (if git-blame-mode
133 (progn
134 (let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
135 (if (eq bgmode 'dark)
136 (setq git-blame-colors git-blame-dark-colors)
137 (setq git-blame-colors git-blame-light-colors)))
138 (setq git-blame-cache (make-hash-table :test 'equal))
139 (git-blame-run))))
141 ;;;###autoload
142 (defun git-reblame ()
143 "Recalculate all blame information in the current buffer"
144 (unless git-blame-mode
145 (error "git-blame is not active"))
146 (interactive)
147 (git-blame-cleanup)
148 (git-blame-run))
150 (defun git-blame-run ()
151 (if git-blame-proc
152 ;; Should maybe queue up a new run here
153 (message "Already running git blame")
154 (let ((display-buf (current-buffer))
155 (blame-buf (get-buffer-create
156 (concat " git blame for " (buffer-name)))))
157 (setq git-blame-proc
158 (start-process "git-blame" blame-buf
159 "git" "blame"
160 "--incremental" "--contents" "-"
161 (file-name-nondirectory buffer-file-name)))
162 (with-current-buffer blame-buf
163 (erase-buffer)
164 (make-local-variable 'git-blame-file)
165 (make-local-variable 'git-blame-current)
166 (setq git-blame-file display-buf)
167 (setq git-blame-current nil))
168 (set-process-filter git-blame-proc 'git-blame-filter)
169 (set-process-sentinel git-blame-proc 'git-blame-sentinel)
170 (process-send-region git-blame-proc (point-min) (point-max))
171 (process-send-eof git-blame-proc))))
173 (defun git-blame-cleanup ()
174 "Remove all blame properties"
175 (mapcar 'delete-overlay git-blame-overlays)
176 (setq git-blame-overlays nil)
177 (let ((modified (buffer-modified-p)))
178 (remove-text-properties (point-min) (point-max) '(point-entered nil))
179 (set-buffer-modified-p modified)))
181 (defun git-blame-sentinel (proc status)
182 (with-current-buffer (process-buffer proc)
183 (with-current-buffer git-blame-file
184 (setq git-blame-proc nil)))
185 ;;(kill-buffer (process-buffer proc))
186 (message "git blame finished"))
188 (defvar in-blame-filter nil)
190 (defun git-blame-filter (proc str)
191 (save-excursion
192 (set-buffer (process-buffer proc))
193 (goto-char (process-mark proc))
194 (insert-before-markers str)
195 (goto-char 0)
196 (unless in-blame-filter
197 (let ((more t)
198 (in-blame-filter t))
199 (while more
200 (setq more (git-blame-parse)))))))
202 (defun git-blame-parse ()
203 (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
204 (let ((hash (match-string 1))
205 (src-line (string-to-number (match-string 2)))
206 (res-line (string-to-number (match-string 3)))
207 (num-lines (string-to-number (match-string 4))))
208 (setq git-blame-current
209 (if (string= hash "0000000000000000000000000000000000000000")
211 (git-blame-new-commit
212 hash src-line res-line num-lines))))
213 (delete-region (point) (match-end 0))
215 ((looking-at "filename \\(.+\\)\n")
216 (let ((filename (match-string 1)))
217 (git-blame-add-info "filename" filename))
218 (delete-region (point) (match-end 0))
220 ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
221 (let ((key (match-string 1))
222 (value (match-string 2)))
223 (git-blame-add-info key value))
224 (delete-region (point) (match-end 0))
226 ((looking-at "boundary\n")
227 (setq git-blame-current nil)
228 (delete-region (point) (match-end 0))
231 nil)))
234 (defun git-blame-new-commit (hash src-line res-line num-lines)
235 (save-excursion
236 (set-buffer git-blame-file)
237 (let ((info (gethash hash git-blame-cache))
238 (inhibit-point-motion-hooks t))
239 (when (not info)
240 (let ((color (pop git-blame-colors)))
241 (unless color
242 (setq color git-blame-ancient-color))
243 (setq info (list hash src-line res-line num-lines
244 (git-describe-commit hash)
245 (cons 'color color))))
246 (puthash hash info git-blame-cache))
247 (goto-line res-line)
248 (while (> num-lines 0)
249 (if (get-text-property (point) 'git-blame)
250 (forward-line)
251 (let* ((start (point))
252 (end (progn (forward-line 1) (point)))
253 (ovl (make-overlay start end)))
254 (push ovl git-blame-overlays)
255 (overlay-put ovl 'git-blame info)
256 (overlay-put ovl 'help-echo hash)
257 (overlay-put ovl 'face (list :background
258 (cdr (assq 'color (nthcdr 5 info)))))
259 ;; the point-entered property doesn't seem to work in overlays
260 ;;(overlay-put ovl 'point-entered
261 ;; `(lambda (x y) (git-blame-identify ,hash)))
262 (let ((modified (buffer-modified-p)))
263 (put-text-property (if (= start 1) start (1- start)) (1- end)
264 'point-entered
265 `(lambda (x y) (git-blame-identify ,hash)))
266 (set-buffer-modified-p modified))))
267 (setq num-lines (1- num-lines))))))
269 (defun git-blame-add-info (key value)
270 (if git-blame-current
271 (nconc git-blame-current (list (cons (intern key) value)))))
273 (defun git-blame-current-commit ()
274 (let ((info (get-char-property (point) 'git-blame)))
275 (if info
276 (car info)
277 (error "No commit info"))))
279 (defun git-describe-commit (hash)
280 (with-temp-buffer
281 (call-process "git" nil t nil
282 "log" "-1" "--pretty=oneline"
283 hash)
284 (buffer-substring (point-min) (1- (point-max)))))
286 (defvar git-blame-last-identification nil)
287 (make-variable-buffer-local 'git-blame-last-identification)
288 (defun git-blame-identify (&optional hash)
289 (interactive)
290 (let ((info (gethash (or hash (git-blame-current-commit)) git-blame-cache)))
291 (when (and info (not (eq info git-blame-last-identification)))
292 (message "%s" (nth 4 info))
293 (setq git-blame-last-identification info))))
295 (provide 'git-blame)
297 ;;; git-blame.el ends here