]> code.delx.au - gnu-emacs/blob - lisp/pcvs-info.el
Revision: miles@gnu.org--gnu-2005/emacs--cvs-trunk--0--patch-254
[gnu-emacs] / lisp / pcvs-info.el
1 ;;; pcvs-info.el --- internal representation of a fileinfo entry
2
3 ;; Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2000, 2004, 2005 Free Software Foundation, Inc.
5
6 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
7 ;; Keywords: pcl-cvs
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 ;; The cvs-fileinfo data structure:
29 ;;
30 ;; When the `cvs update' is ready we parse the output. Every file
31 ;; that is affected in some way is added to the cookie collection as
32 ;; a "fileinfo" (as defined below in cvs-create-fileinfo).
33
34 ;;; Code:
35
36 (eval-when-compile (require 'cl))
37 (require 'pcvs-util)
38 ;;(require 'pcvs-defs)
39
40 ;;;;
41 ;;;; config variables
42 ;;;;
43
44 (defcustom cvs-display-full-path t
45 "*Specifies how the filenames should look like in the listing.
46 If t, their full path name will be displayed, else only the filename."
47 :group 'pcl-cvs
48 :type '(boolean))
49
50 (defcustom cvs-allow-dir-commit nil
51 "*Allow `cvs-mode-commit' on directories.
52 If you commit without any marked file and with the cursor positioned
53 on a directory entry, cvs would commit the whole directory. This seems
54 to confuse some users sometimes."
55 :group 'pcl-cvs
56 :type '(boolean))
57
58 ;;;;
59 ;;;; Faces for fontification
60 ;;;;
61
62 (defface cvs-header-face
63 '((((class color) (background dark))
64 (:foreground "lightyellow" :weight bold))
65 (((class color) (background light))
66 (:foreground "blue4" :weight bold))
67 (t (:weight bold)))
68 "PCL-CVS face used to highlight directory changes."
69 :group 'pcl-cvs)
70
71 (defface cvs-filename-face
72 '((((class color) (background dark))
73 (:foreground "lightblue"))
74 (((class color) (background light))
75 (:foreground "blue4"))
76 (t ()))
77 "PCL-CVS face used to highlight file names."
78 :group 'pcl-cvs)
79
80 (defface cvs-unknown-face
81 '((((class color) (background dark))
82 (:foreground "red"))
83 (((class color) (background light))
84 (:foreground "red"))
85 (t (:slant italic)))
86 "PCL-CVS face used to highlight unknown file status."
87 :group 'pcl-cvs)
88
89 (defface cvs-handled-face
90 '((((class color) (background dark))
91 (:foreground "pink"))
92 (((class color) (background light))
93 (:foreground "pink"))
94 (t ()))
95 "PCL-CVS face used to highlight handled file status."
96 :group 'pcl-cvs)
97
98 (defface cvs-need-action-face
99 '((((class color) (background dark))
100 (:foreground "orange"))
101 (((class color) (background light))
102 (:foreground "orange"))
103 (t (:slant italic)))
104 "PCL-CVS face used to highlight status of files needing action."
105 :group 'pcl-cvs)
106
107 (defface cvs-marked-face
108 '((((min-colors 88) (class color) (background dark))
109 (:foreground "green1" :weight bold))
110 (((class color) (background dark))
111 (:foreground "green" :weight bold))
112 (((class color) (background light))
113 (:foreground "green3" :weight bold))
114 (t (:weight bold)))
115 "PCL-CVS face used to highlight marked file indicator."
116 :group 'pcl-cvs)
117
118 (defface cvs-msg-face
119 '((t (:slant italic)))
120 "PCL-CVS face used to highlight CVS messages."
121 :group 'pcl-cvs)
122
123 (defvar cvs-fi-up-to-date-face 'cvs-handled-face)
124 (defvar cvs-fi-unknown-face 'cvs-unknown-face)
125 (defvar cvs-fi-conflict-face 'font-lock-warning-face)
126
127 ;; There is normally no need to alter the following variable, but if
128 ;; your site has installed CVS in a non-standard way you might have
129 ;; to change it.
130
131 (defvar cvs-bakprefix ".#"
132 "The prefix that CVS prepends to files when rcsmerge'ing.")
133
134 (easy-mmode-defmap cvs-status-map
135 '(([(mouse-2)] . cvs-mode-toggle-mark))
136 "Local keymap for text properties of status")
137
138 ;; Constructor:
139
140 (defstruct (cvs-fileinfo
141 (:constructor nil)
142 (:copier nil)
143 (:constructor -cvs-create-fileinfo (type dir file full-log
144 &key marked subtype
145 merge
146 base-rev
147 head-rev))
148 (:conc-name cvs-fileinfo->))
149 marked ;; t/nil.
150 type ;; See below
151 subtype ;; See below
152 dir ;; Relative directory the file resides in.
153 ;; (concat dir file) should give a valid path.
154 file ;; The file name sans the directory.
155 base-rev ;; During status: This is the revision that the
156 ;; working file is based on.
157 head-rev ;; During status: This is the highest revision in
158 ;; the repository.
159 merge ;; A cons cell containing the (ancestor . head) revisions
160 ;; of the merge that resulted in the current file.
161 ;;removed ;; t if the file no longer exists.
162 full-log ;; The output from cvs, unparsed.
163 ;;mod-time ;; Not used.
164
165 ;; In addition to the above, the following values can be extracted:
166
167 ;; handled ;; t if this file doesn't require further action.
168 ;; full-path ;; The complete relative filename.
169 ;; pp-name ;; The printed file name
170 ;; backup-file;; For MERGED and CONFLICT files after a \"cvs update\",
171 ;; this is a full path to the backup file where the
172 ;; untouched version resides.
173
174 ;; The meaning of the type field:
175
176 ;; Value ---Used by--- Explanation
177 ;; update status
178 ;; NEED-UPDATE x file needs update
179 ;; MODIFIED x x modified by you, unchanged in repository
180 ;; MERGED x x successful merge
181 ;; ADDED x x added by you, not yet committed
182 ;; MISSING x rm'd, but not yet `cvs remove'd
183 ;; REMOVED x x removed by you, not yet committed
184 ;; NEED-MERGE x need merge
185 ;; CONFLICT x conflict when merging
186 ;; ;;MOD-CONFLICT x removed locally, changed in repository.
187 ;; DIRCHANGE x x A change of directory.
188 ;; UNKNOWN x An unknown file.
189 ;; UP-TO-DATE x The file is up-to-date.
190 ;; UPDATED x x file copied from repository
191 ;; PATCHED x x diff applied from repository
192 ;; COMMITTED x x cvs commit'd
193 ;; DEAD An entry that should be removed
194 ;; MESSAGE x x This is a special fileinfo that is used
195 ;; to display a text that should be in
196 ;; full-log."
197 ;; TEMP A temporary message that should be removed
198 )
199 (defun cvs-create-fileinfo (type dir file msg &rest keys)
200 (cvs-check-fileinfo (apply #'-cvs-create-fileinfo type dir file msg keys)))
201
202 ;; Fake selectors:
203
204 (defun cvs-fileinfo->full-path (fileinfo)
205 "Return the full path for the file that is described in FILEINFO."
206 (let ((dir (cvs-fileinfo->dir fileinfo)))
207 (if (eq (cvs-fileinfo->type fileinfo) 'DIRCHANGE)
208 (if (string= dir "") "." (directory-file-name dir))
209 ;; Here, I use `concat' rather than `expand-file-name' because I want
210 ;; the resulting path to stay relative if `dir' is relative.
211 (concat dir (cvs-fileinfo->file fileinfo)))))
212
213 (defun cvs-fileinfo->pp-name (fi)
214 "Return the filename of FI as it should be displayed."
215 (if cvs-display-full-path
216 (cvs-fileinfo->full-path fi)
217 (cvs-fileinfo->file fi)))
218
219 (defun cvs-fileinfo->backup-file (fileinfo)
220 "Construct the file name of the backup file for FILEINFO."
221 (let* ((dir (cvs-fileinfo->dir fileinfo))
222 (file (cvs-fileinfo->file fileinfo))
223 (default-directory (file-name-as-directory (expand-file-name dir)))
224 (files (directory-files "." nil
225 (concat "\\`" (regexp-quote cvs-bakprefix)
226 (regexp-quote file) "\\(\\.[0-9]+\\.[0-9]+\\)+\\'")))
227 bf)
228 (dolist (f files bf)
229 (when (and (file-readable-p f)
230 (or (null bf) (file-newer-than-file-p f bf)))
231 (setq bf (concat dir f))))))
232
233 ;; (defun cvs-fileinfo->handled (fileinfo)
234 ;; "Tell if this requires further action"
235 ;; (memq (cvs-fileinfo->type fileinfo) '(UP-TO-DATE DEAD)))
236
237 \f
238 ;; Predicate:
239
240 (defun cvs-check-fileinfo (fi)
241 "Check FI's conformance to some conventions."
242 (let ((check 'none)
243 (type (cvs-fileinfo->type fi))
244 (subtype (cvs-fileinfo->subtype fi))
245 (marked (cvs-fileinfo->marked fi))
246 (dir (cvs-fileinfo->dir fi))
247 (file (cvs-fileinfo->file fi))
248 (base-rev (cvs-fileinfo->base-rev fi))
249 (head-rev (cvs-fileinfo->head-rev fi))
250 (full-log (cvs-fileinfo->full-log fi)))
251 (if (and (setq check 'marked) (memq marked '(t nil))
252 (setq check 'base-rev) (or (null base-rev) (stringp base-rev))
253 (setq check 'head-rev) (or (null head-rev) (stringp head-rev))
254 (setq check 'full-log) (stringp full-log)
255 (setq check 'dir)
256 (and (stringp dir)
257 (not (file-name-absolute-p dir))
258 (or (string= dir "")
259 (string= dir (file-name-as-directory dir))))
260 (setq check 'file)
261 (and (stringp file)
262 (string= file (file-name-nondirectory file)))
263 (setq check 'type) (symbolp type)
264 (setq check 'consistency)
265 (case type
266 (DIRCHANGE (and (null subtype) (string= "." file)))
267 ((NEED-UPDATE ADDED MISSING DEAD MODIFIED MESSAGE UP-TO-DATE
268 REMOVED NEED-MERGE CONFLICT UNKNOWN MESSAGE)
269 t)))
270 fi
271 (error "Invalid :%s in cvs-fileinfo %s" check fi))))
272
273 \f
274 ;;;;
275 ;;;; State table to indicate what you can do when.
276 ;;;;
277
278 (defconst cvs-states
279 `((NEED-UPDATE update diff ignore)
280 (UP-TO-DATE update nil remove diff safe-rm revert)
281 (MODIFIED update commit undo remove diff merge diff-base)
282 (ADDED update commit remove)
283 (MISSING remove undo update safe-rm revert)
284 (REMOVED commit add undo safe-rm)
285 (NEED-MERGE update undo diff diff-base)
286 (CONFLICT merge remove undo commit diff diff-base)
287 (DIRCHANGE remove update diff ,(if cvs-allow-dir-commit 'commit) tag)
288 (UNKNOWN ignore add remove)
289 (DEAD )
290 (MESSAGE))
291 "Fileinfo state descriptions for pcl-cvs.
292 This is an assoc list. Each element consists of (STATE . FUNS)
293 - STATE (described in `cvs-create-fileinfo') is the key
294 - FUNS is the list of applicable operations.
295 The first one (if any) should be the \"default\" action.
296 Most of the actions have the obvious meaning.
297 `safe-rm' indicates that the file can be removed without losing
298 any information.")
299
300 ;;;;
301 ;;;; Utility functions
302 ;;;;
303
304 (defun cvs-applicable-p (fi-or-type func)
305 "Check if FUNC is applicable to FI-OR-TYPE.
306 If FUNC is nil, always return t.
307 FI-OR-TYPE can either be a symbol (a fileinfo-type) or a fileinfo."
308 (let ((type (if (symbolp fi-or-type) fi-or-type
309 (cvs-fileinfo->type fi-or-type))))
310 (and (not (eq type 'MESSAGE))
311 (eq (car (memq func (cdr (assq type cvs-states)))) func))))
312
313 (defun cvs-add-face (str face &optional keymap &rest props)
314 (when keymap
315 (when (keymapp keymap)
316 (setq props (list* 'keymap keymap props)))
317 (setq props (list* 'mouse-face 'highlight props)))
318 (add-text-properties 0 (length str) (list* 'font-lock-face face props) str)
319 str)
320
321 (defun cvs-fileinfo-pp (fileinfo)
322 "Pretty print FILEINFO. Insert a printed representation in current buffer.
323 For use by the cookie package."
324 (cvs-check-fileinfo fileinfo)
325 (let ((type (cvs-fileinfo->type fileinfo))
326 (subtype (cvs-fileinfo->subtype fileinfo)))
327 (insert
328 (case type
329 (DIRCHANGE (concat "In directory "
330 (cvs-add-face (cvs-fileinfo->full-path fileinfo)
331 'cvs-header-face t
332 'cvs-goal-column t)
333 ":"))
334 (MESSAGE
335 (cvs-add-face (format "Message: %s" (cvs-fileinfo->full-log fileinfo))
336 'cvs-msg-face))
337 (t
338 (let* ((status (if (cvs-fileinfo->marked fileinfo)
339 (cvs-add-face "*" 'cvs-marked-face)
340 " "))
341 (file (cvs-add-face (cvs-fileinfo->pp-name fileinfo)
342 'cvs-filename-face t
343 'cvs-goal-column t))
344 (base (or (cvs-fileinfo->base-rev fileinfo) ""))
345 (head (cvs-fileinfo->head-rev fileinfo))
346 (type
347 (let ((str (case type
348 ;;(MOD-CONFLICT "Not Removed")
349 (DEAD "")
350 (t (capitalize (symbol-name type)))))
351 (face (let ((sym (intern
352 (concat "cvs-fi-"
353 (downcase (symbol-name type))
354 "-face"))))
355 (or (and (boundp sym) (symbol-value sym))
356 'cvs-need-action-face))))
357 (cvs-add-face str face cvs-status-map)))
358 (side (or
359 ;; maybe a subtype
360 (when subtype (downcase (symbol-name subtype)))
361 ;; or the head-rev
362 (when (and head (not (string= head base))) head)
363 ;; or nothing
364 "")))
365 (format "%-11s %s %-11s %-11s %s"
366 side status type base file)))))))
367
368
369 (defun cvs-fileinfo-update (fi fi-new)
370 "Update FI with the information provided in FI-NEW."
371 (let ((type (cvs-fileinfo->type fi-new))
372 (merge (cvs-fileinfo->merge fi-new)))
373 (setf (cvs-fileinfo->type fi) type)
374 (setf (cvs-fileinfo->subtype fi) (cvs-fileinfo->subtype fi-new))
375 (setf (cvs-fileinfo->full-log fi) (cvs-fileinfo->full-log fi-new))
376 (setf (cvs-fileinfo->base-rev fi) (cvs-fileinfo->base-rev fi-new))
377 (setf (cvs-fileinfo->head-rev fi) (cvs-fileinfo->head-rev fi-new))
378 (cond
379 (merge (setf (cvs-fileinfo->merge fi) merge))
380 ((memq type '(UP-TO-DATE NEED-UPDATE))
381 (setf (cvs-fileinfo->merge fi) nil)))))
382
383 (defun cvs-fileinfo< (a b)
384 "Compare fileinfo A with fileinfo B and return t if A is `less'.
385 The ordering defined by this function is such that directories are
386 sorted alphabetically, and inside every directory the DIRCHANGE
387 fileinfo will appear first, followed by all files (alphabetically)."
388 (let ((subtypea (cvs-fileinfo->subtype a))
389 (subtypeb (cvs-fileinfo->subtype b)))
390 (cond
391 ;; Sort according to directories.
392 ((string< (cvs-fileinfo->dir a) (cvs-fileinfo->dir b)) t)
393 ((not (string= (cvs-fileinfo->dir a) (cvs-fileinfo->dir b))) nil)
394
395 ;; The DIRCHANGE entry is always first within the directory.
396 ((eq (cvs-fileinfo->type b) 'DIRCHANGE) nil)
397 ((eq (cvs-fileinfo->type a) 'DIRCHANGE) t)
398
399 ;; All files are sorted by file name.
400 ((string< (cvs-fileinfo->file a) (cvs-fileinfo->file b))))))
401
402 ;;;
403 ;;; Look at CVS/Entries to quickly find a first approximation of the status
404 ;;;
405
406 (defun cvs-fileinfo-from-entries (dir &optional all)
407 "List of fileinfos for DIR, extracted from CVS/Entries.
408 Unless ALL is optional, returns only the files that are not up-to-date.
409 DIR can also be a file."
410 (let* ((singlefile
411 (cond
412 ((equal dir "") nil)
413 ((file-directory-p dir) (setq dir (file-name-as-directory dir)) nil)
414 (t (prog1 (file-name-nondirectory dir)
415 (setq dir (or (file-name-directory dir) ""))))))
416 (file (expand-file-name "CVS/Entries" dir))
417 (fis nil))
418 (if (not (file-readable-p file))
419 (push (cvs-create-fileinfo (if singlefile 'UNKNOWN 'DIRCHANGE)
420 dir (or singlefile ".") "") fis)
421 (with-temp-buffer
422 (insert-file-contents file)
423 (goto-char (point-min))
424 ;; Select the single file entry in case we're only interested in a file.
425 (cond
426 ((not singlefile)
427 (push (cvs-create-fileinfo 'DIRCHANGE dir "." "") fis))
428 ((re-search-forward
429 (concat "^[^/]*/" (regexp-quote singlefile) "/.*") nil t)
430 (setq all t)
431 (goto-char (match-beginning 0))
432 (narrow-to-region (point) (match-end 0)))
433 (t
434 (push (cvs-create-fileinfo 'UNKNOWN dir singlefile "") fis)
435 (narrow-to-region (point-min) (point-min))))
436 (while (looking-at "\\([^/]*\\)/\\([^/]*\\)/\\([^/]*\\)/\\([^/]*\\)/")
437 (if (/= (match-beginning 1) (match-end 1))
438 (setq fis (append (cvs-fileinfo-from-entries
439 (concat dir (file-name-as-directory
440 (match-string 2)))
441 all)
442 fis))
443 (let ((f (match-string 2))
444 (rev (match-string 3))
445 (date (match-string 4))
446 timestamp
447 (type 'MODIFIED)
448 (subtype nil))
449 (cond
450 ((equal (substring rev 0 1) "-")
451 (setq type 'REMOVED rev (substring rev 1)))
452 ((not (file-exists-p (concat dir f))) (setq type 'MISSING))
453 ((equal rev "0") (setq type 'ADDED rev nil))
454 ((equal date "Result of merge") (setq subtype 'MERGED))
455 ((let ((mtime (nth 5 (file-attributes (concat dir f))))
456 (system-time-locale "C"))
457 (setq timestamp (format-time-string "%c" mtime 'utc))
458 ;; Solaris sometimes uses "Wed Sep 05", not "Wed Sep 5".
459 ;; See "grep '[^a-z_]ctime' cvs/src/*.c" for reference.
460 (if (= (aref timestamp 8) ?0)
461 (setq timestamp (concat (substring timestamp 0 8)
462 " " (substring timestamp 9))))
463 (equal timestamp date))
464 (setq type (if all 'UP-TO-DATE)))
465 ((equal date (concat "Result of merge+" timestamp))
466 (setq type 'CONFLICT)))
467 (when type
468 (push (cvs-create-fileinfo type dir f ""
469 :base-rev rev :subtype subtype)
470 fis))))
471 (forward-line 1))))
472 fis))
473
474 (provide 'pcvs-info)
475
476 ;; arch-tag: d85dde07-bdc2-400a-882f-92f398c7b0ba
477 ;;; pcvs-info.el ends here