*** empty log message ***
[emacs.git] / lisp / pcvs-parse.el
blob036837c3bcb6261482442d56a1fe68d1916dc3e1
1 ;;; pcvs-parse.el --- The CVS output parser
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: pcl-cvs
7 ;; Revision: $Id: pcvs-parse.el,v 1.5 2000/12/18 03:17:31 monnier Exp $
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 2, or (at your option)
14 ;; 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; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;;; Bugs:
30 ;; - when merging a modified file, if the merge says that the file already
31 ;; contained in the changes, it marks the file as `up-to-date' although
32 ;; it might still contain further changes.
33 ;; Example: merging a zero-change commit.
35 ;;; Code:
37 (eval-when-compile (require 'cl))
39 (require 'pcvs-util)
40 (require 'pcvs-info)
42 ;; imported from pcvs.el
43 (defvar cvs-execute-single-dir)
45 ;; parse vars
47 (defcustom cvs-update-prog-output-skip-regexp "$"
48 "*A regexp that matches the end of the output from all cvs update programs.
49 That is, output from any programs that are run by CVS (by the flag -u
50 in the `modules' file - see cvs(5)) when `cvs update' is performed should
51 terminate with a line that this regexp matches. It is enough that
52 some part of the line is matched.
54 The default (a single $) fits programs without output."
55 :group 'pcl-cvs
56 :type '(regexp :value "$"))
58 (defcustom cvs-parse-ignored-messages
59 '("Executing ssh-askpass to query the password.*$"
60 ".*Remote host denied X11 forwarding.*$")
61 "*A list of regexps matching messages that should be ignored by the parser.
62 Each regexp should match a whole set of lines and should hence be terminated
63 by `$'."
64 :group 'pcl-cvs
65 :type '(repeat regexp))
67 ;; a few more defvars just to shut up the compiler
68 (defvar cvs-start)
69 (defvar cvs-current-dir)
70 (defvar cvs-current-subdir)
71 (defvar dont-change-disc)
73 ;;;; The parser
75 (defconst cvs-parse-known-commands
76 '("status" "add" "commit" "update" "remove" "checkout" "ci")
77 "List of CVS commands whose output is understood by the parser.")
79 (defun cvs-parse-buffer (parse-spec dont-change-disc &optional subdir)
80 "Parse current buffer according to PARSE-SPEC.
81 PARSE-SPEC is a function of no argument advancing the point and returning
82 either a fileinfo or t (if the matched text should be ignored) or
83 nil if it didn't match anything.
84 DONT-CHANGE-DISC just indicates whether the command was changing the disc
85 or not (useful to tell the difference btween `cvs-examine' and `cvs-update'
86 ouytput.
87 The path names should be interpreted as relative to SUBDIR (defaults
88 to the `default-directory').
89 Return a list of collected entries, or t if an error occured."
90 (goto-char (point-min))
91 (let ((fileinfos ())
92 (cvs-current-dir "")
93 (case-fold-search nil)
94 (cvs-current-subdir (or subdir "")))
95 (while (not (or (eobp) (eq fileinfos t)))
96 (let ((ret (cvs-parse-run-table parse-spec)))
97 (cond
98 ;; it matched a known information message
99 ((cvs-fileinfo-p ret) (push ret fileinfos))
100 ;; it didn't match anything at all (impossible)
101 ((and (consp ret) (cvs-fileinfo-p (car ret)))
102 (setq fileinfos (append ret fileinfos)))
103 ((null ret) (setq fileinfos t))
104 ;; it matched something that should be ignored
105 (t nil))))
106 (nreverse fileinfos)))
109 ;; All those parsing macros/functions should return a success indicator
110 (defsubst cvs-parse-msg () (buffer-substring cvs-start (1- (point))))
112 ;;(defsubst COLLECT (exp) (push exp *result*))
113 ;;(defsubst PROG (e) t)
114 ;;(defmacro SEQ (&rest seqs) (cons 'and seqs))
116 (defmacro cvs-match (re &rest matches)
117 "Try to match RE and extract submatches.
118 If RE matches, advance the point until the line after the match and
119 then assign the variables as specified in MATCHES (via `setq')."
120 (cons 'cvs-do-match
121 (cons re (mapcar (lambda (match)
122 `(cons ',(first match) ,(second match)))
123 matches))))
125 (defun cvs-do-match (re &rest matches)
126 "Internal function for the `cvs-match' macro.
127 Match RE and if successful, execute MATCHES."
128 ;; Is it a match?
129 (when (looking-at re)
130 (goto-char (match-end 0))
131 ;; Skip the newline (unless we already are at the end of the buffer).
132 (when (and (eolp) (< (point) (point-max))) (forward-char))
133 ;; assign the matches
134 (dolist (match matches t)
135 (let ((val (cdr match)))
136 (set (car match) (if (integerp val) (match-string val) val))))))
138 (defmacro cvs-or (&rest alts)
139 "Try each one of the ALTS alternatives until one matches."
140 `(let ((-cvs-parse-point (point)))
141 ,(cons 'or
142 (mapcar (lambda (es)
143 `(or ,es (ignore (goto-char -cvs-parse-point))))
144 alts))))
145 (def-edebug-spec cvs-or t)
147 ;; This is how parser tables should be executed
148 (defun cvs-parse-run-table (parse-spec)
149 "Run PARSE-SPEC and provide sensible default behavior."
150 (unless (bolp) (forward-line 1)) ;this should never be needed
151 (let ((cvs-start (point)))
152 (cvs-or
153 (funcall parse-spec)
155 (dolist (re cvs-parse-ignored-messages)
156 (when (cvs-match re) (return t)))
158 ;; This is a parse error. Create a message-type fileinfo.
159 (and
160 (cvs-match ".*$")
161 (cvs-create-fileinfo 'MESSAGE cvs-current-dir " "
162 (concat " Unknown msg: '" (cvs-parse-msg) "'")
163 :subtype 'ERROR)))))
166 (defun cvs-parsed-fileinfo (type path &optional directory &rest keys)
167 "Create a fileinfo.
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)
180 (file path))
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:
195 ;;;;
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 type base-rev subtype)
202 (cvs-or
204 (cvs-parse-status)
205 (cvs-parse-merge)
206 (cvs-parse-commit)
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.
214 (and
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.
219 ;; C: Conflict
220 ;; U: The file is copied from the repository.
221 ;; P: The file was patched from the repository.
222 ;; ?: Unknown file.
223 (let ((code (aref c 0)))
224 (cvs-parsed-fileinfo
225 (case code
226 (?M 'MODIFIED)
227 (?A 'ADDED)
228 (?R 'REMOVED)
229 (?? 'UNKNOWN)
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.
236 (with-temp-buffer
237 (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
242 ((?U ?P)
243 (if dont-change-disc 'NEED-UPDATE
244 (cons 'UP-TO-DATE (if (eq code ?U) 'UPDATED 'PATCHED)))))
245 path 'trust)))
247 (and
248 (cvs-match "pcl-cvs: descending directory \\(.*\\)$" (dir 1))
249 (setq cvs-current-subdir dir))
251 ;; A special cvs message
252 (and
253 (cvs-match "cvs[.ex]* [a-z]+: ")
254 (cvs-or
256 ;; CVS is descending a subdirectory
257 ;; (status says `examining' while update says `updating')
258 (and
259 (cvs-match "\\(Examining\\|Updating\\) \\(.*\\)$" (dir 2))
260 (let ((dir (if (string= "." dir) "" (file-name-as-directory dir))))
261 (cvs-parsed-fileinfo 'DIRCHANGE "." dir)))
263 ;; [-n update] A new (or pruned) directory appeared but isn't traversed
264 (and
265 (cvs-match "New directory `\\(.*\\)' -- ignored$" (dir 1))
266 (cvs-parsed-fileinfo 'MESSAGE " " (file-name-as-directory dir)))
268 ;; File removed, since it is removed (by third party) in repository.
269 (and
270 (cvs-or
271 (cvs-match "warning: \\(.*\\) is not (any longer) pertinent$" (file 1))
272 (cvs-match "\\(.*\\) is no longer in the repository$" (file 1)))
273 (cvs-parsed-fileinfo 'DEAD file))
275 ;; [add]
276 (and
277 (cvs-or
278 (cvs-match "scheduling file `\\(.*\\)' for addition.*$" (path 1))
279 (cvs-match "re-adding file \\(.*\\) (in place of .*)$" (path 1)))
280 (cvs-parsed-fileinfo 'ADDED path))
282 ;; [add] this will also show up as a `U <file>'
283 (and
284 (cvs-match "\\(.*\\), version \\(.*\\), resurrected$"
285 (path 1) (base-rev 2))
286 (cvs-parsed-fileinfo '(UP-TO-DATE . RESURRECTED) path nil
287 :base-rev base-rev))
289 ;; [remove]
290 (and
291 (cvs-match "removed `\\(.*\\)'$" (path 1))
292 (cvs-parsed-fileinfo 'DEAD path))
294 ;; [remove,merge]
295 (and
296 (cvs-match "scheduling `\\(.*\\)' for removal$" (file 1))
297 (cvs-parsed-fileinfo 'REMOVED file))
299 ;; [update] File removed by you, but not cvs rm'd
300 (and
301 (cvs-match "warning: \\(.*\\) was lost$" (path 1))
302 (cvs-match (concat "U " (regexp-quote path) "$"))
303 (cvs-parsed-fileinfo (if dont-change-disc
304 'MISSING
305 '(UP-TO-DATE . UPDATED))
306 path))
308 ;; Mode conflicts (rather than contents)
309 (and
310 (cvs-match "conflict: ")
311 (cvs-or
312 (cvs-match "removed \\(.*\\) was modified by second party$"
313 (path 1) (subtype 'REMOVED))
314 (cvs-match "\\(.*\\) created independently by second party$"
315 (path 1) (subtype 'ADDED))
316 (cvs-match "\\(.*\\) is modified but no longer in the repository$"
317 (path 1) (subtype 'MODIFIED)))
318 (cvs-match (concat "C " (regexp-quote path)))
319 (cvs-parsed-fileinfo (cons 'CONFLICT subtype) path))
321 ;; Messages that should be shown to the user
322 (and
323 (cvs-or
324 (cvs-match "move away \\(.*\\); it is in the way$" (file 1))
325 (cvs-match "warning: new-born \\(.*\\) has disappeared$" (file 1))
326 (cvs-match "sticky tag .* for file `\\(.*\\)' is not a branch$"
327 (file 1)))
328 (cvs-parsed-fileinfo 'MESSAGE file))
330 ;; File unknown.
331 (and (cvs-match "use `.+ add' to create an entry for \\(.*\\)$" (path 1))
332 (cvs-parsed-fileinfo 'UNKNOWN path))
334 ;; [commit]
335 (and (cvs-match "Up-to-date check failed for `\\(.+\\)'$" (file 1))
336 (cvs-parsed-fileinfo 'NEED-MERGE file))
338 ;; We use cvs-execute-multi-dir but cvs can't handle it
339 ;; Probably because the cvs-client can but the cvs-server can't
340 (and (cvs-match ".* files with '?/'? in their name.*$")
341 (not cvs-execute-single-dir)
342 (setq cvs-execute-single-dir t)
343 (cvs-create-fileinfo
344 'MESSAGE "" " "
345 "*** Add (setq cvs-execute-single-dir t) to your .emacs ***
346 See the FAQ file or the variable's documentation for more info."))
348 ;; Cvs waits for a lock. Ignored: already handled by the process filter
349 (cvs-match "\\[..:..:..\\] \\(waiting for\\|obtained\\) .*lock in .*$")
350 ;; File you removed still exists. Ignore (will be noted as removed).
351 (cvs-match ".* should be removed and is still there$")
352 ;; just a note
353 (cvs-match "use '.+ commit' to \\sw+ th\\sw+ files? permanently$")
354 ;; [add,status] followed by a more complete status description anyway
355 (cvs-match "nothing known about .*$")
356 ;; [update] problem with patch
357 (cvs-match "checksum failure after patch to .*; will refetch$")
358 (cvs-match "refetching unpatchable files$")
359 ;; [commit]
360 (cvs-match "Rebuilding administrative file database$")
361 ;; ???
362 (cvs-match "--> Using per-directory sticky tag `.*'")
364 ;; CVS is running a *info program.
365 (and
366 (cvs-match "Executing.*$")
367 ;; Skip by any output the program may generate to stdout.
368 ;; Note that pcl-cvs will get seriously confused if the
369 ;; program prints anything to stderr.
370 (re-search-forward cvs-update-prog-output-skip-regexp))))
372 (and
373 (cvs-match "cvs[.ex]* \\[[a-z]+ aborted\\]:.*$")
374 (cvs-parsed-fileinfo 'MESSAGE ""))
376 ;; sadly you can't do much with these since the path is in the repository
377 (cvs-match "Directory .* added to the repository$")
381 (defun cvs-parse-merge ()
382 (let (path base-rev head-rev handled type)
383 ;; A merge (maybe with a conflict).
384 (and
385 (cvs-match "RCS file: .*$")
386 ;; Squirrel away info about the files that were retrieved for merging
387 (cvs-match "retrieving revision \\([0-9.]+\\)$" (base-rev 1))
388 (cvs-match "retrieving revision \\([0-9.]+\\)$" (head-rev 1))
389 (cvs-match "Merging differences between [0-9.]+ and [0-9.]+ into \\(.*\\)$"
390 (path 1))
392 ;; eat up potential conflict warnings
393 (cvs-or (cvs-match "\\(rcs\\)?merge:?\\( warning\\)?: \\(overlaps\\|conflicts\\) \\(or other problems \\)?during merge$" (type 'CONFLICT)) t)
394 (cvs-or
395 (and
396 (cvs-match "cvs[.ex]* [a-z]+: ")
397 (cvs-or
398 (cvs-match "conflicts found in \\(.*\\)$" (path 1) (type 'CONFLICT))
399 (cvs-match "could not merge .*$")
400 (cvs-match "restoring \\(.*\\) from backup file .*$" (path 1))))
403 ;; Is it a succesful merge?
404 ;; Figure out result of merging (ie, was there a conflict?)
405 (let ((qfile (regexp-quote path)))
406 (cvs-or
407 ;; Conflict
408 (and
409 (cvs-match (concat "C \\(.*" qfile "\\)$") (path 1) (type 'CONFLICT))
410 ;; C might be followed by a "suprious" U for non-mergeable files
411 (cvs-or (cvs-match (concat "U \\(.*" qfile "\\)$")) t))
412 ;; Successful merge
413 (cvs-match (concat "M \\(.*" qfile "\\)$") (path 1))
414 ;; The file already contained the modifications
415 (cvs-match (concat "^\\(.*" qfile
416 "\\) already contains the differences between .*$")
417 (path 1) (type '(UP-TO-DATE . MERGED)))
419 ;; FIXME: PATH might not be set yet. Sometimes the only path
420 ;; information is in `RCS file: ...' (yuck!!).
421 (cvs-parsed-fileinfo (if dont-change-disc 'NEED-MERGE
422 (or type '(MODIFIED . MERGED))) path nil
423 :merge (cons base-rev head-rev))))))
425 (defun cvs-parse-status ()
426 (let (nofile path base-rev head-rev type)
427 (and
428 (cvs-match
429 "===================================================================$")
430 (cvs-match "File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: "
431 (nofile 1) (path 2))
432 (cvs-or
433 (cvs-match "Needs \\(Checkout\\|Patch\\)$"
434 (type (if nofile 'MISSING 'NEED-UPDATE)))
435 (cvs-match "Up-to-date$"
436 (type (if nofile '(UP-TO-DATE . REMOVED) 'UP-TO-DATE)))
437 (cvs-match ".*[Cc]onflict.*$" (type 'CONFLICT))
438 (cvs-match "Locally Added$" (type 'ADDED))
439 (cvs-match "Locally Removed$" (type 'REMOVED))
440 (cvs-match "Locally Modified$" (type 'MODIFIED))
441 (cvs-match "Needs Merge$" (type 'NEED-MERGE))
442 (cvs-match "Unknown$" (type 'UNKNOWN)))
443 (cvs-match "$")
444 (cvs-or
445 (cvs-match " *Version:[ \t]*\\([0-9.]+\\).*$" (base-rev 1))
446 ;; NOTE: there's no date on the end of the following for server mode...
447 (cvs-match " *Working revision:[ \t]*-?\\([0-9.]+\\).*$" (base-rev 1))
448 ;; Let's not get all worked up if the format changes a bit
449 (cvs-match " *Working revision:.*$"))
450 (cvs-or
451 (cvs-match " *RCS Version:[ \t]*\\([0-9.]+\\)[ \t]*.*$" (head-rev 1))
452 (cvs-match " *Repository revision:[ \t]*\\([0-9.]+\\)[ \t]*\\(.*\\)$"
453 (head-rev 1))
454 (cvs-match " *Repository revision:.*"))
455 (cvs-or
456 (and;;sometimes those fields are missing
457 (cvs-match " *Sticky Tag:[ \t]*\\(.*\\)$") ; FIXME: use it
458 (cvs-match " *Sticky Date:[ \t]*\\(.*\\)$") ; FIXME: use it
459 (cvs-match " *Sticky Options:[ \t]*\\(.*\\)$")) ; FIXME: use it
461 (cvs-match "$")
462 ;; ignore the tags-listing in the case of `status -v'
463 (cvs-or (cvs-match " *Existing Tags:\n\\(\t.*\n\\)*$") t)
464 (cvs-parsed-fileinfo type path nil
465 :base-rev base-rev
466 :head-rev head-rev))))
468 (defun cvs-parse-commit ()
469 (let (path base-rev subtype)
470 (cvs-or
472 (and
473 (cvs-match "\\(Checking in\\|Removing\\) \\(.*\\);$" (path 2))
474 (cvs-match ".*,v <-- .*$")
475 (cvs-or
476 ;; deletion
477 (cvs-match "new revision: delete; previous revision: \\([0-9.]*\\)$"
478 (subtype 'REMOVED) (base-rev 1))
479 ;; addition
480 (cvs-match "initial revision: \\([0-9.]*\\)$"
481 (subtype 'ADDED) (base-rev 1))
482 ;; update
483 (cvs-match "new revision: \\([0-9.]*\\); previous revision: .*$"
484 (subtype 'COMMITTED) (base-rev 1)))
485 (cvs-match "done$")
486 ;; it's important here not to rely on the default directory management
487 ;; because `cvs commit' might begin by a series of Examining messages
488 ;; so the processing of the actual checkin messages might begin with
489 ;; a `current-dir' set to something different from ""
490 (cvs-parsed-fileinfo (cons 'UP-TO-DATE subtype) path 'trust
491 :base-rev base-rev))
493 ;; useless message added before the actual addition: ignored
494 (cvs-match "RCS file: .*\ndone$"))))
497 (provide 'pcvs-parse)
499 ;;; pcl-cvs-parse.el ends here