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