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