git-blame: an Emacs minor mode to view file with git-blame output.
Here's another version of git-blame.el that automatically tries to create a sensible list of colors to use for both light and dark backgrounds. Plus a few minor fixes. To use: 1) Load into emacs: M-x load-file RET git-blame.el RET 2) Open a git-controlled file 3) Blame: M-x git-blame-mode Signed-off-by: Junio C Hamano <junkio@cox.net>
David Kågedal committed
Feb 5, 2007 at 14:22 UTC
28389d45fb2161d4dcdc1bbfabbcc2fb135914c6
1 file changed
+180
contrib/emacs/git-blame.el
new
+180
@@ -0,0 +1,180 @@
1
+;;; git-blame.el
2
+;; David Kågedal <davidk@lysator.liu.se>
3
+;; Message-ID: <87iren2vqx.fsf@morpheus.local>
4
+
5
+(require 'cl)
6
+(defun color-scale (l)
7
+ (let* ((colors ())
8
+ r g b)
9
+ (setq r l)
10
+ (while r
11
+ (setq g l)
12
+ (while g
13
+ (setq b l)
14
+ (while b
15
+ (push (concat "#" (car r) (car g) (car b)) colors)
16
+ (pop b))
17
+ (pop g))
18
+ (pop r))
19
+ colors))
20
+
21
+(defvar git-blame-dark-colors
22
+ (color-scale '("00" "04" "08" "0c"
23
+ "10" "14" "18" "1c"
24
+ "20" "24" "28" "2c"
25
+ "30" "34" "38" "3c")))
26
+
27
+(defvar git-blame-light-colors
28
+ (color-scale '("c0" "c4" "c8" "cc"
29
+ "d0" "d4" "d8" "dc"
30
+ "e0" "e4" "e8" "ec"
31
+ "f0" "f4" "f8" "fc")))
32
+
33
+(defvar git-blame-ancient-color "dark green")
34
+
35
+(defvar git-blame-overlays nil)
36
+(defvar git-blame-cache nil)
37
+
38
+(defvar git-blame-mode nil)
39
+(make-variable-buffer-local 'git-blame-mode)
40
+(push (list 'git-blame-mode " blame") minor-mode-alist)
41
+
42
+(defun git-blame-mode (&optional arg)
43
+ (interactive "P")
44
+ (if arg
45
+ (setq git-blame-mode (eq arg 1))
46
+ (setq git-blame-mode (not git-blame-mode)))
47
+ (make-local-variable 'git-blame-overlays)
48
+ (make-local-variable 'git-blame-colors)
49
+ (make-local-variable 'git-blame-cache)
50
+ (let ((bgmode (cdr (assoc 'background-mode (frame-parameters)))))
51
+ (if (eq bgmode 'dark)
52
+ (setq git-blame-colors git-blame-dark-colors)
53
+ (setq git-blame-colors git-blame-light-colors)))
54
+ (if git-blame-mode
55
+ (git-blame-run)
56
+ (git-blame-cleanup)))
57
+
58
+(defun git-blame-run ()
59
+ (let* ((display-buf (current-buffer))
60
+ (blame-buf (get-buffer-create
61
+ (concat " git blame for " (buffer-name))))
62
+ (proc (start-process "git-blame" blame-buf
63
+ "git" "blame" "--incremental"
64
+ (file-name-nondirectory buffer-file-name))))
65
+ (mapcar 'delete-overlay git-blame-overlays)
66
+ (setq git-blame-overlays nil)
67
+ (setq git-blame-cache (make-hash-table :test 'equal))
68
+ (with-current-buffer blame-buf
69
+ (erase-buffer)
70
+ (make-local-variable 'git-blame-file)
71
+ (make-local-variable 'git-blame-current)
72
+ (setq git-blame-file display-buf)
73
+ (setq git-blame-current nil))
74
+ (set-process-filter proc 'git-blame-filter)
75
+ (set-process-sentinel proc 'git-blame-sentinel)))
76
+
77
+(defun git-blame-cleanup ()
78
+ "Remove all blame properties"
79
+ (mapcar 'delete-overlay git-blame-overlays)
80
+ (setq git-blame-overlays nil)
81
+ (let ((modified (buffer-modified-p)))
82
+ (remove-text-properties (point-min) (point-max) '(point-entered nil))
83
+ (set-buffer-modified-p modified)))
84
+
85
+(defun git-blame-sentinel (proc status)
86
+ ;;(kill-buffer (process-buffer proc))
87
+ (message "git blame finished"))
88
+
89
+(defvar in-blame-filter nil)
90
+
91
+(defun git-blame-filter (proc str)
92
+ (save-excursion
93
+ (set-buffer (process-buffer proc))
94
+ (goto-char (process-mark proc))
95
+ (insert-before-markers str)
96
+ (goto-char 0)
97
+ (unless in-blame-filter
98
+ (let ((more t)
99
+ (in-blame-filter t))
100
+ (while more
101
+ (setq more (git-blame-parse)))))))
102
+
103
+(defun git-blame-parse ()
104
+ (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
105
+ (let ((hash (match-string 1))
106
+ (src-line (string-to-number (match-string 2)))
107
+ (res-line (string-to-number (match-string 3)))
108
+ (num-lines (string-to-number (match-string 4))))
109
+ (setq git-blame-current
110
+ (git-blame-new-commit
111
+ hash src-line res-line num-lines)))
112
+ (delete-region (point) (match-end 0))
113
+ t)
114
+ ((looking-at "filename \\(.+\\)\n")
115
+ (let ((filename (match-string 1)))
116
+ (git-blame-add-info "filename" filename))
117
+ (delete-region (point) (match-end 0))
118
+ t)
119
+ ((looking-at "\\([a-z-]+\\) \\(.+\\)\n")
120
+ (let ((key (match-string 1))
121
+ (value (match-string 2)))
122
+ (git-blame-add-info key value))
123
+ (delete-region (point) (match-end 0))
124
+ t)
125
+ ((looking-at "boundary\n")
126
+ (setq git-blame-current nil)
127
+ (delete-region (point) (match-end 0))
128
+ t)
129
+ (t
130
+ nil)))
131
+
132
+
133
+(defun git-blame-new-commit (hash src-line res-line num-lines)
134
+ (save-excursion
135
+ (set-buffer git-blame-file)
136
+ (let ((info (gethash hash git-blame-cache))
137
+ (inhibit-point-motion-hooks t))
138
+ (when (not info)
139
+ (let ((color (pop git-blame-colors)))
140
+ (unless color
141
+ (setq color git-blame-ancient-color))
142
+ (setq info (list hash src-line res-line num-lines
143
+ (cons 'color color))))
144
+ (puthash hash info git-blame-cache))
145
+ (goto-line res-line)
146
+ (while (> num-lines 0)
147
+ (if (get-text-property (point) 'git-blame)
148
+ (forward-line)
149
+ (let* ((start (point))
150
+ (end (progn (forward-line 1) (point)))
151
+ (ovl (make-overlay start end)))
152
+ (push ovl git-blame-overlays)
153
+ (overlay-put ovl 'git-blame info)
154
+ (overlay-put ovl 'help-echo hash)
155
+ (overlay-put ovl 'face (list :background
156
+ (cdr (assq 'color (cddddr info)))))
157
+ ;;(overlay-put ovl 'point-entered
158
+ ;; `(lambda (x y) (git-blame-identify ,hash)))
159
+ (let ((modified (buffer-modified-p)))
160
+ (put-text-property (if (= start 1) start (1- start)) (1- end)
161
+ 'point-entered
162
+ `(lambda (x y) (git-blame-identify ,hash)))
163
+ (set-buffer-modified-p modified))))
164
+ (setq num-lines (1- num-lines))))))
165
+
166
+(defun git-blame-add-info (key value)
167
+ (if git-blame-current
168
+ (nconc git-blame-current (list (cons (intern key) value)))))
169
+
170
+(defun git-blame-current-commit ()
171
+ (let ((info (get-char-property (point) 'git-blame)))
172
+ (if info
173
+ (car info)
174
+ (error "No commit info"))))
175
+
176
+(defun git-blame-identify (&optional hash)
177
+ (interactive)
178
+ (shell-command
179
+ (format "git log -1 --pretty=oneline %s" (or hash
180
+ (git-blame-current-commit)))))