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