Blame


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