Blob


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