]> code.delx.au - gnu-emacs/blob - lisp/pcvs-parse.el
(mail-extr-all-top-level-domains): Update names.
[gnu-emacs] / 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.11 2002/03/17 20:48:14 monnier 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
271 ;; File removed, since it is removed (by third party) in repository.
272 (and
273 (cvs-or
274 (cvs-match "warning: \\(.*\\) is not (any longer) pertinent$" (file 1))
275 (cvs-match "\\(.*\\) is no longer in the repository$" (file 1)))
276 (cvs-parsed-fileinfo 'DEAD file))
277
278 ;; [add]
279 (and
280 (cvs-or
281 (cvs-match "scheduling file `\\(.*\\)' for addition.*$" (path 1))
282 (cvs-match "re-adding file \\(.*\\) (in place of .*)$" (path 1)))
283 (cvs-parsed-fileinfo 'ADDED path))
284
285 ;; [add] this will also show up as a `U <file>'
286 (and
287 (cvs-match "\\(.*\\), version \\(.*\\), resurrected$"
288 (path 1) (base-rev 2))
289 (cvs-parsed-fileinfo '(UP-TO-DATE . RESURRECTED) path nil
290 :base-rev base-rev))
291
292 ;; [remove]
293 (and
294 (cvs-match "removed `\\(.*\\)'$" (path 1))
295 (cvs-parsed-fileinfo 'DEAD path))
296
297 ;; [remove,merge]
298 (and
299 (cvs-match "scheduling `\\(.*\\)' for removal$" (file 1))
300 (cvs-parsed-fileinfo 'REMOVED file))
301
302 ;; [update] File removed by you, but not cvs rm'd
303 (and
304 (cvs-match "warning: \\(.*\\) was lost$" (path 1))
305 (cvs-match (concat "U " (regexp-quote path) "$"))
306 (cvs-parsed-fileinfo (if dont-change-disc
307 'MISSING
308 '(UP-TO-DATE . UPDATED))
309 path))
310
311 ;; Mode conflicts (rather than contents)
312 (and
313 (cvs-match "conflict: ")
314 (cvs-or
315 (cvs-match "removed \\(.*\\) was modified by second party$"
316 (path 1) (subtype 'REMOVED))
317 (cvs-match "\\(.*\\) created independently by second party$"
318 (path 1) (subtype 'ADDED))
319 (cvs-match "\\(.*\\) is modified but no longer in the repository$"
320 (path 1) (subtype 'MODIFIED)))
321 (cvs-match (concat "C " (regexp-quote path)))
322 (cvs-parsed-fileinfo (cons 'CONFLICT subtype) path))
323
324 ;; Messages that should be shown to the user
325 (and
326 (cvs-or
327 (cvs-match "move away \\(.*\\); it is in the way$" (file 1))
328 (cvs-match "warning: new-born \\(.*\\) has disappeared$" (file 1))
329 (cvs-match "sticky tag .* for file `\\(.*\\)' is not a branch$"
330 (file 1)))
331 (cvs-parsed-fileinfo 'MESSAGE file))
332
333 ;; File unknown.
334 (and (cvs-match "use `.+ add' to create an entry for \\(.*\\)$" (path 1))
335 (cvs-parsed-fileinfo 'UNKNOWN path))
336
337 ;; [commit]
338 (and (cvs-match "Up-to-date check failed for `\\(.+\\)'$" (file 1))
339 (cvs-parsed-fileinfo 'NEED-MERGE file))
340
341 ;; We use cvs-execute-multi-dir but cvs can't handle it
342 ;; Probably because the cvs-client can but the cvs-server can't
343 (and (cvs-match ".* files with '?/'? in their name.*$")
344 (not cvs-execute-single-dir)
345 (setq cvs-execute-single-dir t)
346 (cvs-create-fileinfo
347 'MESSAGE "" " "
348 "*** Add (setq cvs-execute-single-dir t) to your .emacs ***
349 See the FAQ file or the variable's documentation for more info."))
350
351 ;; Cvs waits for a lock. Ignored: already handled by the process filter
352 (cvs-match "\\[..:..:..\\] \\(waiting for\\|obtained\\) .*lock in .*$")
353 ;; File you removed still exists. Ignore (will be noted as removed).
354 (cvs-match ".* should be removed and is still there$")
355 ;; just a note
356 (cvs-match "use '.+ commit' to \\sw+ th\\sw+ files? permanently$")
357 ;; [add,status] followed by a more complete status description anyway
358 (and (cvs-match "nothing known about \\(.*\\)$" (path 1))
359 (cvs-parsed-fileinfo 'DEAD path 'trust))
360 ;; [update] problem with patch
361 (cvs-match "checksum failure after patch to .*; will refetch$")
362 (cvs-match "refetching unpatchable files$")
363 ;; [commit]
364 (cvs-match "Rebuilding administrative file database$")
365 ;; ???
366 (cvs-match "--> Using per-directory sticky tag `.*'")
367
368 ;; CVS is running a *info program.
369 (and
370 (cvs-match "Executing.*$")
371 ;; Skip by any output the program may generate to stdout.
372 ;; Note that pcl-cvs will get seriously confused if the
373 ;; program prints anything to stderr.
374 (re-search-forward cvs-update-prog-output-skip-regexp))))
375
376 (and
377 (cvs-match "cvs[.ex]* \\[[a-z]+ aborted\\]:.*$")
378 (cvs-parsed-fileinfo 'MESSAGE ""))
379
380 ;; sadly you can't do much with these since the path is in the repository
381 (cvs-match "Directory .* added to the repository$")
382 )))
383
384
385 (defun cvs-parse-merge ()
386 (let (path base-rev head-rev handled type)
387 ;; A merge (maybe with a conflict).
388 (and
389 (cvs-match "RCS file: .*$")
390 ;; Squirrel away info about the files that were retrieved for merging
391 (cvs-match "retrieving revision \\([0-9.]+\\)$" (base-rev 1))
392 (cvs-match "retrieving revision \\([0-9.]+\\)$" (head-rev 1))
393 (cvs-match "Merging differences between [0-9.]+ and [0-9.]+ into \\(.*\\)$"
394 (path 1))
395
396 ;; eat up potential conflict warnings
397 (cvs-or (cvs-match "\\(rcs\\)?merge:?\\( warning\\)?: \\(overlaps\\|conflicts\\) \\(or other problems \\)?during merge$" (type 'CONFLICT)) t)
398 (cvs-or
399 (and
400 (cvs-match "cvs[.ex]* [a-z]+: ")
401 (cvs-or
402 (cvs-match "conflicts found in \\(.*\\)$" (path 1) (type 'CONFLICT))
403 (cvs-match "could not merge .*$")
404 (cvs-match "restoring \\(.*\\) from backup file .*$" (path 1))))
405 t)
406
407 ;; Is it a succesful merge?
408 ;; Figure out result of merging (ie, was there a conflict?)
409 (let ((qfile (regexp-quote path)))
410 (cvs-or
411 ;; Conflict
412 (and
413 (cvs-match (concat "C \\(.*" qfile "\\)$") (path 1) (type 'CONFLICT))
414 ;; C might be followed by a "suprious" U for non-mergeable files
415 (cvs-or (cvs-match (concat "U \\(.*" qfile "\\)$")) t))
416 ;; Successful merge
417 (cvs-match (concat "M \\(.*" qfile "\\)$") (path 1))
418 ;; The file already contained the modifications
419 (cvs-match (concat "^\\(.*" qfile
420 "\\) already contains the differences between .*$")
421 (path 1) (type '(UP-TO-DATE . MERGED)))
422 t)
423 ;; FIXME: PATH might not be set yet. Sometimes the only path
424 ;; information is in `RCS file: ...' (yuck!!).
425 (cvs-parsed-fileinfo (if dont-change-disc 'NEED-MERGE
426 (or type '(MODIFIED . MERGED))) path nil
427 :merge (cons base-rev head-rev))))))
428
429 (defun cvs-parse-status ()
430 (let (nofile path base-rev head-rev type)
431 (and
432 (cvs-match
433 "===================================================================$")
434 (cvs-match "File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: "
435 (nofile 1) (path 2))
436 (cvs-or
437 (cvs-match "Needs \\(Checkout\\|Patch\\)$"
438 (type (if nofile 'MISSING 'NEED-UPDATE)))
439 (cvs-match "Up-to-date$"
440 (type (if nofile '(UP-TO-DATE . REMOVED) 'UP-TO-DATE)))
441 (cvs-match "File had conflicts on merge$" (type 'MODIFIED))
442 (cvs-match ".*[Cc]onflict.*$" (type 'CONFLICT))
443 (cvs-match "Locally Added$" (type 'ADDED))
444 (cvs-match "Locally Removed$" (type 'REMOVED))
445 (cvs-match "Locally Modified$" (type 'MODIFIED))
446 (cvs-match "Needs Merge$" (type 'NEED-MERGE))
447 (cvs-match "Unknown$" (type 'UNKNOWN)))
448 (cvs-match "$")
449 (cvs-or
450 (cvs-match " *Version:[ \t]*\\([0-9.]+\\).*$" (base-rev 1))
451 ;; NOTE: there's no date on the end of the following for server mode...
452 (cvs-match " *Working revision:[ \t]*-?\\([0-9.]+\\).*$" (base-rev 1))
453 ;; Let's not get all worked up if the format changes a bit
454 (cvs-match " *Working revision:.*$"))
455 (cvs-or
456 (cvs-match " *RCS Version:[ \t]*\\([0-9.]+\\)[ \t]*.*$" (head-rev 1))
457 (cvs-match " *Repository revision:[ \t]*\\([0-9.]+\\)[ \t]*\\(.*\\)$"
458 (head-rev 1))
459 (cvs-match " *Repository revision:.*"))
460 (cvs-or
461 (and;;sometimes those fields are missing
462 (cvs-match " *Sticky Tag:[ \t]*\\(.*\\)$") ; FIXME: use it
463 (cvs-match " *Sticky Date:[ \t]*\\(.*\\)$") ; FIXME: use it
464 (cvs-match " *Sticky Options:[ \t]*\\(.*\\)$")) ; FIXME: use it
465 t)
466 (cvs-match "$")
467 ;; ignore the tags-listing in the case of `status -v'
468 (cvs-or (cvs-match " *Existing Tags:\n\\(\t.*\n\\)*$") t)
469 (cvs-parsed-fileinfo type path nil
470 :base-rev base-rev
471 :head-rev head-rev))))
472
473 (defun cvs-parse-commit ()
474 (let (path base-rev subtype)
475 (cvs-or
476
477 (and
478 (cvs-match "\\(Checking in\\|Removing\\) \\(.*\\);$" (path 2))
479 (cvs-match ".*,v <-- .*$")
480 (cvs-or
481 ;; deletion
482 (cvs-match "new revision: delete; previous revision: \\([0-9.]*\\)$"
483 (subtype 'REMOVED) (base-rev 1))
484 ;; addition
485 (cvs-match "initial revision: \\([0-9.]*\\)$"
486 (subtype 'ADDED) (base-rev 1))
487 ;; update
488 (cvs-match "new revision: \\([0-9.]*\\); previous revision: .*$"
489 (subtype 'COMMITTED) (base-rev 1)))
490 (cvs-match "done$")
491 (progn
492 ;; Try to remove the temp files used by VC.
493 (vc-delete-automatic-version-backups (expand-file-name path))
494 ;; it's important here not to rely on the default directory management
495 ;; because `cvs commit' might begin by a series of Examining messages
496 ;; so the processing of the actual checkin messages might begin with
497 ;; a `current-dir' set to something different from ""
498 (cvs-parsed-fileinfo (cons 'UP-TO-DATE subtype) path 'trust
499 :base-rev base-rev)))
500
501 ;; useless message added before the actual addition: ignored
502 (cvs-match "RCS file: .*\ndone$"))))
503
504
505 (provide 'pcvs-parse)
506
507 ;;; pcvs-parse.el ends here