Blame


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