Blame


1 406d7b4d 2020-12-26 op ;;; vc-got.el --- Game of Tree backend for VC -*- lexical-binding: t; -*-
2 af5ef7cd 2020-11-29 op
3 406d7b4d 2020-12-26 op ;; Copyright (C) 2020 Omar Polo
4 af5ef7cd 2020-11-29 op
5 406d7b4d 2020-12-26 op ;; Author: Omar Polo <op@venera>
6 406d7b4d 2020-12-26 op ;; Keywords: vc
7 af5ef7cd 2020-11-29 op
8 406d7b4d 2020-12-26 op ;; This program is free software; you can redistribute it and/or modify
9 406d7b4d 2020-12-26 op ;; it under the terms of the GNU General Public License as published by
10 406d7b4d 2020-12-26 op ;; the Free Software Foundation, either version 3 of the License, or
11 406d7b4d 2020-12-26 op ;; (at your option) any later version.
12 af5ef7cd 2020-11-29 op
13 406d7b4d 2020-12-26 op ;; This program is distributed in the hope that it will be useful,
14 406d7b4d 2020-12-26 op ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 406d7b4d 2020-12-26 op ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 406d7b4d 2020-12-26 op ;; GNU General Public License for more details.
17 af5ef7cd 2020-11-29 op
18 406d7b4d 2020-12-26 op ;; You should have received a copy of the GNU General Public License
19 406d7b4d 2020-12-26 op ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
20 406d7b4d 2020-12-26 op
21 af5ef7cd 2020-11-29 op ;;; Commentary
22 af5ef7cd 2020-11-29 op
23 af5ef7cd 2020-11-29 op ;; Backend implementation status
24 af5ef7cd 2020-11-29 op ;;
25 af5ef7cd 2020-11-29 op ;; Function marked with `*' are required, those with `-' are optional.
26 af5ef7cd 2020-11-29 op ;;
27 af5ef7cd 2020-11-29 op ;; FUNCTION NAME STATUS
28 af5ef7cd 2020-11-29 op ;;
29 af5ef7cd 2020-11-29 op ;; BACKEND PROPERTIES:
30 af5ef7cd 2020-11-29 op ;; * revision-granularity DONE
31 af5ef7cd 2020-11-29 op ;; - update-on-retrieve-tag XXX: what should this do?
32 af5ef7cd 2020-11-29 op ;;
33 af5ef7cd 2020-11-29 op ;; STATE-QUERYING FUNCTIONS:
34 af5ef7cd 2020-11-29 op ;; * registered DONE
35 af5ef7cd 2020-11-29 op ;; * state DONE
36 af5ef7cd 2020-11-29 op ;; - dir-status-files DONE
37 a8466f02 2020-12-07 op ;; - dir-extra-headers DONE
38 af5ef7cd 2020-11-29 op ;; - dir-printer NOT IMPLEMENTED
39 af5ef7cd 2020-11-29 op ;; - status-fileinfo-extra NOT IMPLEMENTED
40 af5ef7cd 2020-11-29 op ;; * working-revision DONE
41 af5ef7cd 2020-11-29 op ;; * checkout-model DONE
42 694534b4 2020-12-05 op ;; - mode-line-string DONE
43 23a0b465 2020-11-30 op ;;
44 23a0b465 2020-11-30 op ;; STATE-CHANGING FUNCTIONS:
45 23a0b465 2020-11-30 op ;; * create-repo NOT IMPLEMENTED
46 eb85ad27 2020-12-05 op ;; I don't think got init does what this function is supposed to
47 eb85ad27 2020-12-05 op ;; do.
48 23a0b465 2020-11-30 op ;; * register DONE
49 23a0b465 2020-11-30 op ;; - responsible-p DONE
50 23a0b465 2020-11-30 op ;; - receive-file NOT IMPLEMENTED
51 23a0b465 2020-11-30 op ;; - unregister NOT IMPLEMENTED
52 eb85ad27 2020-12-05 op ;; use remove?
53 23a0b465 2020-11-30 op ;; * checkin DONE
54 23a0b465 2020-11-30 op ;; * find-revision DONE
55 eb85ad27 2020-12-05 op ;; * checkout NOT IMPLEMENTED
56 eb85ad27 2020-12-05 op ;; I'm not sure how to properly implement this. Does filling
57 eb85ad27 2020-12-05 op ;; FILE with the find-revision do the trick? Or use got update?
58 eb85ad27 2020-12-05 op ;; * revert DONE
59 eb85ad27 2020-12-05 op ;; - merge-file NOT IMPLEMENTED
60 eb85ad27 2020-12-05 op ;; - merge-branch DONE
61 eb85ad27 2020-12-05 op ;; - merge-news NOT IMPLEMENTED
62 eb85ad27 2020-12-05 op ;; - pull DONE
63 686eac9a 2020-12-07 op ;; - push DONE
64 686eac9a 2020-12-07 op ;; uses git
65 eb85ad27 2020-12-05 op ;; - steal-lock NOT IMPLEMENTED
66 eb85ad27 2020-12-05 op ;; - modify-change-comment NOT IMPLEMENTED
67 eb85ad27 2020-12-05 op ;; can be implemented via histedit, if I understood correctly
68 eb85ad27 2020-12-05 op ;; what it is supposed to do.
69 eb85ad27 2020-12-05 op ;; - mark-resolved NOT IMPLEMENTED
70 eb85ad27 2020-12-05 op ;; - find-admin-dir NOT IMPLEMENTED
71 eb85ad27 2020-12-05 op ;;
72 eb85ad27 2020-12-05 op ;; HISTORY FUNCTIONS
73 eb85ad27 2020-12-05 op ;; * print-log DONE
74 9ad1eb5d 2020-12-07 op ;; * log-outgoing DONE
75 6eda2f1f 2020-12-26 op ;; * log-incoming DONE
76 eb85ad27 2020-12-05 op ;; - log-search DONE
77 eb85ad27 2020-12-05 op ;; - log-view-mode NOT IMPLEMENTED
78 6eda2f1f 2020-12-26 op ;; - show-log-entry NOT IMPLEMENTED
79 6eda2f1f 2020-12-26 op ;; - comment-history NOT IMPLEMENTED
80 6eda2f1f 2020-12-26 op ;; - update-changelog NOT IMPLEMENTED
81 7c257a7b 2020-12-31 noreply ;; * diff DONE
82 7c257a7b 2020-12-31 noreply ;; - revision-completion-table NOT IMPLEMENTED
83 7c257a7b 2020-12-31 noreply ;; - annotate-command NOT IMPLEMENTED
84 7c257a7b 2020-12-31 noreply ;; - annotate-time NOT IMPLEMENTED
85 7c257a7b 2020-12-31 noreply ;; - annotate-current-time NOT IMPLEMENTED
86 7c257a7b 2020-12-31 noreply ;; - annotate-extract-revision-at-line NOT IMPLEMENTED
87 7c257a7b 2020-12-31 noreply ;; - region-history NOT IMPLEMENTED
88 7c257a7b 2020-12-31 noreply ;; - region-history-mode NOT IMPLEMENTED
89 7c257a7b 2020-12-31 noreply ;; - mergebase NOT IMPLEMENTED
90 7c257a7b 2020-12-31 noreply ;;
91 7c257a7b 2020-12-31 noreply ;; TAG SYSTEM
92 7c257a7b 2020-12-31 noreply ;; - create-tag NOT IMPLEMENTED
93 7c257a7b 2020-12-31 noreply ;; - retrieve-tag NOT IMPLEMENTED
94 7c257a7b 2020-12-31 noreply ;;
95 7c257a7b 2020-12-31 noreply ;; MISCELLANEOUS NOT IMPLEMENTED
96 7c257a7b 2020-12-31 noreply ;; - make-version-backups-p NOT IMPLEMENTED
97 7c257a7b 2020-12-31 noreply ;; - root DONE
98 7c257a7b 2020-12-31 noreply ;; - ignore NOT IMPLEMENTED
99 7c257a7b 2020-12-31 noreply ;; - ignore-completion-table NOT IMPLEMENTED
100 7c257a7b 2020-12-31 noreply ;; - previous-revision NOT IMPLEMENTED
101 7c257a7b 2020-12-31 noreply ;; - next-revision NOT IMPLEMENTED
102 7c257a7b 2020-12-31 noreply ;; - log-edit-mode NOT IMPLEMENTED
103 7c257a7b 2020-12-31 noreply ;; - check-headers NOT IMPLEMENTED
104 7c257a7b 2020-12-31 noreply ;; - delete-file NOT IMPLEMENTED
105 7c257a7b 2020-12-31 noreply ;; - rename-file NOT IMPLEMENTED
106 7c257a7b 2020-12-31 noreply ;; - find-file-hook NOT IMPLEMENTED
107 7c257a7b 2020-12-31 noreply ;; - extra-menu NOT IMPLEMENTED
108 7c257a7b 2020-12-31 noreply ;; - extra-dir-menu NOT IMPLEMENTED
109 7c257a7b 2020-12-31 noreply ;; - conflicted-files NOT IMPLEMENTED
110 7c257a7b 2020-12-31 noreply ;; - repository-url NOT IMPLEMENTED
111 af5ef7cd 2020-11-29 op
112 af5ef7cd 2020-11-29 op ;; TODO: use the idiom
113 af5ef7cd 2020-11-29 op ;; (let (process-file-side-effects) ...)
114 af5ef7cd 2020-11-29 op ;; when the got command WON'T change the file. This can enable some
115 af5ef7cd 2020-11-29 op ;; emacs optimizations
116 af5ef7cd 2020-11-29 op
117 eb85ad27 2020-12-05 op ;; TODO: vc-git has most function that starts with:
118 eb85ad27 2020-12-05 op ;;
119 eb85ad27 2020-12-05 op ;; (let* ((root (vc-git-root default-directory))
120 eb85ad27 2020-12-05 op ;; (buffer (format "*vc-git : %s*" (expand-file-name root)))
121 eb85ad27 2020-12-05 op ;; ...)
122 eb85ad27 2020-12-05 op ;; ...)
123 eb85ad27 2020-12-05 op ;;
124 eb85ad27 2020-12-05 op ;; we should 1) investigate if also other backends do something like
125 eb85ad27 2020-12-05 op ;; this (or if there is a better way) and 2) try to do the same.
126 eb85ad27 2020-12-05 op
127 af5ef7cd 2020-11-29 op ;;; Code:
128 af5ef7cd 2020-11-29 op
129 af5ef7cd 2020-11-29 op (eval-when-compile
130 af5ef7cd 2020-11-29 op (require 'subr-x))
131 af5ef7cd 2020-11-29 op
132 af5ef7cd 2020-11-29 op (require 'cl-lib)
133 518ede14 2020-12-05 op (require 'cl-seq)
134 af5ef7cd 2020-11-29 op (require 'seq)
135 23a0b465 2020-11-30 op (require 'vc)
136 ba123905 2020-12-08 op
137 e79563bd 2020-12-31 noreply (defgroup vc-got nil
138 e79563bd 2020-12-31 noreply "VC GoT backend."
139 e79563bd 2020-12-31 noreply :group 'vc)
140 af5ef7cd 2020-11-29 op
141 e79563bd 2020-12-31 noreply (defcustom vc-got-program "got"
142 e79563bd 2020-12-31 noreply "Name of the Got executable (excluding any arguments)."
143 e79563bd 2020-12-31 noreply :type 'string
144 e79563bd 2020-12-31 noreply :group 'vc-got)
145 e79563bd 2020-12-31 noreply
146 12ca62f2 2020-12-26 op (defcustom vc-got-diff-switches t
147 12ca62f2 2020-12-26 op "String or list of strings specifying switches for Got diff under VC.
148 12ca62f2 2020-12-26 op If nil, use the value of `vc-diff-switches'. If t, use no switches."
149 12ca62f2 2020-12-26 op :type '(choice (const :tag "Unspecified" nil)
150 12ca62f2 2020-12-26 op (const :tag "None" t)
151 12ca62f2 2020-12-26 op (string :tag "Argument String")
152 e79563bd 2020-12-31 noreply (repeat :tag "Argument List" :value ("") string))
153 e79563bd 2020-12-31 noreply :group 'vc-got)
154 12ca62f2 2020-12-26 op
155 af5ef7cd 2020-11-29 op ;; helpers
156 f09d6359 2020-12-31 noreply (defun vc-got--program-version ()
157 f09d6359 2020-12-31 noreply "Returns the version string of used `Got' command."
158 f09d6359 2020-12-31 noreply (let (process-file-side-effects)
159 f09d6359 2020-12-31 noreply (with-temp-buffer
160 f09d6359 2020-12-31 noreply (vc-got--call "-V")
161 f09d6359 2020-12-31 noreply (substring (buffer-string) 4 -1))))
162 af5ef7cd 2020-11-29 op
163 af5ef7cd 2020-11-29 op (defun vc-got-root (file)
164 af5ef7cd 2020-11-29 op "Return the work tree root for FILE, or nil."
165 f6e414a6 2020-11-30 op (or (vc-file-getprop file 'got-root)
166 f6e414a6 2020-11-30 op (vc-file-setprop file 'got-root (vc-find-root file ".got"))))
167 af5ef7cd 2020-11-29 op
168 af5ef7cd 2020-11-29 op (defmacro vc-got-with-worktree (file &rest body)
169 af5ef7cd 2020-11-29 op "Evaluate BODY in the work tree directory of FILE."
170 af5ef7cd 2020-11-29 op (declare (indent defun))
171 af5ef7cd 2020-11-29 op `(when-let (default-directory (vc-got-root ,file))
172 af5ef7cd 2020-11-29 op ,@body))
173 686eac9a 2020-12-07 op
174 686eac9a 2020-12-07 op (defun vc-got--repo-root ()
175 686eac9a 2020-12-07 op "Return the path to the repository root.
176 686eac9a 2020-12-07 op Assume `default-directory' is inside a got worktree."
177 686eac9a 2020-12-07 op (vc-got-with-worktree default-directory
178 686eac9a 2020-12-07 op (with-temp-buffer
179 686eac9a 2020-12-07 op (insert-file-contents ".got/repository")
180 686eac9a 2020-12-07 op (string-trim (buffer-string) nil "\n"))))
181 af5ef7cd 2020-11-29 op
182 af5ef7cd 2020-11-29 op (defun vc-got--call (&rest args)
183 e79563bd 2020-12-31 noreply "Call `vc-got-program' in the `default-directory' with ARGS and put the output in the current buffer."
184 e79563bd 2020-12-31 noreply (apply #'process-file vc-got-program nil (current-buffer) nil args))
185 af5ef7cd 2020-11-29 op
186 23a0b465 2020-11-30 op (defun vc-got--add (files)
187 23a0b465 2020-11-30 op "Add FILES to got, passing `vc-register-switches' to the command invocation."
188 23a0b465 2020-11-30 op (with-temp-buffer
189 23a0b465 2020-11-30 op (apply #'vc-got--call "add" (append vc-register-switches files))))
190 23a0b465 2020-11-30 op
191 0a66694e 2020-01-02 op (defun vc-got--log (&optional path limit start-commit stop-commit
192 0a66694e 2020-01-02 op search-pattern reverse)
193 23a0b465 2020-11-30 op "Execute the log command in the worktree of PATH.
194 518ede14 2020-12-05 op The output in the current buffer.
195 af5ef7cd 2020-11-29 op
196 23a0b465 2020-11-30 op LIMIT limits the maximum number of commit returned.
197 23a0b465 2020-11-30 op
198 518ede14 2020-12-05 op START-COMMIT: start traversing history at the specified commit.
199 9ad1eb5d 2020-12-07 op STOP-COMMIT: stop traversing history at the specified commit.
200 518ede14 2020-12-05 op SEARCH-PATTERN: limit to log messages matched by the regexp given.
201 0a66694e 2020-01-02 op REVERSE: display the log messages in reverse order.
202 518ede14 2020-12-05 op
203 23a0b465 2020-11-30 op Return nil if the command failed or if PATH isn't included in any
204 23a0b465 2020-11-30 op worktree."
205 0a66694e 2020-01-02 op (let (process-file-side-effects)
206 0a66694e 2020-01-02 op (vc-got-with-worktree (or path default-directory)
207 0a66694e 2020-01-02 op (zerop
208 0a66694e 2020-01-02 op (apply #'vc-got--call
209 0a66694e 2020-01-02 op (cl-remove-if #'null
210 0a66694e 2020-01-02 op (flatten-list
211 0a66694e 2020-01-02 op (list "log"
212 0a66694e 2020-01-02 op (when limit (list "-l" (format "%s" limit)))
213 0a66694e 2020-01-02 op (when start-commit (list "-c" start-commit))
214 0a66694e 2020-01-02 op (when stop-commit (list "-x" stop-commit))
215 0a66694e 2020-01-02 op (when search-pattern (list "-s" search-pattern))
216 0a66694e 2020-01-02 op (when reverse '("-R"))
217 0a66694e 2020-01-02 op path))))))))
218 af5ef7cd 2020-11-29 op
219 af5ef7cd 2020-11-29 op (defun vc-got--status (dir-or-file &rest files)
220 af5ef7cd 2020-11-29 op "Return the output of ``got status''.
221 af5ef7cd 2020-11-29 op
222 af5ef7cd 2020-11-29 op DIR-OR-FILE can be either a directory or a file. If FILES is
223 af5ef7cd 2020-11-29 op given, return the status of those files, otherwise the status of
224 af5ef7cd 2020-11-29 op DIR-OR-FILE."
225 af5ef7cd 2020-11-29 op (vc-got-with-worktree dir-or-file
226 af5ef7cd 2020-11-29 op (with-temp-buffer
227 af5ef7cd 2020-11-29 op (if files
228 af5ef7cd 2020-11-29 op (apply #'vc-got--call "status" files)
229 af5ef7cd 2020-11-29 op (vc-got--call "status" dir-or-file))
230 af5ef7cd 2020-11-29 op (buffer-string))))
231 af5ef7cd 2020-11-29 op
232 af5ef7cd 2020-11-29 op (defun vc-got--parse-status-flag (flag)
233 af5ef7cd 2020-11-29 op "Parse FLAG, see `vc-state'."
234 af5ef7cd 2020-11-29 op ;; got outputs nothing if the file is up-to-date
235 af5ef7cd 2020-11-29 op (if (string-empty-p flag)
236 af5ef7cd 2020-11-29 op 'up-to-date
237 af5ef7cd 2020-11-29 op ;; trying to follow the order of the manpage
238 af5ef7cd 2020-11-29 op (cl-case (aref flag 0)
239 af5ef7cd 2020-11-29 op (?M 'edited)
240 af5ef7cd 2020-11-29 op (?A 'added)
241 af5ef7cd 2020-11-29 op (?D 'removed)
242 af5ef7cd 2020-11-29 op (?C 'conflict)
243 af5ef7cd 2020-11-29 op (?! 'missing)
244 af5ef7cd 2020-11-29 op (?~ 'edited) ;XXX: what does it means for a file to be ``obstructed''?
245 af5ef7cd 2020-11-29 op (?? 'unregistered)
246 af5ef7cd 2020-11-29 op (?m 'edited) ;modified file modes
247 af5ef7cd 2020-11-29 op (?N nil))))
248 af5ef7cd 2020-11-29 op
249 af5ef7cd 2020-11-29 op (defun vc-got--parse-status (output)
250 af5ef7cd 2020-11-29 op "Parse the OUTPUT of got status and return an alist of (FILE . STATUS)."
251 af5ef7cd 2020-11-29 op ;; XXX: the output of got is line-oriented and will break if
252 af5ef7cd 2020-11-29 op ;; filenames contains spaces or newlines.
253 af5ef7cd 2020-11-29 op (cl-loop for line in (split-string output "\n" t)
254 af5ef7cd 2020-11-29 op collect (cl-destructuring-bind (status file) (split-string line " " t " ")
255 af5ef7cd 2020-11-29 op `(,file . ,(vc-got--parse-status-flag status)))))
256 af5ef7cd 2020-11-29 op
257 23a0b465 2020-11-30 op (defun vc-got--tree-parse ()
258 23a0b465 2020-11-30 op "Parse into an alist the output of got tree -i in the current buffer."
259 23a0b465 2020-11-30 op (goto-char (point-min))
260 23a0b465 2020-11-30 op (cl-loop
261 23a0b465 2020-11-30 op until (= (point) (point-max))
262 23a0b465 2020-11-30 op collect (let* ((obj-start (point))
263 23a0b465 2020-11-30 op (_ (forward-word))
264 23a0b465 2020-11-30 op (obj (buffer-substring obj-start (point)))
265 23a0b465 2020-11-30 op (_ (forward-char)) ;skip the space
266 23a0b465 2020-11-30 op (filename-start (point))
267 23a0b465 2020-11-30 op (_ (move-end-of-line nil))
268 23a0b465 2020-11-30 op (filename (buffer-substring filename-start (point))))
269 23a0b465 2020-11-30 op ;; goto the start of the next line
270 23a0b465 2020-11-30 op (forward-line)
271 23a0b465 2020-11-30 op (move-beginning-of-line nil)
272 23a0b465 2020-11-30 op `(,filename . ,obj))))
273 23a0b465 2020-11-30 op
274 23a0b465 2020-11-30 op (defun vc-got--tree (commit path)
275 23a0b465 2020-11-30 op (vc-got-with-worktree path
276 23a0b465 2020-11-30 op (with-temp-buffer
277 23a0b465 2020-11-30 op (vc-got--call "tree" "-c" commit "-i" path)
278 23a0b465 2020-11-30 op (vc-got--tree-parse))))
279 23a0b465 2020-11-30 op
280 23a0b465 2020-11-30 op (defun vc-got--cat (commit obj-id)
281 23a0b465 2020-11-30 op "Execute got cat -c COMMIT OBJ-ID in the current buffer."
282 23a0b465 2020-11-30 op (vc-got--call "cat" "-c" commit obj-id))
283 eb85ad27 2020-12-05 op
284 eb85ad27 2020-12-05 op (defun vc-got--revert (&rest files)
285 eb85ad27 2020-12-05 op "Execute got revert FILES..."
286 eb85ad27 2020-12-05 op (vc-got-with-worktree (car files)
287 eb85ad27 2020-12-05 op (with-temp-buffer
288 eb85ad27 2020-12-05 op (apply #'vc-got--call "revert" files))))
289 eb85ad27 2020-12-05 op
290 eb85ad27 2020-12-05 op (defun vc-got--list-branches ()
291 eb85ad27 2020-12-05 op "Return an alist of (branch . commit)."
292 eb85ad27 2020-12-05 op (with-temp-buffer
293 eb85ad27 2020-12-05 op (when (zerop (vc-got--call "branch" "-l"))
294 eb85ad27 2020-12-05 op (goto-char (point-min))
295 eb85ad27 2020-12-05 op (cl-loop
296 eb85ad27 2020-12-05 op until (= (point) (point-max))
297 eb85ad27 2020-12-05 op ;; parse the `* $branchname: $commit', from the end
298 eb85ad27 2020-12-05 op collect (let* ((_ (move-end-of-line nil))
299 eb85ad27 2020-12-05 op (end-commit (point))
300 eb85ad27 2020-12-05 op (_ (backward-word))
301 eb85ad27 2020-12-05 op (start-commit (point))
302 eb85ad27 2020-12-05 op (_ (backward-char 2))
303 eb85ad27 2020-12-05 op (end-branchname (point))
304 eb85ad27 2020-12-05 op (_ (move-beginning-of-line nil))
305 eb85ad27 2020-12-05 op (_ (forward-char 2))
306 eb85ad27 2020-12-05 op (start-branchname (point))
307 eb85ad27 2020-12-05 op (branchname (buffer-substring start-branchname end-branchname))
308 eb85ad27 2020-12-05 op (commit (buffer-substring start-commit end-commit)))
309 eb85ad27 2020-12-05 op (forward-line)
310 eb85ad27 2020-12-05 op (move-beginning-of-line nil)
311 eb85ad27 2020-12-05 op `(,branchname . ,commit))))))
312 eb85ad27 2020-12-05 op
313 694534b4 2020-12-05 op (defun vc-got--current-branch ()
314 694534b4 2020-12-05 op "Return the current branch."
315 694534b4 2020-12-05 op (with-temp-buffer
316 694534b4 2020-12-05 op (when (zerop (vc-got--call "branch"))
317 694534b4 2020-12-05 op (string-trim (buffer-string) "" "\n"))))
318 eb85ad27 2020-12-05 op
319 eb85ad27 2020-12-05 op (defun vc-got--integrate (branch)
320 eb85ad27 2020-12-05 op "Integrate BRANCH into the current one."
321 eb85ad27 2020-12-05 op (with-temp-buffer
322 eb85ad27 2020-12-05 op (vc-got--call "integrate" branch)))
323 23a0b465 2020-11-30 op
324 eb85ad27 2020-12-05 op (defun vc-got--diff (&rest args)
325 eb85ad27 2020-12-05 op "Call got diff with ARGS. The result will be stored in the current buffer."
326 345290bf 2020-12-05 op (apply #'vc-got--call "diff"
327 12ca62f2 2020-12-26 op (append (vc-switches 'got 'diff)
328 12ca62f2 2020-12-26 op (mapcar #'file-relative-name args))))
329 eb85ad27 2020-12-05 op
330 af5ef7cd 2020-11-29 op
331 af5ef7cd 2020-11-29 op ;; Backend properties
332 af5ef7cd 2020-11-29 op
333 af5ef7cd 2020-11-29 op (defun vc-got-revision-granularity ()
334 af5ef7cd 2020-11-29 op "Got has REPOSITORY granularity."
335 af5ef7cd 2020-11-29 op 'repository)
336 af5ef7cd 2020-11-29 op
337 af5ef7cd 2020-11-29 op ;; XXX: what this should do? The description is not entirely clear
338 af5ef7cd 2020-11-29 op (defun vc-got-update-on-retrieve-tag ()
339 af5ef7cd 2020-11-29 op nil)
340 af5ef7cd 2020-11-29 op
341 af5ef7cd 2020-11-29 op
342 af5ef7cd 2020-11-29 op ;; State-querying functions
343 af5ef7cd 2020-11-29 op
344 af5ef7cd 2020-11-29 op ;;;###autoload (defun vc-got-registered (file)
345 af5ef7cd 2020-11-29 op ;;;###autoload "Return non-nil if FILE is registered with got."
346 af5ef7cd 2020-11-29 op ;;;###autoload (when (vc-find-root file ".got")
347 af5ef7cd 2020-11-29 op ;;;###autoload (load "vc-got" nil t)
348 af5ef7cd 2020-11-29 op ;;;###autoload (vc-got-registered file)))
349 af5ef7cd 2020-11-29 op
350 af5ef7cd 2020-11-29 op (defun vc-got-registered (file)
351 af5ef7cd 2020-11-29 op "Return non-nil if FILE is registered with got."
352 af5ef7cd 2020-11-29 op (if (file-directory-p file)
353 af5ef7cd 2020-11-29 op nil ;got doesn't track directories
354 4093d2f9 2020-12-04 op (when (vc-find-root file ".got")
355 4093d2f9 2020-12-04 op (let ((status (vc-got--status file)))
356 4093d2f9 2020-12-04 op (not (or (string-prefix-p "?" status)
357 4093d2f9 2020-12-04 op (string-prefix-p "N" status)))))))
358 af5ef7cd 2020-11-29 op
359 af5ef7cd 2020-11-29 op ;; (vc-got-registered "/usr/ports/mystuff/net/td")
360 af5ef7cd 2020-11-29 op ;; (vc-got-registered "/usr/ports/mystuff/net/td/Makefile")
361 af5ef7cd 2020-11-29 op ;; (vc-got-registered "/usr/ports/mystuff/tmp")
362 af5ef7cd 2020-11-29 op ;; (vc-got-registered "/usr/ports/mystuff/no-existant")
363 af5ef7cd 2020-11-29 op
364 af5ef7cd 2020-11-29 op (defun vc-got-state (file)
365 af5ef7cd 2020-11-29 op "Return the current version control state of FILE. See `vc-state'."
366 af5ef7cd 2020-11-29 op (unless (file-directory-p file)
367 af5ef7cd 2020-11-29 op (vc-got--parse-status-flag (vc-got--status file))))
368 af5ef7cd 2020-11-29 op
369 af5ef7cd 2020-11-29 op ;; (vc-got-state "/usr/ports/mystuff/net/td")
370 af5ef7cd 2020-11-29 op ;; (vc-got-state "/usr/ports/mystuff/net/td/Makefile")
371 af5ef7cd 2020-11-29 op ;; (vc-got-state "/usr/ports/mystuff/tmp")
372 af5ef7cd 2020-11-29 op ;; (vc-got-state "/usr/ports/mystuff/non-existant")
373 af5ef7cd 2020-11-29 op
374 af5ef7cd 2020-11-29 op (defun vc-got-dir-status-files (dir files update-function)
375 af5ef7cd 2020-11-29 op (let* ((files (seq-filter (lambda (file)
376 af5ef7cd 2020-11-29 op (and (not (string= file ".."))
377 af5ef7cd 2020-11-29 op (not (string= file "."))
378 af5ef7cd 2020-11-29 op (not (string= file ".got"))))
379 af5ef7cd 2020-11-29 op (or files
380 af5ef7cd 2020-11-29 op (directory-files dir))))
381 af5ef7cd 2020-11-29 op (statuses (vc-got--parse-status
382 af5ef7cd 2020-11-29 op (apply #'vc-got--status dir files)))
383 af5ef7cd 2020-11-29 op (default-directory dir))
384 af5ef7cd 2020-11-29 op (cl-loop
385 af5ef7cd 2020-11-29 op with result = nil
386 af5ef7cd 2020-11-29 op for file in files
387 af5ef7cd 2020-11-29 op do (setq result
388 af5ef7cd 2020-11-29 op (cons
389 af5ef7cd 2020-11-29 op (if (file-directory-p file)
390 af5ef7cd 2020-11-29 op (list file 'unregistered nil)
391 af5ef7cd 2020-11-29 op (if-let (status (cdr (assoc file statuses #'string=)))
392 af5ef7cd 2020-11-29 op (list file status nil)
393 af5ef7cd 2020-11-29 op (list file 'up-to-date nil)))
394 af5ef7cd 2020-11-29 op result))
395 af5ef7cd 2020-11-29 op finally (funcall update-function result nil))))
396 af5ef7cd 2020-11-29 op
397 af5ef7cd 2020-11-29 op ;; (let ((dir "/usr/ports/mystuff"))
398 af5ef7cd 2020-11-29 op ;; (vc-got-dir-status-files dir nil (lambda (res _t)
399 af5ef7cd 2020-11-29 op ;; (message "got %s" res))))
400 af5ef7cd 2020-11-29 op
401 a8466f02 2020-12-07 op (defun vc-got-dir-extra-headers (_dir)
402 a8466f02 2020-12-07 op (concat
403 a8466f02 2020-12-07 op (propertize "Branch : " 'face 'font-lock-type-face)
404 a8466f02 2020-12-07 op (vc-got--current-branch)))
405 a8466f02 2020-12-07 op
406 af5ef7cd 2020-11-29 op (defun vc-got-working-revision (file)
407 c0c9a339 2020-12-04 op "Return the id of the last commit that touched the FILE or \"0\" for a new (but added) file."
408 af5ef7cd 2020-11-29 op (or
409 af5ef7cd 2020-11-29 op (with-temp-buffer
410 518ede14 2020-12-05 op (when (vc-got--log file 1)
411 af5ef7cd 2020-11-29 op (let (start)
412 af5ef7cd 2020-11-29 op (goto-char (point-min))
413 af5ef7cd 2020-11-29 op (forward-line 1) ;skip the ----- line
414 af5ef7cd 2020-11-29 op (forward-word) ;skip "commit"
415 af5ef7cd 2020-11-29 op (forward-char) ;skip the space
416 af5ef7cd 2020-11-29 op (setq start (point)) ;store start of the SHA
417 af5ef7cd 2020-11-29 op (forward-word) ;goto SHA end
418 af5ef7cd 2020-11-29 op (buffer-substring start (point)))))
419 af5ef7cd 2020-11-29 op ;; special case: if this file is added but has no previous commits
420 af5ef7cd 2020-11-29 op ;; touching it, got log will fail (as expected), but we have to
421 af5ef7cd 2020-11-29 op ;; return "0".
422 af5ef7cd 2020-11-29 op (when (eq (vc-got-state file) 'added)
423 af5ef7cd 2020-11-29 op "0")))
424 af5ef7cd 2020-11-29 op
425 af5ef7cd 2020-11-29 op ;; (vc-got-working-revision "/usr/ports/mystuff/non-existant")
426 af5ef7cd 2020-11-29 op ;; (vc-got-working-revision "/usr/ports/mystuff/CVS")
427 af5ef7cd 2020-11-29 op ;; (vc-got-working-revision "/usr/ports/mystuff/tmp")
428 af5ef7cd 2020-11-29 op ;; (vc-got-working-revision "/usr/ports/mystuff/net/td/Makefile")
429 af5ef7cd 2020-11-29 op
430 af5ef7cd 2020-11-29 op (defun vc-got-checkout-model (_files)
431 af5ef7cd 2020-11-29 op 'implicit)
432 af5ef7cd 2020-11-29 op
433 694534b4 2020-12-05 op (defun vc-got-mode-line-string (file)
434 694534b4 2020-12-05 op "Return the VC mode line string for FILE."
435 694534b4 2020-12-05 op (vc-got-with-worktree file
436 694534b4 2020-12-05 op (let ((def (vc-default-mode-line-string 'Got file)))
437 694534b4 2020-12-05 op (concat (substring def 0 4) (vc-got--current-branch)))))
438 694534b4 2020-12-05 op
439 23a0b465 2020-11-30 op
440 23a0b465 2020-11-30 op ;; state-changing functions
441 23a0b465 2020-11-30 op
442 23a0b465 2020-11-30 op (defun vc-got-create-repo (_backend)
443 23a0b465 2020-11-30 op (error "vc got: create-repo not implemented"))
444 23a0b465 2020-11-30 op
445 23a0b465 2020-11-30 op (defun vc-got-register (files &optional _comment)
446 23a0b465 2020-11-30 op "Register FILES, passing `vc-register-switches' to the backend command."
447 23a0b465 2020-11-30 op (vc-got--add files))
448 23a0b465 2020-11-30 op
449 9e805da8 2020-11-30 op (defalias 'vc-got-responsible-p #'vc-got-root)
450 23a0b465 2020-11-30 op
451 23a0b465 2020-11-30 op (defun vc-got-checkin (files comment &optional _rev)
452 23a0b465 2020-11-30 op "Commit FILES with COMMENT as commit message."
453 23a0b465 2020-11-30 op (with-temp-buffer
454 30dcedec 2020-12-05 op (apply #'vc-got--call "commit" "-m"
455 30dcedec 2020-12-05 op ;; emacs add ``Summary:'' at the start of the commit
456 30dcedec 2020-12-05 op ;; message. vc-git doesn't seem to treat this specially.
457 30dcedec 2020-12-05 op ;; Since it's annoying, remove it.
458 30dcedec 2020-12-05 op (string-remove-prefix "Summary: " comment)
459 30dcedec 2020-12-05 op files)))
460 23a0b465 2020-11-30 op
461 23a0b465 2020-11-30 op (defun vc-got-find-revision (file rev buffer)
462 c0c9a339 2020-12-04 op "Fill BUFFER with the content of FILE in the given revision REV."
463 23a0b465 2020-11-30 op (when-let (obj-id (assoc file (vc-got--tree rev file) #'string=))
464 23a0b465 2020-11-30 op (with-current-buffer buffer
465 23a0b465 2020-11-30 op (vc-got-with-worktree file
466 23a0b465 2020-11-30 op (vc-got--cat rev obj-id)))))
467 23a0b465 2020-11-30 op
468 55091167 2020-12-05 op (defun vc-got-find-ignore-file (file)
469 55091167 2020-12-05 op "Return the gitignore file that controls FILE."
470 55091167 2020-12-05 op (expand-file-name ".gitignore"
471 55091167 2020-12-05 op (vc-got-root file)))
472 55091167 2020-12-05 op
473 eb85ad27 2020-12-05 op (defun vc-got-checkout (_file &optional _rev)
474 eb85ad27 2020-12-05 op "Checkout revision REV of FILE. If REV is t, checkout from the head."
475 eb85ad27 2020-12-05 op (error "vc got: checkout not implemented"))
476 eb85ad27 2020-12-05 op
477 eb85ad27 2020-12-05 op (defun vc-got-revert (file &optional _content-done)
478 eb85ad27 2020-12-05 op "Revert FILE back to working revision."
479 eb85ad27 2020-12-05 op (vc-got--revert file))
480 eb85ad27 2020-12-05 op
481 eb85ad27 2020-12-05 op (defun vc-got-merge-branch ()
482 eb85ad27 2020-12-05 op "Prompt for a branch and integrate it into the current one."
483 eb85ad27 2020-12-05 op ;; XXX: be smart and try to "got rebase" if "got integrate" fails?
484 eb85ad27 2020-12-05 op (let* ((branches (cl-loop for (branch . commit) in (vc-got--list-branches)
485 eb85ad27 2020-12-05 op collect branch))
486 eb85ad27 2020-12-05 op (branch (completing-read "Merge from branch: " branches)))
487 eb85ad27 2020-12-05 op (when branch
488 eb85ad27 2020-12-05 op (vc-got--integrate branch))))
489 686eac9a 2020-12-07 op
490 686eac9a 2020-12-07 op (defun vc-got--push-pull (cmd op prompt root)
491 686eac9a 2020-12-07 op "Execute CMD OP, or prompt the user if PROMPT is non-nil.
492 686eac9a 2020-12-07 op ROOT is the worktree root."
493 686eac9a 2020-12-07 op (let ((buffer (format "*vc-got : %s*" (expand-file-name root))))
494 686eac9a 2020-12-07 op (when-let (cmd (if prompt
495 686eac9a 2020-12-07 op (split-string
496 686eac9a 2020-12-07 op (read-shell-command (format "%s %s command: " cmd op)
497 686eac9a 2020-12-07 op (format "%s %s" cmd op))
498 686eac9a 2020-12-07 op " " t)
499 686eac9a 2020-12-07 op (list cmd op)))
500 686eac9a 2020-12-07 op (apply #'vc-do-command buffer 0 (car cmd) nil (cdr cmd)))))
501 eb85ad27 2020-12-05 op
502 eb85ad27 2020-12-05 op (defun vc-got-pull (prompt)
503 eb85ad27 2020-12-05 op "Execute got pull, prompting the user for the full command if PROMPT is not nil."
504 e79563bd 2020-12-31 noreply (vc-got--push-pull vc-got-program "fetch" prompt (vc-got-root default-directory)))
505 686eac9a 2020-12-07 op
506 686eac9a 2020-12-07 op (defun vc-got-push (prompt)
507 686eac9a 2020-12-07 op "Run git push (not got!) in the repository dir.
508 686eac9a 2020-12-07 op If PROMPT is non-nil, prompt for the git command to run."
509 eb85ad27 2020-12-05 op (let* ((root (vc-got-root default-directory))
510 686eac9a 2020-12-07 op (default-directory (vc-got--repo-root)))
511 686eac9a 2020-12-07 op (vc-got--push-pull "git" "push" prompt root)))
512 eb85ad27 2020-12-05 op
513 eb85ad27 2020-12-05 op (defun vc-got-print-log (files buffer &optional _shortlog start-revision limit)
514 eb85ad27 2020-12-05 op "Insert the revision log for FILES into BUFFER.
515 eb85ad27 2020-12-05 op
516 eb85ad27 2020-12-05 op LIMIT limits the number of commits, optionally starting at START-REVISION."
517 eb85ad27 2020-12-05 op (with-current-buffer buffer
518 eb85ad27 2020-12-05 op ;; the *vc-diff* may be read only
519 4571b1fd 2020-12-05 op (let ((inhibit-read-only t))
520 eb85ad27 2020-12-05 op (cl-loop for file in files
521 4571b1fd 2020-12-05 op do (vc-got--log (file-relative-name file) limit start-revision)))))
522 eb85ad27 2020-12-05 op
523 9ad1eb5d 2020-12-07 op ;; XXX: this includes also the latest commit in REMOTE-LOCATION.
524 9ad1eb5d 2020-12-07 op (defun vc-got-log-outgoing (buffer remote-location)
525 9ad1eb5d 2020-12-07 op "Fill BUFFER with the diff between the local worktree branch and REMOTE-LOCATION."
526 9ad1eb5d 2020-12-07 op (vc-setup-buffer buffer)
527 9ad1eb5d 2020-12-07 op (let ((rl (if (or (not remote-location) (string-empty-p remote-location))
528 9ad1eb5d 2020-12-07 op (concat "origin/" (vc-got--current-branch))
529 9ad1eb5d 2020-12-07 op remote-location))
530 9ad1eb5d 2020-12-07 op (inhibit-read-only t))
531 9ad1eb5d 2020-12-07 op (with-current-buffer buffer
532 9ad1eb5d 2020-12-07 op (vc-got--log nil nil nil rl))))
533 6eda2f1f 2020-12-26 op
534 6eda2f1f 2020-12-26 op (defun vc-got-incoming (buffer remote-location)
535 6eda2f1f 2020-12-26 op "Fill BUFFER with the diff between the REMOTE-LOCATION and the local worktree branch."
536 6eda2f1f 2020-12-26 op (let ((rl (if (or (not remote-location) (string-empty-p remote-location))
537 6eda2f1f 2020-12-26 op (concat "origin/" (vc-got--current-branch))
538 6eda2f1f 2020-12-26 op remote-location))
539 6eda2f1f 2020-12-26 op (inhibit-read-only t))
540 6eda2f1f 2020-12-26 op (with-current-buffer buffer
541 6eda2f1f 2020-12-26 op (vc-got--log nil nil (vc-got--current-branch) rl))))
542 9ad1eb5d 2020-12-07 op
543 eb85ad27 2020-12-05 op (defun vc-got-log-search (buffer pattern)
544 eb85ad27 2020-12-05 op "Search commits for PATTERN and write the results found in BUFFER."
545 eb85ad27 2020-12-05 op (with-current-buffer buffer
546 eb85ad27 2020-12-05 op (let ((inhibit-read-only t))
547 9ad1eb5d 2020-12-07 op (vc-got--log nil nil nil nil pattern))))
548 eb85ad27 2020-12-05 op
549 eb85ad27 2020-12-05 op ;; TODO: async
550 f18d3e11 2020-12-05 op ;; TODO: return 0 or 1
551 eb85ad27 2020-12-05 op (defun vc-got-diff (files &optional rev1 rev2 buffer _async)
552 eb85ad27 2020-12-05 op "Insert into BUFFER (or *vc-diff*) the diff for FILES from REV1 to REV2."
553 f457868b 2020-12-07 op (let* ((buffer (get-buffer-create (or buffer "*vc-diff*")))
554 eb85ad27 2020-12-05 op (inhibit-read-only t))
555 eb85ad27 2020-12-05 op (with-current-buffer buffer
556 eb85ad27 2020-12-05 op (vc-got-with-worktree (car files)
557 eb85ad27 2020-12-05 op (cond ((and (null rev1)
558 eb85ad27 2020-12-05 op (null rev2))
559 986bd9a0 2020-12-27 op (dolist (file files)
560 986bd9a0 2020-12-27 op (vc-got--diff file)))
561 eb85ad27 2020-12-05 op (t (error "Not implemented")))))))
562 eb85ad27 2020-12-05 op
563 af5ef7cd 2020-11-29 op (provide 'vc-got)
564 af5ef7cd 2020-11-29 op ;;; vc-got.el ends here