forked from purcell/git-timemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-timemachine.el
executable file
·205 lines (174 loc) · 7.36 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
;;; git-timemachine.el --- Walk through git revisions of a file
;; Copyright (C) 2014 Peter Stiernström
;; Author: Peter Stiernström <[email protected]>
;; Version: 2.2
;; 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)
(defvar git-timemachine-directory nil)
(defvar git-timemachine-revision nil)
(defvar git-timemachine-file nil)
(make-variable-buffer-local 'git-timemachine-directory)
(make-variable-buffer-local 'git-timemachine-revision)
(make-variable-buffer-local 'git-timemachine-file)
(defun git-timemachine--revisions ()
"List git revisions of current buffers file."
(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" "--pretty=format:%H:%ar:%ad" file))
(error "Failed: 'git log --pretty=format:%%H:%%ar:%%ad' %s" file))
(goto-char (point-min))
(let ((lines)
(commit-number (count-lines (point-min) (point-max))))
(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)))
(push (list commit commit-number date-relative date-full) lines)))
(setq commit-number (1- commit-number))
(forward-line 1))
(nreverse lines)))))
(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) ;; takes the hash
revisions
:key #'car ;; only compare hashes
: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 revision requested."
(interactive "nEnter revision number: ")
(let ((revision (nth (1- rev-number) (reverse (git-timemachine--revisions))))
(num-revisions (length (git-timemachine--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))
(commit-index (nth 1 revision))
(date-relative (nth 2 revision))
(date-full (nth 3 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 ":" git-timemachine-file)))
(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 'bold) " " n-of-m)))
(setq git-timemachine-revision revision)
(goto-char current-position)
(when git-timemachine-show-minibuffer-details
(message (format "commit %s %s (%s)" commit 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)
(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