|
0 |
;;; git-messenger.el --- Pop up last commit information of current line -*- lexical-binding: t -*-
|
|
1 |
|
|
2 |
;; Copyright (C) 2017 by Syohei YOSHIDA
|
|
3 |
|
|
4 |
;; Author: Syohei YOSHIDA <syohex@gmail.com>
|
|
5 |
;; URL: https://github.com/syohex/emacs-git-messenger
|
|
6 |
;; Version: 0.18
|
|
7 |
;; Package-Requires: ((emacs "24.3") (popup "0.5.0"))
|
|
8 |
|
|
9 |
;; This program is free software; you can redistribute it and/or modify
|
|
10 |
;; it under the terms of the GNU General Public License as published by
|
|
11 |
;; the Free Software Foundation, either version 3 of the License, or
|
|
12 |
;; (at your option) any later version.
|
|
13 |
|
|
14 |
;; This program is distributed in the hope that it will be useful,
|
|
15 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 |
;; GNU General Public License for more details.
|
|
18 |
|
|
19 |
;; You should have received a copy of the GNU General Public License
|
|
20 |
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
21 |
|
|
22 |
;;; Commentary:
|
|
23 |
|
|
24 |
;; This package provides a function called git-messenger:popup-message
|
|
25 |
;; that when called will pop-up the last git commit message for the
|
|
26 |
;; current line. This uses the git-blame tool internally.
|
|
27 |
;;
|
|
28 |
;; Example usage:
|
|
29 |
;; (require 'git-messenger)
|
|
30 |
;; (global-set-key (kbd "C-x v p") 'git-messenger:popup-message)
|
|
31 |
;;
|
|
32 |
|
|
33 |
;;; Code:
|
|
34 |
|
|
35 |
(require 'cl-lib)
|
|
36 |
(require 'popup)
|
|
37 |
|
|
38 |
(declare-function magit-show-commit "magit-diff")
|
|
39 |
|
|
40 |
(defgroup git-messenger nil
|
|
41 |
"git messenger"
|
|
42 |
:group 'vc)
|
|
43 |
|
|
44 |
(defcustom git-messenger:show-detail nil
|
|
45 |
"Pop up commit ID and author name too"
|
|
46 |
:type 'boolean)
|
|
47 |
|
|
48 |
(defcustom git-messenger:before-popup-hook nil
|
|
49 |
"Hook run before popup commit message. This hook is taken popup-ed message"
|
|
50 |
:type 'hook)
|
|
51 |
|
|
52 |
(defcustom git-messenger:after-popup-hook nil
|
|
53 |
"Hook run after popup commit message. This hook is taken popup-ed message"
|
|
54 |
:type 'hook)
|
|
55 |
|
|
56 |
(defcustom git-messenger:popup-buffer-hook nil
|
|
57 |
"Hook run after popup buffer(popup diff, popup show etc)"
|
|
58 |
:type 'hook)
|
|
59 |
|
|
60 |
(defcustom git-messenger:handled-backends '(git svn hg)
|
|
61 |
"List of version control backends for which `git-messenger' will be used.
|
|
62 |
Entries in this list will be tried in order to determine whether a
|
|
63 |
file is under that sort of version control."
|
|
64 |
:type '(repeat symbol))
|
|
65 |
|
|
66 |
(defcustom git-messenger:use-magit-popup nil
|
|
67 |
"Use magit-show-commit instead pop-to-buffer"
|
|
68 |
:type 'boolean)
|
|
69 |
|
|
70 |
(defvar git-messenger:last-message nil
|
|
71 |
"Last message displayed by git-messenger.
|
|
72 |
|
|
73 |
This is set before the pop-up is displayed so accessible in the hooks
|
|
74 |
and menus.")
|
|
75 |
|
|
76 |
(defvar git-messenger:last-commit-id nil
|
|
77 |
"Last commit id for the last message displayed.
|
|
78 |
|
|
79 |
This is set before the pop-up is displayed so accessible in the hooks
|
|
80 |
and menus.")
|
|
81 |
|
|
82 |
(defvar git-messenger:vcs nil)
|
|
83 |
|
|
84 |
(defconst git-messenger:directory-of-vcs
|
|
85 |
'((git . ".git")
|
|
86 |
(svn . ".svn")
|
|
87 |
(hg . ".hg")))
|
|
88 |
|
|
89 |
(defun git-messenger:blame-arguments (vcs file line)
|
|
90 |
(let ((basename (file-name-nondirectory file)))
|
|
91 |
(cl-case vcs
|
|
92 |
(git (list "--no-pager" "blame" "-w" "-L"
|
|
93 |
(format "%d,+1" line)
|
|
94 |
"--porcelain" basename))
|
|
95 |
(svn (list "blame" basename))
|
|
96 |
(hg (list "blame" "-wuc" basename)))))
|
|
97 |
|
|
98 |
(defsubst git-messenger:cat-file-arguments (commit-id)
|
|
99 |
(list "--no-pager" "cat-file" "commit" commit-id))
|
|
100 |
|
|
101 |
(defsubst git-messenger:vcs-command (vcs)
|
|
102 |
(cl-case vcs
|
|
103 |
(git "git")
|
|
104 |
(svn "svn")
|
|
105 |
(hg "hg")))
|
|
106 |
|
|
107 |
(defun git-messenger:execute-command (vcs args output)
|
|
108 |
(cl-case vcs
|
|
109 |
(git (apply 'process-file "git" nil output nil args))
|
|
110 |
(svn
|
|
111 |
(let ((process-environment (cons "LANG=C" process-environment)))
|
|
112 |
(apply 'process-file "svn" nil output nil args)))
|
|
113 |
(hg
|
|
114 |
(let ((process-environment (cons
|
|
115 |
"HGPLAIN=1"
|
|
116 |
(cons "LANG=utf-8" process-environment))))
|
|
117 |
(apply 'process-file "hg" nil output nil args)))))
|
|
118 |
|
|
119 |
(defun git-messenger:git-commit-info-at-line ()
|
|
120 |
(let* ((id-line (buffer-substring-no-properties
|
|
121 |
(line-beginning-position) (line-end-position)))
|
|
122 |
(commit-id (car (split-string id-line)))
|
|
123 |
(author (if (re-search-forward "^author \\(.+\\)$" nil t)
|
|
124 |
(match-string-no-properties 1)
|
|
125 |
"unknown")))
|
|
126 |
(cons commit-id author)))
|
|
127 |
|
|
128 |
(defun git-messenger:hg-commit-info-at-line (line)
|
|
129 |
(forward-line (1- line))
|
|
130 |
(if (looking-at "^\\s-*\\(\\S-+\\)\\s-+\\([a-z0-9]+\\)")
|
|
131 |
(cons (match-string-no-properties 2) (match-string-no-properties 1))
|
|
132 |
(cons "-" "-")))
|
|
133 |
|
|
134 |
(defun git-messenger:svn-commit-info-at-line (line)
|
|
135 |
(forward-line (1- line))
|
|
136 |
(if (looking-at "^\\s-*\\([0-9]+\\)\\s-+\\(\\S-+\\)")
|
|
137 |
(cons (match-string-no-properties 1) (match-string-no-properties 2))
|
|
138 |
(cons "-" "-")))
|
|
139 |
|
|
140 |
(defun git-messenger:commit-info-at-line (vcs file line)
|
|
141 |
(with-temp-buffer
|
|
142 |
(let ((args (git-messenger:blame-arguments vcs file line)))
|
|
143 |
(unless (zerop (git-messenger:execute-command vcs args t))
|
|
144 |
(error "Failed: '%s blame'" (git-messenger:vcs-command vcs)))
|
|
145 |
(goto-char (point-min))
|
|
146 |
(cl-case vcs
|
|
147 |
(git (git-messenger:git-commit-info-at-line))
|
|
148 |
(svn (git-messenger:svn-commit-info-at-line line))
|
|
149 |
(hg (git-messenger:hg-commit-info-at-line line))))))
|
|
150 |
|
|
151 |
(defsubst git-messenger:not-committed-id-p (commit-id)
|
|
152 |
(or (string-match-p "\\`\\(?:0+\\|-\\)\\'" commit-id)))
|
|
153 |
|
|
154 |
(defun git-messenger:git-commit-message (commit-id)
|
|
155 |
(let ((args (git-messenger:cat-file-arguments commit-id)))
|
|
156 |
(unless (zerop (git-messenger:execute-command 'git args t))
|
|
157 |
(error "Failed: 'git cat-file'"))
|
|
158 |
(goto-char (point-min))
|
|
159 |
(forward-paragraph)
|
|
160 |
(buffer-substring-no-properties (point) (point-max))))
|
|
161 |
|
|
162 |
(defun git-messenger:hg-commit-message (commit-id)
|
|
163 |
(let ((args (list "log" "-T" "{desc}" "-r" commit-id)))
|
|
164 |
(unless (zerop (git-messenger:execute-command 'hg args t))
|
|
165 |
(error "Failed: 'hg log"))
|
|
166 |
(buffer-substring-no-properties (point-min) (point-max))))
|
|
167 |
|
|
168 |
(defun git-messenger:svn-commit-message (commit-id)
|
|
169 |
(let ((args (list "log" "-c" commit-id)))
|
|
170 |
(unless (zerop (git-messenger:execute-command 'svn args t))
|
|
171 |
(error "Failed: 'svn log"))
|
|
172 |
(let (end)
|
|
173 |
(goto-char (point-max))
|
|
174 |
(when (re-search-backward "^-\\{25\\}" nil t)
|
|
175 |
(setq end (point)))
|
|
176 |
(buffer-substring-no-properties (point-min) (or end (point-max))))))
|
|
177 |
|
|
178 |
(defun git-messenger:commit-message (vcs commit-id)
|
|
179 |
(with-temp-buffer
|
|
180 |
(if (git-messenger:not-committed-id-p commit-id)
|
|
181 |
"* not yet committed *"
|
|
182 |
(cl-case vcs
|
|
183 |
(git (git-messenger:git-commit-message commit-id))
|
|
184 |
(svn (git-messenger:svn-commit-message commit-id))
|
|
185 |
(hg (git-messenger:hg-commit-message commit-id))))))
|
|
186 |
|
|
187 |
(defun git-messenger:commit-date (commit-id)
|
|
188 |
(let ((args (list "--no-pager" "show" "--pretty=%cd" commit-id)))
|
|
189 |
(with-temp-buffer
|
|
190 |
(unless (zerop (git-messenger:execute-command 'git args t))
|
|
191 |
(error "Failed 'git show'"))
|
|
192 |
(goto-char (point-min))
|
|
193 |
(buffer-substring-no-properties
|
|
194 |
(line-beginning-position) (line-end-position)))))
|
|
195 |
|
|
196 |
(defun git-messenger:hg-commit-date (commit-id)
|
|
197 |
(let ((args (list "log" "-T" "{date|rfc822date}" "-r" commit-id)))
|
|
198 |
(with-temp-buffer
|
|
199 |
(unless (zerop (git-messenger:execute-command 'hg args t))
|
|
200 |
(error "Failed 'hg log'"))
|
|
201 |
(goto-char (point-min))
|
|
202 |
(buffer-substring-no-properties
|
|
203 |
(line-beginning-position) (line-end-position)))))
|
|
204 |
|
|
205 |
(defun git-messenger:format-detail (vcs commit-id author message)
|
|
206 |
(cl-case vcs
|
|
207 |
(git (let ((date (git-messenger:commit-date commit-id)))
|
|
208 |
(format "commit : %s \nAuthor : %s\nDate : %s \n%s"
|
|
209 |
(substring commit-id 0 8) author date message)))
|
|
210 |
(hg (let ((date (git-messenger:hg-commit-date commit-id)))
|
|
211 |
(format "commit : %s \nAuthor : %s\nDate : %s \n%s"
|
|
212 |
commit-id author date message)))
|
|
213 |
(svn (with-temp-buffer
|
|
214 |
(insert message)
|
|
215 |
(goto-char (point-min))
|
|
216 |
(forward-line 1)
|
|
217 |
(let ((line (buffer-substring-no-properties (point) (line-end-position)))
|
|
218 |
(re "^\\s-*\\(?:r[0-9]+\\)\\s-+|\\s-+\\([^|]+\\)|\\s-+\\([^|]+\\)"))
|
|
219 |
(unless (string-match re line)
|
|
220 |
(error "Can't get revision %s" line))
|
|
221 |
(let ((author (match-string-no-properties 1 line))
|
|
222 |
(date (match-string-no-properties 2 line)))
|
|
223 |
(forward-paragraph)
|
|
224 |
(format "commit : r%s \nAuthor : %s\nDate : %s\n%s"
|
|
225 |
commit-id author date
|
|
226 |
(buffer-substring-no-properties (point) (point-max)))))))))
|
|
227 |
|
|
228 |
(defun git-messenger:show-detail-p (commit-id)
|
|
229 |
(and (or git-messenger:show-detail current-prefix-arg)
|
|
230 |
(not (git-messenger:not-committed-id-p commit-id))))
|
|
231 |
|
|
232 |
(defun git-messenger:popup-close ()
|
|
233 |
(interactive)
|
|
234 |
(throw 'git-messenger-loop t))
|
|
235 |
|
|
236 |
(defun git-messenger:copy-message ()
|
|
237 |
"Copy current displayed commit message to kill-ring."
|
|
238 |
(interactive)
|
|
239 |
(when git-messenger:last-message
|
|
240 |
(kill-new git-messenger:last-message))
|
|
241 |
(git-messenger:popup-close))
|
|
242 |
|
|
243 |
(defun git-messenger:copy-commit-id ()
|
|
244 |
"Copy current displayed commit id to kill-ring."
|
|
245 |
(interactive)
|
|
246 |
(when git-messenger:last-commit-id
|
|
247 |
(kill-new git-messenger:last-commit-id))
|
|
248 |
(git-messenger:popup-close))
|
|
249 |
|
|
250 |
(defun git-messenger:popup-common (vcs args &optional mode)
|
|
251 |
(with-current-buffer (get-buffer-create "*git-messenger*")
|
|
252 |
(view-mode -1)
|
|
253 |
(fundamental-mode)
|
|
254 |
(erase-buffer)
|
|
255 |
(unless (zerop (git-messenger:execute-command vcs args t))
|
|
256 |
(error "Failed: '%s(args=%s)'" (git-messenger:vcs-command vcs) args))
|
|
257 |
(if git-messenger:use-magit-popup
|
|
258 |
(magit-show-commit git-messenger:last-commit-id)
|
|
259 |
(pop-to-buffer (current-buffer))
|
|
260 |
(when mode
|
|
261 |
(funcall mode)))
|
|
262 |
(run-hooks 'git-messenger:popup-buffer-hook)
|
|
263 |
(view-mode +1)
|
|
264 |
(goto-char (point-min)))
|
|
265 |
(git-messenger:popup-close))
|
|
266 |
|
|
267 |
(defun git-messenger:popup-svn-show ()
|
|
268 |
(git-messenger:popup-common
|
|
269 |
'svn (list "diff" "-c" git-messenger:last-commit-id) 'diff-mode))
|
|
270 |
|
|
271 |
(defun git-messenger:popup-hg-show ()
|
|
272 |
(git-messenger:popup-common
|
|
273 |
'hg (list "diff" "-c" git-messenger:last-commit-id) 'diff-mode))
|
|
274 |
|
|
275 |
(defun git-messenger:popup-diff ()
|
|
276 |
(interactive)
|
|
277 |
(cl-case git-messenger:vcs
|
|
278 |
(git (let ((args (list "--no-pager" "diff" "--no-ext-diff"
|
|
279 |
(concat git-messenger:last-commit-id "^!"))))
|
|
280 |
(git-messenger:popup-common 'git args 'diff-mode)))
|
|
281 |
(svn (git-messenger:popup-svn-show))
|
|
282 |
(hg (git-messenger:popup-hg-show))))
|
|
283 |
|
|
284 |
(defun git-messenger:popup-show ()
|
|
285 |
(interactive)
|
|
286 |
(cl-case git-messenger:vcs
|
|
287 |
(git (let ((args (list "--no-pager" "show" "--no-ext-diff" "--stat"
|
|
288 |
git-messenger:last-commit-id)))
|
|
289 |
(git-messenger:popup-common 'git args)))
|
|
290 |
(svn (git-messenger:popup-svn-show))
|
|
291 |
(hg (let ((args (list "log" "--stat" "-r"
|
|
292 |
git-messenger:last-commit-id)))
|
|
293 |
(git-messenger:popup-common 'hg args)))))
|
|
294 |
|
|
295 |
(defun git-messenger:popup-show-verbose ()
|
|
296 |
(interactive)
|
|
297 |
(cl-case git-messenger:vcs
|
|
298 |
(git (let ((args (list "--no-pager" "show" "--no-ext-diff" "--stat" "-p"
|
|
299 |
git-messenger:last-commit-id)))
|
|
300 |
(git-messenger:popup-common 'git args)))
|
|
301 |
(svn (error "'svn' does not support `popup-show-verbose'"))
|
|
302 |
(hg (let ((args (list "log" "-p" "--stat" "-r"
|
|
303 |
git-messenger:last-commit-id)))
|
|
304 |
(git-messenger:popup-common 'hg args)))))
|
|
305 |
|
|
306 |
(defvar git-messenger-map
|
|
307 |
(let ((map (make-sparse-keymap)))
|
|
308 |
;; key bindings
|
|
309 |
(define-key map (kbd "q") 'git-messenger:popup-close)
|
|
310 |
(define-key map (kbd "c") 'git-messenger:copy-commit-id)
|
|
311 |
(define-key map (kbd "d") 'git-messenger:popup-diff)
|
|
312 |
(define-key map (kbd "s") 'git-messenger:popup-show)
|
|
313 |
(define-key map (kbd "S") 'git-messenger:popup-show-verbose)
|
|
314 |
(define-key map (kbd "M-w") 'git-messenger:copy-message)
|
|
315 |
(define-key map (kbd ",") 'git-messenger:show-parent)
|
|
316 |
map)
|
|
317 |
"Key mappings of git-messenger. This is enabled when commit message is popup-ed.")
|
|
318 |
|
|
319 |
(defun git-messenger:find-vcs ()
|
|
320 |
(let ((longest 0)
|
|
321 |
result)
|
|
322 |
(dolist (vcs git-messenger:handled-backends result)
|
|
323 |
(let* ((dir (assoc-default vcs git-messenger:directory-of-vcs))
|
|
324 |
(vcs-root (locate-dominating-file default-directory dir)))
|
|
325 |
(when (and vcs-root (> (length vcs-root) longest))
|
|
326 |
(setq longest (length vcs-root)
|
|
327 |
result vcs))))))
|
|
328 |
|
|
329 |
(defun git-messenger:svn-message (msg)
|
|
330 |
(with-temp-buffer
|
|
331 |
(insert msg)
|
|
332 |
(goto-char (point-min))
|
|
333 |
(forward-paragraph)
|
|
334 |
(buffer-substring-no-properties (point) (point-max))))
|
|
335 |
|
|
336 |
(defvar git-messenger:func-prompt
|
|
337 |
'((git-messenger:popup-show . "Show")
|
|
338 |
(git-messenger:popup-show-verbose . "Show verbose")
|
|
339 |
(git-messenger:popup-close . "Close")
|
|
340 |
(git-messenger:copy-commit-id . "Copy hash")
|
|
341 |
(git-messenger:popup-diff . "Diff")
|
|
342 |
(git-messenger:copy-message . "Copy message")
|
|
343 |
(git-messenger:show-parent . "Go Parent")
|
|
344 |
(git-messenger:popup-close . "Quit")))
|
|
345 |
|
|
346 |
(defsubst git-messenger:function-to-key (func)
|
|
347 |
(key-description (car-safe (where-is-internal func git-messenger-map))))
|
|
348 |
|
|
349 |
(defun git-messenger:prompt ()
|
|
350 |
(mapconcat (lambda (fp)
|
|
351 |
(let ((key (git-messenger:function-to-key (car fp))))
|
|
352 |
(format "[%s]%s" key (cdr fp))))
|
|
353 |
git-messenger:func-prompt " "))
|
|
354 |
|
|
355 |
(defun git-messenger:show-parent ()
|
|
356 |
(interactive)
|
|
357 |
(let ((file (buffer-file-name (buffer-base-buffer))))
|
|
358 |
(cl-case git-messenger:vcs
|
|
359 |
(git (with-temp-buffer
|
|
360 |
(unless (zerop (process-file "git" nil t nil
|
|
361 |
"blame" "--increment" git-messenger:last-commit-id "--" file))
|
|
362 |
(error "No parent commit ID"))
|
|
363 |
(goto-char (point-min))
|
|
364 |
(when (re-search-forward (concat "^" git-messenger:last-commit-id) nil t)
|
|
365 |
(when (re-search-forward "previous \\(\\S-+\\)" nil t)
|
|
366 |
(let ((parent (match-string-no-properties 1)))
|
|
367 |
(setq git-messenger:last-commit-id parent
|
|
368 |
git-messenger:last-message (git-messenger:commit-message 'git parent)))))
|
|
369 |
(throw 'git-messenger-loop nil)))
|
|
370 |
(otherwise (error "%s does not support for getting parent commit ID" git-messenger:vcs)))))
|
|
371 |
|
|
372 |
;;;###autoload
|
|
373 |
(defun git-messenger:popup-message ()
|
|
374 |
(interactive)
|
|
375 |
(let* ((vcs (git-messenger:find-vcs))
|
|
376 |
(file (buffer-file-name (buffer-base-buffer)))
|
|
377 |
(line (line-number-at-pos))
|
|
378 |
(commit-info (git-messenger:commit-info-at-line vcs file line))
|
|
379 |
(commit-id (car commit-info))
|
|
380 |
(author (cdr commit-info))
|
|
381 |
(msg (git-messenger:commit-message vcs commit-id))
|
|
382 |
(popuped-message (if (git-messenger:show-detail-p commit-id)
|
|
383 |
(git-messenger:format-detail vcs commit-id author msg)
|
|
384 |
(cl-case vcs
|
|
385 |
(git msg)
|
|
386 |
(svn (if (string= commit-id "-")
|
|
387 |
msg
|
|
388 |
(git-messenger:svn-message msg)))
|
|
389 |
(hg msg)))))
|
|
390 |
(setq git-messenger:vcs vcs
|
|
391 |
git-messenger:last-message popuped-message
|
|
392 |
git-messenger:last-commit-id commit-id)
|
|
393 |
(let (finish)
|
|
394 |
(run-hook-with-args 'git-messenger:before-popup-hook popuped-message)
|
|
395 |
(while (not finish)
|
|
396 |
(let ((menu (popup-tip git-messenger:last-message :nowait t)))
|
|
397 |
(unwind-protect
|
|
398 |
(setq finish (catch 'git-messenger-loop
|
|
399 |
(popup-menu-event-loop menu git-messenger-map 'popup-menu-fallback
|
|
400 |
:prompt (git-messenger:prompt))
|
|
401 |
t))
|
|
402 |
(popup-delete menu)))))
|
|
403 |
(run-hook-with-args 'git-messenger:after-popup-hook popuped-message)))
|
|
404 |
|
|
405 |
(provide 'git-messenger)
|
|
406 |
|
|
407 |
;; Local Variables:
|
|
408 |
;; coding: utf-8
|
|
409 |
;; indent-tabs-mode: nil
|
|
410 |
;; End:
|
|
411 |
|
|
412 |
;;; git-messenger.el ends here
|