* pcvs.el: Add a minimal leading commentary.
[emacs.git] / lisp / pcvs-parse.el
blob818506b6916097a061ec1cd5f62db4330b60805c
1 ;;; pcvs-parse.el --- The CVS output parser
3 ;; Copyright (C) 1991-2000 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: pcl-cvs
7 ;; Version: $Name: $
8 ;; Revision: $Id: pcvs-parse.el,v 1.1 2000/03/11 03:42:29 monnier Exp $
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 2, or (at your option)
15 ;; 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
30 ;;; Code:
32 (eval-when-compile (require 'cl))
34 (require 'pcvs-util)
35 (require 'pcvs-info)
37 ;; imported from pcvs.el
38 (defvar cvs-execute-single-dir)
40 ;; parse vars
42 (defcustom cvs-update-prog-output-skip-regexp "$"
43 "*A regexp that matches the end of the output from all cvs update programs.
44 That is, output from any programs that are run by CVS (by the flag -u
45 in the `modules' file - see cvs(5)) when `cvs update' is performed should
46 terminate with a line that this regexp matches. It is enough that
47 some part of the line is matched.
49 The default (a single $) fits programs without output."
50 :group 'pcl-cvs
51 :type '(regexp :value "$"))
53 (defcustom cvs-parse-ignored-messages
54 '("Executing ssh-askpass to query the password.*$"
55 ".*Remote host denied X11 forwarding.*$")
56 "*A list of regexps matching messages that should be ignored by the parser.
57 Each regexp should match a whole set of lines and should hence be terminated
58 by `$'."
59 :group 'pcl-cvs
60 :type '(repeat regexp))
62 ;; a few more defvars just to shut up the compiler
63 (defvar cvs-start)
64 (defvar cvs-current-dir)
65 (defvar cvs-current-subdir)
66 (defvar dont-change-disc)
68 ;;;; The parser
70 (defconst cvs-parse-known-commands
71 '("status" "add" "commit" "update" "remove" "checkout" "ci")
72 "List of CVS commands whose output is understood by the parser.")
74 (defun cvs-parse-buffer (parse-spec dont-change-disc &optional subdir)
75 "Parse current buffer according to PARSE-SPEC.
76 PARSE-SPEC is a function of no argument advancing the point and returning
77 either a fileinfo or t (if the matched text should be ignored) or
78 nil if it didn't match anything.
79 DONT-CHANGE-DISC just indicates whether the command was changing the disc
80 or not (useful to tell the difference btween `cvs-examine' and `cvs-update'
81 ouytput.
82 The path names should be interpreted as relative to SUBDIR (defaults
83 to the `default-directory').
84 Return a list of collected entries, or t if an error occured."
85 (goto-char (point-min))
86 (let ((fileinfos ())
87 (cvs-current-dir "")
88 (case-fold-search nil)
89 (cvs-current-subdir (or subdir "")))
90 (while (not (or (eobp) (eq fileinfos t)))
91 (let ((ret (cvs-parse-run-table parse-spec)))
92 (cond
93 ;; it matched a known information message
94 ((cvs-fileinfo-p ret) (push ret fileinfos))
95 ;; it didn't match anything at all (impossible)
96 ((and (consp ret) (cvs-fileinfo-p (car ret)))
97 (setq fileinfos (append ret fileinfos)))
98 ((null ret) (setq fileinfos t))
99 ;; it matched something that should be ignored
100 (t nil))))
101 (nreverse fileinfos)))
104 ;; All those parsing macros/functions should return a success indicator
105 (defsubst cvs-parse-msg () (buffer-substring cvs-start (1- (point))))
107 ;;(defsubst COLLECT (exp) (push exp *result*))
108 ;;(defsubst PROG (e) t)
109 ;;(defmacro SEQ (&rest seqs) (cons 'and seqs))
111 (defmacro cvs-match (re &rest matches)
112 "Try to match RE and extract submatches.
113 If RE matches, advance the point until the line after the match and
114 then assign the variables as specified in MATCHES (via `setq')."
115 (cons 'cvs-do-match
116 (cons re (mapcar (lambda (match)
117 `(cons ',(first match) ,(second match)))
118 matches))))
120 (defun cvs-do-match (re &rest matches)
121 "Internal function for the `cvs-match' macro.
122 Match RE and if successful, execute MATCHES."
123 ;; Is it a match?
124 (when (looking-at re)
125 (goto-char (match-end 0))
126 ;; Skip the newline (unless we already are at the end of the buffer).
127 (when (and (eolp) (< (point) (point-max))) (forward-char))
128 ;; assign the matches
129 (dolist (match matches t)
130 (let ((val (cdr match)))
131 (set (car match) (if (integerp val) (match-string val) val))))))
133 (defmacro cvs-or (&rest alts)
134 "Try each one of the ALTS alternatives until one matches."
135 `(let ((-cvs-parse-point (point)))
136 ,(cons 'or
137 (mapcar (lambda (es)
138 `(or ,es (ignore (goto-char -cvs-parse-point))))
139 alts))))
140 (def-edebug-spec cvs-or t)
142 ;; This is how parser tables should be executed
143 (defun cvs-parse-run-table (parse-spec)
144 "Run PARSE-SPEC and provide sensible default behavior."
145 (unless (bolp) (forward-line 1)) ;this should never be needed
146 (let ((cvs-start (point)))
147 (cvs-or
148 (funcall parse-spec)
150 (dolist (re cvs-parse-ignored-messages)
151 (when (cvs-match re) (return t)))
153 ;; This is a parse error. Create a message-type fileinfo.
154 (and
155 (cvs-match ".*$")
156 (cvs-create-fileinfo 'MESSAGE cvs-current-dir " "
157 (concat " Unknown msg: '" (cvs-parse-msg) "'")
158 :subtype 'ERROR)))))
161 (defun cvs-parsed-fileinfo (type path &optional directory &rest keys)
162 "Create a fileinfo.
163 TYPE can either be a type symbol or a cons of the form (TYPE . SUBTYPE).
164 PATH is the filename.
165 DIRECTORY influences the way PATH is interpreted:
166 - if it's a string, it denotes the directory in which PATH (which should then be
167 a plain file name with no directory component) resides.
168 - if it's nil, the PATH should not be trusted: if it has a directory
169 component, use it, else, assume it is relative to the current directory.
170 - else, the PATH should be trusted to be relative to the root
171 directory (i.e. if there is no directory component, it means the file
172 is inside the main directory).
173 The remaining KEYS are passed directly to `cvs-create-fileinfo'."
174 (let ((dir directory)
175 (file path))
176 ;; only trust the directory if it's a string
177 (unless (stringp directory)
178 ;; else, if the directory is true, the path should be trusted
179 (setq dir (or (file-name-directory path) (if directory "")))
180 (setq file (file-name-nondirectory path)))
182 (let ((type (if (consp type) (car type) type))
183 (subtype (if (consp type) (cdr type))))
184 (when dir (setq cvs-current-dir dir))
185 (apply 'cvs-create-fileinfo type
186 (concat cvs-current-subdir (or dir cvs-current-dir))
187 file (cvs-parse-msg) :subtype subtype keys))))
190 ;;;; CVS Process Parser Tables:
191 ;;;;
192 ;;;; The table for status and update could actually be merged since they
193 ;;;; don't conflict. But they don't overlap much either.
195 (defun cvs-parse-table ()
196 "Table of message objects for `cvs-parse-process'."
197 (let (c file dir path type base-rev subtype)
198 (cvs-or
200 (cvs-parse-status)
201 (cvs-parse-merge)
202 (cvs-parse-commit)
204 ;; this is not necessary because the fileinfo merging will remove
205 ;; such duplicate info and luckily the second info is the one we want.
206 ;; (and (cvs-match "M \\(.*\\)$" (path 1))
207 ;; (cvs-parse-merge path))
209 ;; Normal file state indicator.
210 (and
211 (cvs-match "\\([MARCUPNJ?]\\) \\(.*\\)$" (c 1) (path 2))
212 ;; M: The file is modified by the user, and untouched in the repository.
213 ;; A: The file is "cvs add"ed, but not "cvs ci"ed.
214 ;; R: The file is "cvs remove"ed, but not "cvs ci"ed.
215 ;; C: Conflict
216 ;; U: The file is copied from the repository.
217 ;; P: The file was patched from the repository.
218 ;; ?: Unknown file.
219 (let ((code (aref c 0)))
220 (cvs-parsed-fileinfo (case code
221 (?M 'MODIFIED)
222 (?A 'ADDED)
223 (?R 'REMOVED)
224 (?? 'UNKNOWN)
225 (?C 'CONFLICT) ;(if dont-change-disc 'NEED-MERGE
226 (?J 'NEED-MERGE) ;not supported by standard CVS
227 ((?U ?P)
228 (if dont-change-disc
229 'NEED-UPDATE
230 (cons 'UP-TO-DATE
231 (if (eq code ?U) 'UPDATED 'PATCHED)))))
232 path 'trust)))
234 (and
235 (cvs-match "pcl-cvs: descending directory \\(.*\\)$" (dir 1))
236 (setq cvs-current-subdir dir))
238 ;; A special cvs message
239 (and
240 (cvs-match "cvs[.ex]* [a-z]+: ")
241 (cvs-or
243 ;; CVS is descending a subdirectory
244 ;; (status says `examining' while update says `updating')
245 (and
246 (cvs-match "\\(Examining\\|Updating\\) \\(.*\\)$" (dir 2))
247 (let ((dir (if (string= "." dir) "" (file-name-as-directory dir))))
248 (cvs-parsed-fileinfo 'DIRCHANGE "." dir)))
250 ;; [-n update] A new (or pruned) directory appeared but isn't traversed
251 (and
252 (cvs-match "New directory `\\(.*\\)' -- ignored$" (dir 1))
253 (cvs-parsed-fileinfo 'MESSAGE " " (file-name-as-directory dir)))
255 ;; File removed, since it is removed (by third party) in repository.
256 (and
257 (cvs-or
258 (cvs-match "warning: \\(.*\\) is not (any longer) pertinent$" (file 1))
259 (cvs-match "\\(.*\\) is no longer in the repository$" (file 1)))
260 (cvs-parsed-fileinfo 'DEAD file))
262 ;; [add]
263 (and
264 (cvs-or
265 (cvs-match "scheduling file `\\(.*\\)' for addition.*$" (path 1))
266 (cvs-match "re-adding file \\(.*\\) (in place of .*)$" (path 1)))
267 (cvs-parsed-fileinfo 'ADDED path))
269 ;; [add] this will also show up as a `U <file>'
270 (and
271 (cvs-match "\\(.*\\), version \\(.*\\), resurrected$"
272 (path 1) (base-rev 2))
273 (cvs-parsed-fileinfo '(UP-TO-DATE . RESURRECTED) path nil
274 :base-rev base-rev))
276 ;; [remove]
277 (and
278 (cvs-match "removed `\\(.*\\)'$" (path 1))
279 (cvs-parsed-fileinfo 'DEAD path))
281 ;; [remove,merge]
282 (and
283 (cvs-match "scheduling `\\(.*\\)' for removal$" (file 1))
284 (cvs-parsed-fileinfo 'REMOVED file))
286 ;; [update] File removed by you, but not cvs rm'd
287 (and
288 (cvs-match "warning: \\(.*\\) was lost$" (path 1))
289 (cvs-match (concat "U " (regexp-quote path) "$"))
290 (cvs-parsed-fileinfo (if dont-change-disc
291 'MISSING
292 '(UP-TO-DATE . UPDATED))
293 path))
295 ;; Mode conflicts (rather than contents)
296 (and
297 (cvs-match "conflict: ")
298 (cvs-or
299 (cvs-match "removed \\(.*\\) was modified by second party$"
300 (path 1) (subtype 'REMOVED))
301 (cvs-match "\\(.*\\) created independently by second party$"
302 (path 1) (subtype 'ADDED))
303 (cvs-match "\\(.*\\) is modified but no longer in the repository$"
304 (path 1) (subtype 'MODIFIED)))
305 (cvs-match (concat "C " (regexp-quote path)))
306 (cvs-parsed-fileinfo (cons 'CONFLICT subtype) path))
308 ;; Messages that should be shown to the user
309 (and
310 (cvs-or
311 (cvs-match "move away \\(.*\\); it is in the way$" (file 1))
312 (cvs-match "warning: new-born \\(.*\\) has disappeared$" (file 1))
313 (cvs-match "sticky tag .* for file `\\(.*\\)' is not a branch$"
314 (file 1)))
315 (cvs-parsed-fileinfo 'MESSAGE file))
317 ;; File unknown.
318 (and (cvs-match "use `.+ add' to create an entry for \\(.*\\)$" (path 1))
319 (cvs-parsed-fileinfo 'UNKNOWN path))
321 ;; [commit]
322 (and (cvs-match "Up-to-date check failed for `\\(.+\\)'$" (file 1))
323 (cvs-parsed-fileinfo 'NEED-MERGE file))
325 ;; We use cvs-execute-multi-dir but cvs can't handle it
326 ;; Probably because the cvs-client can but the cvs-server can't
327 (and (cvs-match ".* files with '?/'? in their name.*$")
328 (not cvs-execute-single-dir)
329 (setq cvs-execute-single-dir t)
330 (cvs-create-fileinfo
331 'MESSAGE "" " "
332 "*** Add (setq cvs-execute-single-dir t) to your .emacs ***
333 See the FAQ file or the variable's documentation for more info."))
335 ;; Cvs waits for a lock. Ignored: already handled by the process filter
336 (cvs-match "\\[..:..:..\\] \\(waiting for\\|obtained\\) .*lock in .*$")
337 ;; File you removed still exists. Ignore (will be noted as removed).
338 (cvs-match ".* should be removed and is still there$")
339 ;; just a note
340 (cvs-match "use '.+ commit' to \\sw+ th\\sw+ files? permanently$")
341 ;; [add,status] followed by a more complete status description anyway
342 (cvs-match "nothing known about .*$")
343 ;; [update] problem with patch
344 (cvs-match "checksum failure after patch to .*; will refetch$")
345 (cvs-match "refetching unpatchable files$")
346 ;; [commit]
347 (cvs-match "Rebuilding administrative file database$")
349 ;; CVS is running a *info program.
350 (and
351 (cvs-match "Executing.*$")
352 ;; Skip by any output the program may generate to stdout.
353 ;; Note that pcl-cvs will get seriously confused if the
354 ;; program prints anything to stderr.
355 (re-search-forward cvs-update-prog-output-skip-regexp))))
357 (and
358 (cvs-match "cvs[.ex]* \\[[a-z]+ aborted\\]:.*$")
359 (cvs-parsed-fileinfo 'MESSAGE ""))
361 ;; sadly you can't do much with these since the path is in the repository
362 (cvs-match "Directory .* added to the repository$")
366 (defun cvs-parse-merge ()
367 (let (path base-rev head-rev handled type)
368 ;; A merge (maybe with a conflict).
369 (and
370 (cvs-match "RCS file: .*$")
371 ;; Squirrel away info about the files that were retrieved for merging
372 (cvs-match "retrieving revision \\([0-9.]+\\)$" (base-rev 1))
373 (cvs-match "retrieving revision \\([0-9.]+\\)$" (head-rev 1))
374 (cvs-match "Merging differences between [0-9.]+ and [0-9.]+ into \\(.*\\)$"
375 (path 1))
377 ;; eat up potential conflict warnings
378 (cvs-or (cvs-match "\\(rcs\\)?merge:?\\( warning\\)?: \\(overlaps\\|conflicts\\) \\(or other problems \\)?during merge$" (type 'CONFLICT)) t)
379 (cvs-or
380 (and
381 (cvs-match "cvs[.ex]* [a-z]+: ")
382 (cvs-or
383 (cvs-match "conflicts found in \\(.*\\)$" (path 1) (type 'CONFLICT))
384 (cvs-match "could not merge .*$")
385 (cvs-match "restoring \\(.*\\) from backup file .*$" (path 1))))
388 ;; Is it a succesful merge?
389 ;; Figure out result of merging (ie, was there a conflict?)
390 (let ((qfile (regexp-quote path)))
391 (cvs-or
392 ;; Conflict
393 (and
394 (cvs-match (concat "C \\(.*" qfile "\\)$") (path 1) (type 'CONFLICT))
395 ;; C might be followed by a "suprious" U for non-mergeable files
396 (cvs-or (cvs-match (concat "U \\(.*" qfile "\\)$")) t))
397 ;; Successful merge
398 (cvs-match (concat "M \\(.*" qfile "\\)$") (path 1))
399 ;; The file already contained the modifications
400 (cvs-match (concat "^\\(.*" qfile
401 "\\) already contains the differences between .*$")
402 (path 1) (type '(UP-TO-DATE . MERGED)))
404 (cvs-parsed-fileinfo (if dont-change-disc 'NEED-MERGE
405 (or type '(MODIFIED . MERGED))) path nil
406 :merge (cons base-rev head-rev))))))
408 (defun cvs-parse-status ()
409 (let (nofile path base-rev head-rev type)
410 (and
411 (cvs-match
412 "===================================================================$")
413 (cvs-match "File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: "
414 (nofile 1) (path 2))
415 (cvs-or
416 (cvs-match "Needs \\(Checkout\\|Patch\\)$"
417 (type (if nofile 'MISSING 'NEED-UPDATE)))
418 (cvs-match "Up-to-date$"
419 (type (if nofile '(UP-TO-DATE . REMOVED) 'UP-TO-DATE)))
420 (cvs-match ".*[Cc]onflict.*$" (type 'CONFLICT))
421 (cvs-match "Locally Added$" (type 'ADDED))
422 (cvs-match "Locally Removed$" (type 'REMOVED))
423 (cvs-match "Locally Modified$" (type 'MODIFIED))
424 (cvs-match "Needs Merge$" (type 'NEED-MERGE))
425 (cvs-match "Unknown$" (type 'UNKNOWN)))
426 (cvs-match "$")
427 (cvs-or
428 (cvs-match " *Version:[ \t]*\\([0-9.]+\\).*$" (base-rev 1))
429 ;; NOTE: there's no date on the end of the following for server mode...
430 (cvs-match " *Working revision:[ \t]*-?\\([0-9.]+\\).*$" (base-rev 1))
431 ;; Let's not get all worked up if the format changes a bit
432 (cvs-match " *Working revision:.*$"))
433 (cvs-or
434 (cvs-match " *RCS Version:[ \t]*\\([0-9.]+\\)[ \t]*.*$" (head-rev 1))
435 (cvs-match " *Repository revision:[ \t]*\\([0-9.]+\\)[ \t]*\\(.*\\)$"
436 (head-rev 1))
437 (cvs-match " *Repository revision:.*"))
438 (cvs-or
439 (and;;sometimes those fields are missing
440 (cvs-match " *Sticky Tag:[ \t]*\\(.*\\)$") ; FIXME: use it
441 (cvs-match " *Sticky Date:[ \t]*\\(.*\\)$") ; FIXME: use it
442 (cvs-match " *Sticky Options:[ \t]*\\(.*\\)$")) ; FIXME: use it
444 (cvs-match "$")
445 ;; ignore the tags-listing in the case of `status -v'
446 (cvs-or (cvs-match " *Existing Tags:\n\\(\t.*\n\\)*$") t)
447 (cvs-parsed-fileinfo type path nil
448 :base-rev base-rev
449 :head-rev head-rev))))
451 (defun cvs-parse-commit ()
452 (let (path base-rev subtype)
453 (cvs-or
455 (and
456 (cvs-match "\\(Checking in\\|Removing\\) \\(.*\\);$" (path 2))
457 (cvs-match ".*,v <-- .*$")
458 (cvs-or
459 ;; deletion
460 (cvs-match "new revision: delete; previous revision: \\([0-9.]*\\)$"
461 (subtype 'REMOVED) (base-rev 1))
462 ;; addition
463 (cvs-match "initial revision: \\([0-9.]*\\)$"
464 (subtype 'ADDED) (base-rev 1))
465 ;; update
466 (cvs-match "new revision: \\([0-9.]*\\); previous revision: .*$"
467 (subtype 'COMMITTED) (base-rev 1)))
468 (cvs-match "done$")
469 ;; it's important here not to rely on the default directory management
470 ;; because `cvs commit' might begin by a series of Examining messages
471 ;; so the processing of the actual checkin messages might begin with
472 ;; a `current-dir' set to something different from ""
473 (cvs-parsed-fileinfo (cons 'UP-TO-DATE subtype) path 'trust
474 :base-rev base-rev))
476 ;; useless message added before the actual addition: ignored
477 (cvs-match "RCS file: .*\ndone$"))))
480 (provide 'pcvs-parse)
482 ;;; pcl-cvs-parse.el ends here