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