-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanything-git-files.el
302 lines (257 loc) · 11.7 KB
/
anything-git-files.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
;;; anything-git-files.el --- anything for git files
;; Author: INA Lintaro <tarao.gnn at gmail.com>
;; Version: 0.1
;; Package-Requires: ((anything "1.3.9"))
;; Keywords: anything, git
;; This file is NOT part of GNU Emacs.
;;; License:
;;
;; 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/>.
;;; Code:
(eval-when-compile (require 'cl))
(require 'vc-git)
(require 'anything-config)
(require 'sha1 nil t)
(defvar anything-git-files:cached nil)
(defvar anything-git-files:update-delay 0.1)
(defvar anything-git-files:update-timer nil)
(defvar anything-git-files:last-update 0)
(defgroup anything-git-files nil
"anything for git files."
:prefix "anything-git-files:" :group 'anything)
(defcustom anything-git-files:update-submodules-once nil
"t means to update file list in submodules only once."
:type 'boolean
:group 'anything-git-files)
(defconst anything-git-files:ls-args
'((modified . ("--modified"))
(untracked . ("--others" "--exclude-standard"))
(all . nil)))
(defconst anything-git-files:update-check-functions
'((modified . anything-git-files:status-updated-p)
(untracked . anything-git-files:t)
(all . anything-git-files:head-updated-p)))
(defconst anything-git-files:status-expire 1)
(defsubst anything-git-files:chomp (str)
(replace-regexp-in-string "[\r\n]+$" "" str))
(defun anything-git-files:hash (obj)
obj)
(cond ((fboundp 'secure-hash)
(defun anything-git-files:hash (obj)
(secure-hash 'sha1 obj)))
((fboundp 'sha1) (fset 'anything-git-files:hash 'sha1)))
(defun anything-git-files:command-to-string (&rest args)
(with-output-to-string
(with-current-buffer standard-output
(apply 'vc-git-command (current-buffer) 0 nil args))))
(defun anything-git-files:root-1 ()
(file-name-as-directory
(anything-git-files:chomp
(anything-git-files:command-to-string "rev-parse" "--show-toplevel"))))
(defun anything-git-files:root ()
(or (vc-file-getprop default-directory 'git-root)
(vc-file-setprop default-directory 'git-root
(anything-git-files:root-1))))
(defun anything-git-files:head (&optional root)
(let ((default-directory (or root default-directory)))
(anything-git-files:chomp
(anything-git-files:command-to-string "rev-parse" "HEAD"))))
(defun anything-git-files:ls (buffer &rest args)
(apply 'vc-git-command buffer 0 nil "ls-files" args))
(defun anything-git-files:ls-async (buffer callback &rest args)
(let ((proc (apply 'vc-git-command buffer 'async nil "ls-files" args)))
(set-process-sentinel proc callback)
proc))
(defun anything-git-files:status-1 ()
(anything-git-files:command-to-string "status" "--porcelain" "-uno"))
(defun anything-git-files:status-hash (&optional root)
"Get hash value of \"git status\" for ROOT repository.
The status and its hash value will be reused until
`anything-git-files:status-expire' seconds after the last time
they have been updated."
(let* ((default-directory (or root default-directory))
(prop 'anything-git-files:status-hash)
(info (vc-file-getprop default-directory prop))
(last (plist-get info :last))
(now (float-time)))
(when (or (not (numberp last))
(> now (+ anything-git-files:status-expire last)))
(let ((hash (anything-git-files:hash (anything-git-files:status-1))))
(setq info (plist-put info :hash hash))))
(setq info (plist-put info :last now))
(vc-file-setprop default-directory prop info)
(plist-get info :hash)))
(defun anything-git-files:once-updated-p (root &optional key)
(let* ((key (or (and key (format "-%s" key)) ""))
(prop (intern (format "anything-git-files:once-updated%s" key)))
(updated (vc-file-getprop root prop)))
(unless updated
(vc-file-setprop root prop t)
t)))
(defun anything-git-files:t (&rest args) t)
(defun anything-git-files:head-updated-p (root &optional key)
(let* ((key (or (and key (format "-%s" key)) ""))
(prop (intern (format "anything-git-files:last-head%s" key)))
(last-head (vc-file-getprop root prop))
(head (anything-git-files:head root)))
(unless (and last-head (string= head last-head))
(vc-file-setprop root prop head)
t)))
(defun anything-git-files:status-updated-p (root &optional key)
(let* ((key (or (and key (format "-%s" key)) ""))
(prop (intern (format "anything-git-files:last-status%s" key)))
(last-status (vc-file-getprop root prop))
(status (anything-git-files:status-hash root)))
(unless (and last-status (string= status last-status))
(vc-file-setprop root prop status)
t)))
(defun anything-git-files:updated-p (mode root &optional key update-once)
"Check if the status hash value for ROOT repository is updated.
MODE specifies how to check the update status. The update status
is tracked for each KEY separately."
(let ((funs (or (and update-once 'anything-git-files:once-updated-p)
(cdr (assq mode anything-git-files:update-check-functions))
'(anything-git-files:head-updated-p
anything-git-files:status-updated-p))))
(unless (listp funs) (setq funs (list funs)))
(loop for fun in funs
thereis (funcall fun root key))))
(defun anything-git-files:candidates (what &optional root update-once)
(let* ((root (or root
(anything-attr 'default-directory)
(anything-git-files:root)))
(buffer-name (format " *anything candidates:%s:%s*" root what))
(buffer (get-buffer-create buffer-name)))
(anything-attrset 'default-directory root) ; saved for `display-to-real'
(when (and (not (member buffer-name anything-git-files:cached))
(anything-git-files:updated-p what root what update-once))
(let ((default-directory root)
(args (cdr (assq what anything-git-files:ls-args)))
(callback 'anything-git-files:sentinel))
(apply 'anything-git-files:ls-async buffer callback
"--full-name" args)
(push buffer-name anything-git-files:cached)))
(anything-candidate-buffer buffer)
(anything-candidates-in-buffer)))
(defun anything-git-files:sentinel (process event)
(when (equal event "finished\n")
(anything-git-files:throttled-update)))
(defun anything-git-files:update-1 ()
(setq anything-git-files:last-update (float-time)
anything-git-files:update-timer nil)
(when (and (anything-window) (buffer-live-p (get-buffer anything-buffer)))
(with-anything-window
(with-current-buffer anything-buffer
(let ((line (line-number-at-pos)))
(anything-update)
(goto-char (point-min))
(forward-line (1- line))
(anything-skip-noncandidate-line 'next)
(anything-mark-current-line)
(anything-display-mode-line (anything-get-current-source)))))))
(defun anything-git-files:throttled-update ()
(if (<= (- (float-time) anything-git-files:last-update)
anything-git-files:update-delay)
(unless anything-git-files:update-timer
(setq anything-git-files:update-timer
(run-at-time anything-git-files:update-delay nil
'anything-git-files:update-1)))
(when anything-git-files:update-timer
(cancel-timer anything-git-files:update-timer))
(anything-git-files:update-1)))
(defun anything-git-files:init ()
(anything-attrset 'default-directory (anything-git-files:root)))
(defun anything-git-files:cleanup ()
(anything-new-timer 'anything-process-delayed-sources-timer nil)
(setq anything-git-files:cached nil))
(defun anything-git-files:candidates-fun (what &optional root update-once)
`(lambda () (anything-git-files:candidates ',what ,root ,update-once)))
(defun anything-git-files:display-to-real (name)
(expand-file-name name (anything-attr 'default-directory)))
(defun anything-git-files:source (what &optional root repository update-once)
(let ((name (concat (format "Git %s" (capitalize (format "%s" what)))
(or (and repository (format " in %s" repository)) ""))))
`((name . ,name)
(init . anything-git-files:init)
(cleanup . anything-git-files:cleanup)
(candidates . ,(anything-git-files:candidates-fun what root update-once))
(delayed)
(volatile)
(match identity)
(type . file)
(display-to-real . anything-git-files:display-to-real))))
(defun anything-git-files:submodules-by-dot (&optional dotgitmodule)
(let ((exp "^[[:space:]]*path[[:space:]]*=[[:space:]]*\\(.*\\)[[:space:]]*$")
(result (list)))
(with-temp-buffer
(insert-file-contents-literally dotgitmodule)
(goto-char (point-min))
(while (re-search-forward exp nil t)
(push (match-string 1) result))
(reverse result))))
(defun anything-git-files:submodules-by-foreach (&optional root)
(let ((default-directory root)
(args '("submodule" "--quiet" "foreach" "echo $path")))
(loop for module in (split-string
(anything-git-files:chomp
(apply 'anything-git-files:command-to-string args))
"[\r\n]+")
if (> (length module) 0)
collect module)))
(defun anything-git-files:submodules (&optional root)
(let* ((root (or root (anything-git-files:root)))
(dotgitmodule (expand-file-name ".gitmodules" root)))
(if (file-exists-p dotgitmodule)
(anything-git-files:submodules-by-dot dotgitmodule)
(anything-git-files:submodules-by-foreach root))))
;;;###autoload
(defun anything-git-files:git-p (&optional root)
(ignore-errors (anything-git-files:head root)))
;;;###autoload
(defvar anything-git-files:modified-source nil)
(setq anything-git-files:modified-source
(anything-git-files:source 'modified))
;;;###autoload
(defvar anything-git-files:untracked-source nil)
(setq anything-git-files:untracked-source
(anything-git-files:source 'untracked))
;;;###autoload
(defvar anything-git-files:all-source nil)
(setq anything-git-files:all-source
(anything-git-files:source 'all))
;;;###autoload
(defun anything-git-files:submodule-sources (kinds &optional root)
(let* ((root (or root (anything-git-files:root)))
(modules (anything-git-files:submodules root))
(kinds (if (listp kinds) kinds (list kinds)))
(once anything-git-files:update-submodules-once))
(loop for module in modules
append (loop for what in kinds
for path = (file-name-as-directory
(expand-file-name module root))
if (file-exists-p path)
collect (anything-git-files:source
what path module once)))))
;;;###autoload
(defun anything-git-files ()
"`anything' for opening files managed by Git."
(interactive)
(anything-other-buffer `(anything-git-files:modified-source
anything-git-files:untracked-source
anything-git-files:all-source
,@(anything-git-files:submodule-sources
'(modified untracked all)))
"*anything for git files*"))
(provide 'anything-git-files)
;;; anything-git-files.el ends here