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: 1.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 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 "^\\* \\(.+\\): \\([[:word:]]+\\)$" nil t)
326 (push (cons (match-string 1) (match-string 2)) alist))
327 alist)))))
329 (defun vc-got--current-branch ()
330 "Return the current branch."
331 (let (process-file-side-effects)
332 (with-temp-buffer
333 (when (zerop (vc-got--call "branch"))
334 (string-trim (buffer-string) "" "\n")))))
336 (defun vc-got--integrate (branch)
337 "Integrate BRANCH into the current one."
338 (with-temp-buffer
339 (zerop (vc-got--call "integrate" branch))))
341 (defun vc-got--update (branch &optional paths)
342 "Update to a different commit or BRANCH.
343 Optionally restrict the update operation to files at or within
344 the specified PATHS."
345 (with-temp-buffer
346 (unless (zerop (vc-got--call "update" "-b" branch "--" paths))
347 (error "[vc-got] can't update to branch %s: %s"
348 branch
349 (buffer-string)))))
351 (defun vc-got--diff (&rest files)
352 "Call got diff against FILES.
353 The result will be stored in the current buffer."
354 (let (process-file-side-effects)
355 (zerop (vc-got--call "diff"
356 (vc-switches 'got 'diff)
357 "--"
358 (mapcar #'file-relative-name files)))))
360 (defun vc-got--unstage (file-or-directory)
361 "Unstage all the staged hunks at or within FILE-OR-DIRECTORY.
362 If it's nil, unstage every staged changes across the entire work
363 tree."
364 (zerop (vc-got--call "unstage" "--" file-or-directory)))
366 (defun vc-got--remove (file &optional force keep-local)
367 "Use got to remove FILE.
368 If FORCE is non-nil perform the operation even if a file contains
369 local modification. If KEEP-LOCAL is non-nil keep the affected
370 files on disk."
371 (vc-got-with-worktree (or file default-directory)
372 (with-temp-buffer
373 (zerop (vc-got--call "remove"
374 (and force "-f")
375 (and keep-local "-k")
376 "--"
377 file)))))
379 (defun vc-got--ref ()
380 "Return a list of all references."
381 (let ((process-file-side-effects nil)
382 (re "^refs/\\(heads\\|remotes\\|tags\\)/\\(.*\\):")
383 ;; hardcoding HEAD because it's always present and the regexp
384 ;; won't match it.
385 (table (list "HEAD")))
386 (vc-got-with-worktree default-directory
387 (with-temp-buffer
388 (when (zerop (vc-got--call "ref" "-l"))
389 (goto-char (point-min))
390 (while (re-search-forward re nil t)
391 (push (match-string 2) table))
392 table)))))
394 (defun vc-got--branch (name)
395 "Try to create and switch to the branch called NAME."
396 (let (process-file-side-effects)
397 (vc-got-with-worktree default-directory
398 (with-temp-buffer
399 (if (zerop (vc-got--call "branch" "--" name))
401 (error "[vc-got] can't create branch %s: %s" name
402 (buffer-string)))))))
405 ;; Backend properties
407 (defun vc-got-revision-granularity ()
408 "Got has REPOSITORY granularity."
409 'repository)
411 (defun vc-got-update-on-retrieve-tag ()
412 "Like vc-git, vc-got don't need to buffers on `retrieve-tag'."
413 nil)
416 ;; State-querying functions
418 ;;;###autoload (defun vc-got-registered (file)
419 ;;;###autoload "Return non-nil if FILE is registered with got."
420 ;;;###autoload (when (vc-find-root file ".got")
421 ;;;###autoload (load "vc-got" nil t)
422 ;;;###autoload (vc-got-registered file)))
424 (defun vc-got-registered (file)
425 "Return non-nil if FILE is registered with got."
426 (if (file-directory-p file)
427 nil ; got doesn't track directories
428 (when (vc-find-root file ".got")
429 (let ((s (vc-got-state file)))
430 (not (or (eq s 'unregistered)
431 (null s)))))))
433 (defun vc-got-state (file)
434 "Return the current version control state of FILE. See `vc-state'."
435 (unless (file-directory-p file)
436 (let (process-file-side-effects)
437 ;; Manually calling got status and checking the result inline to
438 ;; avoid building the data structure in vc-got--status.
439 (with-temp-buffer
440 (when (zerop (vc-got--call "status" "--" file))
441 (goto-char (point-min))
442 (if (eobp)
443 'up-to-date
444 (vc-got--parse-status-char (char-after))))))))
446 (defun vc-got--dir-filter-files (files)
447 "Remove ., .. and .got from FILES."
448 (cl-loop for file in files
449 unless (or (string= file "..")
450 (string= file ".")
451 (string= file ".got"))
452 collect file))
454 (defun vc-got-dir-status-files (dir files update-function)
455 "Build the status for FILES in DIR.
456 The builded result is given to the callback UPDATE-FUNCTION. If
457 FILES is nil, consider all the files in DIR."
458 (let* ((fs (vc-got--dir-filter-files (or files (directory-files dir))))
459 ;; XXX: we call with files, wich will probably be nil on the
460 ;; first run, so we catch deleted, missing and edited files
461 ;; in subdirectories.
462 (res (vc-got--status nil dir files))
463 double-check)
464 (cl-loop for file in fs
465 do (when (and (not (cdr (assoc file res #'string=)))
466 (not (file-directory-p file))
467 ;; if file doesn't exists, it's a
468 ;; untracked file that was removed.
469 (file-exists-p file))
470 ;; if we don't know the status of a file here, it's
471 ;; either up-to-date or ignored. Save it for a
472 ;; double check
473 (push file double-check)))
474 (cl-loop with statuses = (vc-got--status nil dir double-check)
475 for file in double-check
476 unless (eq 'unregistered (cadr (assoc file statuses #'string=)))
477 do (push (list file 'up-to-date nil) res))
478 (funcall update-function res nil)))
480 (defun vc-got-dir-extra-headers (dir)
481 "Return a string for the `vc-dir' buffer heading for directory DIR."
482 (let ((remote (vc-got-repository-url dir)))
483 (concat (propertize "Repository : " 'face 'font-lock-type-face)
484 (vc-got--repo-root) "\n"
485 (when remote
486 (concat
487 (propertize "Remote URL : " 'face 'font-lock-type-face)
488 (vc-got-repository-url dir) "\n"))
489 (propertize "Branch : " 'face 'font-lock-type-face)
490 (vc-got--current-branch))))
492 (defun vc-got-dir-printer (info)
493 "Pretty-printer for the vc-dir-fileinfo structure INFO."
494 (let* ((isdir (vc-dir-fileinfo->directory info))
495 (state (if isdir "" (vc-dir-fileinfo->state info)))
496 (stage-state (vc-dir-fileinfo->extra info))
497 (filename (vc-dir-fileinfo->name info)))
498 (insert
499 " "
500 (propertize
501 (format "%c" (if (vc-dir-fileinfo->marked info) ?* ? ))
502 'face 'font-lock-type-face)
503 " "
504 (propertize
505 (format "%-12s" state)
506 'face (cond ((eq state 'up-to-date) 'font-lock-builtin-face)
507 ((memq state '(missing conflict)) 'font-lock-warning-face)
508 ((eq state 'edited) 'font-lock-constant-face)
509 (t 'font-lock-variable-name-face))
510 'mouse-face 'highlight
511 'keymap (vc-got--with-emacs-version<= "28.0.50"
512 vc-dir-status-mouse-map))
514 " " (propertize
515 (if stage-state
516 (format "%c" stage-state)
517 " ")
518 'face (cond ((memq stage-state '(?A ?E)) 'font-lock-constant-face)
519 ((eq stage-state ?R) 'font-lock-warning-face)
520 (t 'font-lock-variable-name-face)))
521 " "
522 (propertize filename
523 'face (if isdir 'font-lock-comment-delimiter-face
524 'font-lock-function-name-face)
525 'help-echo
526 (if isdir
527 (concat
528 "Directory\n"
529 "VC operations can be applied to it\n"
530 "mouse-3: Pop-up menu")
531 "File\nmouse-3: Pop-up menu")
532 'mouse-face 'highlight
533 'keymap vc-dir-filename-mouse-map))))
535 (defun vc-got-working-revision (file)
536 "Return the last commit that touched FILE or \"0\" if it's newly added."
537 (or
538 (with-temp-buffer
539 (when (vc-got--log file 1)
540 (let (start)
541 (goto-char (point-min))
542 (forward-word) ; skip "commit"
543 (forward-char) ; skip the space
544 (setq start (point)) ; store start of the SHA
545 (forward-word) ; goto SHA end
546 (buffer-substring start (point)))))
547 ;; special case: if this file is added but has no previous commits
548 ;; touching it, got log will fail (as expected), but we have to
549 ;; return "0".
550 (when (eq (vc-got-state file) 'added)
551 "0")))
553 (defun vc-got-checkout-model (_files)
554 "Return the checkout model.
555 Got uses an implicit checkout model for every file."
556 'implicit)
558 (defun vc-got-mode-line-string (file)
559 "Return the VC mode line string for FILE."
560 (vc-got-with-worktree file
561 (let ((def (vc-default-mode-line-string 'Got file)))
562 (concat (substring def 0 4) (vc-got--current-branch)))))
565 ;; state-changing functions
567 (defun vc-got-create-repo (_backend)
568 "Create an empty repository in the current directory."
569 (error "[vc-got] create-repo not implemented"))
571 (defun vc-got-register (files &optional _comment)
572 "Register FILES, passing `vc-register-switches' to the backend command."
573 (vc-got--add files))
575 (defalias 'vc-got-responsible-p #'vc-got-root)
577 (defun vc-got-unregister (file)
578 "Unregister FILE."
579 (vc-got--remove file t t))
581 (defun vc-got-checkin (files comment &optional _rev)
582 "Commit FILES with COMMENT as commit message."
583 (with-temp-buffer
584 (unless (zerop (vc-got--call "commit" "-m"
585 (log-edit-extract-headers nil comment)
586 "--"
587 files))
588 (error "[vc-got] can't commit: %s" (buffer-string)))))
590 (defun vc-got-find-revision (file rev buffer)
591 "Fill BUFFER with the content of FILE in the given revision REV."
592 (with-current-buffer buffer
593 (vc-got-with-worktree file
594 (vc-got--cat rev (file-relative-name file)))))
596 (defun vc-got-checkout (_file &optional _rev)
597 "Checkout revision REV of FILE.
598 If REV is t, checkout from the head."
599 (error "[vc-got] checkout not implemented"))
601 (defun vc-got-revert (file &optional _content-done)
602 "Revert FILE back to working revision."
603 (vc-got--revert file))
605 (defun vc-got-merge-branch ()
606 "Prompt for a branch and integrate it into the current one."
607 ;; XXX: be smart and try to "got rebase" if "got integrate" fails?
608 (let* ((branches (cl-loop for (branch . commit) in (vc-got--list-branches)
609 collect branch))
610 (branch (completing-read "Merge from branch: " branches)))
611 (when branch
612 (vc-got--integrate branch))))
614 (defun vc-got--proc-filter (proc s)
615 "Custom output filter for async process PROC.
616 It's like `vc-process-filter' but supports \r inside S."
617 (let ((buffer (process-buffer proc)))
618 (when (buffer-live-p buffer)
619 (with-current-buffer buffer
620 (save-excursion
621 (let ((buffer-undo-list t)
622 (inhibit-read-only t))
623 (goto-char (process-mark proc))
624 (if (not (string-match ".*\r\\(.*\\)" s))
625 (insert s)
626 ;; handle \r
627 (end-of-line)
628 (let ((end (point)))
629 (beginning-of-line)
630 (delete-region (point) end))
631 (insert (match-string 1 s)))
632 (set-marker (process-mark proc) (point))))))))
634 (defun vc-got--push-pull (cmd op prompt)
635 "Execute CMD OP, or prompt the user if PROMPT is non-nil."
636 (let ((buffer (format "*vc-got : %s*" (expand-file-name default-directory))))
637 (when-let (cmd (if prompt
638 (split-string
639 (read-shell-command (format "%s %s command: " cmd op)
640 (format "%s %s " cmd op))
641 " " t)
642 (list cmd op)))
643 (apply #'vc-do-async-command buffer default-directory cmd)
644 ;; this comes from vc-git.el. We're using git to push, so in
645 ;; part it makes sense, but we should revisit for full Got
646 ;; support.
647 (with-current-buffer buffer
648 (vc-compilation-mode 'got)
649 (let ((comp-cmd (mapconcat #'identity cmd " "))
650 (proc (get-buffer-process buffer)))
651 (setq-local compile-command comp-cmd)
652 (setq-local compilation-directory default-directory)
653 (setq-local compilation-arguments (list comp-cmd
654 nil
655 (lambda (_ign) buffer)
656 nil))
657 ;; Setup a custom process filter that handles \r.
658 (set-process-filter proc #'vc-got--proc-filter)))
659 (vc-set-async-update buffer))))
661 ;; TODO: this could be expanded. After a pull the worktree needs to
662 ;; be updated, either with a ``got update -b branch-name'' and
663 ;; eventually a rebase.
664 (defun vc-got-pull (prompt)
665 "Execute a pull prompting for the full command if PROMPT is not nil."
666 (let ((default-directory (vc-got-root default-directory)))
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 (let ((default-directory (vc-got--repo-root)))
673 (vc-got--push-pull vc-got-program "send" prompt)))
676 ;; History functions
678 (defun vc-got-print-log (files buffer &optional _shortlog start-revision limit)
679 "Insert the revision log for FILES into BUFFER.
680 LIMIT limits the number of commits, optionally starting at
681 START-REVISION."
682 (with-current-buffer buffer
683 ;; the *vc-diff* may be read only
684 (let ((inhibit-read-only t))
685 (cl-loop for file in files
686 do (vc-got--log (file-relative-name file)
687 limit
688 start-revision)))))
690 (defun vc-got-log-outgoing (buffer remote-location)
691 "Fill BUFFER with the diff between the local worktree branch and REMOTE-LOCATION."
692 (vc-setup-buffer buffer)
693 (let ((rl (vc-got-next-revision
694 nil
695 (if (or (not remote-location) (string-empty-p remote-location))
696 (concat "origin/" (vc-got--current-branch))
697 remote-location)))
698 (inhibit-read-only t))
699 (with-current-buffer buffer
700 (vc-got--log nil nil nil rl))))
702 (defun vc-got-incoming (buffer remote-location)
703 "Fill BUFFER with the diff between the REMOTE-LOCATION and the local worktree branch."
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 (dolist (file files)
750 (vc-got--diff file)))
751 ((and (null rev1)
752 rev2)
753 ;; TODO: this includes the whole diff while to respect
754 ;; the vc semantics we should filter only the diff for
755 ;; files in FILES.
756 ;;
757 ;; XXX: this includes also the commit message, I
758 ;; consider it a feature over the usual vc behaviour of
759 ;; showing only the diff.
760 (vc-got--log nil 1 rev2 nil nil nil t))
761 ;;
762 ;; TODO: if rev1 is nil, diff from the current version until
763 ;; rev2.
764 ;;
765 ;; TODO 2: if rev2 is nil as well, diff against an empty
766 ;; tree (i.e. get the patch from `got log -p rev1')
767 (t (vc-got--diff rev1 rev2)))))))
769 (defun vc-got-revision-completion-table (_files)
770 "Return a completion table for existing revisions.
771 Ignores FILES because GoT doesn't have the concept of ``file
772 revisions''; instead, like with git, you have tags and branches."
773 (letrec ((table (lazy-completion-table
774 table (lambda () (vc-got--ref)))))
775 table))
777 (defun vc-got-annotate-command (file buf &optional rev)
778 "Show annotated contents of FILE in buffer BUF. If given, use revision REV."
779 (let (process-file-side-effects)
780 (with-current-buffer buf
781 ;; FIXME: vc-ensure-vc-buffer won't recognise this buffer as managed
782 ;; by got unless vc-parent-buffer points to a buffer managed by got.
783 ;; investigate why this is needed.
784 (setq-local vc-parent-buffer (find-file-noselect file))
785 (vc-got--call "blame"
786 (when rev (list "-c" rev))
787 "--"
788 file))))
790 (defconst vc-got--annotate-re
791 (concat "^[0-9]\\{1,\\}) " ; line number followed by )
792 "\\([a-z0-9]+\\) " ; SHA-1 of commit
793 "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\) " ; year-mm-dd
794 "\\([^ ]\\)+ ") ; author
795 "Regexp to match annotation output lines.
797 Provides capture groups for:
798 1. revision id
799 2. date of commit
800 3. author of commit")
802 (defconst vc-got--commit-re "^commit \\([a-z0-9]+\\)"
803 "Regexp to match commit lines.
804 Provides capture group for the commit revision id.")
806 (defun vc-got-annotate-time ()
807 "Return the time of the next line of annotation at or after point.
808 Value is returned as floating point fractional number of days."
809 ;; XXX: to behave like vc-git here we should call re-search-forward
810 ;; instead of looking-at, as it makes the fontification of the line
811 ;; start AFTER the info. The problem is, due to the format of the
812 ;; blame, it produces an ugly result, with colors starting at
813 ;; different offsets depending on how long the commiter name is.
814 (when (looking-at vc-got--annotate-re)
815 (let ((str (match-string-no-properties 2)))
816 (vc-annotate-convert-time
817 (encode-time 0 0 0
818 (string-to-number (substring str 8 10))
819 (string-to-number (substring str 5 7))
820 (string-to-number (substring str 0 4)))))))
822 (defun vc-got-annotate-extract-revision-at-line ()
823 "Return revision corresponding to the current line or nil."
824 (save-excursion
825 (beginning-of-line)
826 (when (looking-at vc-got--annotate-re)
827 (match-string-no-properties 1))))
830 ;; Tag system
832 (defun vc-got--tag-callback (tag)
833 "`log-edit' callback for `vc-got-create-tag'.
834 Creates the TAG using the content of the current buffer."
835 (interactive)
836 (let ((msg (buffer-substring-no-properties (point-min)
837 (point-max))))
838 (with-temp-buffer
839 (unless (zerop (vc-got--call "tag"
840 "-m"
841 (log-edit-extract-headers nil msg)
842 "--"
843 tag))
844 (error "[vc-got] can't create tag %s: %s" tag (buffer-string))))))
846 (defun vc-got-create-tag (_dir name branchp)
847 "Attach the tag NAME to the state of the worktree.
848 DIR is ignored (tags are global, not per-file). If BRANCHP is
849 true, NAME should create a new branch otherwise it will pop-up a
850 `log-edit' buffer to provide the tag message."
851 ;; TODO: vc reccomends to ensure that all the file are in a clean
852 ;; state, but is it useful?
853 (if branchp
854 (vc-got--branch name)
855 (let ((buf (get-buffer-create "*vc-got tag*")))
856 (with-current-buffer buf
857 (erase-buffer)
858 (save-excursion
859 (insert "Summary: tag " name "\n\n"))
860 (move-end-of-line 1)
861 (switch-to-buffer buf)
862 (log-edit (lambda ()
863 (interactive)
864 (unwind-protect
865 (vc-got--tag-callback name)
866 (kill-buffer buf))))))))
868 (defun vc-got-retrieve-tag (dir name _update)
869 "Switch to the tag NAME for files at or below DIR."
870 (let ((default-directory dir))
871 (vc-got--update name dir)))
874 ;; Miscellaneous
876 (defun vc-got-find-ignore-file (file)
877 "Return the gitignore file that controls FILE."
878 (expand-file-name ".gitignore"
879 (vc-got-root file)))
881 (defun vc-got-previous-revision (file rev)
882 "Return the revision number that precedes REV for FILE or nil."
883 (with-temp-buffer
884 (vc-got--log file 2 rev nil nil t)
885 (goto-char (point-min))
886 (keep-lines "^commit")
887 (when (looking-at vc-got--commit-re)
888 (match-string-no-properties 1))))
890 (defun vc-got-next-revision (file rev)
891 "Return the revision number that follows REV for FILE or nil."
892 (with-temp-buffer
893 (vc-got--log file nil nil rev)
894 (keep-lines "^commit" (point-min) (point-max))
895 (goto-char (point-max))
896 (forward-line -1) ; return from empty line to last actual commit
897 (unless (= (point) (point-min))
898 (forward-line -1)
899 (when (looking-at vc-got--commit-re)
900 (match-string-no-properties 1)))))
902 (defun vc-got-delete-file (file)
903 "Delete FILE locally and mark it deleted in work tree."
904 (vc-got--remove file t))
906 (defun vc-got-find-file-hook ()
907 "Activate `smerge-mode' if there is a conflict."
908 ;; just like vc-git-find-file-hook
909 (when (and buffer-file-name
910 (eq (vc-state buffer-file-name 'Got) 'conflict)
911 (save-excursion
912 (goto-char (point-min))
913 (re-search-forward "^<<<<<<< " nil 'noerror)))
914 (smerge-start-session)
915 (vc-message-unresolved-conflicts buffer-file-name)))
917 (defun vc-got-conflicted-files (dir)
918 "Return the list of files with conflicts in directory DIR."
919 (let* ((root (vc-got-root dir))
920 (default-directory root)
921 (process-file-side-effects nil))
922 (cl-loop for (file status _) in (vc-got--status "C" ".")
923 when (and (eq status 'conflict)
924 (file-in-directory-p file dir))
925 collect file)))
927 (defun vc-got-repository-url (_file &optional remote-name)
928 "Return URL for REMOTE-NAME, or for \"origin\" if nil."
929 (let* ((default-directory (vc-got--repo-root))
930 (remote-name (or remote-name "origin"))
931 (heading (concat "[remote \"" remote-name "\"]"))
932 (conf (cond ((file-exists-p ".git/config") ".git/config")
933 ((file-exists-p ".git") nil)
934 ((file-exists-p "config") "config")))
935 found)
936 (when conf
937 (with-temp-buffer
938 (insert-file-contents conf)
939 (goto-char (point-min))
940 (when (search-forward heading nil t)
941 (forward-line)
942 (while (and (not found)
943 (looking-at ".*=") ; too broad?
944 (not (= (point) (point-max))))
945 (when (looking-at ".*url = \\(.*\\)")
946 (setq found (match-string-no-properties 1)))
947 (forward-line))
948 found)))))
952 ;; Automatically register the backend and add ".got" to the exclusion
953 ;; list.
955 ;;;###autoload
956 (add-to-list 'vc-handled-backends 'Got)
958 ;;;###autoload
959 (add-to-list 'vc-directory-exclusion-list ".got")
961 (provide 'vc-got)
962 ;;; vc-got.el ends here