Handle uncommitted changes and cache descriptions
[git/gitweb.git] / contrib / emacs / git-blame.el
blobc99437e5e5bdd337b4538e695a9a3a6a94af0ffc
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 '("00" "04" "08" "0c"
99 "10" "14" "18" "1c"
100 "20" "24" "28" "2c"
101 "30" "34" "38" "3c")))
103 (defvar git-blame-light-colors
104 (color-scale '("c0" "c4" "c8" "cc"
105 "d0" "d4" "d8" "dc"
106 "e0" "e4" "e8" "ec"
107 "f0" "f4" "f8" "fc")))
109 (defvar git-blame-ancient-color "dark green")
111 (defvar git-blame-overlays nil)
112 (defvar git-blame-cache nil)
114 (defvar git-blame-mode nil)
115 (make-variable-buffer-local 'git-blame-mode)
116 (unless (assq 'git-blame-mode minor-mode-alist)
117 (setq minor-mode-alist
118 (cons (list 'git-blame-mode " blame")
119 minor-mode-alist)))
121 ;;;###autoload
122 (defun git-blame-mode (&optional arg)
123 (interactive "P")
124 (if arg
125 (setq git-blame-mode (eq arg 1))
126 (setq git-blame-mode (not git-blame-mode)))
127 (make-local-variable 'git-blame-overlays)
128 (make-local-variable 'git-blame-colors)
129 (make-local-variable 'git-blame-cache)
130 (let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
131 (if (eq bgmode 'dark)
132 (setq git-blame-colors git-blame-dark-colors)
133 (setq git-blame-colors git-blame-light-colors)))
134 (if git-blame-mode
135 (git-blame-run)
136 (git-blame-cleanup)))
138 (defun git-blame-run ()
139 (let* ((display-buf (current-buffer))
140 (blame-buf (get-buffer-create
141 (concat " git blame for " (buffer-name))))
142 (proc (start-process "git-blame" blame-buf
143 "git" "blame" "--incremental"
144 (file-name-nondirectory buffer-file-name))))
145 (mapcar 'delete-overlay git-blame-overlays)
146 (setq git-blame-overlays nil)
147 (setq git-blame-cache (make-hash-table :test 'equal))
148 (with-current-buffer blame-buf
149 (erase-buffer)
150 (make-local-variable 'git-blame-file)
151 (make-local-variable 'git-blame-current)
152 (setq git-blame-file display-buf)
153 (setq git-blame-current nil))
154 (set-process-filter proc 'git-blame-filter)
155 (set-process-sentinel proc 'git-blame-sentinel)))
157 (defun git-blame-cleanup ()
158 "Remove all blame properties"
159 (mapcar 'delete-overlay git-blame-overlays)
160 (setq git-blame-overlays nil)
161 (let ((modified (buffer-modified-p)))
162 (remove-text-properties (point-min) (point-max) '(point-entered nil))
163 (set-buffer-modified-p modified)))
165 (defun git-blame-sentinel (proc status)
166 ;;(kill-buffer (process-buffer proc))
167 (message "git blame finished"))
169 (defvar in-blame-filter nil)
171 (defun git-blame-filter (proc str)
172 (save-excursion
173 (set-buffer (process-buffer proc))
174 (goto-char (process-mark proc))
175 (insert-before-markers str)
176 (goto-char 0)
177 (unless in-blame-filter
178 (let ((more t)
179 (in-blame-filter t))
180 (while more
181 (setq more (git-blame-parse)))))))
183 (defun git-blame-parse ()
184 (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
185 (let ((hash (match-string 1))
186 (src-line (string-to-number (match-string 2)))
187 (res-line (string-to-number (match-string 3)))
188 (num-lines (string-to-number (match-string 4))))
189 (setq git-blame-current
190 (if (string= hash "0000000000000000000000000000000000000000")
192 (git-blame-new-commit
193 hash src-line res-line num-lines))))
194 (delete-region (point) (match-end 0))
196 ((looking-at "filename \\(.+\\)\n")
197 (let ((filename (match-string 1)))
198 (git-blame-add-info "filename" filename))
199 (delete-region (point) (match-end 0))
201 ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
202 (let ((key (match-string 1))
203 (value (match-string 2)))
204 (git-blame-add-info key value))
205 (delete-region (point) (match-end 0))
207 ((looking-at "boundary\n")
208 (setq git-blame-current nil)
209 (delete-region (point) (match-end 0))
212 nil)))
215 (defun git-blame-new-commit (hash src-line res-line num-lines)
216 (save-excursion
217 (set-buffer git-blame-file)
218 (let ((info (gethash hash git-blame-cache))
219 (inhibit-point-motion-hooks t))
220 (when (not info)
221 (let ((color (pop git-blame-colors)))
222 (unless color
223 (setq color git-blame-ancient-color))
224 (setq info (list hash src-line res-line num-lines
225 (git-describe-commit hash)
226 (cons 'color color))))
227 (puthash hash info git-blame-cache))
228 (goto-line res-line)
229 (while (> num-lines 0)
230 (if (get-text-property (point) 'git-blame)
231 (forward-line)
232 (let* ((start (point))
233 (end (progn (forward-line 1) (point)))
234 (ovl (make-overlay start end)))
235 (push ovl git-blame-overlays)
236 (overlay-put ovl 'git-blame info)
237 (overlay-put ovl 'help-echo hash)
238 (overlay-put ovl 'face (list :background
239 (cdr (assq 'color (nthcdr 5 info)))))
240 ;; the point-entered property doesn't seem to work in overlays
241 ;;(overlay-put ovl 'point-entered
242 ;; `(lambda (x y) (git-blame-identify ,hash)))
243 (let ((modified (buffer-modified-p)))
244 (put-text-property (if (= start 1) start (1- start)) (1- end)
245 'point-entered
246 `(lambda (x y) (git-blame-identify ,hash)))
247 (set-buffer-modified-p modified))))
248 (setq num-lines (1- num-lines))))))
250 (defun git-blame-add-info (key value)
251 (if git-blame-current
252 (nconc git-blame-current (list (cons (intern key) value)))))
254 (defun git-blame-current-commit ()
255 (let ((info (get-char-property (point) 'git-blame)))
256 (if info
257 (car info)
258 (error "No commit info"))))
260 (defun git-describe-commit (hash)
261 (with-temp-buffer
262 (call-process "git" nil t nil
263 "log" "-1" "--pretty=oneline"
264 hash)
265 (buffer-substring (point-min) (1- (point-max)))))
267 (defvar git-blame-last-identification nil)
268 (make-variable-buffer-local 'git-blame-last-identification)
269 (defun git-blame-identify (&optional hash)
270 (interactive)
271 (let ((info (gethash (or hash (git-blame-current-commit)) git-blame-cache)))
272 (when (and info (not (eq info git-blame-last-identification)))
273 (message "%s" (nth 4 info))
274 (setq git-blame-last-identification info))))
276 (provide 'git-blame)
278 ;;; git-blame.el ends here