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