Some fixes to follow coding conventions.
[emacs.git] / lisp / pcvs-info.el
blobba172471930a2620306b4ef236bbb85c2d25fafa
1 ;;; pcvs-info.el --- internal representation of a fileinfo entry
3 ;; Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@cs.yale.edu>
6 ;; Keywords: pcl-cvs
7 ;; Revision: $Id: pcvs-info.el,v 1.6 2000/12/18 03:17:31 monnier Exp $
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-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))
50 (defvar global-font-lock-mode)
51 (defvar font-lock-auto-fontify)
52 (defcustom cvs-highlight
53 (or (and (boundp 'font-lock-auto-fontify) font-lock-auto-fontify)
54 (and (boundp 'global-font-lock-mode) global-font-lock-mode))
55 "*Whether to use text highlighting (à la font-lock) or not."
56 :group 'pcl-cvs
57 :type '(boolean))
59 (defcustom cvs-allow-dir-commit nil
60 "*Allow `cvs-mode-commit' on directories.
61 If you commit without any marked file and with the cursor positioned
62 on a directory entry, cvs would commit the whole directory. This seems
63 to confuse some users sometimes."
64 :group 'pcl-cvs
65 :type '(boolean))
67 ;;;;
68 ;;;; Faces for fontification
69 ;;;;
71 (defface cvs-header-face
72 '((((class color) (background dark))
73 (:foreground "lightyellow" :bold t))
74 (((class color) (background light))
75 (:foreground "blue4" :bold t))
76 (t (:bold t)))
77 "PCL-CVS face used to highlight directory changes."
78 :group 'pcl-cvs)
80 (defface cvs-filename-face
81 '((((class color) (background dark))
82 (:foreground "lightblue"))
83 (((class color) (background light))
84 (:foreground "blue4"))
85 (t ()))
86 "PCL-CVS face used to highlight file names."
87 :group 'pcl-cvs)
89 (defface cvs-unknown-face
90 '((((class color) (background dark))
91 (:foreground "red"))
92 (((class color) (background light))
93 (:foreground "red"))
94 (t (:italic t)))
95 "PCL-CVS face used to highlight unknown file status."
96 :group 'pcl-cvs)
98 (defface cvs-handled-face
99 '((((class color) (background dark))
100 (:foreground "pink"))
101 (((class color) (background light))
102 (:foreground "pink"))
103 (t ()))
104 "PCL-CVS face used to highlight handled file status."
105 :group 'pcl-cvs)
107 (defface cvs-need-action-face
108 '((((class color) (background dark))
109 (:foreground "orange"))
110 (((class color) (background light))
111 (:foreground "orange"))
112 (t (:italic t)))
113 "PCL-CVS face used to highlight status of files needing action."
114 :group 'pcl-cvs)
116 (defface cvs-marked-face
117 '((((class color) (background dark))
118 (:foreground "green" :bold t))
119 (((class color) (background light))
120 (:foreground "green3" :bold t))
121 (t (:bold t)))
122 "PCL-CVS face used to highlight marked file indicator."
123 :group 'pcl-cvs)
125 (defface cvs-msg-face
126 '((t (:italic t)))
127 "PCL-CVS face used to highlight CVS messages."
128 :group 'pcl-cvs)
130 (defvar cvs-fi-up-to-date-face 'cvs-handled-face)
131 (defvar cvs-fi-unknown-face 'cvs-unknown-face)
132 (defvar cvs-fi-conflict-face 'font-lock-warning-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
136 ;; to change it.
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")
145 ;; Constructor:
147 (defstruct (cvs-fileinfo
148 (:constructor nil)
149 (:copier nil)
150 (:constructor -cvs-create-fileinfo (type dir file full-log
151 &key marked subtype
152 merge
153 base-rev
154 head-rev))
155 (:conc-name cvs-fileinfo->))
156 marked ;; t/nil.
157 type ;; See below
158 subtype ;; See below
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
165 ;; the repository.
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
184 ;; update status
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
203 ;; full-log."
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)))
209 ;; Fake selectors:
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) "\\.")))
235 (dolist (f files bf)
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)))
245 ;; Predicate:
247 (defun boolp (x) (or (eq t x) (null x)))
248 (defun cvs-check-fileinfo (fi)
249 "Check FI's conformance to some conventions."
250 (let ((check 'none)
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)
263 (setq check 'dir)
264 (and (stringp dir)
265 (not (file-name-absolute-p dir))
266 (or (string= dir "")
267 (string= dir (file-name-as-directory dir))))
268 (setq check 'file)
269 (and (stringp file)
270 (string= file (file-name-nondirectory file)))
271 (setq check 'type) (symbolp type)
272 (setq check 'consistency)
273 (case type
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)
277 t)))
279 (error "Invalid :%s in cvs-fileinfo %s" check fi))))
282 ;;;;
283 ;;;; State table to indicate what you can do when.
284 ;;;;
286 (defconst cvs-states
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)
297 (DEAD )
298 (MESSAGE))
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
306 any information.")
308 ;;;;
309 ;;;; Utility functions
310 ;;;;
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)
322 (when cvs-highlight
323 (add-text-properties 0 (length str)
324 (list* 'face face
325 (when keymap
326 (list* 'mouse-face 'highlight
327 (when (keymapp keymap)
328 (list 'keymap keymap)))))
329 str))
330 str)
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)))
338 (insert
339 (case type
340 (DIRCHANGE (concat "In directory "
341 (cvs-add-face (cvs-fileinfo->full-path fileinfo)
342 'cvs-header-face t)
343 ":"))
344 (MESSAGE
345 (cvs-add-face (format "Message: %s" (cvs-fileinfo->full-log fileinfo))
346 'cvs-msg-face))
348 (let* ((status (if (cvs-fileinfo->marked fileinfo)
349 (cvs-add-face "*" 'cvs-marked-face)
350 " "))
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))
355 (type
356 (let ((str (case type
357 ;;(MOD-CONFLICT "Not Removed")
358 (DEAD "")
359 (t (capitalize (symbol-name type)))))
360 (face (let ((sym (intern
361 (concat "cvs-fi-"
362 (downcase (symbol-name type))
363 "-face"))))
364 (or (and (boundp sym) (symbol-value sym))
365 'cvs-need-action-face))))
366 (cvs-add-face str face cvs-status-map)))
367 (side (or
368 ;; maybe a subtype
369 (when subtype (downcase (symbol-name subtype)))
370 ;; or the head-rev
371 (when (and head (not (string= head base))) head)
372 ;; or nothing
373 "")))
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))
387 (cond
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)))
399 (cond
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."
419 (let* ((singlefile
420 (cond
421 ((equal dir "") nil)
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))
426 (fis nil))
427 (if (not (file-readable-p file))
428 (push (cvs-create-fileinfo (if singlefile 'UNKNOWN 'DIRCHANGE)
429 dir (or singlefile ".") "") fis)
430 (with-temp-buffer
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.
434 (cond
435 ((not singlefile)
436 (push (cvs-create-fileinfo 'DIRCHANGE dir "." "") fis))
437 ((re-search-forward
438 (concat "^[^/]*/" (regexp-quote singlefile) "/.*") nil t)
439 (setq all 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
449 (match-string 2)))
450 all)
451 fis))
452 (let ((f (match-string 2))
453 (rev (match-string 3))
454 (date (match-string 4))
455 timestamp
456 (type 'MODIFIED)
457 (subtype nil))
458 (cond
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))
467 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 ;;; pcvs-info.el ends here