Followup to last change in browse-url.el
[emacs.git] / lisp / vc / pcvs-parse.el
blobdbd25d93a1e1dab90bd59defd3c21a64b616a313
1 ;;; pcvs-parse.el --- the CVS output parser
3 ;; Copyright (C) 1991-2018 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords: pcl-cvs
7 ;; Package: pcvs
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Bugs:
28 ;; - when merging a modified file, if the merge says that the file already
29 ;; contained in the changes, it marks the file as `up-to-date' although
30 ;; it might still contain further changes.
31 ;; Example: merging a zero-change commit.
33 ;;; Code:
35 (eval-when-compile (require 'cl-lib))
36 (require 'pcvs-util)
37 (require 'pcvs-info)
39 ;; imported from pcvs.el
40 (defvar cvs-execute-single-dir)
42 ;; parse vars
44 (defcustom cvs-update-prog-output-skip-regexp "$"
45 "A regexp that matches the end of the output from all cvs update programs.
46 That is, output from any programs that are run by CVS (by the flag -u
47 in the `modules' file - see cvs(5)) when `cvs update' is performed should
48 terminate with a line that this regexp matches. It is enough that
49 some part of the line is matched.
51 The default (a single $) fits programs without output."
52 :group 'pcl-cvs
53 :type '(regexp :value "$"))
55 (defcustom cvs-parse-ignored-messages
56 '("Executing ssh-askpass to query the password.*$"
57 ".*Remote host denied X11 forwarding.*$")
58 "A list of regexps matching messages that should be ignored by the parser.
59 Each regexp should match a whole set of lines and should hence be terminated
60 by `$'."
61 :group 'pcl-cvs
62 :type '(repeat regexp))
64 ;; a few more defvars just to shut up the compiler
65 (defvar cvs-start)
66 (defvar cvs-current-dir)
67 (defvar cvs-current-subdir)
68 (defvar dont-change-disc)
70 ;;;; The parser
72 (defconst cvs-parse-known-commands
73 '("status" "add" "commit" "update" "remove" "checkout" "ci")
74 "List of CVS commands whose output is understood by the parser.")
76 (defun cvs-parse-buffer (parse-spec dont-change-disc &optional subdir)
77 "Parse current buffer according to PARSE-SPEC.
78 PARSE-SPEC is a function of no argument advancing the point and returning
79 either a fileinfo or t (if the matched text should be ignored) or
80 nil if it didn't match anything.
81 DONT-CHANGE-DISC just indicates whether the command was changing the disc
82 or not (useful to tell the difference between `cvs-examine' and `cvs-update'
83 output.
84 The path names should be interpreted as relative to SUBDIR (defaults
85 to the `default-directory').
86 Return a list of collected entries, or t if an error occurred."
87 (goto-char (point-min))
88 (let ((fileinfos ())
89 (cvs-current-dir "")
90 (case-fold-search nil)
91 (cvs-current-subdir (or subdir "")))
92 (while (not (or (eobp) (eq fileinfos t)))
93 (let ((ret (cvs-parse-run-table parse-spec)))
94 (cond
95 ;; it matched a known information message
96 ((cvs-fileinfo-p ret) (push ret fileinfos))
97 ;; it didn't match anything at all (impossible)
98 ((and (consp ret) (cvs-fileinfo-p (car ret)))
99 (setq fileinfos (append ret fileinfos)))
100 ((null ret) (setq fileinfos t))
101 ;; it matched something that should be ignored
102 (t nil))))
103 (nreverse fileinfos)))
106 ;; All those parsing macros/functions should return a success indicator
107 (defsubst cvs-parse-msg () (buffer-substring cvs-start (1- (point))))
109 ;;(defsubst COLLECT (exp) (push exp *result*))
110 ;;(defsubst PROG (e) t)
111 ;;(defmacro SEQ (&rest seqs) (cons 'and seqs))
113 (defmacro cvs-match (re &rest matches)
114 "Try to match RE and extract submatches.
115 If RE matches, advance the point until the line after the match and
116 then assign the variables as specified in MATCHES (via `setq')."
117 (cons 'cvs-do-match
118 (cons re (mapcar (lambda (match)
119 `(cons ',(car match) ,(cadr match)))
120 matches))))
122 (defun cvs-do-match (re &rest matches)
123 "Internal function for the `cvs-match' macro.
124 Match RE and if successful, execute MATCHES."
125 ;; Is it a match?
126 (when (looking-at re)
127 (goto-char (match-end 0))
128 ;; Skip the newline (unless we already are at the end of the buffer).
129 (when (and (eolp) (< (point) (point-max))) (forward-char))
130 ;; assign the matches
131 (dolist (match matches t)
132 (let ((val (cdr match)))
133 (set (car match) (if (integerp val) (match-string val) val))))))
135 (defmacro cvs-or (&rest alts)
136 "Try each one of the ALTS alternatives until one matches."
137 `(let ((-cvs-parse-point (point)))
138 ,(cons 'or
139 (mapcar (lambda (es)
140 `(or ,es (ignore (goto-char -cvs-parse-point))))
141 alts))))
142 (def-edebug-spec cvs-or t)
144 ;; This is how parser tables should be executed
145 (defun cvs-parse-run-table (parse-spec)
146 "Run PARSE-SPEC and provide sensible default behavior."
147 (unless (bolp) (forward-line 1)) ;this should never be needed
148 (let ((cvs-start (point)))
149 (cvs-or
150 (funcall parse-spec)
152 (cl-dolist (re cvs-parse-ignored-messages)
153 (when (cvs-match re) (cl-return t)))
155 ;; This is a parse error. Create a message-type fileinfo.
156 (and
157 (cvs-match ".*$")
158 (cvs-create-fileinfo 'MESSAGE cvs-current-dir " "
159 ;; (concat " Unknown msg: '"
160 (cvs-parse-msg) ;; "'")
161 :subtype 'ERROR)))))
164 (defun cvs-parsed-fileinfo (type path &optional directory &rest keys)
165 "Create a fileinfo.
166 TYPE can either be a type symbol or a cons of the form (TYPE . SUBTYPE).
167 PATH is the filename.
168 DIRECTORY influences the way PATH is interpreted:
169 - if a string, it denotes the directory in which PATH (which should then be
170 a plain file name with no directory component) resides.
171 - if it's nil, the PATH should not be trusted: if it has a directory
172 component, use it, else, assume it is relative to the current directory.
173 - else, the PATH should be trusted to be relative to the root directory
174 (i.e. if there is no directory component, it means the file is inside
175 the main directory).
176 The remaining KEYS are passed directly to `cvs-create-fileinfo'."
177 (let ((dir directory)
178 (file path))
179 ;; only trust the directory if it's a string
180 (unless (stringp directory)
181 ;; else, if the directory is true, the path should be trusted
182 (setq dir (or (file-name-directory path) (if directory "")))
183 (setq file (file-name-nondirectory path)))
185 (let ((type (if (consp type) (car type) type))
186 (subtype (if (consp type) (cdr type))))
187 (when dir (setq cvs-current-dir dir))
188 (apply 'cvs-create-fileinfo type
189 (concat cvs-current-subdir (or dir cvs-current-dir))
190 file (cvs-parse-msg) :subtype subtype keys))))
192 ;;;; CVS Process Parser Tables:
193 ;;;;
194 ;;;; The table for status and update could actually be merged since they
195 ;;;; don't conflict. But they don't overlap much either.
197 (defun cvs-parse-table ()
198 "Table of message objects for `cvs-parse-process'."
199 (let (c file dir path base-rev subtype)
200 (cvs-or
202 (cvs-parse-status)
203 (cvs-parse-merge)
204 (cvs-parse-commit)
206 ;; this is not necessary because the fileinfo merging will remove
207 ;; such duplicate info and luckily the second info is the one we want.
208 ;; (and (cvs-match "M \\(.*\\)$" (path 1))
209 ;; (cvs-parse-merge path))
211 ;; Normal file state indicator.
212 (and
213 (cvs-match "\\([MARCUPNJ?]\\) \\(.*\\)$" (c 1) (path 2))
214 ;; M: The file is modified by the user, and untouched in the repository.
215 ;; A: The file is "cvs add"ed, but not "cvs ci"ed.
216 ;; R: The file is "cvs remove"ed, but not "cvs ci"ed.
217 ;; C: Conflict
218 ;; U: The file is copied from the repository.
219 ;; P: The file was patched from the repository.
220 ;; ?: Unknown file.
221 (let ((code (aref c 0)))
222 (cvs-parsed-fileinfo
223 (pcase code
224 (?M 'MODIFIED)
225 (?A 'ADDED)
226 (?R 'REMOVED)
227 (?? 'UNKNOWN)
229 (if (not dont-change-disc) 'CONFLICT
230 ;; This is ambiguous. We should look for conflict markers in the
231 ;; file to decide between CONFLICT and NEED-MERGE. With CVS-1.10
232 ;; servers, this should not be necessary, because they return
233 ;; a complete merge output.
234 (with-temp-buffer
235 (ignore-errors (insert-file-contents path))
236 (goto-char (point-min))
237 (if (re-search-forward "^<<<<<<< " nil t)
238 'CONFLICT 'NEED-MERGE))))
239 (?J 'NEED-MERGE) ;not supported by standard CVS
240 ((or ?U ?P)
241 (if dont-change-disc 'NEED-UPDATE
242 (cons 'UP-TO-DATE (if (eq code ?U) 'UPDATED 'PATCHED)))))
243 path 'trust)))
245 (and
246 (cvs-match "pcl-cvs: descending directory \\(.*\\)$" (dir 1))
247 (setq cvs-current-subdir dir))
249 ;; A special cvs message
250 (and
251 (let ((case-fold-search t))
252 (cvs-match "cvs[.a-z]* [a-z]+: "))
253 (cvs-or
255 ;; CVS is descending a subdirectory
256 ;; (status says `examining' while update says `updating')
257 (and
258 (cvs-match "\\(Examining\\|Updating\\) \\(.*\\)$" (dir 2))
259 (let ((dir (if (string= "." dir) "" (file-name-as-directory dir))))
260 (cvs-parsed-fileinfo 'DIRCHANGE "." dir)))
262 ;; [-n update] A new (or pruned) directory appeared but isn't traversed
263 (and
264 (cvs-match "New directory `\\(.*\\)' -- ignored$" (dir 1))
265 ;; (cvs-parsed-fileinfo 'MESSAGE " " (file-name-as-directory dir))
266 ;; These messages either correspond to a true new directory
267 ;; that an update will bring in, or to a directory that's empty
268 ;; on the current branch (either because it only exists in other
269 ;; branches, or because it's been removed).
270 (if (ignore-errors
271 (with-temp-buffer
272 (ignore-errors
273 (insert-file-contents
274 (expand-file-name ".cvsignore" (file-name-directory dir))))
275 (goto-char (point-min))
276 (re-search-forward
277 (concat "^" (regexp-quote (file-name-nondirectory dir)) "/$")
278 nil t)))
279 t ;The user requested to ignore those messages.
280 (cvs-parsed-fileinfo '(NEED-UPDATE . NEW-DIR) dir t)))
282 ;; File removed, since it is removed (by third party) in repository.
283 (and
284 (cvs-or
285 ;; some cvs versions output quotes around these files
286 (cvs-match "warning: `\\(.*\\)' is not (any longer) pertinent$" (file 1))
287 (cvs-match "warning: \\(.*\\) is not (any longer) pertinent$" (file 1))
288 (cvs-match "`\\(.*\\)' is no longer in the repository$" (file 1))
289 (cvs-match "\\(.*\\) is no longer in the repository$" (file 1)))
290 (cvs-parsed-fileinfo
291 (if dont-change-disc '(NEED-UPDATE . REMOVED) 'DEAD) file))
293 ;; [add]
294 (and
295 (cvs-or
296 (cvs-match "scheduling file `\\(.*\\)' for addition.*$" (path 1))
297 (cvs-match "re-adding file \\(.*\\) (in place of .*)$" (path 1)))
298 (cvs-parsed-fileinfo 'ADDED path))
300 ;; [add] this will also show up as a `U <file>'
301 (and
302 (cvs-match "`?\\(.*?\\)'?, version \\(.*\\), resurrected$"
303 (path 1) (base-rev 2))
304 ;; FIXME: resurrection only brings back the original version,
305 ;; not the latest on the branch, so `up-to-date' is not always
306 ;; what we want.
307 (cvs-parsed-fileinfo '(UP-TO-DATE . RESURRECTED) path nil
308 :base-rev base-rev))
310 ;; [remove]
311 (and
312 (cvs-match "removed `\\(.*\\)'$" (path 1))
313 (cvs-parsed-fileinfo 'DEAD path))
315 ;; [remove,merge]
316 (and
317 (cvs-match "scheduling `\\(.*\\)' for removal$" (file 1))
318 (cvs-parsed-fileinfo 'REMOVED file))
320 ;; [update] File removed by you, but not cvs rm'd
321 (and
322 (cvs-match "warning: \\(.*\\) was lost$" (path 1))
323 (cvs-match (concat "U " (regexp-quote path) "$"))
324 (cvs-parsed-fileinfo (if dont-change-disc
325 'MISSING
326 '(UP-TO-DATE . UPDATED))
327 path))
329 ;; Mode conflicts (rather than contents)
330 (and
331 (cvs-match "conflict: ")
332 (cvs-or
333 (cvs-match "removed \\(.*\\) was modified by second party$"
334 (path 1) (subtype 'REMOVED))
335 (cvs-match "\\(.*\\) created independently by second party$"
336 (path 1) (subtype 'ADDED))
337 (cvs-match "\\(.*\\) is modified but no longer in the repository$"
338 (path 1) (subtype 'MODIFIED)))
339 (cvs-match (concat "C " (regexp-quote path)))
340 (cvs-parsed-fileinfo (cons 'CONFLICT subtype) path))
342 ;; Messages that should be shown to the user
343 (and
344 (cvs-or
345 (cvs-match "move away \\(.*\\); it is in the way$" (file 1))
346 (cvs-match "warning: new-born \\(.*\\) has disappeared$" (file 1))
347 (cvs-match "sticky tag .* for file `\\(.*\\)' is not a branch$"
348 (file 1)))
349 (cvs-parsed-fileinfo 'MESSAGE file))
351 ;; File unknown.
352 (and (cvs-match "use `.+ add' to create an entry for \\(.*\\)$" (path 1))
353 (cvs-parsed-fileinfo 'UNKNOWN path))
355 ;; [commit]
356 (and (cvs-match "Up-to-date check failed for `\\(.+\\)'$" (file 1))
357 (cvs-parsed-fileinfo 'NEED-MERGE file))
359 ;; We use cvs-execute-multi-dir but cvs can't handle it
360 ;; Probably because the cvs-client can but the cvs-server can't
361 (and (cvs-match ".* files with '?/'? in their name.*$")
362 (not cvs-execute-single-dir)
363 (setq cvs-execute-single-dir t)
364 (cvs-create-fileinfo
365 'MESSAGE "" " "
366 "*** Add (setq cvs-execute-single-dir t) to your .emacs ***
367 See the FAQ file or the variable's documentation for more info."))
369 ;; Cvs waits for a lock. Ignored: already handled by the process filter
370 (cvs-match "\\[..:..:..\\] \\(waiting for\\|obtained\\) .*lock in .*$")
371 ;; File you removed still exists. Ignore (will be noted as removed).
372 (cvs-match ".* should be removed and is still there$")
373 ;; just a note
374 (cvs-match "use ['`].+ commit' to \\sw+ th\\sw+ files? permanently$")
375 ;; [add,status] followed by a more complete status description anyway
376 (and (cvs-match "nothing known about \\(.*\\)$" (path 1))
377 (cvs-parsed-fileinfo 'DEAD path 'trust))
378 ;; [update] problem with patch
379 (cvs-match "checksum failure after patch to .*; will refetch$")
380 (cvs-match "refetching unpatchable files$")
381 ;; [commit]
382 (cvs-match "Rebuilding administrative file database$")
383 ;; ???
384 (cvs-match "--> Using per-directory sticky tag `.*'")
386 ;; CVS is running a *info program.
387 (and
388 (cvs-match "Executing.*$")
389 ;; Skip by any output the program may generate to stdout.
390 ;; Note that pcl-cvs will get seriously confused if the
391 ;; program prints anything to stderr.
392 (re-search-forward cvs-update-prog-output-skip-regexp))))
394 (and
395 (cvs-match "cvs[.ex]* \\[[a-z]+ aborted\\]:.*$")
396 (cvs-parsed-fileinfo 'MESSAGE ""))
398 ;; sadly you can't do much with these since the path is in the repository
399 (cvs-match "Directory .* added to the repository$")
403 (defun cvs-parse-merge ()
404 (let (path base-rev head-rev type)
405 ;; A merge (maybe with a conflict).
406 (and
407 (cvs-match "RCS file: .*$")
408 ;; Squirrel away info about the files that were retrieved for merging
409 (cvs-match "retrieving revision \\([0-9.]+\\)$" (base-rev 1))
410 (cvs-match "retrieving revision \\([0-9.]+\\)$" (head-rev 1))
411 (cvs-match "Merging differences between [0-9.]+ and [0-9.]+ into \\(.*\\)$"
412 (path 1))
414 ;; eat up potential conflict warnings
415 (cvs-or (cvs-match "\\(rcs\\)?merge:?\\( warning\\)?: \\(overlaps\\|conflicts\\) \\(or other problems \\)?during merge$" (type 'CONFLICT)) t)
416 (cvs-or
417 (and
418 (cvs-match "cvs[.ex]* [a-z]+: ")
419 (cvs-or
420 (cvs-match "conflicts found in \\(.*\\)$" (path 1) (type 'CONFLICT))
421 (cvs-match "could not merge .*$")
422 (cvs-match "restoring \\(.*\\) from backup file .*$" (path 1))))
425 ;; Is it a successful merge?
426 ;; Figure out result of merging (ie, was there a conflict?)
427 (let ((qfile (regexp-quote path)))
428 (cvs-or
429 ;; Conflict
430 (and
431 (cvs-match (concat "C \\(.*" qfile "\\)$") (path 1) (type 'CONFLICT))
432 ;; C might be followed by a "spurious" U for non-mergable files
433 (cvs-or (cvs-match (concat "U \\(.*" qfile "\\)$")) t))
434 ;; Successful merge
435 (cvs-match (concat "M \\(.*" qfile "\\)$") (path 1))
436 ;; The file already contained the modifications
437 (cvs-match (concat "^\\(.*" qfile
438 "\\) already contains the differences between .*$")
439 (path 1) (type '(UP-TO-DATE . MERGED)))
441 ;; FIXME: PATH might not be set yet. Sometimes the only path
442 ;; information is in `RCS file: ...' (yuck!!).
443 (cvs-parsed-fileinfo (if dont-change-disc 'NEED-MERGE
444 (or type '(MODIFIED . MERGED))) path nil
445 :merge (cons base-rev head-rev))))))
447 (defun cvs-parse-status ()
448 (let (nofile path base-rev head-rev type)
449 (and
450 (cvs-match
451 "===================================================================$")
452 (cvs-match "File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: "
453 (nofile 1) (path 2))
454 (cvs-or
455 (cvs-match "Needs \\(Checkout\\|Patch\\)$"
456 (type (if nofile 'MISSING 'NEED-UPDATE)))
457 (cvs-match "Up-to-date$"
458 (type (if nofile '(UP-TO-DATE . REMOVED) 'UP-TO-DATE)))
459 (cvs-match "File had conflicts on merge$" (type 'MODIFIED))
460 (cvs-match ".*[Cc]onflict.*$" (type 'CONFLICT))
461 (cvs-match "Locally Added$" (type 'ADDED))
462 (cvs-match "Locally Removed$" (type 'REMOVED))
463 (cvs-match "Locally Modified$" (type 'MODIFIED))
464 (cvs-match "Needs Merge$" (type 'NEED-MERGE))
465 (cvs-match "Entry Invalid" (type '(NEED-MERGE . REMOVED)))
466 (cvs-match ".*$" (type 'UNKNOWN)))
467 (cvs-match "$")
468 (cvs-or
469 (cvs-match " *Version:[ \t]*\\([0-9.]+\\).*$" (base-rev 1))
470 ;; NOTE: there's no date on the end of the following for server mode...
471 (cvs-match " *Working revision:[ \t]*-?\\([0-9.]+\\).*$" (base-rev 1))
472 ;; Let's not get all worked up if the format changes a bit
473 (cvs-match " *Working revision:.*$"))
474 (cvs-or
475 (cvs-match " *RCS Version:[ \t]*\\([0-9.]+\\)[ \t]*.*$" (head-rev 1))
476 (cvs-match " *Repository revision:[ \t]*\\([0-9.]+\\)[ \t]*\\(.*\\)$"
477 (head-rev 1))
478 (cvs-match " *Repository revision:.*"))
479 (cvs-or (cvs-match " *Expansion option:.*") t) ;Optional CVSNT thingie.
480 (cvs-or (cvs-match " *Commit Identifier:.*") t) ;Optional CVSNT thingie.
481 (cvs-or
482 (and ;; Sometimes those fields are missing.
483 (cvs-match " *Sticky Tag:[ \t]*\\(.*\\)$") ; FIXME: use it.
484 (cvs-match " *Sticky Date:[ \t]*\\(.*\\)$") ; FIXME: use it.
485 (cvs-match " *Sticky Options:[ \t]*\\(.*\\)$")) ; FIXME: use it.
487 (cvs-or (cvs-match " *Merge From:.*") t) ;Optional CVSNT thingie.
488 (cvs-match "$")
489 ;; ignore the tags-listing in the case of `status -v'
490 (cvs-or (cvs-match " *Existing Tags:\n\\(\t.*\n\\)*$") t)
491 (cvs-parsed-fileinfo type path nil
492 :base-rev base-rev
493 :head-rev head-rev))))
495 (defun cvs-parse-commit ()
496 (let (path file base-rev subtype)
497 (cvs-or
499 (and
500 (cvs-or
501 (cvs-match "\\(Checking in\\|Removing\\) \\(.*\\);$" (path 2))
503 (cvs-match ".*,v <-- \\(.*\\)$" (file 1))
504 (cvs-or
505 ;; deletion
506 (cvs-match "new revision: delete; previous revision: \\([0-9.]*\\)$"
507 (subtype 'REMOVED) (base-rev 1))
508 ;; addition
509 (cvs-match "initial revision: \\([0-9.]*\\)$"
510 (subtype 'ADDED) (base-rev 1))
511 ;; update
512 (cvs-match "new revision: \\([0-9.]*\\); previous revision: .*$"
513 (subtype 'COMMITTED) (base-rev 1)))
514 (cvs-or (cvs-match "done$") t)
515 ;; In cvs-1.12.9 commit messages have been changed and became
516 ;; ambiguous. More specifically, the `path' above is not given.
517 ;; We assume here that in future releases the corresponding info will
518 ;; be put into `file'.
519 (progn
520 ;; Try to remove the temp files used by VC.
521 (vc-delete-automatic-version-backups (expand-file-name (or path file)))
522 ;; it's important here not to rely on the default directory management
523 ;; because `cvs commit' might begin by a series of Examining messages
524 ;; so the processing of the actual checkin messages might begin with
525 ;; a `current-dir' set to something different from ""
526 (cvs-parsed-fileinfo (cons 'UP-TO-DATE subtype)
527 (or path file) 'trust
528 :base-rev base-rev)))
530 ;; useless message added before the actual addition: ignored
531 (cvs-match "RCS file: .*\ndone$"))))
534 (provide 'pcvs-parse)
536 ;;; pcvs-parse.el ends here