-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-show.el
196 lines (169 loc) · 6.68 KB
/
git-show.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
;;; git-show.el --- FAST search of committed files in a git repo.
;; This file is not part of Emacs
;; Copyright (C) 2011 Jose Pablo Barrantes
;; Created: 18/Dec/11
;; Version: 0.1.0
;;; Installation:
;; Put this file where you defined your `load-path` directory or just
;; add the following line to your emacs config file:
;; (load-file "/path/to/git-show.el")
;; Finally require it:
;; (require 'git-show)
;; Requirements:
;; http://www.emacswiki.org/emacs/Helm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'helm)
;;; --------------------------------------------------------------------
;;; - Customization
;;;
(defcustom git-show/git-exec
"git --no-pager "
"Git executable."
:group 'git-show
:type 'string)
(defcustom git-show/sha-command
"log --pretty=format:'%h %an %ar %s'"
"Git show SHA command."
:group 'git-show
:type 'string)
(defcustom git-show/tmp-dir
"/tmp/git-show/"
"Git show temp directory."
:group 'git-show
:type 'string)
;;; --------------------------------------------------------------------
;;; - Vars
;;;
(defvar git-show/ls-tree
"cd %s && git ls-tree --name-only -r %s "
"Git show ls-tree command.")
;;; --------------------------------------------------------------------
;;; - Helpers
;;;
(defun git-show/tmp-file (sha candidate)
"Creates the temp file name for the git-show function."
(concat sha ":" (replace-regexp-in-string ".*/" "" candidate)))
(defun git-show/tmp-file-full-path (tmp-file)
"Adds the `git-show/tmp-dir' directory to the tmp-file name. "
(concat git-show/tmp-dir tmp-file))
(defun git-show/show-cmd (sha candidate tmp-file-path)
"Creates the string for the git-show command."
(concat git-show/git-exec
"show " sha ":" candidate " >" tmp-file-path))
(defun git-show/find-git-repo (dir)
"Recursively search for a .git/ directory."
(if (string= "/" dir)
(message "not in a git repo.")
(if (file-exists-p (expand-file-name ".git/" dir))
dir
(git-show/find-git-repo (expand-file-name "../" dir)))))
(defun git-show/make-tmp-dir ()
"Test if the temp directory exists, if not it creates it."
(interactive)
(if (not (file-exists-p git-show/tmp-dir))
(make-directory git-show/tmp-dir)))
(defun git-show/mode-line (commit-msg)
"Display the context on which the file is being searched."
(concat "[Context: " commit-msg "]"))
(defun git-show/sha ()
"Used internally to get the SHA for rendering the files properly via
the git-ls-tree utility."
(helm-other-buffer
'((name . "Get SHA")
;; TODO: Refresh search if executed in a new repo context,
;; results seem to be getting cached or something... I don't know
;; how to refresh it. ATM if this is executed in a different
;; repo, it will render the git-log(1) of the first one!.
(candidates . git-show/sha-init)
(candidate-number-limit . 9999)
(candidates-in-buffer)
(action . (lambda (candidate) candidate)))
"*Git Show*"))
(defun git-show/sha-init ()
"Initialize git-show/sha via `git-show/git-exec' and
`git-show/sha-command' process for identifing the SHA to use for the
file search."
(setq mode-line-format
'(" " mode-line-buffer-identification " "
(line-number-mode "%l") " "
(:eval (propertize "(Git Show Process Running) "
'face '((:foreground "red"))))))
(setq cmd
(concat git-show/git-exec git-show/sha-command))
(prog1
(start-process-shell-command "git-show-process" nil cmd)
(set-process-sentinel (get-process "git-show-process")
#'(lambda (process event)
(when (string= event "finished\n")
(kill-local-variable 'mode-line-format)
(with-helm-window
(helm-update-move-first-line)))))))
;;; --------------------------------------------------------------------
;;; - Interctive Functions
;;;
;;;###autoload
(defun git-show ()
"Use the initially used SHA for listing the files and search for a
specific one to display its content."
(interactive)
(setq sha-msg (git-show/sha))
(setq sha (replace-regexp-in-string " .*" "" sha-msg))
(setq git-show-mode-line (git-show/mode-line sha-msg))
(helm-other-buffer
'((name . "Get file")
(init
. (lambda ()
(setq cmd
(format git-show/ls-tree
(git-show/find-git-repo default-directory)
sha))
(call-process-shell-command
cmd nil (helm-candidate-buffer 'global))))
(type . string)
(mode-line . git-show-mode-line)
(candidate-number-limit . 9999)
(candidates-in-buffer)
(action . (lambda (candidate)
(kill-local-variable 'mode-line-format)
(setq tmp-file (git-show/tmp-file sha candidate))
(setq tmp-file-path (git-show/tmp-file-full-path tmp-file))
(setq show-cmd (git-show/show-cmd
sha
candidate
tmp-file-path))
(git-show/make-tmp-dir)
(call-process-shell-command show-cmd)
(find-file tmp-file-path)
(setq mode-line-format
'(" " mode-line-buffer-identification " "
(:eval (propertize
(replace-regexp-in-string
"\\([0-9a-fA-F]\\{7\\}\\)" "" sha-msg)
'face '((:foreground "yellow"))))))
(when (fboundp 'redraw-modeline) (redraw-modeline)))))
"*Git Show*")); TODO: Clean this up...
;;;###autoload
(defun git-show-rm-tmp ()
"Removes `git-show/tmp-dir' directory"
(interactive)
(if (file-exists-p git-show/tmp-dir)
(delete-directory git-show/tmp-dir t)))
(provide 'git-show)
;; git-show.el ends here