-
Notifications
You must be signed in to change notification settings - Fork 32
/
git-timemachine.el
124 lines (103 loc) · 4.26 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
;;; git-timemachine.el --- Walk through git revisions of a file -*- lexical-binding: t -*-
;; Copyright (C) 2014 Peter Stiernström
;; Author: Peter Stiernström <peter@stiernstrom.se>
;; Version: 0.1
;; URL: https://github.com/pidu/git-timemachine
;; Package-Requires: ((emacs "24") (cl-lib "0.5"))
;; 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).
(require 'subr-x)
(require 'cl-lib)
;;; Code:
(defun git-timemachine--revisions ()
"List git revisions of current buffers file."
(split-string
(shell-command-to-string
(format "cd %s && git log --pretty=format:%s %s"
(shell-quote-argument timemachine-git-directory)
(shell-quote-argument "%h")
(shell-quote-argument timemachine-file)))
nil t "\s+"))
(defun git-timemachine-show-current-revision ()
"Show last (current) revision of file."
(interactive)
(git-timemachine-show-revision (car (git-timemachine--revisions))))
(defun git-timemachine-show-previous-revision ()
"Show previous revision of file."
(interactive)
(git-timemachine-show-revision (cadr (member timemachine-revision (git-timemachine--revisions)))))
(defun git-timemachine-show-next-revision ()
"Show next revision of file."
(interactive)
(git-timemachine-show-revision (cadr (member timemachine-revision (reverse (git-timemachine--revisions))))))
(defun git-timemachine-show-revision (revision)
"Show a REVISION (commit hash) of the current file."
(when revision
(let ((current-position (point)))
(setq buffer-read-only nil)
(erase-buffer)
(insert
(shell-command-to-string
(format "cd %s && git show %s:%s"
(shell-quote-argument timemachine-git-directory)
(shell-quote-argument revision)
(shell-quote-argument timemachine-file))))
(setq buffer-read-only t)
(set-buffer-modified-p nil)
(let* ((revisions (git-timemachine--revisions))
(n-of-m (format "(%d/%d)" (- (length revisions) (cl-position revision revisions :test 'equal)) (length revisions))))
(setq mode-line-format (list "Commit: " revision " -- %b -- " n-of-m " -- [%p]")))
(setq timemachine-revision revision)
(goto-char current-position))))
(defun git-timemachine-quit ()
"Exit the timemachine."
(interactive)
(kill-buffer))
(defun git-timemachine-kill-revision ()
"Kill the current revisions commit hash."
(interactive)
(let ((this-revision timemachine-revision))
(with-temp-buffer
(insert this-revision)
(kill-region (point-min) (point-max)))))
(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)
("q" . git-timemachine-quit)
("w" . git-timemachine-kill-revision))
:group 'git-timemachine)
;;;###autoload
(defun git-timemachine ()
"Enable git timemachine for file of current buffer."
(interactive)
(let* ((git-directory (concat (string-trim-right (shell-command-to-string "git rev-parse --show-toplevel")) "/"))
(relative-file (string-remove-prefix git-directory (buffer-file-name)))
(timemachine-buffer (format "timemachine:%s" (buffer-name))))
(with-current-buffer (get-buffer-create timemachine-buffer)
(setq buffer-file-name relative-file)
(set-auto-mode)
(git-timemachine-mode)
(setq-local timemachine-git-directory git-directory)
(setq-local timemachine-file relative-file)
(setq-local timemachine-revision nil)
(git-timemachine-show-current-revision)
(switch-to-buffer timemachine-buffer))))
(provide 'git-timemachine)
;;; git-timemachine.el ends here