forked from purcell/git-timemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-timemachine.el
executable file
·240 lines (206 loc) · 8.93 KB
/
git-timemachine.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
;;; git-timemachine.el --- Walk through git revisions of a file
;; Copyright (C) 2014 Peter Stiernström
;; Author: Peter Stiernström <[email protected]>
;; Version: 2.7
;; URL: https://github.com/pidu/git-timemachine
;; Keywords: git
;; This file is not part of GNU Emacs
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Use git-timemachine to browse historic versions of a file with p
;;; (previous) and n (next).
;;; Code:
(require 'vc-git)
(require 'cl-lib)
(defcustom git-timemachine-abbreviation-length 12
"Number of chars from the full sha1 hash to use for abbreviation."
:group 'git-timemachine)
(defcustom git-timemachine-show-minibuffer-details t
"Non-nil means that details of the commit (its hash and date)
will be shown in the minibuffer while navigating commits."
:group 'git-timemachine)
(defface git-timemachine-commit
'((default :weight bold))
"Face for git timemachine commit sha"
:group 'git-timemachine)
(defface git-timemachine-minibuffer-detail-face
'((t (:foreground "yellow")))
"How to display the minibuffer detail"
:group 'git-timemachine)
(defcustom git-timemachine-minibuffer-detail
'subject
"What to display when `git-timemachine-show-minibuffer-details` is t.
Available values are:
`commit` : The SHA hash of the commit
`subject`: The subject of the commit message"
:type '(radio (const :tag "Commit SHA" commit) (const :tag "Commit Subject" subject))
:group 'git-timemachine)
(defvar-local git-timemachine-directory nil)
(defvar-local git-timemachine-revision nil)
(defvar-local git-timemachine-file nil)
(defvar-local git-timemachine--revisions-cache nil)
(defun git-timemachine--revisions ()
"List git revisions of current buffers file."
(if git-timemachine--revisions-cache
git-timemachine--revisions-cache
(setq git-timemachine--revisions-cache
(prog2
(message "Fetching Revisions...")
(let ((default-directory git-timemachine-directory)
(file git-timemachine-file))
(with-temp-buffer
(unless (zerop (process-file vc-git-program nil t nil "--no-pager" "log" "--name-only" "--follow" "--pretty=format:%H:%ar:%ad:%s" file))
(error "Git log command exited with non-zero exit status for file: %s" file))
(goto-char (point-min))
(let ((lines)
(commit-number (/ (1+ (count-lines (point-min) (point-max))) 3)))
(while (not (eobp))
(let ((line (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(string-match "\\([^:]*\\):\\([^:]*\\):\\(.*\\):\\(.*\\)" line)
(let ((commit (match-string 1 line))
(date-relative (match-string 2 line))
(date-full (match-string 3 line))
(subject (match-string 4 line)))
(forward-line 1)
(let ((file-name (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(push (list commit file-name commit-number date-relative date-full subject) lines))))
(setq commit-number (1- commit-number))
(forward-line 2))
(nreverse lines))))
(message "Fetching Revisions...done")))))
(defun git-timemachine-show-current-revision ()
"Show last (current) revision of file."
(interactive)
(git-timemachine-show-revision (car (git-timemachine--revisions))))
(defun git-timemachine--next-revision (revisions)
"Return the revision following the current revision in REVISIONS."
(cadr (cl-member (car git-timemachine-revision) revisions :key #'car :test #'string=)))
(defun git-timemachine-show-previous-revision ()
"Show previous revision of file."
(interactive)
(git-timemachine-show-revision (git-timemachine--next-revision (git-timemachine--revisions))))
(defun git-timemachine-show-next-revision ()
"Show next revision of file."
(interactive)
(git-timemachine-show-revision (git-timemachine--next-revision (reverse (git-timemachine--revisions)))))
(defun git-timemachine-show-nth-revision (rev-number)
"Show the REV-NUMBER revision."
(interactive "nEnter revision number: ")
(let* ((revisions (reverse (git-timemachine--revisions)))
(revision (nth (1- rev-number) revisions))
(num-revisions (length revisions)))
(if revision (git-timemachine-show-revision revision)
(message "Only %d revisions exist." num-revisions))))
(defun git-timemachine-show-revision (revision)
"Show a REVISION (commit hash) of the current file."
(when revision
(let ((current-position (point))
(commit (car revision))
(revision-file-name (nth 1 revision))
(commit-index (nth 2 revision))
(date-relative (nth 3 revision))
(date-full (nth 4 revision))
(subject (nth 5 revision)))
(setq buffer-read-only nil)
(erase-buffer)
(let ((default-directory git-timemachine-directory))
(process-file vc-git-program nil t nil "--no-pager" "show"
(concat commit ":" revision-file-name)))
(setq buffer-read-only t)
(set-buffer-modified-p nil)
(let* ((revisions (git-timemachine--revisions))
(n-of-m (format "(%d/%d %s)" commit-index (length revisions) date-relative)))
(setq mode-line-buffer-identification
(list (propertized-buffer-identification "%12b") "@"
(propertize (git-timemachine-abbreviate commit) 'face 'git-timemachine-commit) " name:" revision-file-name" " n-of-m)))
(setq git-timemachine-revision revision)
(goto-char current-position)
(when git-timemachine-show-minibuffer-details
(git-timemachine--show-minibuffer-details revision)))))
(defun git-timemachine--show-minibuffer-details (revision)
"Show details for REVISION in minibuffer."
(let ((detail
(if (eq git-timemachine-minibuffer-detail 'commit)
(car revision)
(nth 5 revision)))
(date-relative (nth 3 revision))
(date-full (nth 4 revision)))
(message (format "%s [%s (%s)]" (propertize detail 'face 'git-timemachine-minibuffer-detail-face) date-full date-relative))))
(defun git-timemachine-abbreviate (revision)
"Return REVISION abbreviated to `git-timemachine-abbreviation-length' chars."
(substring revision 0 git-timemachine-abbreviation-length))
(defun git-timemachine-quit ()
"Exit the timemachine."
(interactive)
(kill-buffer))
(defun git-timemachine-kill-revision ()
"Kill the current revisions abbreviated commit hash."
(interactive)
(let ((revision (car git-timemachine-revision)))
(message revision)
(kill-new revision)))
(defun git-timemachine-kill-abbreviated-revision ()
"Kill the current revisions full commit hash."
(interactive)
(let ((revision (git-timemachine-abbreviate (car git-timemachine-revision))))
(message revision)
(kill-new revision)))
(define-minor-mode git-timemachine-mode
"Git Timemachine, feel the wings of history."
:init-value nil
:lighter " Timemachine"
:keymap
'(("p" . git-timemachine-show-previous-revision)
("n" . git-timemachine-show-next-revision)
("g" . git-timemachine-show-nth-revision)
("q" . git-timemachine-quit)
("w" . git-timemachine-kill-abbreviated-revision)
("W" . git-timemachine-kill-revision))
:group 'git-timemachine)
(defun git-timemachine-validate (file)
"Validate that there is a FILE and that it belongs to a git repository.
Call with the value of 'buffer-file-name."
(unless file
(error "This buffer is not visiting a file"))
(unless (vc-git-registered file)
(error "This file is not git tracked")))
;;;###autoload
(defun git-timemachine-toggle ()
"Toggle git timemachine mode."
(interactive)
(if (bound-and-true-p git-timemachine-mode)
(git-timemachine-quit)
(git-timemachine)))
;;;###autoload
(defun git-timemachine ()
"Enable git timemachine for file of current buffer."
(interactive)
(setq git-timemachine--revisions-cache nil)
(git-timemachine-validate (buffer-file-name))
(let ((git-directory (expand-file-name (vc-git-root (buffer-file-name))))
(file-name (buffer-file-name))
(timemachine-buffer (format "timemachine:%s" (buffer-name)))
(cur-line (line-number-at-pos))
(mode major-mode))
(with-current-buffer (get-buffer-create timemachine-buffer)
(switch-to-buffer timemachine-buffer)
(setq buffer-file-name file-name)
(funcall mode)
(setq git-timemachine-directory git-directory
git-timemachine-file (file-relative-name file-name git-directory)
git-timemachine-revision nil)
(git-timemachine-show-current-revision)
(goto-char (point-min))
(forward-line (1- cur-line))
(git-timemachine-mode))))
(provide 'git-timemachine)
;;; git-timemachine.el ends here