Trailing whitespace deleted.
[bpt/emacs.git] / lisp / pcvs-parse.el
CommitLineData
3afbc435 1;;; pcvs-parse.el --- the CVS output parser
5b467bf4 2
ed297fd6
SM
3;; Copyright (C) 1991,92,93,94,95,96,97,98,99,2000,2002
4;; Free Software Foundation, Inc.
5b467bf4
SM
5
6;; Author: Stefan Monnier <monnier@cs.yale.edu>
7;; Keywords: pcl-cvs
f1180544 8;; Revision: $Id: pcvs-parse.el,v 1.13 2002/09/03 01:23:15 monnier Exp $
5b467bf4
SM
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
1e98637e
SM
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.
5b467bf4
SM
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.
50That is, output from any programs that are run by CVS (by the flag -u
51in the `modules' file - see cvs(5)) when `cvs update' is performed should
52terminate with a line that this regexp matches. It is enough that
53some part of the line is matched.
54
55The 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.
63Each regexp should match a whole set of lines and should hence be terminated
64by `$'."
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.
82PARSE-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.
85DONT-CHANGE-DISC just indicates whether the command was changing the disc
6be6d579
SM
86 or not (useful to tell the difference between `cvs-examine' and `cvs-update'
87 output.
5b467bf4
SM
88The path names should be interpreted as relative to SUBDIR (defaults
89 to the `default-directory').
6be6d579 90Return a list of collected entries, or t if an error occurred."
5b467bf4
SM
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.
119If RE matches, advance the point until the line after the match and
120then 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.
128Match 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 " "
6be6d579
SM
163 ;; (concat " Unknown msg: '"
164 (cvs-parse-msg) ;; "'")
5b467bf4
SM
165 :subtype 'ERROR)))))
166
167\f
168(defun cvs-parsed-fileinfo (type path &optional directory &rest keys)
169 "Create a fileinfo.
170TYPE can either be a type symbol or a cons of the form (TYPE . SUBTYPE).
171PATH is the filename.
172DIRECTORY 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).
180The 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))))
5b467bf4
SM
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
f1180544 205
5b467bf4
SM
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))
f1180544 214
5b467bf4
SM
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)))
e6a4ba73
SM
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)))
5b467bf4
SM
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
1f3d429d
SM
255 (let ((case-fold-search t))
256 (cvs-match "cvs[.a-z]* [a-z]+: "))
5b467bf4
SM
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))
f9e7890c
SM
269 ;; (cvs-parsed-fileinfo 'MESSAGE " " (file-name-as-directory dir))
270 (cvs-parsed-fileinfo '(NEED-UPDATE . NEW-DIR) dir))
5b467bf4
SM
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 'DEAD file))
278
279 ;; [add]
280 (and
281 (cvs-or
282 (cvs-match "scheduling file `\\(.*\\)' for addition.*$" (path 1))
283 (cvs-match "re-adding file \\(.*\\) (in place of .*)$" (path 1)))
284 (cvs-parsed-fileinfo 'ADDED path))
285
286 ;; [add] this will also show up as a `U <file>'
287 (and
288 (cvs-match "\\(.*\\), version \\(.*\\), resurrected$"
289 (path 1) (base-rev 2))
f9e7890c
SM
290 ;; FIXME: resurrection only brings back the original version,
291 ;; not the latest on the branch, so `up-to-date' is not always
292 ;; what we want.
5b467bf4
SM
293 (cvs-parsed-fileinfo '(UP-TO-DATE . RESURRECTED) path nil
294 :base-rev base-rev))
295
296 ;; [remove]
297 (and
298 (cvs-match "removed `\\(.*\\)'$" (path 1))
299 (cvs-parsed-fileinfo 'DEAD path))
300
301 ;; [remove,merge]
302 (and
303 (cvs-match "scheduling `\\(.*\\)' for removal$" (file 1))
304 (cvs-parsed-fileinfo 'REMOVED file))
305
306 ;; [update] File removed by you, but not cvs rm'd
307 (and
308 (cvs-match "warning: \\(.*\\) was lost$" (path 1))
309 (cvs-match (concat "U " (regexp-quote path) "$"))
310 (cvs-parsed-fileinfo (if dont-change-disc
311 'MISSING
312 '(UP-TO-DATE . UPDATED))
313 path))
f1180544 314
5b467bf4
SM
315 ;; Mode conflicts (rather than contents)
316 (and
317 (cvs-match "conflict: ")
318 (cvs-or
319 (cvs-match "removed \\(.*\\) was modified by second party$"
320 (path 1) (subtype 'REMOVED))
321 (cvs-match "\\(.*\\) created independently by second party$"
322 (path 1) (subtype 'ADDED))
323 (cvs-match "\\(.*\\) is modified but no longer in the repository$"
324 (path 1) (subtype 'MODIFIED)))
325 (cvs-match (concat "C " (regexp-quote path)))
326 (cvs-parsed-fileinfo (cons 'CONFLICT subtype) path))
327
328 ;; Messages that should be shown to the user
329 (and
330 (cvs-or
331 (cvs-match "move away \\(.*\\); it is in the way$" (file 1))
332 (cvs-match "warning: new-born \\(.*\\) has disappeared$" (file 1))
333 (cvs-match "sticky tag .* for file `\\(.*\\)' is not a branch$"
334 (file 1)))
335 (cvs-parsed-fileinfo 'MESSAGE file))
f1180544 336
5b467bf4
SM
337 ;; File unknown.
338 (and (cvs-match "use `.+ add' to create an entry for \\(.*\\)$" (path 1))
339 (cvs-parsed-fileinfo 'UNKNOWN path))
340
cb3430a1
SM
341 ;; [commit]
342 (and (cvs-match "Up-to-date check failed for `\\(.+\\)'$" (file 1))
343 (cvs-parsed-fileinfo 'NEED-MERGE file))
344
5b467bf4
SM
345 ;; We use cvs-execute-multi-dir but cvs can't handle it
346 ;; Probably because the cvs-client can but the cvs-server can't
347 (and (cvs-match ".* files with '?/'? in their name.*$")
348 (not cvs-execute-single-dir)
349 (setq cvs-execute-single-dir t)
350 (cvs-create-fileinfo
351 'MESSAGE "" " "
352 "*** Add (setq cvs-execute-single-dir t) to your .emacs ***
353 See the FAQ file or the variable's documentation for more info."))
f1180544 354
5b467bf4
SM
355 ;; Cvs waits for a lock. Ignored: already handled by the process filter
356 (cvs-match "\\[..:..:..\\] \\(waiting for\\|obtained\\) .*lock in .*$")
357 ;; File you removed still exists. Ignore (will be noted as removed).
358 (cvs-match ".* should be removed and is still there$")
359 ;; just a note
360 (cvs-match "use '.+ commit' to \\sw+ th\\sw+ files? permanently$")
361 ;; [add,status] followed by a more complete status description anyway
ed297fd6
SM
362 (and (cvs-match "nothing known about \\(.*\\)$" (path 1))
363 (cvs-parsed-fileinfo 'DEAD path 'trust))
5b467bf4
SM
364 ;; [update] problem with patch
365 (cvs-match "checksum failure after patch to .*; will refetch$")
366 (cvs-match "refetching unpatchable files$")
367 ;; [commit]
368 (cvs-match "Rebuilding administrative file database$")
1e98637e
SM
369 ;; ???
370 (cvs-match "--> Using per-directory sticky tag `.*'")
f1180544 371
5b467bf4
SM
372 ;; CVS is running a *info program.
373 (and
374 (cvs-match "Executing.*$")
375 ;; Skip by any output the program may generate to stdout.
376 ;; Note that pcl-cvs will get seriously confused if the
377 ;; program prints anything to stderr.
378 (re-search-forward cvs-update-prog-output-skip-regexp))))
379
380 (and
381 (cvs-match "cvs[.ex]* \\[[a-z]+ aborted\\]:.*$")
382 (cvs-parsed-fileinfo 'MESSAGE ""))
f1180544 383
5b467bf4
SM
384 ;; sadly you can't do much with these since the path is in the repository
385 (cvs-match "Directory .* added to the repository$")
386 )))
387
388
389(defun cvs-parse-merge ()
390 (let (path base-rev head-rev handled type)
391 ;; A merge (maybe with a conflict).
392 (and
393 (cvs-match "RCS file: .*$")
394 ;; Squirrel away info about the files that were retrieved for merging
395 (cvs-match "retrieving revision \\([0-9.]+\\)$" (base-rev 1))
396 (cvs-match "retrieving revision \\([0-9.]+\\)$" (head-rev 1))
397 (cvs-match "Merging differences between [0-9.]+ and [0-9.]+ into \\(.*\\)$"
398 (path 1))
399
400 ;; eat up potential conflict warnings
401 (cvs-or (cvs-match "\\(rcs\\)?merge:?\\( warning\\)?: \\(overlaps\\|conflicts\\) \\(or other problems \\)?during merge$" (type 'CONFLICT)) t)
402 (cvs-or
403 (and
404 (cvs-match "cvs[.ex]* [a-z]+: ")
405 (cvs-or
406 (cvs-match "conflicts found in \\(.*\\)$" (path 1) (type 'CONFLICT))
407 (cvs-match "could not merge .*$")
408 (cvs-match "restoring \\(.*\\) from backup file .*$" (path 1))))
409 t)
410
411 ;; Is it a succesful merge?
412 ;; Figure out result of merging (ie, was there a conflict?)
413 (let ((qfile (regexp-quote path)))
414 (cvs-or
415 ;; Conflict
416 (and
417 (cvs-match (concat "C \\(.*" qfile "\\)$") (path 1) (type 'CONFLICT))
418 ;; C might be followed by a "suprious" U for non-mergeable files
419 (cvs-or (cvs-match (concat "U \\(.*" qfile "\\)$")) t))
420 ;; Successful merge
421 (cvs-match (concat "M \\(.*" qfile "\\)$") (path 1))
422 ;; The file already contained the modifications
423 (cvs-match (concat "^\\(.*" qfile
424 "\\) already contains the differences between .*$")
425 (path 1) (type '(UP-TO-DATE . MERGED)))
426 t)
1e98637e
SM
427 ;; FIXME: PATH might not be set yet. Sometimes the only path
428 ;; information is in `RCS file: ...' (yuck!!).
5b467bf4
SM
429 (cvs-parsed-fileinfo (if dont-change-disc 'NEED-MERGE
430 (or type '(MODIFIED . MERGED))) path nil
431 :merge (cons base-rev head-rev))))))
432
433(defun cvs-parse-status ()
434 (let (nofile path base-rev head-rev type)
435 (and
436 (cvs-match
437 "===================================================================$")
438 (cvs-match "File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: "
439 (nofile 1) (path 2))
440 (cvs-or
441 (cvs-match "Needs \\(Checkout\\|Patch\\)$"
442 (type (if nofile 'MISSING 'NEED-UPDATE)))
443 (cvs-match "Up-to-date$"
444 (type (if nofile '(UP-TO-DATE . REMOVED) 'UP-TO-DATE)))
1f3d429d 445 (cvs-match "File had conflicts on merge$" (type 'MODIFIED))
5b467bf4
SM
446 (cvs-match ".*[Cc]onflict.*$" (type 'CONFLICT))
447 (cvs-match "Locally Added$" (type 'ADDED))
448 (cvs-match "Locally Removed$" (type 'REMOVED))
449 (cvs-match "Locally Modified$" (type 'MODIFIED))
450 (cvs-match "Needs Merge$" (type 'NEED-MERGE))
f9e7890c 451 (cvs-match "Entry Invalid" (type '(NEED-MERGE . REMOVED)))
5b467bf4
SM
452 (cvs-match "Unknown$" (type 'UNKNOWN)))
453 (cvs-match "$")
454 (cvs-or
455 (cvs-match " *Version:[ \t]*\\([0-9.]+\\).*$" (base-rev 1))
456 ;; NOTE: there's no date on the end of the following for server mode...
457 (cvs-match " *Working revision:[ \t]*-?\\([0-9.]+\\).*$" (base-rev 1))
458 ;; Let's not get all worked up if the format changes a bit
459 (cvs-match " *Working revision:.*$"))
460 (cvs-or
461 (cvs-match " *RCS Version:[ \t]*\\([0-9.]+\\)[ \t]*.*$" (head-rev 1))
462 (cvs-match " *Repository revision:[ \t]*\\([0-9.]+\\)[ \t]*\\(.*\\)$"
463 (head-rev 1))
464 (cvs-match " *Repository revision:.*"))
465 (cvs-or
466 (and;;sometimes those fields are missing
467 (cvs-match " *Sticky Tag:[ \t]*\\(.*\\)$") ; FIXME: use it
468 (cvs-match " *Sticky Date:[ \t]*\\(.*\\)$") ; FIXME: use it
469 (cvs-match " *Sticky Options:[ \t]*\\(.*\\)$")) ; FIXME: use it
470 t)
471 (cvs-match "$")
472 ;; ignore the tags-listing in the case of `status -v'
473 (cvs-or (cvs-match " *Existing Tags:\n\\(\t.*\n\\)*$") t)
474 (cvs-parsed-fileinfo type path nil
475 :base-rev base-rev
476 :head-rev head-rev))))
477
478(defun cvs-parse-commit ()
479 (let (path base-rev subtype)
480 (cvs-or
481
482 (and
483 (cvs-match "\\(Checking in\\|Removing\\) \\(.*\\);$" (path 2))
484 (cvs-match ".*,v <-- .*$")
485 (cvs-or
486 ;; deletion
487 (cvs-match "new revision: delete; previous revision: \\([0-9.]*\\)$"
488 (subtype 'REMOVED) (base-rev 1))
489 ;; addition
490 (cvs-match "initial revision: \\([0-9.]*\\)$"
491 (subtype 'ADDED) (base-rev 1))
492 ;; update
493 (cvs-match "new revision: \\([0-9.]*\\); previous revision: .*$"
494 (subtype 'COMMITTED) (base-rev 1)))
495 (cvs-match "done$")
2f8e4cab
SM
496 (progn
497 ;; Try to remove the temp files used by VC.
3911563a 498 (vc-delete-automatic-version-backups (expand-file-name path))
2f8e4cab
SM
499 ;; it's important here not to rely on the default directory management
500 ;; because `cvs commit' might begin by a series of Examining messages
501 ;; so the processing of the actual checkin messages might begin with
502 ;; a `current-dir' set to something different from ""
503 (cvs-parsed-fileinfo (cons 'UP-TO-DATE subtype) path 'trust
504 :base-rev base-rev)))
f1180544 505
5b467bf4
SM
506 ;; useless message added before the actual addition: ignored
507 (cvs-match "RCS file: .*\ndone$"))))
508
509
510(provide 'pcvs-parse)
511
3afbc435 512;;; pcvs-parse.el ends here