Blob


1 ;;; vc-got.el --- VC backend for Game of Trees VCS -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2021-2022 Free Software Foundation, Inc.
5 ;; Author: Omar Polo <op@omarpolo.com>
6 ;; Timo Myyrä <timo.myyra@bittivirhe.fi>
7 ;; URL: https://projects.omarpolo.com/vc-got.html
8 ;; Keywords: vc tools
9 ;; Version: 1.1.1
10 ;; Package-Requires: ((emacs "25.1"))
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; vc-got is a VC backend for the Game of Trees (got) version control
28 ;; system.
29 ;;
30 ;; Backend implementation status
31 ;;
32 ;; Function marked with `*' are required, those with `-' are optional.
33 ;;
34 ;; FUNCTION NAME STATUS
35 ;;
36 ;; BACKEND PROPERTIES:
37 ;; * revision-granularity DONE
38 ;; - update-on-retrieve-tag XXX: what should this do?
39 ;;
40 ;; STATE-QUERYING FUNCTIONS:
41 ;; * registered DONE
42 ;; * state DONE
43 ;; - dir-status-files DONE
44 ;; - dir-extra-headers DONE
45 ;; - dir-printer DONE
46 ;; - status-fileinfo-extra NOT IMPLEMENTED
47 ;; * working-revision DONE
48 ;; * checkout-model DONE
49 ;; - mode-line-string DONE
50 ;;
51 ;; STATE-CHANGING FUNCTIONS:
52 ;; * create-repo NOT IMPLEMENTED
53 ;; I don't think got init does what this function is supposed to
54 ;; do.
55 ;; * register DONE
56 ;; - responsible-p DONE
57 ;; - receive-file NOT NEEDED, default `register' is fine
58 ;; - unregister DONE
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 ;; - push DONE
70 ;; - steal-lock NOT NEEDED, `got' is not using locks
71 ;; - modify-change-comment NOT IMPLEMENTED
72 ;; can be implemented via histedit, if I understood correctly
73 ;; what it is supposed to do.
74 ;; - mark-resolved NOT NEEDED
75 ;; got notice by itself when a file doesn't have any pending
76 ;; conflicts to be resolved.
77 ;; - find-admin-dir NOT NEEDED
78 ;;
79 ;; HISTORY FUNCTIONS
80 ;; * print-log DONE
81 ;; * log-outgoing DONE
82 ;; * log-incoming DONE
83 ;; - log-search DONE
84 ;; - log-view-mode DONE
85 ;; - show-log-entry NOT IMPLEMENTED
86 ;; - comment-history NOT IMPLEMENTED
87 ;; - update-changelog NOT IMPLEMENTED
88 ;; * diff DONE
89 ;; - revision-completion-table DONE
90 ;; - annotate-command DONE
91 ;; - annotate-time DONE
92 ;; - annotate-current-time NOT NEEDED
93 ;; the default time handling is enough.
94 ;; - annotate-extract-revision-at-line DONE
95 ;; - region-history NOT IMPLEMENTED
96 ;; - region-history-mode NOT IMPLEMENTED
97 ;; - mergebase NOT IMPLEMENTED
98 ;;
99 ;; TAG SYSTEM
100 ;; - create-tag DONE
101 ;; - retrieve-tag DONE
102 ;;
103 ;; MISCELLANEOUS NOT IMPLEMENTED
104 ;; - make-version-backups-p NOT NEEDED, `got' works fine locally
105 ;; - root DONE
106 ;; - ignore NOT NEEDED, the default action is good
107 ;; - ignore-completion-table NOT NEEDED, the default action is good
108 ;; - find-ignore-file DONE
109 ;; - previous-revision DONE
110 ;; - next-revision DONE
111 ;; - log-edit-mode NOT IMPLEMENTED
112 ;; - check-headers NOT NEEDED, `got' does not use headers
113 ;; - delete-file DONE
114 ;; - rename-file NOT IMPLEMENTED
115 ;; - find-file-hook DONE
116 ;; - extra-menu NOT IMPLEMENTED
117 ;; - extra-dir-menu NOT IMPLEMENTED, same as above
118 ;; - conflicted-files DONE
119 ;; - repository-url DONE
121 ;;; Code:
123 ;; TODO: vc-git has most function that starts with:
124 ;;
125 ;; (let* ((root (vc-git-root default-directory))
126 ;; (buffer (format "*vc-git : %s*" (expand-file-name root)))
127 ;; ...)
128 ;; ...)
129 ;;
130 ;; we should 1) investigate if also other backends do something like
131 ;; this (or if there is a better way) and 2) try to do the same.
133 (eval-when-compile
134 (require 'subr-x))
136 (require 'cl-lib)
137 (require 'seq)
138 (require 'vc)
139 (require 'vc-annotate)
141 ;; FIXME: avoid loading this? We only need it for
142 ;; log-edit-extract-headers in vc-got-checkin.
143 (require 'log-edit)
145 ;; FIXME: avoid loading this? We only need it for
146 ;; vc-dir-filename-mouse-map in our custom printer.
147 (require 'vc-dir)
149 ;; FIXME: avoid loading this? We only need it for
150 ;; compilation-{directory,arguments}.
151 (require 'compile)
153 ;; FIXME: avoid loading this? We only need it for
154 ;; log-view-{file-re,per-file-logs,message-re}.
155 (require 'log-view)
157 (defgroup vc-got nil
158 "VC GoT backend."
159 :group 'vc)
161 (defcustom vc-got-program "got"
162 "Name of the Got executable (excluding any arguments)."
163 :type 'string)
165 (defcustom vc-got-diff-switches t
166 "String or list of strings specifying switches for Got diff under VC.
167 If nil, use the value of `vc-diff-switches'. If t, use no switches."
168 :type '(choice (const :tag "Unspecified" nil)
169 (const :tag "None" t)
170 (string :tag "Argument String")
171 (repeat :tag "Argument List" :value ("") string)))
173 ;; helpers
174 (defmacro vc-got--with-emacs-version<= (version &rest body)
175 "Eval BODY only when the Emacs version in greater or equal VERSION."
176 (declare (debug body)
177 (indent defun))
178 (when (version<= version emacs-version)
179 `(progn ,@body)))
181 (defun vc-got--program-version ()
182 "Return string representing the got version."
183 (let (process-file-side-effects)
184 (with-temp-buffer
185 (vc-got--call "-V")
186 (substring (buffer-string) 4 -1))))
188 (defun vc-got-root (file)
189 "Return the work tree root for FILE, or nil."
190 (vc-find-root file ".got"))
192 (defmacro vc-got-with-worktree (file &rest body)
193 "Evaluate BODY in the work tree directory of FILE."
194 (declare (debug t) (indent defun))
195 `(when-let (default-directory (vc-got-root ,file))
196 ,@body))
198 (defun vc-got--repo-root ()
199 "Return the path to the repository root.
200 Assume `default-directory' is inside a got worktree."
201 (vc-got-with-worktree default-directory
202 (with-temp-buffer
203 (insert-file-contents ".got/repository")
204 (string-trim (buffer-string) "" "\n"))))
206 (defun vc-got--call (&rest args)
207 "Call `vc-got-program' with ARGS.
208 The output will be placed in the current buffer."
209 (apply #'process-file vc-got-program nil (current-buffer) nil
210 (apply #'nconc (mapcar (lambda (s) (if (listp s) s (list s))) args))))
212 (defun vc-got--add (files)
213 "Add FILES to got, passing `vc-register-switches' to the command invocation."
214 (with-temp-buffer
215 (vc-got--call "add" vc-register-switches "--" files)))
217 (defun vc-got--log (&optional path limit start-commit stop-commit
218 search-pattern reverse include-diff)
219 "Execute the log command in the worktree of PATH in the current buffer.
220 LIMIT limits the maximum number of commit returned.
222 START-COMMIT: start traversing history at the specified commit.
223 STOP-COMMIT: stop traversing history at the specified commit.
224 SEARCH-PATTERN: limit to log messages matched by the regexp given.
225 REVERSE: display the log messages in reverse order.
226 INCLUDE-DIFF: display the patch of modifications made in each commit.
228 Return nil if the command failed or if PATH isn't included in any
229 worktree."
230 (let ((process-file-side-effects nil))
231 (vc-got-with-worktree (or path default-directory)
232 (when (zerop
233 (save-excursion
234 (vc-got--call "log"
235 (and limit (list "-l" (format "%s" limit)))
236 (and start-commit (list "-c" start-commit))
237 (and stop-commit (list "-x" stop-commit))
238 (and search-pattern (list "-s" search-pattern))
239 (and reverse '("-R"))
240 (and include-diff '("-p"))
241 ;; "--"
242 path)))
243 (save-excursion
244 (delete-matching-lines
245 "^-----------------------------------------------$")
246 t)))))
248 (defun vc-got--status (status-codes dir-or-file &optional files)
249 "Return a list of lists (FILE STATUS STAGE-STATUS).
250 DIR-OR-FILE can be either a directory or a file. If FILES is
251 given, return the status of those files, otherwise the status of
252 DIR-OR-FILE. STATUS-CODES is either nil, or a string that's
253 passed as the -s flag to got status to limit the types of status
254 to report (e.g. \"CD\" to report only conflicts and deleted
255 files)."
256 (with-temp-buffer
257 (let* ((default-directory (expand-file-name
258 (if (file-directory-p dir-or-file)
259 dir-or-file
260 (file-name-directory dir-or-file))))
261 (root (vc-got-root default-directory))
262 (process-file-side-effects))
263 (when (zerop (vc-got--call "status"
264 (and status-codes (list "-s" status-codes))
265 "--"
266 (or files dir-or-file)))
267 (goto-char (point-min))
268 (cl-loop until (eobp)
269 collect (vc-got--parse-status-line root)
270 do (forward-line))))))
272 (defun vc-got--parse-status-line (root)
273 "Parse a line of the the output of status.
274 ROOT is the root of the repo."
275 ;; the format of each line is
276 ;; <status-char> <stage-char> <spc> <filename> \n
277 (let* ((file-status (prog1 (vc-got--parse-status-char
278 (char-after))
279 (forward-char)))
280 (stage-status (let* ((c (char-after)))
281 (prog1
282 (when (member c '(?M ?A ?D))
283 c)
284 (forward-char))))
285 (filename (progn
286 (forward-char)
287 (buffer-substring (point)
288 (line-end-position)))))
289 (list (file-relative-name (expand-file-name filename root)
290 default-directory)
291 (or file-status (and stage-status 'up-to-date))
292 stage-status)))
294 (defun vc-got--parse-status-char (c)
295 "Parse status char C into a symbol accepted by `vc-state'."
296 (cl-case c
297 (?M 'edited)
298 (?A 'added)
299 (?D 'removed)
300 (?C 'conflict)
301 (?! 'missing)
302 (?~ 'edited) ; XXX: what does it means for a file to be ``obstructed''?
303 (?? 'unregistered)
304 (?m 'edited) ; modified file modes
305 (?N nil)))
307 (defun vc-got--cat (commit obj-id)
308 "Execute got cat -c COMMIT OBJ-ID in the current buffer."
309 (let (process-file-side-effects)
310 (zerop (vc-got--call "cat" "-c" commit obj-id))))
312 (defun vc-got--revert (&rest files)
313 "Execute got revert FILES."
314 (vc-got-with-worktree (car files)
315 (with-temp-buffer
316 (zerop (vc-got--call "revert" "--" files)))))
318 (defun vc-got--list-branches ()
319 "Return an alist of (branch . commit)."
320 (let (process-file-side-effects)
321 (with-temp-buffer
322 (when (zerop (vc-got--call "branch" "-l"))
323 (let (alist)
324 (goto-char (point-min))
325 (while (re-search-forward "^\\*?[[:space:]]+\\(.+\\): \\([[:word:]]+\\)$"
326 nil t)
327 (push (cons (match-string 1) (match-string 2)) alist))
328 alist)))))
330 (defun vc-got--current-branch ()
331 "Return the current branch."
332 (let (process-file-side-effects)
333 (with-temp-buffer
334 (when (zerop (vc-got--call "branch"))
335 (string-trim (buffer-string) "" "\n")))))
337 (defun vc-got--integrate (branch)
338 "Integrate BRANCH into the current one."
339 (with-temp-buffer
340 (zerop (vc-got--call "integrate" branch))))
342 (defun vc-got--update (branch &optional paths)
343 "Update to a different commit or BRANCH.
344 Optionally restrict the update operation to files at or within
345 the specified PATHS."
346 (with-temp-buffer
347 (unless (zerop (vc-got--call "update" "-b" branch "--" paths))
348 (error "[vc-got] can't update to branch %s: %s"
349 branch
350 (buffer-string)))))
352 (defun vc-got--diff-files (files)
353 "Compute the local modifications to FILES."
354 (let (process-file-side-effects)
355 (zerop (vc-got--call "diff" (vc-switches 'got 'diff) "-P" "--"
356 files))))
358 (defun vc-got--diff-objects (obj1 obj2)
359 "Diff the two objects OBJ1 and OBJ2.
360 OBJ1 and OBJ2 are interpreted as a reference, tag name, or an
361 object ID SHA1 hash."
362 (let (process-file-side-effects)
363 (zerop (vc-got--call "diff" (vc-switches 'got 'diff) "--" obj1 obj2))))
365 (defun vc-got--unstage (file-or-directory)
366 "Unstage all the staged hunks at or within FILE-OR-DIRECTORY.
367 If it's nil, unstage every staged changes across the entire work
368 tree."
369 (zerop (vc-got--call "unstage" "--" file-or-directory)))
371 (defun vc-got--remove (file &optional force keep-local)
372 "Use got to remove FILE.
373 If FORCE is non-nil perform the operation even if a file contains
374 local modification. If KEEP-LOCAL is non-nil keep the affected
375 files on disk."
376 (vc-got-with-worktree (or file default-directory)
377 (with-temp-buffer
378 (zerop (vc-got--call "remove"
379 (and force "-f")
380 (and keep-local "-k")
381 "--"
382 file)))))
384 (defun vc-got--ref ()
385 "Return a list of all references."
386 (let ((process-file-side-effects nil)
387 (re "^refs/\\(heads\\|remotes\\|tags\\)/\\(.*\\):")
388 ;; hardcoding HEAD because it's always present and the regexp
389 ;; won't match it.
390 (table (list "HEAD")))
391 (vc-got-with-worktree default-directory
392 (with-temp-buffer
393 (when (zerop (vc-got--call "ref" "-l"))
394 (goto-char (point-min))
395 (while (re-search-forward re nil t)
396 (push (match-string 2) table))
397 table)))))
399 (defun vc-got--branch (name)
400 "Try to create and switch to the branch called NAME."
401 (let (process-file-side-effects)
402 (vc-got-with-worktree default-directory
403 (with-temp-buffer
404 (or (zerop (vc-got--call "branch" "--" name))
405 (error "[vc-got] can't create branch %s: %s" name
406 (buffer-string)))))))
409 ;; Backend properties
411 (defun vc-got-revision-granularity ()
412 "Got has REPOSITORY granularity."
413 'repository)
415 (defun vc-got-update-on-retrieve-tag ()
416 "Like vc-git, vc-got don't need to buffers on `retrieve-tag'."
417 nil)
420 ;; State-querying functions
422 ;;;###autoload (defun vc-got-registered (file)
423 ;;;###autoload "Return non-nil if FILE is registered with got."
424 ;;;###autoload (when (vc-find-root file ".got")
425 ;;;###autoload (load "vc-got" nil t)
426 ;;;###autoload (vc-got-registered file)))
428 (defun vc-got-registered (file)
429 "Return non-nil if FILE is registered with got."
430 (if (file-directory-p file)
431 nil ; got doesn't track directories
432 (when (vc-find-root file ".got")
433 (let ((s (vc-got-state file)))
434 (not (memq s '(nil unregistered)))))))
436 (defun vc-got-state (file)
437 "Return the current version control state of FILE. See `vc-state'."
438 (unless (file-directory-p file)
439 (let (process-file-side-effects)
440 ;; Manually calling got status and checking the result inline to
441 ;; avoid building the data structure in vc-got--status.
442 (with-temp-buffer
443 (when (zerop (vc-got--call "status" "--" file))
444 (goto-char (point-min))
445 (if (eobp)
446 'up-to-date
447 (vc-got--parse-status-char (char-after))))))))
449 (defun vc-got--dir-filter-files (files)
450 "Remove ., .. and .got from FILES."
451 (cl-loop for file in files
452 unless (or (string= file "..")
453 (string= file ".")
454 (string= file ".got"))
455 collect file))
457 (defun vc-got-dir-status-files (dir files update-function)
458 "Build the status for FILES in DIR.
459 The builded result is given to the callback UPDATE-FUNCTION. If
460 FILES is nil, consider all the files in DIR."
461 (let* ((fs (vc-got--dir-filter-files (or files (directory-files dir))))
462 ;; XXX: we call with files, wich will probably be nil on the
463 ;; first run, so we catch deleted, missing and edited files
464 ;; in subdirectories.
465 (res (vc-got--status nil dir files))
466 double-check)
467 (cl-loop for file in fs
468 do (when (and (not (cdr (assoc file res #'string=)))
469 (not (file-directory-p file))
470 ;; if file doesn't exists, it's a
471 ;; untracked file that was removed.
472 (file-exists-p file))
473 ;; if we don't know the status of a file here, it's
474 ;; either up-to-date or ignored. Save it for a
475 ;; double check
476 (push file double-check)))
477 (cl-loop with statuses = (vc-got--status nil dir double-check)
478 for file in double-check
479 unless (eq 'unregistered (cadr (assoc file statuses #'string=)))
480 do (push (list file 'up-to-date nil) res))
481 (funcall update-function res nil)))
483 (defun vc-got-dir-extra-headers (dir)
484 "Return a string for the `vc-dir' buffer heading for directory DIR."
485 (let ((remote (vc-got-repository-url dir)))
486 (concat (propertize "Repository : " 'face 'font-lock-type-face)
487 (vc-got--repo-root) "\n"
488 (when remote
489 (concat
490 (propertize "Remote URL : " 'face 'font-lock-type-face)
491 (vc-got-repository-url dir) "\n"))
492 (propertize "Branch : " 'face 'font-lock-type-face)
493 (vc-got--current-branch))))
495 (defun vc-got-dir-printer (info)
496 "Pretty-printer for the vc-dir-fileinfo structure INFO."
497 (let* ((isdir (vc-dir-fileinfo->directory info))
498 (state (if isdir "" (vc-dir-fileinfo->state info)))
499 (stage-state (vc-dir-fileinfo->extra info))
500 (filename (vc-dir-fileinfo->name info)))
501 (insert
502 " "
503 (propertize
504 (format "%c" (if (vc-dir-fileinfo->marked info) ?* ? ))
505 'face 'font-lock-type-face)
506 " "
507 (propertize
508 (format "%-12s" state)
509 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
510 ((memq state '(missing conflict)) 'font-lock-warning-face)
511 ((eq state 'edited) 'font-lock-constant-face)
512 (t 'font-lock-variable-name-face))
513 'mouse-face 'highlight
514 'keymap (vc-got--with-emacs-version<= "28.0.50"
515 vc-dir-status-mouse-map))
516 " " (propertize
517 (if stage-state
518 (format "%c" stage-state)
519 " ")
520 'face (cond ((memq stage-state '(?A ?E)) 'font-lock-constant-face)
521 ((eq stage-state ?R) 'font-lock-warning-face)
522 (t 'font-lock-variable-name-face)))
523 " "
524 (propertize filename
525 'face (if isdir 'font-lock-comment-delimiter-face
526 'font-lock-function-name-face)
527 'help-echo
528 (if isdir
529 (concat
530 "Directory\n"
531 "VC operations can be applied to it\n"
532 "mouse-3: Pop-up menu")
533 "File\nmouse-3: Pop-up menu")
534 'mouse-face 'highlight
535 'keymap vc-dir-filename-mouse-map))))
537 (defun vc-got-working-revision (file)
538 "Return the last commit that touched FILE or \"0\" if it's newly added."
539 (or
540 (with-temp-buffer
541 (when (vc-got--log file 1)
542 (let (start)
543 (goto-char (point-min))
544 (forward-word) ; skip "commit"
545 (forward-char) ; skip the space
546 (setq start (point)) ; store start of the SHA
547 (forward-word) ; goto SHA end
548 (buffer-substring start (point)))))
549 ;; special case: if this file is added but has no previous commits
550 ;; touching it, got log will fail (as expected), but we have to
551 ;; return "0".
552 (when (eq (vc-got-state file) 'added)
553 "0")))
555 (defun vc-got-checkout-model (_files)
556 "Return the checkout model.
557 Got uses an implicit checkout model for every file."
558 'implicit)
560 (defun vc-got-mode-line-string (file)
561 "Return the VC mode line string for FILE."
562 (vc-got-with-worktree file
563 (let ((def (vc-default-mode-line-string 'Got file)))
564 (concat (substring def 0 4) (vc-got--current-branch)))))
567 ;; state-changing functions
569 (defun vc-got-create-repo (_backend)
570 "Create an empty repository in the current directory."
571 (error "[vc-got] create-repo not implemented"))
573 (defun vc-got-register (files &optional _comment)
574 "Register FILES, passing `vc-register-switches' to the backend command."
575 (vc-got--add files))
577 (defalias 'vc-got-responsible-p #'vc-got-root)
579 (defun vc-got-unregister (file)
580 "Unregister FILE."
581 (vc-got--remove file t t))
583 (defun vc-got-checkin (files comment &optional _rev)
584 "Commit FILES with COMMENT as commit message."
585 (with-temp-buffer
586 (unless (zerop (vc-got--call "commit" "-m"
587 (log-edit-extract-headers nil comment)
588 "--"
589 files))
590 (error "[vc-got] can't commit: %s" (buffer-string)))))
592 (defun vc-got-find-revision (file rev buffer)
593 "Fill BUFFER with the content of FILE in the given revision REV."
594 (with-current-buffer buffer
595 (vc-got-with-worktree file
596 (vc-got--cat rev (file-relative-name file)))))
598 (defun vc-got-checkout (_file &optional _rev)
599 "Checkout revision REV of FILE.
600 If REV is t, checkout from the head."
601 (error "[vc-got] checkout not implemented"))
603 (defun vc-got-revert (file &optional _content-done)
604 "Revert FILE back to working revision."
605 (vc-got--revert file))
607 (defun vc-got-merge-branch ()
608 "Prompt for a branch and integrate it into the current one."
609 ;; XXX: be smart and try to "got rebase" if "got integrate" fails?
610 (let* ((branches (cl-loop for (branch . commit) in (vc-got--list-branches)
611 collect branch))
612 (branch (completing-read "Merge from branch: " branches)))
613 (when branch
614 (vc-got--integrate branch))))
616 (defun vc-got--proc-filter (proc s)
617 "Custom output filter for async process PROC.
618 It's like `vc-process-filter' but supports \r inside S."
619 (let ((buffer (process-buffer proc)))
620 (when (buffer-live-p buffer)
621 (with-current-buffer buffer
622 (save-excursion
623 (let ((buffer-undo-list t)
624 (inhibit-read-only t))
625 (goto-char (process-mark proc))
626 (if (not (string-match ".*\r\\(.*\\)" s))
627 (insert s)
628 ;; handle \r
629 (end-of-line)
630 (let ((end (point)))
631 (beginning-of-line)
632 (delete-region (point) end))
633 (insert (match-string 1 s)))
634 (set-marker (process-mark proc) (point))))))))
636 (defun vc-got--push-pull (cmd op prompt)
637 "Execute CMD OP, or prompt the user if PROMPT is non-nil."
638 (let ((buffer (format "*vc-got : %s*" (expand-file-name default-directory))))
639 (when-let (cmd (if prompt
640 (split-string
641 (read-shell-command (format "%s %s command: " cmd op)
642 (format "%s %s " cmd op))
643 " " t)
644 (list cmd op)))
645 (apply #'vc-do-async-command buffer default-directory cmd)
646 (with-current-buffer buffer
647 (vc-compilation-mode 'got)
648 (let ((comp-cmd (mapconcat #'identity cmd " "))
649 (proc (get-buffer-process buffer)))
650 (setq-local compile-command comp-cmd)
651 (setq-local compilation-directory default-directory)
652 (setq-local compilation-arguments (list comp-cmd
653 nil
654 (lambda (_ign) buffer)
655 nil))
656 ;; Setup a custom process filter that handles \r.
657 (set-process-filter proc #'vc-got--proc-filter)))
658 (vc-set-async-update buffer))))
660 ;; TODO: this could be expanded. After a pull the worktree needs to
661 ;; be updated, either with a ``got update -b branch-name'' or ``got
662 ;; update -b remote/branchname'' plus a rebase.
663 (defun vc-got-pull (prompt)
664 "Execute a fetch prompting for the full command if PROMPT is not nil."
665 (vc-got--push-pull vc-got-program "fetch" prompt))
667 (defun vc-got-push (prompt)
668 "Execute a send prompting for the full command if PROMPT is not nil."
669 (vc-got--push-pull vc-got-program "send" prompt))
672 ;; History functions
674 (defun vc-got-print-log (files buffer &optional _shortlog start-revision limit)
675 "Insert the revision log for FILES into BUFFER.
676 LIMIT limits the number of commits, optionally starting at
677 START-REVISION."
678 (with-current-buffer buffer
679 ;; the *vc-diff* may be read only
680 (let ((inhibit-read-only t))
681 (cl-loop for file in files
682 do (vc-got--log (file-relative-name file)
683 limit
684 start-revision)))))
686 (defun vc-got-log-outgoing (buffer remote-location)
687 "Fill BUFFER with the diff between the local worktree branch and REMOTE-LOCATION."
688 (vc-setup-buffer buffer)
689 (let ((rl (vc-got-next-revision
690 nil
691 (if (or (not remote-location) (string-empty-p remote-location))
692 (concat "origin/" (vc-got--current-branch))
693 remote-location)))
694 (inhibit-read-only t))
695 (with-current-buffer buffer
696 (vc-got--log nil nil nil rl))))
698 (defun vc-got-incoming (buffer remote-location)
699 "Fill BUFFER with the incoming diff from REMOTE-LOCATION.
700 That is, the diff between REMOTE-LOCATION and the local repository."
701 (let ((rl (if (or (not remote-location) (string-empty-p remote-location))
702 (concat "origin/" (vc-got--current-branch))
703 remote-location))
704 (inhibit-read-only t))
705 (with-current-buffer buffer
706 (vc-got--log nil nil (vc-got--current-branch) rl))))
708 (defun vc-got-log-search (buffer pattern)
709 "Search commits for PATTERN and write the results found in BUFFER."
710 (with-current-buffer buffer
711 (let ((inhibit-read-only t))
712 (vc-got--log nil nil nil nil pattern))))
714 (define-derived-mode vc-got-log-view-mode log-view-mode "Got-Log-View"
715 "Got-specific log-view mode.
716 Heavily inspired by `vc-git-log-view-mode'."
717 (require 'add-log)
718 (setq-local log-view-file-re regexp-unmatchable)
719 (setq-local log-view-per-file-logs nil)
720 (setq-local log-view-message-re "^commit +\\([0-9a-z]+\\)")
721 (setq-local log-view-font-lock-keywords
722 (append
723 `((,log-view-message-re (1 'change-log-acknowledgment)))
724 ;; Handle the case:
725 ;; user: foo@bar
726 '(("^from: \\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
727 (1 'change-log-email))
728 ;; Handle the case:
729 ;; user: FirstName LastName <foo@bar>
730 ("^from: \\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
731 (1 'change-log-name)
732 (2 'change-log-email))
733 ("^date: \\(.+\\)" (1 'change-log-date))))))
735 ;; TODO: async
736 ;; TODO: return 0 or 1
737 (defun vc-got-diff (files &optional rev1 rev2 buffer _async)
738 "Insert into BUFFER (or *vc-diff*) the diff for FILES from REV1 to REV2."
739 (let* ((buffer (get-buffer-create (or buffer "*vc-diff*")))
740 (inhibit-read-only t))
741 (with-current-buffer buffer
742 (vc-got-with-worktree (or (car files)
743 default-directory)
744 (cond ((and (null rev1)
745 (null rev2))
746 (vc-got--diff-files files))
747 ((and (null rev1)
748 rev2)
749 ;; TODO: this includes the whole diff while to respect
750 ;; the vc semantics we should filter only the diff for
751 ;; files in FILES.
752 ;;
753 ;; XXX: this includes also the commit message, I
754 ;; consider it a feature over the usual vc behaviour of
755 ;; showing only the diff.
756 (vc-got--log nil 1 rev2 nil nil nil t))
757 ;;
758 ;; TODO: if rev1 is nil, diff from the current version until
759 ;; rev2.
760 ;;
761 ;; TODO 2: if rev2 is nil as well, diff against an empty
762 ;; tree (i.e. get the patch from `got log -p rev1')
763 (t (vc-got--diff-objects rev1 rev2)))))))
765 (defun vc-got-revision-completion-table (_files)
766 "Return a completion table for existing revisions.
767 Ignores FILES because GoT doesn't have the concept of ``file
768 revisions''; instead, like with git, you have tags and branches."
769 (letrec ((table (lazy-completion-table
770 table (lambda () (vc-got--ref)))))
771 table))
773 (defun vc-got-annotate-command (file buf &optional rev)
774 "Show annotated contents of FILE in buffer BUF. If given, use revision REV."
775 (let (process-file-side-effects)
776 (with-current-buffer buf
777 ;; FIXME: vc-ensure-vc-buffer won't recognise this buffer as managed
778 ;; by got unless vc-parent-buffer points to a buffer managed by got.
779 ;; investigate why this is needed.
780 (setq-local vc-parent-buffer (find-file-noselect file))
781 (vc-got--call "blame"
782 (when rev (list "-c" rev))
783 "--"
784 file))))
786 (defconst vc-got--annotate-re
787 (concat "^[0-9]\\{1,\\}) " ; line number followed by )
788 "\\([a-z0-9]+\\) " ; SHA-1 of commit
789 "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\) " ; year-mm-dd
790 "\\([^ ]\\)+ ") ; author
791 "Regexp to match annotation output lines.
792 Provides capture groups for:
793 1. revision id
794 2. date of commit
795 3. author of commit")
797 (defconst vc-got--commit-re "^commit \\([a-z0-9]+\\)"
798 "Regexp to match commit lines.
799 Provides capture group for the commit revision id.")
801 (defun vc-got-annotate-time ()
802 "Return the time of the next line of annotation at or after point.
803 Value is returned as floating point fractional number of days."
804 ;; XXX: to behave like vc-git here we should call re-search-forward
805 ;; instead of looking-at, as it makes the fontification of the line
806 ;; start AFTER the info. The problem is, due to the format of the
807 ;; blame, it produces an ugly result, with colors starting at
808 ;; different offsets depending on how long the committer name is.
809 (when (looking-at vc-got--annotate-re)
810 (let ((str (match-string-no-properties 2)))
811 (vc-annotate-convert-time
812 (encode-time 0 0 0
813 (string-to-number (substring str 8 10))
814 (string-to-number (substring str 5 7))
815 (string-to-number (substring str 0 4)))))))
817 (defun vc-got-annotate-extract-revision-at-line ()
818 "Return revision corresponding to the current line or nil."
819 (save-excursion
820 (beginning-of-line)
821 (when (looking-at vc-got--annotate-re)
822 (match-string-no-properties 1))))
825 ;; Tag system
827 (defun vc-got--tag-callback (tag)
828 "`log-edit' callback for `vc-got-create-tag'.
829 Creates the TAG using the content of the current buffer."
830 (interactive)
831 (let ((msg (buffer-substring-no-properties (point-min)
832 (point-max))))
833 (with-temp-buffer
834 (unless (zerop (vc-got--call "tag"
835 "-m"
836 (log-edit-extract-headers nil msg)
837 "--"
838 tag))
839 (error "[vc-got] can't create tag %s: %s" tag (buffer-string))))))
841 (defun vc-got-create-tag (_dir name branchp)
842 "Attach the tag NAME to the state of the worktree.
843 DIR is ignored (tags are global, not per-file). If BRANCHP is
844 true, NAME should create a new branch otherwise it will pop-up a
845 `log-edit' buffer to provide the tag message."
846 ;; TODO: vc reccomends to ensure that all the file are in a clean
847 ;; state, but is it useful?
848 (if branchp
849 (vc-got--branch name)
850 (let ((buf (get-buffer-create "*vc-got tag*")))
851 (with-current-buffer buf
852 (erase-buffer)
853 (save-excursion
854 (insert "Summary: tag " name "\n\n"))
855 (move-end-of-line 1)
856 (switch-to-buffer buf)
857 (log-edit (lambda ()
858 (interactive)
859 (unwind-protect
860 (vc-got--tag-callback name)
861 (kill-buffer buf))))))))
863 (defun vc-got-retrieve-tag (dir name _update)
864 "Switch to the tag NAME for files at or below DIR."
865 (let ((default-directory dir))
866 (vc-got--update name dir)))
869 ;; Miscellaneous
871 (defun vc-got-find-ignore-file (file)
872 "Return the gitignore file that controls FILE."
873 (expand-file-name ".gitignore"
874 (vc-got-root file)))
876 (defun vc-got-previous-revision (file rev)
877 "Return the revision number that precedes REV for FILE or nil."
878 (with-temp-buffer
879 (vc-got--log file 2 rev nil nil t)
880 (goto-char (point-min))
881 (keep-lines "^commit")
882 (when (looking-at vc-got--commit-re)
883 (match-string-no-properties 1))))
885 (defun vc-got-next-revision (file rev)
886 "Return the revision number that follows REV for FILE or nil."
887 (with-temp-buffer
888 (vc-got--log file nil nil rev)
889 (keep-lines "^commit" (point-min) (point-max))
890 (goto-char (point-max))
891 (forward-line -1) ; return from empty line to last actual commit
892 (unless (= (point) (point-min))
893 (forward-line -1)
894 (when (looking-at vc-got--commit-re)
895 (match-string-no-properties 1)))))
897 (defun vc-got-delete-file (file)
898 "Delete FILE locally and mark it deleted in work tree."
899 (vc-got--remove file t))
901 (defun vc-got-find-file-hook ()
902 "Activate `smerge-mode' if there is a conflict."
903 ;; just like vc-git-find-file-hook
904 (when (and buffer-file-name
905 (eq (vc-state buffer-file-name 'Got) 'conflict)
906 (save-excursion
907 (goto-char (point-min))
908 (re-search-forward "^<<<<<<< " nil 'noerror)))
909 (smerge-start-session)
910 (vc-message-unresolved-conflicts buffer-file-name)))
912 (defun vc-got-conflicted-files (dir)
913 "Return the list of files with conflicts in directory DIR."
914 (let* ((root (vc-got-root dir))
915 (default-directory root)
916 (process-file-side-effects nil))
917 (cl-loop for (file status _) in (vc-got--status "C" ".")
918 when (and (eq status 'conflict)
919 (file-in-directory-p file dir))
920 collect file)))
922 (defun vc-got-repository-url (_file &optional remote-name)
923 "Return URL for REMOTE-NAME, or for \"origin\" if nil."
924 (let* ((default-directory (vc-got--repo-root))
925 (remote-name (or remote-name "origin"))
926 (heading (concat "[remote \"" remote-name "\"]"))
927 (conf (cond ((file-exists-p ".git/config") ".git/config")
928 ((file-exists-p ".git") nil)
929 ((file-exists-p "config") "config")))
930 found)
931 (when conf
932 (with-temp-buffer
933 (insert-file-contents conf)
934 (goto-char (point-min))
935 (when (search-forward heading nil t)
936 (forward-line)
937 (while (and (not found)
938 (looking-at ".*=") ; too broad?
939 (not (= (point) (point-max))))
940 (when (looking-at ".*url = \\(.*\\)")
941 (setq found (match-string-no-properties 1)))
942 (forward-line))
943 found)))))
947 ;; Automatically register the backend and add ".got" to the exclusion
948 ;; list.
950 ;;;###autoload
951 (add-to-list 'vc-handled-backends 'Got)
953 ;;;###autoload
954 (add-to-list 'vc-directory-exclusion-list ".got")
956 (provide 'vc-got)
957 ;;; vc-got.el ends here