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 '("00" "04" "08" "0c"
101 "30" "34" "38" "3c")))
103 (defvar git-blame-light-colors
104 (color-scale '("c0" "c4" "c8" "cc"
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")
122 (defun git-blame-mode (&optional 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
)))
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
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
)
173 (set-buffer (process-buffer proc
))
174 (goto-char (process-mark proc
))
175 (insert-before-markers str
)
177 (unless in-blame-filter
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 (git-blame-new-commit
191 hash src-line res-line num-lines
)))
192 (delete-region (point) (match-end 0))
194 ((looking-at "filename \\(.+\\)\n")
195 (let ((filename (match-string 1)))
196 (git-blame-add-info "filename" filename
))
197 (delete-region (point) (match-end 0))
199 ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
200 (let ((key (match-string 1))
201 (value (match-string 2)))
202 (git-blame-add-info key value
))
203 (delete-region (point) (match-end 0))
205 ((looking-at "boundary\n")
206 (setq git-blame-current nil
)
207 (delete-region (point) (match-end 0))
213 (defun git-blame-new-commit (hash src-line res-line num-lines
)
215 (set-buffer git-blame-file
)
216 (let ((info (gethash hash git-blame-cache
))
217 (inhibit-point-motion-hooks t
))
219 (let ((color (pop git-blame-colors
)))
221 (setq color git-blame-ancient-color
))
222 (setq info
(list hash src-line res-line num-lines
223 (cons 'color color
))))
224 (puthash hash info git-blame-cache
))
226 (while (> num-lines
0)
227 (if (get-text-property (point) 'git-blame
)
229 (let* ((start (point))
230 (end (progn (forward-line 1) (point)))
231 (ovl (make-overlay start end
)))
232 (push ovl git-blame-overlays
)
233 (overlay-put ovl
'git-blame info
)
234 (overlay-put ovl
'help-echo hash
)
235 (overlay-put ovl
'face
(list :background
236 (cdr (assq 'color
(cddddr info
)))))
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-blame-identify (&optional hash
)
259 (format "git log -1 --pretty=oneline %s" (or hash
260 (git-blame-current-commit)))))
264 ;;; git-blame.el ends here