Blob


1 ;;; vc-got.el --- VC backend for Game of Trees VCS -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2021 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
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 (if (zerop (vc-got--call "branch" "--" name))
406 (error "[vc-got] can't create branch %s: %s" name
407 (buffer-string)))))))
410 ;; Backend properties
412 (defun vc-got-revision-granularity ()
413 "Got has REPOSITORY granularity."
414 'repository)
416 (defun vc-got-update-on-retrieve-tag ()
417 "Like vc-git, vc-got don't need to buffers on `retrieve-tag'."
418 nil)
421 ;; State-querying functions
423 ;;;###autoload (defun vc-got-registered (file)
424 ;;;###autoload "Return non-nil if FILE is registered with got."
425 ;;;###autoload (when (vc-find-root file ".got")
426 ;;;###autoload (load "vc-got" nil t)
427 ;;;###autoload (vc-got-registered file)))
429 (defun vc-got-registered (file)
430 "Return non-nil if FILE is registered with got."
431 (if (file-directory-p file)
432 nil ; got doesn't track directories
433 (when (vc-find-root file ".got")
434 (let ((s (vc-got-state file)))
435 (not (or (eq s 'unregistered)
436 (null s)))))))
438 (defun vc-got-state (file)
439 "Return the current version control state of FILE. See `vc-state'."
440 (unless (file-directory-p file)
441 (let (process-file-side-effects)
442 ;; Manually calling got status and checking the result inline to
443 ;; avoid building the data structure in vc-got--status.
444 (with-temp-buffer
445 (when (zerop (vc-got--call "status" "--" file))
446 (goto-char (point-min))
447 (if (eobp)
448 'up-to-date
449 (vc-got--parse-status-char (char-after))))))))
451 (defun vc-got--dir-filter-files (files)
452 "Remove ., .. and .got from FILES."
453 (cl-loop for file in files
454 unless (or (string= file "..")
455 (string= file ".")
456 (string= file ".got"))
457 collect file))
459 (defun vc-got-dir-status-files (dir files update-function)
460 "Build the status for FILES in DIR.
461 The builded result is given to the callback UPDATE-FUNCTION. If
462 FILES is nil, consider all the files in DIR."
463 (let* ((fs (vc-got--dir-filter-files (or files (directory-files dir))))
464 ;; XXX: we call with files, wich will probably be nil on the
465 ;; first run, so we catch deleted, missing and edited files
466 ;; in subdirectories.
467 (res (vc-got--status nil dir files))
468 double-check)
469 (cl-loop for file in fs
470 do (when (and (not (cdr (assoc file res #'string=)))
471 (not (file-directory-p file))
472 ;; if file doesn't exists, it's a
473 ;; untracked file that was removed.
474 (file-exists-p file))
475 ;; if we don't know the status of a file here, it's
476 ;; either up-to-date or ignored. Save it for a
477 ;; double check
478 (push file double-check)))
479 (cl-loop with statuses = (vc-got--status nil dir double-check)
480 for file in double-check
481 unless (eq 'unregistered (cadr (assoc file statuses #'string=)))
482 do (push (list file 'up-to-date nil) res))
483 (funcall update-function res nil)))
485 (defun vc-got-dir-extra-headers (dir)
486 "Return a string for the `vc-dir' buffer heading for directory DIR."
487 (let ((remote (vc-got-repository-url dir)))
488 (concat (propertize "Repository : " 'face 'font-lock-type-face)
489 (vc-got--repo-root) "\n"
490 (when remote
491 (concat
492 (propertize "Remote URL : " 'face 'font-lock-type-face)
493 (vc-got-repository-url dir) "\n"))
494 (propertize "Branch : " 'face 'font-lock-type-face)
495 (vc-got--current-branch))))
497 (defun vc-got-dir-printer (info)
498 "Pretty-printer for the vc-dir-fileinfo structure INFO."
499 (let* ((isdir (vc-dir-fileinfo->directory info))
500 (state (if isdir "" (vc-dir-fileinfo->state info)))
501 (stage-state (vc-dir-fileinfo->extra info))
502 (filename (vc-dir-fileinfo->name info)))
503 (insert
504 " "
505 (propertize
506 (format "%c" (if (vc-dir-fileinfo->marked info) ?* ? ))
507 'face 'font-lock-type-face)
508 " "
509 (propertize
510 (format "%-12s" state)
511 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
512 ((memq state '(missing conflict)) 'font-lock-warning-face)
513 ((eq state 'edited) 'font-lock-constant-face)
514 (t 'font-lock-variable-name-face))
515 'mouse-face 'highlight
516 'keymap (vc-got--with-emacs-version<= "28.0.50"
517 vc-dir-status-mouse-map))
518 " " (propertize
519 (if stage-state
520 (format "%c" stage-state)
521 " ")
522 'face (cond ((memq stage-state '(?A ?E)) 'font-lock-constant-face)
523 ((eq stage-state ?R) 'font-lock-warning-face)
524 (t 'font-lock-variable-name-face)))
525 " "
526 (propertize filename
527 'face (if isdir 'font-lock-comment-delimiter-face
528 'font-lock-function-name-face)
529 'help-echo
530 (if isdir
531 (concat
532 "Directory\n"
533 "VC operations can be applied to it\n"
534 "mouse-3: Pop-up menu")
535 "File\nmouse-3: Pop-up menu")
536 'mouse-face 'highlight
537 'keymap vc-dir-filename-mouse-map))))
539 (defun vc-got-working-revision (file)
540 "Return the last commit that touched FILE or \"0\" if it's newly added."
541 (or
542 (with-temp-buffer
543 (when (vc-got--log file 1)
544 (let (start)
545 (goto-char (point-min))
546 (forward-word) ; skip "commit"
547 (forward-char) ; skip the space
548 (setq start (point)) ; store start of the SHA
549 (forward-word) ; goto SHA end
550 (buffer-substring start (point)))))
551 ;; special case: if this file is added but has no previous commits
552 ;; touching it, got log will fail (as expected), but we have to
553 ;; return "0".
554 (when (eq (vc-got-state file) 'added)
555 "0")))
557 (defun vc-got-checkout-model (_files)
558 "Return the checkout model.
559 Got uses an implicit checkout model for every file."
560 'implicit)
562 (defun vc-got-mode-line-string (file)
563 "Return the VC mode line string for FILE."
564 (vc-got-with-worktree file
565 (let ((def (vc-default-mode-line-string 'Got file)))
566 (concat (substring def 0 4) (vc-got--current-branch)))))
569 ;; state-changing functions
571 (defun vc-got-create-repo (_backend)
572 "Create an empty repository in the current directory."
573 (error "[vc-got] create-repo not implemented"))
575 (defun vc-got-register (files &optional _comment)
576 "Register FILES, passing `vc-register-switches' to the backend command."
577 (vc-got--add files))
579 (defalias 'vc-got-responsible-p #'vc-got-root)
581 (defun vc-got-unregister (file)
582 "Unregister FILE."
583 (vc-got--remove file t t))
585 (defun vc-got-checkin (files comment &optional _rev)
586 "Commit FILES with COMMENT as commit message."
587 (with-temp-buffer
588 (unless (zerop (vc-got--call "commit" "-m"
589 (log-edit-extract-headers nil comment)
590 "--"
591 files))
592 (error "[vc-got] can't commit: %s" (buffer-string)))))
594 (defun vc-got-find-revision (file rev buffer)
595 "Fill BUFFER with the content of FILE in the given revision REV."
596 (with-current-buffer buffer
597 (vc-got-with-worktree file
598 (vc-got--cat rev (file-relative-name file)))))
600 (defun vc-got-checkout (_file &optional _rev)
601 "Checkout revision REV of FILE.
602 If REV is t, checkout from the head."
603 (error "[vc-got] checkout not implemented"))
605 (defun vc-got-revert (file &optional _content-done)
606 "Revert FILE back to working revision."
607 (vc-got--revert file))
609 (defun vc-got-merge-branch ()
610 "Prompt for a branch and integrate it into the current one."
611 ;; XXX: be smart and try to "got rebase" if "got integrate" fails?
612 (let* ((branches (cl-loop for (branch . commit) in (vc-got--list-branches)
613 collect branch))
614 (branch (completing-read "Merge from branch: " branches)))
615 (when branch
616 (vc-got--integrate branch))))
618 (defun vc-got--proc-filter (proc s)
619 "Custom output filter for async process PROC.
620 It's like `vc-process-filter' but supports \r inside S."
621 (let ((buffer (process-buffer proc)))
622 (when (buffer-live-p buffer)
623 (with-current-buffer buffer
624 (save-excursion
625 (let ((buffer-undo-list t)
626 (inhibit-read-only t))
627 (goto-char (process-mark proc))
628 (if (not (string-match ".*\r\\(.*\\)" s))
629 (insert s)
630 ;; handle \r
631 (end-of-line)
632 (let ((end (point)))
633 (beginning-of-line)
634 (delete-region (point) end))
635 (insert (match-string 1 s)))
636 (set-marker (process-mark proc) (point))))))))
638 (defun vc-got--push-pull (cmd op prompt)
639 "Execute CMD OP, or prompt the user if PROMPT is non-nil."
640 (let ((buffer (format "*vc-got : %s*" (expand-file-name default-directory))))
641 (when-let (cmd (if prompt
642 (split-string
643 (read-shell-command (format "%s %s command: " cmd op)
644 (format "%s %s " cmd op))
645 " " t)
646 (list cmd op)))
647 (apply #'vc-do-async-command buffer default-directory cmd)
648 (with-current-buffer buffer
649 (vc-compilation-mode 'got)
650 (let ((comp-cmd (mapconcat #'identity cmd " "))
651 (proc (get-buffer-process buffer)))
652 (setq-local compile-command comp-cmd)
653 (setq-local compilation-directory default-directory)
654 (setq-local compilation-arguments (list comp-cmd
655 nil
656 (lambda (_ign) buffer)
657 nil))
658 ;; Setup a custom process filter that handles \r.
659 (set-process-filter proc #'vc-got--proc-filter)))
660 (vc-set-async-update buffer))))
662 ;; TODO: this could be expanded. After a pull the worktree needs to
663 ;; be updated, either with a ``got update -b branch-name'' or ``got
664 ;; update -b remote/branchname'' plus a rebase.
665 (defun vc-got-pull (prompt)
666 "Execute a pull prompting for the full command if PROMPT is not nil."
667 (vc-got--push-pull vc-got-program "fetch" prompt))
669 (defun vc-got-push (prompt)
670 "Run git push (not got!) in the repository dir.
671 If PROMPT is non-nil, prompt for the git command to run."
672 (vc-got--push-pull vc-got-program "send" prompt))
675 ;; History functions
677 (defun vc-got-print-log (files buffer &optional _shortlog start-revision limit)
678 "Insert the revision log for FILES into BUFFER.
679 LIMIT limits the number of commits, optionally starting at
680 START-REVISION."
681 (with-current-buffer buffer
682 ;; the *vc-diff* may be read only
683 (let ((inhibit-read-only t))
684 (cl-loop for file in files
685 do (vc-got--log (file-relative-name file)
686 limit
687 start-revision)))))
689 (defun vc-got-log-outgoing (buffer remote-location)
690 "Fill BUFFER with the diff between the local worktree branch and REMOTE-LOCATION."
691 (vc-setup-buffer buffer)
692 (let ((rl (vc-got-next-revision
693 nil
694 (if (or (not remote-location) (string-empty-p remote-location))
695 (concat "origin/" (vc-got--current-branch))
696 remote-location)))
697 (inhibit-read-only t))
698 (with-current-buffer buffer
699 (vc-got--log nil nil nil rl))))
701 (defun vc-got-incoming (buffer remote-location)
702 "Fill BUFFER with the incoming diff from REMOTE-LOCATION.
703 That is, the diff between REMOTE-LOCATION and the local repository."
704 (let ((rl (if (or (not remote-location) (string-empty-p remote-location))
705 (concat "origin/" (vc-got--current-branch))
706 remote-location))
707 (inhibit-read-only t))
708 (with-current-buffer buffer
709 (vc-got--log nil nil (vc-got--current-branch) rl))))
711 (defun vc-got-log-search (buffer pattern)
712 "Search commits for PATTERN and write the results found in BUFFER."
713 (with-current-buffer buffer
714 (let ((inhibit-read-only t))
715 (vc-got--log nil nil nil nil pattern))))
717 (define-derived-mode vc-got-log-view-mode log-view-mode "Got-Log-View"
718 "Got-specific log-view mode.
719 Heavily inspired by `vc-git-log-view-mode'."
720 (require 'add-log)
721 (setq-local log-view-file-re regexp-unmatchable)
722 (setq-local log-view-per-file-logs nil)
723 (setq-local log-view-message-re "^commit +\\([0-9a-z]+\\)")
724 (setq-local log-view-font-lock-keywords
725 (append
726 `((,log-view-message-re (1 'change-log-acknowledgment)))
727 ;; Handle the case:
728 ;; user: foo@bar
729 '(("^from: \\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
730 (1 'change-log-email))
731 ;; Handle the case:
732 ;; user: FirstName LastName <foo@bar>
733 ("^from: \\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
734 (1 'change-log-name)
735 (2 'change-log-email))
736 ("^date: \\(.+\\)" (1 'change-log-date))))))
738 ;; TODO: async
739 ;; TODO: return 0 or 1
740 (defun vc-got-diff (files &optional rev1 rev2 buffer _async)
741 "Insert into BUFFER (or *vc-diff*) the diff for FILES from REV1 to REV2."
742 (let* ((buffer (get-buffer-create (or buffer "*vc-diff*")))
743 (inhibit-read-only t))
744 (with-current-buffer buffer
745 (vc-got-with-worktree (or (car files)
746 default-directory)
747 (cond ((and (null rev1)
748 (null rev2))
749 (vc-got--diff-files files))
750 ((and (null rev1)
751 rev2)
752 ;; TODO: this includes the whole diff while to respect
753 ;; the vc semantics we should filter only the diff for
754 ;; files in FILES.
755 ;;
756 ;; XXX: this includes also the commit message, I
757 ;; consider it a feature over the usual vc behaviour of
758 ;; showing only the diff.
759 (vc-got--log nil 1 rev2 nil nil nil t))
760 ;;
761 ;; TODO: if rev1 is nil, diff from the current version until
762 ;; rev2.
763 ;;
764 ;; TODO 2: if rev2 is nil as well, diff against an empty
765 ;; tree (i.e. get the patch from `got log -p rev1')
766 (t (vc-got--diff-objects rev1 rev2)))))))
768 (defun vc-got-revision-completion-table (_files)
769 "Return a completion table for existing revisions.
770 Ignores FILES because GoT doesn't have the concept of ``file
771 revisions''; instead, like with git, you have tags and branches."
772 (letrec ((table (lazy-completion-table
773 table (lambda () (vc-got--ref)))))
774 table))
776 (defun vc-got-annotate-command (file buf &optional rev)
777 "Show annotated contents of FILE in buffer BUF. If given, use revision REV."
778 (let (process-file-side-effects)
779 (with-current-buffer buf
780 ;; FIXME: vc-ensure-vc-buffer won't recognise this buffer as managed
781 ;; by got unless vc-parent-buffer points to a buffer managed by got.
782 ;; investigate why this is needed.
783 (setq-local vc-parent-buffer (find-file-noselect file))
784 (vc-got--call "blame"
785 (when rev (list "-c" rev))
786 "--"
787 file))))
789 (defconst vc-got--annotate-re
790 (concat "^[0-9]\\{1,\\}) " ; line number followed by )
791 "\\([a-z0-9]+\\) " ; SHA-1 of commit
792 "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\) " ; year-mm-dd
793 "\\([^ ]\\)+ ") ; author
794 "Regexp to match annotation output lines.
795 Provides capture groups for:
796 1. revision id
797 2. date of commit
798 3. author of commit")
800 (defconst vc-got--commit-re "^commit \\([a-z0-9]+\\)"
801 "Regexp to match commit lines.
802 Provides capture group for the commit revision id.")
804 (defun vc-got-annotate-time ()
805 "Return the time of the next line of annotation at or after point.
806 Value is returned as floating point fractional number of days."
807 ;; XXX: to behave like vc-git here we should call re-search-forward
808 ;; instead of looking-at, as it makes the fontification of the line
809 ;; start AFTER the info. The problem is, due to the format of the
810 ;; blame, it produces an ugly result, with colors starting at
811 ;; different offsets depending on how long the committer name is.
812 (when (looking-at vc-got--annotate-re)
813 (let ((str (match-string-no-properties 2)))
814 (vc-annotate-convert-time
815 (encode-time 0 0 0
816 (string-to-number (substring str 8 10))
817 (string-to-number (substring str 5 7))
818 (string-to-number (substring str 0 4)))))))
820 (defun vc-got-annotate-extract-revision-at-line ()
821 "Return revision corresponding to the current line or nil."
822 (save-excursion
823 (beginning-of-line)
824 (when (looking-at vc-got--annotate-re)
825 (match-string-no-properties 1))))
828 ;; Tag system
830 (defun vc-got--tag-callback (tag)
831 "`log-edit' callback for `vc-got-create-tag'.
832 Creates the TAG using the content of the current buffer."
833 (interactive)
834 (let ((msg (buffer-substring-no-properties (point-min)
835 (point-max))))
836 (with-temp-buffer
837 (unless (zerop (vc-got--call "tag"
838 "-m"
839 (log-edit-extract-headers nil msg)
840 "--"
841 tag))
842 (error "[vc-got] can't create tag %s: %s" tag (buffer-string))))))
844 (defun vc-got-create-tag (_dir name branchp)
845 "Attach the tag NAME to the state of the worktree.
846 DIR is ignored (tags are global, not per-file). If BRANCHP is
847 true, NAME should create a new branch otherwise it will pop-up a
848 `log-edit' buffer to provide the tag message."
849 ;; TODO: vc reccomends to ensure that all the file are in a clean
850 ;; state, but is it useful?
851 (if branchp
852 (vc-got--branch name)
853 (let ((buf (get-buffer-create "*vc-got tag*")))
854 (with-current-buffer buf
855 (erase-buffer)
856 (save-excursion
857 (insert "Summary: tag " name "\n\n"))
858 (move-end-of-line 1)
859 (switch-to-buffer buf)
860 (log-edit (lambda ()
861 (interactive)
862 (unwind-protect
863 (vc-got--tag-callback name)
864 (kill-buffer buf))))))))
866 (defun vc-got-retrieve-tag (dir name _update)
867 "Switch to the tag NAME for files at or below DIR."
868 (let ((default-directory dir))
869 (vc-got--update name dir)))
872 ;; Miscellaneous
874 (defun vc-got-find-ignore-file (file)
875 "Return the gitignore file that controls FILE."
876 (expand-file-name ".gitignore"
877 (vc-got-root file)))
879 (defun vc-got-previous-revision (file rev)
880 "Return the revision number that precedes REV for FILE or nil."
881 (with-temp-buffer
882 (vc-got--log file 2 rev nil nil t)
883 (goto-char (point-min))
884 (keep-lines "^commit")
885 (when (looking-at vc-got--commit-re)
886 (match-string-no-properties 1))))
888 (defun vc-got-next-revision (file rev)
889 "Return the revision number that follows REV for FILE or nil."
890 (with-temp-buffer
891 (vc-got--log file nil nil rev)
892 (keep-lines "^commit" (point-min) (point-max))
893 (goto-char (point-max))
894 (forward-line -1) ; return from empty line to last actual commit
895 (unless (= (point) (point-min))
896 (forward-line -1)
897 (when (looking-at vc-got--commit-re)
898 (match-string-no-properties 1)))))
900 (defun vc-got-delete-file (file)
901 "Delete FILE locally and mark it deleted in work tree."
902 (vc-got--remove file t))
904 (defun vc-got-find-file-hook ()
905 "Activate `smerge-mode' if there is a conflict."
906 ;; just like vc-git-find-file-hook
907 (when (and buffer-file-name
908 (eq (vc-state buffer-file-name 'Got) 'conflict)
909 (save-excursion
910 (goto-char (point-min))
911 (re-search-forward "^<<<<<<< " nil 'noerror)))
912 (smerge-start-session)
913 (vc-message-unresolved-conflicts buffer-file-name)))
915 (defun vc-got-conflicted-files (dir)
916 "Return the list of files with conflicts in directory DIR."
917 (let* ((root (vc-got-root dir))
918 (default-directory root)
919 (process-file-side-effects nil))
920 (cl-loop for (file status _) in (vc-got--status "C" ".")
921 when (and (eq status 'conflict)
922 (file-in-directory-p file dir))
923 collect file)))
925 (defun vc-got-repository-url (_file &optional remote-name)
926 "Return URL for REMOTE-NAME, or for \"origin\" if nil."
927 (let* ((default-directory (vc-got--repo-root))
928 (remote-name (or remote-name "origin"))
929 (heading (concat "[remote \"" remote-name "\"]"))
930 (conf (cond ((file-exists-p ".git/config") ".git/config")
931 ((file-exists-p ".git") nil)
932 ((file-exists-p "config") "config")))
933 found)
934 (when conf
935 (with-temp-buffer
936 (insert-file-contents conf)
937 (goto-char (point-min))
938 (when (search-forward heading nil t)
939 (forward-line)
940 (while (and (not found)
941 (looking-at ".*=") ; too broad?
942 (not (= (point) (point-max))))
943 (when (looking-at ".*url = \\(.*\\)")
944 (setq found (match-string-no-properties 1)))
945 (forward-line))
946 found)))))
950 ;; Automatically register the backend and add ".got" to the exclusion
951 ;; list.
953 ;;;###autoload
954 (add-to-list 'vc-handled-backends 'Got)
956 ;;;###autoload
957 (add-to-list 'vc-directory-exclusion-list ".got")
959 (provide 'vc-got)
960 ;;; vc-got.el ends here