1 ;;; pcvs-info.el --- Internal representation of a fileinfo entry
3 ;; Copyright (C) 1991-2000 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
8 ;; Revision: $Id: pcvs-info.el,v 1.2 2000/03/22 02:56:52 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)
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.
29 ;; The cvs-fileinfo data structure:
31 ;; When the `cvs update' is ready we parse the output. Every file
32 ;; that is affected in some way is added to the cookie collection as
33 ;; a "fileinfo" (as defined below in cvs-create-fileinfo).
37 (eval-when-compile (require 'cl
))
39 ;;(require 'pcvs-defs)
45 (defcustom cvs-display-full-path t
46 "*Specifies how the filenames should look like in the listing.
47 If t, their full path name will be displayed, else only the filename."
51 (defvar global-font-lock-mode
)
52 (defvar font-lock-auto-fontify
)
53 (defcustom cvs-highlight
54 (or (and (boundp 'font-lock-auto-fontify
) font-lock-auto-fontify
)
55 (and (boundp 'global-font-lock-mode
) global-font-lock-mode
))
56 "*Whether to use text highlighting (à la font-lock) or not."
60 (defcustom cvs-allow-dir-commit nil
61 "*Allow `cvs-mode-commit' on directories.
62 If you commit without any marked file and with the cursor positioned
63 on a directory entry, cvs would commit the whole directory. This seems
64 to confuse some users sometimes."
69 ;;;; Faces for fontification
72 (defface cvs-header-face
73 '((((class color
) (background dark
))
74 (:foreground
"lightyellow" :bold t
))
75 (((class color
) (background light
))
76 (:foreground
"blue4" :bold t
))
78 "PCL-CVS face used to highlight directory changes."
81 (defface cvs-filename-face
82 '((((class color
) (background dark
))
83 (:foreground
"lightblue"))
84 (((class color
) (background light
))
85 (:foreground
"blue4"))
87 "PCL-CVS face used to highlight file names."
90 (defface cvs-unknown-face
91 '((((class color
) (background dark
))
93 (((class color
) (background light
))
96 "PCL-CVS face used to highlight unknown file status."
99 (defface cvs-handled-face
100 '((((class color
) (background dark
))
101 (:foreground
"pink"))
102 (((class color
) (background light
))
103 (:foreground
"pink"))
105 "PCL-CVS face used to highlight handled file status."
108 (defface cvs-need-action-face
109 '((((class color
) (background dark
))
110 (:foreground
"orange"))
111 (((class color
) (background light
))
112 (:foreground
"orange"))
114 "PCL-CVS face used to highlight status of files needing action."
117 (defface cvs-marked-face
118 '((((class color
) (background dark
))
119 (:foreground
"green" :bold t
))
120 (((class color
) (background light
))
121 (:foreground
"green3" :bold t
))
123 "PCL-CVS face used to highlight marked file indicator."
126 (defface cvs-msg-face
128 "PCL-CVS face used to highlight CVS messages."
131 (defvar cvs-fi-up-to-date-face
'cvs-handled-face
)
132 (defvar cvs-fi-unknown-face
'cvs-unknown-face
)
134 ;; There is normally no need to alter the following variable, but if
135 ;; your site has installed CVS in a non-standard way you might have
138 (defvar cvs-bakprefix
".#"
139 "The prefix that CVS prepends to files when rcsmerge'ing.")
141 (easy-mmode-defmap cvs-status-map
142 '(([(mouse-2)] . cvs-mouse-toggle-mark
))
143 "Local keymap for text properties of status")
147 (defstruct (cvs-fileinfo
150 (:constructor -cvs-create-fileinfo
(type dir file full-log
155 (:conc-name cvs-fileinfo-
>))
159 dir
;; Relative directory the file resides in.
160 ;; (concat dir file) should give a valid path.
161 file
;; The file name sans the directory.
162 base-rev
;; During status: This is the revision that the
163 ;; working file is based on.
164 head-rev
;; During status: This is the highest revision in
166 merge
;; A cons cell containing the (ancestor . head) revisions
167 ;; of the merge that resulted in the current file.
168 ;;removed ;; t if the file no longer exists.
169 full-log
;; The output from cvs, unparsed.
170 ;;mod-time ;; Not used.
172 ;; In addition to the above, the following values can be extracted:
174 ;; handled ;; t if this file doesn't require further action.
175 ;; full-path ;; The complete relative filename.
176 ;; pp-name ;; The printed file name
177 ;; backup-file;; For MERGED and CONFLICT files after a \"cvs update\",
178 ;; this is a full path to the backup file where the
179 ;; untouched version resides.
181 ;; The meaning of the type field:
183 ;; Value ---Used by--- Explanation
185 ;; NEED-UPDATE x file needs update
186 ;; MODIFIED x x modified by you, unchanged in repository
187 ;; MERGED x x successful merge
188 ;; ADDED x x added by you, not yet committed
189 ;; MISSING x rm'd, but not yet `cvs remove'd
190 ;; REMOVED x x removed by you, not yet committed
191 ;; NEED-MERGE x need merge
192 ;; CONFLICT x conflict when merging
193 ;; ;;MOD-CONFLICT x removed locally, changed in repository.
194 ;; DIRCHANGE x x A change of directory.
195 ;; UNKNOWN x An unknown file.
196 ;; UP-TO-DATE x The file is up-to-date.
197 ;; UPDATED x x file copied from repository
198 ;; PATCHED x x diff applied from repository
199 ;; COMMITTED x x cvs commit'd
200 ;; DEAD An entry that should be removed
201 ;; MESSAGE x x This is a special fileinfo that is used
202 ;; to display a text that should be in
204 ;; TEMP A temporary message that should be removed
206 (defun cvs-create-fileinfo (type dir file msg
&rest keys
)
207 (cvs-check-fileinfo (apply #'-cvs-create-fileinfo type dir file msg keys
)))
211 (defun cvs-fileinfo->full-path
(fileinfo)
212 "Return the full path for the file that is described in FILEINFO."
213 (let ((dir (cvs-fileinfo->dir fileinfo
)))
214 (if (eq (cvs-fileinfo->type fileinfo
) 'DIRCHANGE
)
215 (if (string= dir
"") "." (directory-file-name dir
))
216 ;; Here, I use `concat' rather than `expand-file-name' because I want
217 ;; the resulting path to stay relative if `dir' is relative.
218 (concat dir
(cvs-fileinfo->file fileinfo
)))))
220 (defun cvs-fileinfo->pp-name
(fi)
221 "Return the filename of FI as it should be displayed."
222 (if cvs-display-full-path
223 (cvs-fileinfo->full-path fi
)
224 (cvs-fileinfo->file fi
)))
226 (defun cvs-fileinfo->backup-file
(fileinfo)
227 "Construct the file name of the backup file for FILEINFO."
228 (let* ((dir (cvs-fileinfo->dir fileinfo
))
229 (file (cvs-fileinfo->file fileinfo
))
230 (default-directory (file-name-as-directory (expand-file-name dir
)))
231 (files (directory-files "." nil
232 (concat "^" (regexp-quote cvs-bakprefix
)
233 (regexp-quote file
) "\\.")))
236 (when (and (file-readable-p f
)
237 (or (null bf
) (file-newer-than-file-p f bf
)))
238 (setq bf
(concat dir f
))))))
240 ;; (defun cvs-fileinfo->handled (fileinfo)
241 ;; "Tell if this requires further action"
242 ;; (memq (cvs-fileinfo->type fileinfo) '(UP-TO-DATE DEAD)))
247 (defun boolp (x) (or (eq t x
) (null x
)))
248 (defun cvs-check-fileinfo (fi)
249 "Check FI's conformance to some conventions."
251 (type (cvs-fileinfo->type fi
))
252 (subtype (cvs-fileinfo->subtype fi
))
253 (marked (cvs-fileinfo->marked fi
))
254 (dir (cvs-fileinfo->dir fi
))
255 (file (cvs-fileinfo->file fi
))
256 (base-rev (cvs-fileinfo->base-rev fi
))
257 (head-rev (cvs-fileinfo->head-rev fi
))
258 (full-log (cvs-fileinfo->full-log fi
)))
259 (if (and (setq check
'marked
) (boolp marked
)
260 (setq check
'base-rev
) (or (null base-rev
) (stringp base-rev
))
261 (setq check
'head-rev
) (or (null head-rev
) (stringp head-rev
))
262 (setq check
'full-log
) (stringp full-log
)
265 (not (file-name-absolute-p dir
))
267 (string= dir
(file-name-as-directory dir
))))
270 (string= file
(file-name-nondirectory file
)))
271 (setq check
'type
) (symbolp type
)
272 (setq check
'consistency
)
274 (DIRCHANGE (and (null subtype
) (string= "." file
)))
275 ((NEED-UPDATE ADDED MISSING DEAD MODIFIED MESSAGE UP-TO-DATE
276 REMOVED NEED-MERGE CONFLICT UNKNOWN MESSAGE
)
279 (error "Invalid :%s in cvs-fileinfo %s" check fi
))))
283 ;;;; State table to indicate what you can do when.
287 `((NEED-UPDATE update diff
)
288 (UP-TO-DATE update nil remove diff safe-rm revert
)
289 (MODIFIED update commit undo remove diff merge diff-base
)
290 (ADDED update commit remove
)
291 (MISSING remove undo update safe-rm revert
)
292 (REMOVED commit add undo safe-rm
)
293 (NEED-MERGE update undo diff diff-base
)
294 (CONFLICT merge remove undo commit diff diff-base
)
295 (DIRCHANGE remove update diff
,(if cvs-allow-dir-commit
'commit
) tag
)
296 (UNKNOWN ignore add remove
)
299 "Fileinfo state descriptions for pcl-cvs.
300 This is an assoc list. Each element consists of (STATE . FUNS)
301 - STATE (described in `cvs-create-fileinfo') is the key
302 - FUNS is the list of applicable operations.
303 The first one (if any) should be the \"default\" action.
304 Most of the actions have the obvious meaning.
305 `safe-rm' indicates that the file can be removed without losing
309 ;;;; Utility functions
312 (defun cvs-applicable-p (fi-or-type func
)
313 "Check if FUNC is applicable to FI-OR-TYPE.
314 If FUNC is nil, always return t.
315 FI-OR-TYPE can either be a symbol (a fileinfo-type) or a fileinfo."
316 (let ((type (if (symbolp fi-or-type
) fi-or-type
317 (cvs-fileinfo->type fi-or-type
))))
318 (and (not (eq type
'MESSAGE
))
319 (eq (car (memq func
(cdr (assq type cvs-states
)))) func
))))
321 (defun cvs-add-face (str face
&optional keymap
)
323 (add-text-properties 0 (length str
)
326 (list* 'mouse-face
'highlight
327 (when (keymapp keymap
)
328 (list 'keymap keymap
)))))
332 (defun cvs-fileinfo-pp (fileinfo)
333 "Pretty print FILEINFO. Insert a printed representation in current buffer.
334 For use by the cookie package."
335 (cvs-check-fileinfo fileinfo
)
336 (let ((type (cvs-fileinfo->type fileinfo
))
337 (subtype (cvs-fileinfo->subtype fileinfo
)))
340 (DIRCHANGE (concat "In directory "
341 (cvs-add-face (cvs-fileinfo->full-path fileinfo
)
345 (cvs-add-face (format "Message: %s" (cvs-fileinfo->full-log fileinfo
))
348 (let* ((status (if (cvs-fileinfo->marked fileinfo
)
349 (cvs-add-face "*" 'cvs-marked-face
)
351 (file (cvs-add-face (cvs-fileinfo->pp-name fileinfo
)
352 'cvs-filename-face t
))
353 (base (or (cvs-fileinfo->base-rev fileinfo
) ""))
354 (head (cvs-fileinfo->head-rev fileinfo
))
356 (let ((str (case type
357 ;;(MOD-CONFLICT "Not Removed")
359 (t (capitalize (symbol-name type
)))))
360 (face (let ((sym (intern
362 (downcase (symbol-name type
))
364 (or (and (boundp sym
) (symbol-value sym
))
365 'cvs-need-action-face
))))
366 (cvs-add-face str face cvs-status-map
)))
369 (when subtype
(downcase (symbol-name subtype
)))
371 (when (and head
(not (string= head base
))) head
)
374 (format "%-11s %s %-11s %-11s %s"
375 side status type base file
)))))))
378 (defun cvs-fileinfo-update (fi fi-new
)
379 "Update FI with the information provided in FI-NEW."
380 (let ((type (cvs-fileinfo->type fi-new
))
381 (merge (cvs-fileinfo->merge fi-new
)))
382 (setf (cvs-fileinfo->type fi
) type
)
383 (setf (cvs-fileinfo->subtype fi
) (cvs-fileinfo->subtype fi-new
))
384 (setf (cvs-fileinfo->full-log fi
) (cvs-fileinfo->full-log fi-new
))
385 (setf (cvs-fileinfo->base-rev fi
) (cvs-fileinfo->base-rev fi-new
))
386 (setf (cvs-fileinfo->head-rev fi
) (cvs-fileinfo->head-rev fi-new
))
388 (merge (setf (cvs-fileinfo->merge fi
) merge
))
389 ((memq type
'(UP-TO-DATE NEED-UPDATE
))
390 (setf (cvs-fileinfo->merge fi
) nil
)))))
392 (defun cvs-fileinfo< (a b
)
393 "Compare fileinfo A with fileinfo B and return t if A is `less'.
394 The ordering defined by this function is such that directories are
395 sorted alphabetically, and inside every directory the DIRCHANGE
396 fileinfo will appear first, followed by all files (alphabetically)."
397 (let ((subtypea (cvs-fileinfo->subtype a
))
398 (subtypeb (cvs-fileinfo->subtype b
)))
400 ;; Sort according to directories.
401 ((string< (cvs-fileinfo->dir a
) (cvs-fileinfo->dir b
)) t
)
402 ((not (string= (cvs-fileinfo->dir a
) (cvs-fileinfo->dir b
))) nil
)
404 ;; The DIRCHANGE entry is always first within the directory.
405 ((eq (cvs-fileinfo->type b
) 'DIRCHANGE
) nil
)
406 ((eq (cvs-fileinfo->type a
) 'DIRCHANGE
) t
)
408 ;; All files are sorted by file name.
409 ((string< (cvs-fileinfo->file a
) (cvs-fileinfo->file b
))))))
412 ;;; Look at CVS/Entries to quickly find a first approximation of the status
415 (defun cvs-fileinfo-from-entries (dir &optional all
)
416 "List of fileinfos for DIR, extracted from CVS/Entries.
417 Unless ALL is optional, returns only the files that are not up-to-date.
418 DIR can also be a file."
422 ((file-directory-p dir
) (setq dir
(file-name-as-directory dir
)) nil
)
423 (t (prog1 (file-name-nondirectory dir
)
424 (setq dir
(or (file-name-directory dir
) ""))))))
425 (file (expand-file-name "CVS/Entries" dir
))
427 (if (not (file-readable-p file
))
428 (push (cvs-create-fileinfo (if singlefile
'UNKNOWN
'DIRCHANGE
)
429 dir
(or singlefile
".") "") fis
)
431 (insert-file-contents file
)
432 (goto-char (point-min))
433 ;; Select the single file entry in case we're only interested in a file.
436 (push (cvs-create-fileinfo 'DIRCHANGE dir
"." "") fis
))
438 (concat "^[^/]*/" (regexp-quote singlefile
) "/.*") nil t
)
440 (goto-char (match-beginning 0))
441 (narrow-to-region (point) (match-end 0)))
443 (push (cvs-create-fileinfo 'UNKNOWN dir singlefile
"") fis
)
444 (narrow-to-region (point-min) (point-min))))
445 (while (looking-at "\\([^/]*\\)/\\([^/]*\\)/\\([^/]*\\)/\\([^/]*\\)/")
446 (if (/= (match-beginning 1) (match-end 1))
447 (setq fis
(append (cvs-fileinfo-from-entries
448 (concat dir
(file-name-as-directory
452 (let ((f (match-string 2))
453 (rev (match-string 3))
454 (date (match-string 4))
459 ((equal (substring rev
0 1) "-")
460 (setq type
'REMOVED rev
(substring rev
1)))
461 ((not (file-exists-p (concat dir f
))) (setq type
'MISSING
))
462 ((equal rev
"0") (setq type
'ADDED rev nil
))
463 ((equal date
"Result of merge") (setq subtype
'MERGED
))
464 ((let ((mtime (nth 5 (file-attributes (concat dir f
))))
465 (system-time-locale "C"))
466 (equal (setq timestamp
(format-time-string "%c" mtime
'utc
))
468 (setq type
(if all
'UP-TO-DATE
)))
469 ((equal date
(concat "Result of merge+" timestamp
))
470 (setq type
'CONFLICT
)))
472 (push (cvs-create-fileinfo type dir f
""
473 :base-rev rev
:subtype subtype
)
480 ;;; pcl-cvs-info.el ends here