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