1 ;;; pcvs-parse.el --- the CVS output parser
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; - when merging a modified file, if the merge says that the file already
30 ;; contained in the changes, it marks the file as `up-to-date' although
31 ;; it might still contain further changes.
32 ;; Example: merging a zero-change commit.
36 (eval-when-compile (require 'cl))
41 ;; imported from pcvs.el
42 (defvar cvs-execute-single-dir)
46 (defcustom cvs-update-prog-output-skip-regexp "$"
47 "A regexp that matches the end of the output from all cvs update programs.
48 That is, output from any programs that are run by CVS (by the flag -u
49 in the `modules' file - see cvs(5)) when `cvs update' is performed should
50 terminate with a line that this regexp matches. It is enough that
51 some part of the line is matched.
53 The default (a single $) fits programs without output."
55 :type '(regexp :value "$"))
57 (defcustom cvs-parse-ignored-messages
58 '("Executing ssh-askpass to query the password.*$"
59 ".*Remote host denied X11 forwarding.*$")
60 "A list of regexps matching messages that should be ignored by the parser.
61 Each regexp should match a whole set of lines and should hence be terminated
64 :type '(repeat regexp))
66 ;; a few more defvars just to shut up the compiler
68 (defvar cvs-current-dir)
69 (defvar cvs-current-subdir)
70 (defvar dont-change-disc)
74 (defconst cvs-parse-known-commands
75 '("status" "add" "commit" "update" "remove" "checkout" "ci")
76 "List of CVS commands whose output is understood by the parser.")
78 (defun cvs-parse-buffer (parse-spec dont-change-disc &optional subdir)
79 "Parse current buffer according to PARSE-SPEC.
80 PARSE-SPEC is a function of no argument advancing the point and returning
81 either a fileinfo or t (if the matched text should be ignored) or
82 nil if it didn't match anything.
83 DONT-CHANGE-DISC just indicates whether the command was changing the disc
84 or not (useful to tell the difference between `cvs-examine' and `cvs-update'
86 The path names should be interpreted as relative to SUBDIR (defaults
87 to the `default-directory').
88 Return a list of collected entries, or t if an error occurred."
89 (goto-char (point-min))
92 (case-fold-search nil)
93 (cvs-current-subdir (or subdir "")))
94 (while (not (or (eobp) (eq fileinfos t)))
95 (let ((ret (cvs-parse-run-table parse-spec)))
97 ;; it matched a known information message
98 ((cvs-fileinfo-p ret) (push ret fileinfos))
99 ;; it didn't match anything at all (impossible)
100 ((and (consp ret) (cvs-fileinfo-p (car ret)))
101 (setq fileinfos (append ret fileinfos)))
102 ((null ret) (setq fileinfos t))
103 ;; it matched something that should be ignored
105 (nreverse fileinfos)))
108 ;; All those parsing macros/functions should return a success indicator
109 (defsubst cvs-parse-msg () (buffer-substring cvs-start (1- (point))))
111 ;;(defsubst COLLECT (exp) (push exp *result*))
112 ;;(defsubst PROG (e) t)
113 ;;(defmacro SEQ (&rest seqs) (cons 'and seqs))
115 (defmacro cvs-match (re &rest matches)
116 "Try to match RE and extract submatches.
117 If RE matches, advance the point until the line after the match and
118 then assign the variables as specified in MATCHES (via `setq')."
120 (cons re (mapcar (lambda (match)
121 `(cons ',(first match) ,(second match)))
124 (defun cvs-do-match (re &rest matches)
125 "Internal function for the `cvs-match' macro.
126 Match RE and if successful, execute MATCHES."
128 (when (looking-at re)
129 (goto-char (match-end 0))
130 ;; Skip the newline (unless we already are at the end of the buffer).
131 (when (and (eolp) (< (point) (point-max))) (forward-char))
132 ;; assign the matches
133 (dolist (match matches t)
134 (let ((val (cdr match)))
135 (set (car match) (if (integerp val) (match-string val) val))))))
137 (defmacro cvs-or (&rest alts)
138 "Try each one of the ALTS alternatives until one matches."
139 `(let ((-cvs-parse-point (point)))
142 `(or ,es (ignore (goto-char -cvs-parse-point))))
144 (def-edebug-spec cvs-or t)
146 ;; This is how parser tables should be executed
147 (defun cvs-parse-run-table (parse-spec)
148 "Run PARSE-SPEC and provide sensible default behavior."
149 (unless (bolp) (forward-line 1)) ;this should never be needed
150 (let ((cvs-start (point)))
154 (dolist (re cvs-parse-ignored-messages)
155 (when (cvs-match re) (return t)))
157 ;; This is a parse error. Create a message-type fileinfo.
160 (cvs-create-fileinfo 'MESSAGE cvs-current-dir " "
161 ;; (concat " Unknown msg: '"
162 (cvs-parse-msg) ;; "'")
166 (defun cvs-parsed-fileinfo (type path &optional directory &rest keys)
168 TYPE can either be a type symbol or a cons of the form (TYPE . SUBTYPE).
169 PATH is the filename.
170 DIRECTORY influences the way PATH is interpreted:
171 - if it's a string, it denotes the directory in which PATH (which should then be
172 a plain file name with no directory component) resides.
173 - if it's nil, the PATH should not be trusted: if it has a directory
174 component, use it, else, assume it is relative to the current directory.
175 - else, the PATH should be trusted to be relative to the root
176 directory (i.e. if there is no directory component, it means the file
177 is inside the main directory).
178 The remaining KEYS are passed directly to `cvs-create-fileinfo'."
179 (let ((dir directory)
181 ;; only trust the directory if it's a string
182 (unless (stringp directory)
183 ;; else, if the directory is true, the path should be trusted
184 (setq dir (or (file-name-directory path) (if directory "")))
185 (setq file (file-name-nondirectory path)))
187 (let ((type (if (consp type) (car type) type))
188 (subtype (if (consp type) (cdr type))))
189 (when dir (setq cvs-current-dir dir))
190 (apply 'cvs-create-fileinfo type
191 (concat cvs-current-subdir (or dir cvs-current-dir))
192 file (cvs-parse-msg) :subtype subtype keys))))
194 ;;;; CVS Process Parser Tables:
196 ;;;; The table for status and update could actually be merged since they
197 ;;;; don't conflict. But they don't overlap much either.
199 (defun cvs-parse-table ()
200 "Table of message objects for `cvs-parse-process'."
201 (let (c file dir path base-rev subtype)
208 ;; this is not necessary because the fileinfo merging will remove
209 ;; such duplicate info and luckily the second info is the one we want.
210 ;; (and (cvs-match "M \\(.*\\)$" (path 1))
211 ;; (cvs-parse-merge path))
213 ;; Normal file state indicator.
215 (cvs-match "\\([MARCUPNJ?]\\) \\(.*\\)$" (c 1) (path 2))
216 ;; M: The file is modified by the user, and untouched in the repository.
217 ;; A: The file is "cvs add"ed, but not "cvs ci"ed.
218 ;; R: The file is "cvs remove"ed, but not "cvs ci"ed.
220 ;; U: The file is copied from the repository.
221 ;; P: The file was patched from the repository.
223 (let ((code (aref c 0)))
231 (if (not dont-change-disc) 'CONFLICT
232 ;; This is ambiguous. We should look for conflict markers in the
233 ;; file to decide between CONFLICT and NEED-MERGE. With CVS-1.10
234 ;; servers, this should not be necessary, because they return
235 ;; a complete merge output.
237 (ignore-errors (insert-file-contents path))
238 (goto-char (point-min))
239 (if (re-search-forward "^<<<<<<< " nil t)
240 'CONFLICT 'NEED-MERGE))))
241 (?J 'NEED-MERGE) ;not supported by standard CVS
243 (if dont-change-disc 'NEED-UPDATE
244 (cons 'UP-TO-DATE (if (eq code ?U) 'UPDATED 'PATCHED)))))
248 (cvs-match "pcl-cvs: descending directory \\(.*\\)$" (dir 1))
249 (setq cvs-current-subdir dir))
251 ;; A special cvs message
253 (let ((case-fold-search t))
254 (cvs-match "cvs[.a-z]* [a-z]+: "))
257 ;; CVS is descending a subdirectory
258 ;; (status says `examining' while update says `updating')
260 (cvs-match "\\(Examining\\|Updating\\) \\(.*\\)$" (dir 2))
261 (let ((dir (if (string= "." dir) "" (file-name-as-directory dir))))
262 (cvs-parsed-fileinfo 'DIRCHANGE "." dir)))
264 ;; [-n update] A new (or pruned) directory appeared but isn't traversed
266 (cvs-match "New directory `\\(.*\\)' -- ignored$" (dir 1))
267 ;; (cvs-parsed-fileinfo 'MESSAGE " " (file-name-as-directory dir))
268 ;; These messages either correspond to a true new directory
269 ;; that an update will bring in, or to a directory that's empty
270 ;; on the current branch (either because it only exists in other
271 ;; branches, or because it's been removed).
275 (insert-file-contents
276 (expand-file-name ".cvsignore" (file-name-directory dir))))
277 (goto-char (point-min))
279 (concat "^" (regexp-quote (file-name-nondirectory dir)) "/$")
281 t ;The user requested to ignore those messages.
282 (cvs-parsed-fileinfo '(NEED-UPDATE . NEW-DIR) dir t)))
284 ;; File removed, since it is removed (by third party) in repository.
287 ;; some cvs versions output quotes around these files
288 (cvs-match "warning: `\\(.*\\)' is not (any longer) pertinent$" (file 1))
289 (cvs-match "warning: \\(.*\\) is not (any longer) pertinent$" (file 1))
290 (cvs-match "`\\(.*\\)' is no longer in the repository$" (file 1))
291 (cvs-match "\\(.*\\) is no longer in the repository$" (file 1)))
293 (if dont-change-disc '(NEED-UPDATE . REMOVED) 'DEAD) file))
298 (cvs-match "scheduling file `\\(.*\\)' for addition.*$" (path 1))
299 (cvs-match "re-adding file \\(.*\\) (in place of .*)$" (path 1)))
300 (cvs-parsed-fileinfo 'ADDED path))
302 ;; [add] this will also show up as a `U <file>'
304 (cvs-match "`?\\(.*?\\)'?, version \\(.*\\), resurrected$"
305 (path 1) (base-rev 2))
306 ;; FIXME: resurrection only brings back the original version,
307 ;; not the latest on the branch, so `up-to-date' is not always
309 (cvs-parsed-fileinfo '(UP-TO-DATE . RESURRECTED) path nil
314 (cvs-match "removed `\\(.*\\)'$" (path 1))
315 (cvs-parsed-fileinfo 'DEAD path))
319 (cvs-match "scheduling `\\(.*\\)' for removal$" (file 1))
320 (cvs-parsed-fileinfo 'REMOVED file))
322 ;; [update] File removed by you, but not cvs rm'd
324 (cvs-match "warning: \\(.*\\) was lost$" (path 1))
325 (cvs-match (concat "U " (regexp-quote path) "$"))
326 (cvs-parsed-fileinfo (if dont-change-disc
328 '(UP-TO-DATE . UPDATED))
331 ;; Mode conflicts (rather than contents)
333 (cvs-match "conflict: ")
335 (cvs-match "removed \\(.*\\) was modified by second party$"
336 (path 1) (subtype 'REMOVED))
337 (cvs-match "\\(.*\\) created independently by second party$"
338 (path 1) (subtype 'ADDED))
339 (cvs-match "\\(.*\\) is modified but no longer in the repository$"
340 (path 1) (subtype 'MODIFIED)))
341 (cvs-match (concat "C " (regexp-quote path)))
342 (cvs-parsed-fileinfo (cons 'CONFLICT subtype) path))
344 ;; Messages that should be shown to the user
347 (cvs-match "move away \\(.*\\); it is in the way$" (file 1))
348 (cvs-match "warning: new-born \\(.*\\) has disappeared$" (file 1))
349 (cvs-match "sticky tag .* for file `\\(.*\\)' is not a branch$"
351 (cvs-parsed-fileinfo 'MESSAGE file))
354 (and (cvs-match "use `.+ add' to create an entry for \\(.*\\)$" (path 1))
355 (cvs-parsed-fileinfo 'UNKNOWN path))
358 (and (cvs-match "Up-to-date check failed for `\\(.+\\)'$" (file 1))
359 (cvs-parsed-fileinfo 'NEED-MERGE file))
361 ;; We use cvs-execute-multi-dir but cvs can't handle it
362 ;; Probably because the cvs-client can but the cvs-server can't
363 (and (cvs-match ".* files with '?/'? in their name.*$")
364 (not cvs-execute-single-dir)
365 (setq cvs-execute-single-dir t)
368 "*** Add (setq cvs-execute-single-dir t) to your .emacs ***
369 See the FAQ file or the variable's documentation for more info."))
371 ;; Cvs waits for a lock. Ignored: already handled by the process filter
372 (cvs-match "\\[..:..:..\\] \\(waiting for\\|obtained\\) .*lock in .*$")
373 ;; File you removed still exists. Ignore (will be noted as removed).
374 (cvs-match ".* should be removed and is still there$")
376 (cvs-match "use ['`].+ commit' to \\sw+ th\\sw+ files? permanently$")
377 ;; [add,status] followed by a more complete status description anyway
378 (and (cvs-match "nothing known about \\(.*\\)$" (path 1))
379 (cvs-parsed-fileinfo 'DEAD path 'trust))
380 ;; [update] problem with patch
381 (cvs-match "checksum failure after patch to .*; will refetch$")
382 (cvs-match "refetching unpatchable files$")
384 (cvs-match "Rebuilding administrative file database$")
386 (cvs-match "--> Using per-directory sticky tag `.*'")
388 ;; CVS is running a *info program.
390 (cvs-match "Executing.*$")
391 ;; Skip by any output the program may generate to stdout.
392 ;; Note that pcl-cvs will get seriously confused if the
393 ;; program prints anything to stderr.
394 (re-search-forward cvs-update-prog-output-skip-regexp))))
397 (cvs-match "cvs[.ex]* \\[[a-z]+ aborted\\]:.*$")
398 (cvs-parsed-fileinfo 'MESSAGE ""))
400 ;; sadly you can't do much with these since the path is in the repository
401 (cvs-match "Directory .* added to the repository$")
405 (defun cvs-parse-merge ()
406 (let (path base-rev head-rev type)
407 ;; A merge (maybe with a conflict).
409 (cvs-match "RCS file: .*$")
410 ;; Squirrel away info about the files that were retrieved for merging
411 (cvs-match "retrieving revision \\([0-9.]+\\)$" (base-rev 1))
412 (cvs-match "retrieving revision \\([0-9.]+\\)$" (head-rev 1))
413 (cvs-match "Merging differences between [0-9.]+ and [0-9.]+ into \\(.*\\)$"
416 ;; eat up potential conflict warnings
417 (cvs-or (cvs-match "\\(rcs\\)?merge:?\\( warning\\)?: \\(overlaps\\|conflicts\\) \\(or other problems \\)?during merge$" (type 'CONFLICT)) t)
420 (cvs-match "cvs[.ex]* [a-z]+: ")
422 (cvs-match "conflicts found in \\(.*\\)$" (path 1) (type 'CONFLICT))
423 (cvs-match "could not merge .*$")
424 (cvs-match "restoring \\(.*\\) from backup file .*$" (path 1))))
427 ;; Is it a succesful merge?
428 ;; Figure out result of merging (ie, was there a conflict?)
429 (let ((qfile (regexp-quote path)))
433 (cvs-match (concat "C \\(.*" qfile "\\)$") (path 1) (type 'CONFLICT))
434 ;; C might be followed by a "suprious" U for non-mergeable files
435 (cvs-or (cvs-match (concat "U \\(.*" qfile "\\)$")) t))
437 (cvs-match (concat "M \\(.*" qfile "\\)$") (path 1))
438 ;; The file already contained the modifications
439 (cvs-match (concat "^\\(.*" qfile
440 "\\) already contains the differences between .*$")
441 (path 1) (type '(UP-TO-DATE . MERGED)))
443 ;; FIXME: PATH might not be set yet. Sometimes the only path
444 ;; information is in `RCS file: ...' (yuck!!).
445 (cvs-parsed-fileinfo (if dont-change-disc 'NEED-MERGE
446 (or type '(MODIFIED . MERGED))) path nil
447 :merge (cons base-rev head-rev))))))
449 (defun cvs-parse-status ()
450 (let (nofile path base-rev head-rev type)
453 "===================================================================$")
454 (cvs-match "File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: "
457 (cvs-match "Needs \\(Checkout\\|Patch\\)$"
458 (type (if nofile 'MISSING 'NEED-UPDATE)))
459 (cvs-match "Up-to-date$"
460 (type (if nofile '(UP-TO-DATE . REMOVED) 'UP-TO-DATE)))
461 (cvs-match "File had conflicts on merge$" (type 'MODIFIED))
462 (cvs-match ".*[Cc]onflict.*$" (type 'CONFLICT))
463 (cvs-match "Locally Added$" (type 'ADDED))
464 (cvs-match "Locally Removed$" (type 'REMOVED))
465 (cvs-match "Locally Modified$" (type 'MODIFIED))
466 (cvs-match "Needs Merge$" (type 'NEED-MERGE))
467 (cvs-match "Entry Invalid" (type '(NEED-MERGE . REMOVED)))
468 (cvs-match ".*$" (type 'UNKNOWN)))
471 (cvs-match " *Version:[ \t]*\\([0-9.]+\\).*$" (base-rev 1))
472 ;; NOTE: there's no date on the end of the following for server mode...
473 (cvs-match " *Working revision:[ \t]*-?\\([0-9.]+\\).*$" (base-rev 1))
474 ;; Let's not get all worked up if the format changes a bit
475 (cvs-match " *Working revision:.*$"))
477 (cvs-match " *RCS Version:[ \t]*\\([0-9.]+\\)[ \t]*.*$" (head-rev 1))
478 (cvs-match " *Repository revision:[ \t]*\\([0-9.]+\\)[ \t]*\\(.*\\)$"
480 (cvs-match " *Repository revision:.*"))
481 (cvs-or (cvs-match " *Expansion option:.*") t) ;Optional CVSNT thingie.
482 (cvs-or (cvs-match " *Commit Identifier:.*") t) ;Optional CVSNT thingie.
484 (and ;; Sometimes those fields are missing.
485 (cvs-match " *Sticky Tag:[ \t]*\\(.*\\)$") ; FIXME: use it.
486 (cvs-match " *Sticky Date:[ \t]*\\(.*\\)$") ; FIXME: use it.
487 (cvs-match " *Sticky Options:[ \t]*\\(.*\\)$")) ; FIXME: use it.
489 (cvs-or (cvs-match " *Merge From:.*") t) ;Optional CVSNT thingie.
491 ;; ignore the tags-listing in the case of `status -v'
492 (cvs-or (cvs-match " *Existing Tags:\n\\(\t.*\n\\)*$") t)
493 (cvs-parsed-fileinfo type path nil
495 :head-rev head-rev))))
497 (defun cvs-parse-commit ()
498 (let (path file base-rev subtype)
503 (cvs-match "\\(Checking in\\|Removing\\) \\(.*\\);$" (path 2))
505 (cvs-match ".*,v <-- \\(.*\\)$" (file 1))
508 (cvs-match "new revision: delete; previous revision: \\([0-9.]*\\)$"
509 (subtype 'REMOVED) (base-rev 1))
511 (cvs-match "initial revision: \\([0-9.]*\\)$"
512 (subtype 'ADDED) (base-rev 1))
514 (cvs-match "new revision: \\([0-9.]*\\); previous revision: .*$"
515 (subtype 'COMMITTED) (base-rev 1)))
516 (cvs-or (cvs-match "done$") t)
517 ;; In cvs-1.12.9 commit messages have been changed and became
518 ;; ambiguous. More specifically, the `path' above is not given.
519 ;; We assume here that in future releases the corresponding info will
520 ;; be put into `file'.
522 ;; Try to remove the temp files used by VC.
523 (vc-delete-automatic-version-backups (expand-file-name (or path file)))
524 ;; it's important here not to rely on the default directory management
525 ;; because `cvs commit' might begin by a series of Examining messages
526 ;; so the processing of the actual checkin messages might begin with
527 ;; a `current-dir' set to something different from ""
528 (cvs-parsed-fileinfo (cons 'UP-TO-DATE subtype)
529 (or path file) 'trust
530 :base-rev base-rev)))
532 ;; useless message added before the actual addition: ignored
533 (cvs-match "RCS file: .*\ndone$"))))
536 (provide 'pcvs-parse)
538 ;; arch-tag: 35418375-1a23-40a0-957d-96b0262f91d6
539 ;;; pcvs-parse.el ends here