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://git.omarpolo.com/vc-got/
8 ;; Keywords: vc tools
9 ;; Version: 0
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 ;; This file contains a VC backend for the Game of Trees (got) version
28 ;; control system.
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 ;; TODO: vc-git has most function that starts with:
122 ;;
123 ;; (let* ((root (vc-git-root default-directory))
124 ;; (buffer (format "*vc-git : %s*" (expand-file-name root)))
125 ;; ...)
126 ;; ...)
127 ;;
128 ;; we should 1) investigate if also other backends do something like
129 ;; this (or if there is a better way) and 2) try to do the same.
131 ;;; Code:
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)
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.
227 Return nil if the command failed or if PATH isn't included in any
228 worktree."
229 (let ((process-file-side-effects nil))
230 (vc-got-with-worktree (or path default-directory)
231 (when (zerop
232 (save-excursion
233 (vc-got--call "log"
234 (and limit (list "-l" (format "%s" limit)))
235 (and start-commit (list "-c" start-commit))
236 (and stop-commit (list "-x" stop-commit))
237 (and search-pattern (list "-s" search-pattern))
238 (and reverse '("-R"))
239 "--"
240 path)))
241 (save-excursion
242 (delete-matching-lines
243 "^-----------------------------------------------$")
244 t)))))
246 (defun vc-got--status (status-codes dir-or-file &optional files)
247 "Return a list of lists '(FILE STATUS STAGE-STATUS).
248 DIR-OR-FILE can be either a directory or a file. If FILES is
249 given, return the status of those files, otherwise the status of
250 DIR-OR-FILE. STATUS-CODES is either nil, or a string that's
251 passed as the -s flag to got status to limit the types of status
252 to report (e.g. \"CD\" to report only conflicts and deleted
253 files)."
254 (with-temp-buffer
255 (let* ((default-directory (expand-file-name
256 (if (file-directory-p dir-or-file)
257 dir-or-file
258 (file-name-directory dir-or-file))))
259 (root (vc-got-root default-directory))
260 (process-file-side-effects))
261 (when (zerop (vc-got--call "status"
262 (and status-codes (list "-s" status-codes))
263 "--"
264 (or files dir-or-file)))
265 (goto-char (point-min))
266 (cl-loop until (eobp)
267 collect (vc-got--parse-status-line root)
268 do (forward-line))))))
270 (defun vc-got--parse-status-line (root)
271 "Parse a line of the the output of status.
272 ROOT is the root of the repo."
273 ;; the format of each line is
274 ;; <status-char> <stage-char> <spc> <filename> \n
275 (let* ((file-status (prog1 (vc-got--parse-status-char
276 (char-after))
277 (forward-char)))
278 (stage-status (let* ((c (char-after)))
279 (prog1
280 (when (member c '(?M ?A ?D))
281 c)
282 (forward-char))))
283 (filename (progn
284 (forward-char)
285 (buffer-substring (point)
286 (line-end-position)))))
287 (list (file-relative-name (expand-file-name filename root)
288 default-directory)
289 (or file-status (and stage-status 'up-to-date))
290 stage-status)))
292 (defun vc-got--parse-status-char (c)
293 "Parse status char C into a symbol accepted by `vc-state'."
294 (cl-case c
295 (?M 'edited)
296 (?A 'added)
297 (?D 'removed)
298 (?C 'conflict)
299 (?! 'missing)
300 (?~ 'edited) ; XXX: what does it means for a file to be ``obstructed''?
301 (?? 'unregistered)
302 (?m 'edited) ; modified file modes
303 (?N nil)))
305 (defun vc-got--cat (commit obj-id)
306 "Execute got cat -c COMMIT OBJ-ID in the current buffer."
307 (let (process-file-side-effects)
308 (zerop (vc-got--call "cat" "-c" commit obj-id))))
310 (defun vc-got--revert (&rest files)
311 "Execute got revert FILES."
312 (vc-got-with-worktree (car files)
313 (with-temp-buffer
314 (zerop (vc-got--call "revert" "--" files)))))
316 (defun vc-got--list-branches ()
317 "Return an alist of (branch . commit)."
318 (let (process-file-side-effects)
319 (with-temp-buffer
320 (when (zerop (vc-got--call "branch" "-l"))
321 (let (alist)
322 (goto-char (point-min))
323 (while (re-search-forward "^\\* \\(.+\\): \\([[:word:]]+\\)$" nil t)
324 (push (cons (match-string 1) (match-string 2)) alist))
325 alist)))))
327 (defun vc-got--current-branch ()
328 "Return the current branch."
329 (let (process-file-side-effects)
330 (with-temp-buffer
331 (when (zerop (vc-got--call "branch"))
332 (string-trim (buffer-string) "" "\n")))))
334 (defun vc-got--integrate (branch)
335 "Integrate BRANCH into the current one."
336 (with-temp-buffer
337 (zerop (vc-got--call "integrate" branch))))
339 (defun vc-got--update (branch &optional paths)
340 "Update to a different commit or BRANCH.
341 Optionally restrict the update operation to files at or within
342 the specified PATHS."
343 (with-temp-buffer
344 (unless (zerop (vc-got--call "update" "-b" branch "--" paths))
345 (error "[vc-got] can't update to branch %s: %s"
346 branch
347 (buffer-string)))))
349 (defun vc-got--diff (&rest files)
350 "Call got diff against FILES.
351 The result will be stored in the current buffer."
352 (let (process-file-side-effects)
353 (zerop (vc-got--call "diff"
354 (vc-switches 'got 'diff)
355 "--"
356 (mapcar #'file-relative-name files)))))
358 (defun vc-got--unstage (file-or-directory)
359 "Unstage all the staged hunks at or within FILE-OR-DIRECTORY.
360 If it's nil, unstage every staged changes across the entire work
361 tree."
362 (zerop (vc-got--call "unstage" "--" file-or-directory)))
364 (defun vc-got--remove (file &optional force keep-local)
365 "Use got to remove FILE.
366 If FORCE is non-nil perform the operation even if a file contains
367 local modification. If KEEP-LOCAL is non-nil keep the affected
368 files on disk."
369 (vc-got-with-worktree (or file default-directory)
370 (with-temp-buffer
371 (zerop (vc-got--call "remove"
372 (and force "-f")
373 (and keep-local "-k")
374 "--"
375 file)))))
377 (defun vc-got--ref ()
378 "Return a list of all references."
379 (let ((process-file-side-effects nil)
380 (re "^refs/\\(heads\\|remotes\\|tags\\)/\\(.*\\):")
381 ;; hardcoding HEAD because it's always present and the regexp
382 ;; won't match it.
383 (table (list "HEAD")))
384 (vc-got-with-worktree default-directory
385 (with-temp-buffer
386 (when (zerop (vc-got--call "ref" "-l"))
387 (goto-char (point-min))
388 (while (re-search-forward re nil t)
389 (push (match-string 2) table))
390 table)))))
392 (defun vc-got--branch (name)
393 "Try to create and switch to the branch called NAME."
394 (let (process-file-side-effects)
395 (vc-got-with-worktree default-directory
396 (with-temp-buffer
397 (if (zerop (vc-got--call "branch" "--" name))
399 (error "[vc-got] can't create branch %s: %s" name
400 (buffer-string)))))))
403 ;; Backend properties
405 (defun vc-got-revision-granularity ()
406 "Got has REPOSITORY granularity."
407 'repository)
409 (defun vc-got-update-on-retrieve-tag ()
410 "Like vc-git, vc-got don't need to buffers on `retrieve-tag'."
411 nil)
414 ;; State-querying functions
416 ;;;###autoload (defun vc-got-registered (file)
417 ;;;###autoload "Return non-nil if FILE is registered with got."
418 ;;;###autoload (when (vc-find-root file ".got")
419 ;;;###autoload (load "vc-got" nil t)
420 ;;;###autoload (vc-got-registered file)))
422 (defun vc-got-registered (file)
423 "Return non-nil if FILE is registered with got."
424 (if (file-directory-p file)
425 nil ; got doesn't track directories
426 (when (vc-find-root file ".got")
427 (let ((s (vc-got-state file)))
428 (not (or (eq s 'unregistered)
429 (null s)))))))
431 (defun vc-got-state (file)
432 "Return the current version control state of FILE. See `vc-state'."
433 (unless (file-directory-p file)
434 (let (process-file-side-effects)
435 ;; Manually calling got status and checking the result inline to
436 ;; avoid building the data structure in vc-got--status.
437 (with-temp-buffer
438 (when (zerop (vc-got--call "status" "--" file))
439 (goto-char (point-min))
440 (if (eobp)
441 'up-to-date
442 (vc-got--parse-status-char (char-after))))))))
444 (defun vc-got--dir-filter-files (files)
445 "Remove ., .. and .got from FILES."
446 (cl-loop for file in files
447 unless (or (string= file "..")
448 (string= file ".")
449 (string= file ".got"))
450 collect file))
452 (defun vc-got-dir-status-files (dir files update-function)
453 "Build the status for FILES in DIR.
454 The builded result is given to the callback UPDATE-FUNCTION. If
455 FILES is nil, consider all the files in DIR."
456 (let* ((fs (vc-got--dir-filter-files (or files (directory-files dir))))
457 ;; XXX: we call with files, wich will probably be nil on the
458 ;; first run, so we catch deleted, missing and edited files
459 ;; in subdirectories.
460 (res (vc-got--status nil dir files))
461 double-check)
462 (cl-loop for file in fs
463 do (when (and (not (cdr (assoc file res #'string=)))
464 (not (file-directory-p file))
465 ;; if file doesn't exists, it's a
466 ;; untracked file that was removed.
467 (file-exists-p file))
468 ;; if we don't know the status of a file here, it's
469 ;; either up-to-date or ignored. Save it for a
470 ;; double check
471 (push file double-check)))
472 (cl-loop with statuses = (vc-got--status nil dir double-check)
473 for file in double-check
474 unless (eq 'unregistered (cadr (assoc file statuses #'string=)))
475 do (push (list file 'up-to-date nil) res))
476 (funcall update-function res nil)))
478 (defun vc-got-dir-extra-headers (dir)
479 "Return a string for the `vc-dir' buffer heading for directory DIR."
480 (let ((remote (vc-got-repository-url dir)))
481 (concat (propertize "Repository : " 'face 'font-lock-type-face)
482 (vc-got--repo-root) "\n"
483 (when remote
484 (concat
485 (propertize "Remote URL : " 'face 'font-lock-type-face)
486 (vc-got-repository-url dir) "\n"))
487 (propertize "Branch : " 'face 'font-lock-type-face)
488 (vc-got--current-branch))))
490 (defun vc-got-dir-printer (info)
491 "Pretty-printer for the vc-dir-fileinfo structure INFO."
492 (let* ((isdir (vc-dir-fileinfo->directory info))
493 (state (if isdir "" (vc-dir-fileinfo->state info)))
494 (stage-state (vc-dir-fileinfo->extra info))
495 (filename (vc-dir-fileinfo->name info)))
496 (insert
497 " "
498 (propertize
499 (format "%c" (if (vc-dir-fileinfo->marked info) ?* ? ))
500 'face 'font-lock-type-face)
501 " "
502 (propertize
503 (format "%-12s" state)
504 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
505 ((memq state '(missing conflict)) 'font-lock-warning-face)
506 ((eq state 'edited) 'font-lock-constant-face)
507 (t 'font-lock-variable-name-face))
508 'mouse-face 'highlight
509 'keymap (vc-got--with-emacs-version<= "28.0.50"
510 vc-dir-status-mouse-map))
512 " " (propertize
513 (if stage-state
514 (format "%c" stage-state)
515 " ")
516 'face (cond ((memq stage-state '(?A ?E)) 'font-lock-constant-face)
517 ((eq stage-state ?R) 'font-lock-warning-face)
518 (t 'font-lock-variable-name-face)))
519 " "
520 (propertize filename
521 'face (if isdir 'font-lock-comment-delimiter-face
522 'font-lock-function-name-face)
523 'help-echo
524 (if isdir
525 (concat
526 "Directory\n"
527 "VC operations can be applied to it\n"
528 "mouse-3: Pop-up menu")
529 "File\nmouse-3: Pop-up menu")
530 'mouse-face 'highlight
531 'keymap vc-dir-filename-mouse-map))))
533 (defun vc-got-working-revision (file)
534 "Return the last commit that touched FILE or \"0\" if it's newly added."
535 (or
536 (with-temp-buffer
537 (when (vc-got--log file 1)
538 (let (start)
539 (goto-char (point-min))
540 (forward-word) ; skip "commit"
541 (forward-char) ; skip the space
542 (setq start (point)) ; store start of the SHA
543 (forward-word) ; goto SHA end
544 (buffer-substring start (point)))))
545 ;; special case: if this file is added but has no previous commits
546 ;; touching it, got log will fail (as expected), but we have to
547 ;; return "0".
548 (when (eq (vc-got-state file) 'added)
549 "0")))
551 (defun vc-got-checkout-model (_files)
552 "Return the checkout model.
553 Got uses an implicit checkout model for every file."
554 'implicit)
556 (defun vc-got-mode-line-string (file)
557 "Return the VC mode line string for FILE."
558 (vc-got-with-worktree file
559 (let ((def (vc-default-mode-line-string 'Got file)))
560 (concat (substring def 0 4) (vc-got--current-branch)))))
563 ;; state-changing functions
565 (defun vc-got-create-repo (_backend)
566 "Create an empty repository in the current directory."
567 (error "[vc-got] create-repo not implemented"))
569 (defun vc-got-register (files &optional _comment)
570 "Register FILES, passing `vc-register-switches' to the backend command."
571 (vc-got--add files))
573 (defalias 'vc-got-responsible-p #'vc-got-root)
575 (defun vc-got-unregister (file)
576 "Unregister FILE."
577 (vc-got--remove file t t))
579 (defun vc-got-checkin (files comment &optional _rev)
580 "Commit FILES with COMMENT as commit message."
581 (with-temp-buffer
582 (unless (zerop (vc-got--call "commit" "-m"
583 (log-edit-extract-headers nil comment)
584 "--"
585 files))
586 (error "[vc-got] can't commit: %s" (buffer-string)))))
588 (defun vc-got-find-revision (file rev buffer)
589 "Fill BUFFER with the content of FILE in the given revision REV."
590 (with-current-buffer buffer
591 (vc-got-with-worktree file
592 (vc-got--cat rev (file-relative-name file)))))
594 (defun vc-got-checkout (_file &optional _rev)
595 "Checkout revision REV of FILE.
596 If REV is t, checkout from the head."
597 (error "[vc-got] checkout not implemented"))
599 (defun vc-got-revert (file &optional _content-done)
600 "Revert FILE back to working revision."
601 (vc-got--revert file))
603 (defun vc-got-merge-branch ()
604 "Prompt for a branch and integrate it into the current one."
605 ;; XXX: be smart and try to "got rebase" if "got integrate" fails?
606 (let* ((branches (cl-loop for (branch . commit) in (vc-got--list-branches)
607 collect branch))
608 (branch (completing-read "Merge from branch: " branches)))
609 (when branch
610 (vc-got--integrate branch))))
612 (defun vc-got--proc-filter (proc s)
613 "Custom output filter for async process PROC.
614 It's like `vc-process-filter' but supports \r inside S."
615 (let ((buffer (process-buffer proc)))
616 (when (buffer-live-p buffer)
617 (with-current-buffer buffer
618 (save-excursion
619 (let ((buffer-undo-list t)
620 (inhibit-read-only t))
621 (goto-char (process-mark proc))
622 (if (not (string-match ".*\r\\(.*\\)" s))
623 (insert s)
624 ;; handle \r
625 (end-of-line)
626 (let ((end (point)))
627 (beginning-of-line)
628 (delete-region (point) end))
629 (insert (match-string 1 s)))
630 (set-marker (process-mark proc) (point))))))))
632 (defun vc-got--push-pull (cmd op prompt)
633 "Execute CMD OP, or prompt the user if PROMPT is non-nil."
634 (let ((buffer (format "*vc-got : %s*" (expand-file-name default-directory))))
635 (when-let (cmd (if prompt
636 (split-string
637 (read-shell-command (format "%s %s command: " cmd op)
638 (format "%s %s " cmd op))
639 " " t)
640 (list cmd op)))
641 (apply #'vc-do-async-command buffer default-directory cmd)
642 ;; this comes from vc-git.el. We're using git to push, so in
643 ;; part it makes sense, but we should revisit for full Got
644 ;; support.
645 (with-current-buffer buffer
646 (vc-compilation-mode 'got)
647 (let ((comp-cmd (mapconcat #'identity cmd " "))
648 (proc (get-buffer-process buffer)))
649 (setq-local compile-command comp-cmd)
650 (setq-local compilation-directory default-directory)
651 (setq-local compilation-arguments (list comp-cmd
652 nil
653 (lambda (_ign) buffer)
654 nil))
655 ;; Setup a custom process filter that handles \r.
656 (set-process-filter proc #'vc-got--proc-filter)))
657 (vc-set-async-update buffer))))
659 ;; TODO: this could be expanded. After a pull the worktree needs to
660 ;; be updated, either with a ``got update -b branch-name'' and
661 ;; eventually a rebase.
662 (defun vc-got-pull (prompt)
663 "Execute a pull prompting for the full command if PROMPT is not nil."
664 (let ((default-directory (vc-got-root default-directory)))
665 (vc-got--push-pull vc-got-program "fetch" prompt)))
667 (defun vc-got-push (prompt)
668 "Run git push (not got!) in the repository dir.
669 If PROMPT is non-nil, prompt for the git command to run."
670 (let ((default-directory (vc-got--repo-root)))
671 (vc-got--push-pull vc-got-program "send" prompt)))
674 ;; History functions
676 (defun vc-got-print-log (files buffer &optional _shortlog start-revision limit)
677 "Insert the revision log for FILES into BUFFER.
678 LIMIT limits the number of commits, optionally starting at
679 START-REVISION."
680 (with-current-buffer buffer
681 ;; the *vc-diff* may be read only
682 (let ((inhibit-read-only t))
683 (cl-loop for file in files
684 do (vc-got--log (file-relative-name file)
685 limit
686 start-revision)))))
688 (defun vc-got-log-outgoing (buffer remote-location)
689 "Fill BUFFER with the diff between the local worktree branch and REMOTE-LOCATION."
690 (vc-setup-buffer buffer)
691 (let ((rl (vc-got-next-revision
692 nil
693 (if (or (not remote-location) (string-empty-p remote-location))
694 (concat "origin/" (vc-got--current-branch))
695 remote-location)))
696 (inhibit-read-only t))
697 (with-current-buffer buffer
698 (vc-got--log nil nil nil rl))))
700 (defun vc-got-incoming (buffer remote-location)
701 "Fill BUFFER with the diff between the REMOTE-LOCATION and the local worktree branch."
702 (let ((rl (if (or (not remote-location) (string-empty-p remote-location))
703 (concat "origin/" (vc-got--current-branch))
704 remote-location))
705 (inhibit-read-only t))
706 (with-current-buffer buffer
707 (vc-got--log nil nil (vc-got--current-branch) rl))))
709 (defun vc-got-log-search (buffer pattern)
710 "Search commits for PATTERN and write the results found in BUFFER."
711 (with-current-buffer buffer
712 (let ((inhibit-read-only t))
713 (vc-got--log nil nil nil nil pattern))))
715 (define-derived-mode vc-got-log-view-mode log-view-mode "Got-Log-View"
716 "Got-specific log-view mode.
717 Heavily inspired by `vc-git-log-view-mode'."
718 (require 'add-log)
719 (setq-local log-view-file-re regexp-unmatchable)
720 (setq-local log-view-per-file-logs nil)
721 (setq-local log-view-message-re "^commit +\\([0-9a-z]+\\)")
722 (setq-local log-view-font-lock-keywords
723 (append
724 `((,log-view-message-re (1 'change-log-acknowledgment)))
725 ;; Handle the case:
726 ;; user: foo@bar
727 '(("^from: \\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)"
728 (1 'change-log-email))
729 ;; Handle the case:
730 ;; user: FirstName LastName <foo@bar>
731 ("^from: \\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
732 (1 'change-log-name)
733 (2 'change-log-email))
734 ("^date: \\(.+\\)" (1 'change-log-date))))))
736 ;; TODO: async
737 ;; TODO: return 0 or 1
738 (defun vc-got-diff (files &optional rev1 rev2 buffer _async)
739 "Insert into BUFFER (or *vc-diff*) the diff for FILES from REV1 to REV2."
740 (let* ((buffer (get-buffer-create (or buffer "*vc-diff*")))
741 (inhibit-read-only t))
742 (with-current-buffer buffer
743 (vc-got-with-worktree (or (car files)
744 default-directory)
745 (if (and (null rev1)
746 (null rev2))
747 (dolist (file files)
748 (vc-got--diff file))
749 ;; TODO: if rev1 is nil, diff from the current version until
750 ;; rev2.
751 ;;
752 ;; TODO: if rev2 is nil as well, diff against an empty tree
753 ;; (i.e. get the patch from `got log -p rev1')
754 ;;
755 ;; TODO: it would be nice to optionally include FILES here,
756 ;; it would make the `=' key on the *Annotate* buffer do the
757 ;; right thing, but AFAICS got doesn't provide something
758 ;; like this. Probably only hacking something with ``log
759 ;; -p'' and filtering?
760 (vc-got--diff rev1 rev2))))))
762 (defun vc-got-revision-completion-table (_files)
763 "Return a completion table for existing revisions.
764 Ignores FILES because GoT doesn't have the concept of ``file
765 revisions''; instead, like with git, you have tags and branches."
766 (letrec ((table (lazy-completion-table
767 table (lambda () (vc-got--ref)))))
768 table))
770 (defun vc-got-annotate-command (file buf &optional rev)
771 "Show annotated contents of FILE in buffer BUF. If given, use revision REV."
772 (let (process-file-side-effects)
773 (with-current-buffer buf
774 ;; FIXME: vc-ensure-vc-buffer won't recognise this buffer as managed
775 ;; by got unless vc-parent-buffer points to a buffer managed by got.
776 ;; investigate why this is needed.
777 (setq-local vc-parent-buffer (find-file-noselect file))
778 (vc-got--call "blame"
779 (when rev (list "-c" rev))
780 "--"
781 file))))
783 (defconst vc-got--annotate-re
784 (concat "^[0-9]\\{1,\\}) " ; line number followed by )
785 "\\([a-z0-9]+\\) " ; SHA-1 of commit
786 "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\) " ; year-mm-dd
787 "\\([^ ]\\)+ ") ; author
788 "Regexp to match annotation output lines.
790 Provides capture groups for:
791 1. revision id
792 2. date of commit
793 3. author of commit")
795 (defconst vc-got--commit-re "^commit \\([a-z0-9]+\\)"
796 "Regexp to match commit lines.
798 Provides capture group for the commit revision id.")
800 (defun vc-got-annotate-time ()
801 "Return the time of the next line of annotation at or after point.
802 Value is returned as floating point fractional number of days."
803 (save-excursion
804 (beginning-of-line)
805 (when (looking-at vc-got--annotate-re)
806 (let ((str (match-string-no-properties 2)))
807 (vc-annotate-convert-time
808 (encode-time 0 0 0
809 (string-to-number (substring str 8 10))
810 (string-to-number (substring str 5 7))
811 (string-to-number (substring str 0 4))))))))
813 (defun vc-got-annotate-extract-revision-at-line ()
814 "Return revision corresponding to the current line or nil."
815 (save-excursion
816 (beginning-of-line)
817 (when (looking-at vc-got--annotate-re)
818 (match-string-no-properties 1))))
821 ;; Tag system
823 (defun vc-got--tag-callback (tag)
824 "`log-edit' callback for `vc-got-create-tag'.
825 Creates the TAG using the content of the current buffer."
826 (interactive)
827 (let ((msg (buffer-substring-no-properties (point-min)
828 (point-max))))
829 (with-temp-buffer
830 (unless (zerop (vc-got--call "tag"
831 "-m"
832 (log-edit-extract-headers nil msg)
833 "--"
834 tag))
835 (error "[vc-got] can't create tag %s: %s" tag (buffer-string))))))
837 (defun vc-got-create-tag (_dir name branchp)
838 "Attach the tag NAME to the state of the worktree.
839 DIR is ignored (tags are global, not per-file). If BRANCHP is
840 true, NAME should create a new branch otherwise it will pop-up a
841 `log-edit' buffer to provide the tag message."
842 ;; TODO: vc reccomends to ensure that all the file are in a clean
843 ;; state, but is it useful?
844 (if branchp
845 (vc-got--branch name)
846 (let ((buf (get-buffer-create "*vc-got tag*")))
847 (with-current-buffer buf
848 (erase-buffer)
849 (save-excursion
850 (insert "Summary: tag " name "\n\n"))
851 (move-end-of-line 1)
852 (switch-to-buffer buf)
853 (log-edit (lambda ()
854 (interactive)
855 (unwind-protect
856 (vc-got--tag-callback name)
857 (kill-buffer buf))))))))
859 (defun vc-got-retrieve-tag (dir name _update)
860 "Switch to the tag NAME for files at or below DIR."
861 (let ((default-directory dir))
862 (vc-got--update name dir)))
865 ;; Miscellaneous
867 (defun vc-got-find-ignore-file (file)
868 "Return the gitignore file that controls FILE."
869 (expand-file-name ".gitignore"
870 (vc-got-root file)))
872 (defun vc-got-previous-revision (file rev)
873 "Return the revision number that precedes REV for FILE or nil."
874 (with-temp-buffer
875 (vc-got--log file 2 rev nil nil t)
876 (goto-char (point-min))
877 (keep-lines "^commit")
878 (when (looking-at vc-got--commit-re)
879 (match-string-no-properties 1))))
881 (defun vc-got-next-revision (file rev)
882 "Return the revision number that follows REV for FILE or nil."
883 (with-temp-buffer
884 (vc-got--log file nil nil rev)
885 (keep-lines "^commit" (point-min) (point-max))
886 (goto-char (point-max))
887 (forward-line -1) ; return from empty line to last actual commit
888 (unless (= (point) (point-min))
889 (forward-line -1)
890 (when (looking-at vc-got--commit-re)
891 (match-string-no-properties 1)))))
893 (defun vc-got-delete-file (file)
894 "Delete FILE locally and mark it deleted in work tree."
895 (vc-got--remove file t))
897 (defun vc-got-find-file-hook ()
898 "Activate `smerge-mode' if there is a conflict."
899 ;; just like vc-git-find-file-hook
900 (when (and buffer-file-name
901 (eq (vc-state buffer-file-name 'Got) 'conflict)
902 (save-excursion
903 (goto-char (point-min))
904 (re-search-forward "^<<<<<<< " nil 'noerror)))
905 (smerge-start-session)
906 (vc-message-unresolved-conflicts buffer-file-name)))
908 (defun vc-got-conflicted-files (dir)
909 "Return the list of files with conflicts in directory DIR."
910 (let* ((root (vc-got-root dir))
911 (default-directory root)
912 (process-file-side-effects nil))
913 (cl-loop for (file status _) in (vc-got--status "C" ".")
914 when (and (eq status 'conflict)
915 (file-in-directory-p file dir))
916 collect file)))
918 (defun vc-got-repository-url (_file &optional remote-name)
919 "Return URL for REMOTE-NAME, or for \"origin\" if nil."
920 (let* ((default-directory (vc-got--repo-root))
921 (remote-name (or remote-name "origin"))
922 (heading (concat "[remote \"" remote-name "\"]"))
923 (conf (cond ((file-exists-p ".git/config") ".git/config")
924 ((file-exists-p ".git") nil)
925 ((file-exists-p "config") "config")))
926 found)
927 (when conf
928 (with-temp-buffer
929 (insert-file-contents conf)
930 (goto-char (point-min))
931 (when (search-forward heading nil t)
932 (forward-line)
933 (while (and (not found)
934 (looking-at ".*=") ; too broad?
935 (not (= (point) (point-max))))
936 (when (looking-at ".*url = \\(.*\\)")
937 (setq found (match-string-no-properties 1)))
938 (forward-line))
939 found)))))
943 ;; Automatically register the backend and add ".got" to the exclusion
944 ;; list.
946 ;;;###autoload
947 (add-to-list 'vc-handled-backends 'Got)
949 ;;;###autoload
950 (add-to-list 'vc-directory-exclusion-list ".got")
952 (provide 'vc-got)
953 ;;; vc-got.el ends here