Bind set-case-syntax-charset to iso-8859-4. Never provide latin-4.
[bpt/emacs.git] / lisp / vc-cvs.el
CommitLineData
c1b25099
GM
1;;; vc-cvs.el --- non-resident support for CVS version-control
2
54be33bc 3;; Copyright (C) 1995,98,99,2000,2001 Free Software Foundation, Inc.
c1b25099
GM
4
5;; Author: FSF (see vc.el for full credits)
6;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7
df8e0a91 8;; $Id: vc-cvs.el,v 1.33 2002/02/21 20:16:47 spiegel Exp $
c1b25099
GM
9
10;; This file is part of GNU Emacs.
11
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)
15;; any later version.
16
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.
21
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.
26
27;;; Commentary:
28
29;;; Code:
30
ed5d446e 31(eval-when-compile
54be33bc 32 (require 'vc))
89e65817 33
9b0630e5 34;;;
8f98485f
AS
35;;; Customization options
36;;;
37
c1b25099
GM
38(defcustom vc-cvs-register-switches nil
39 "*Extra switches for registering a file into CVS.
40A string or list of strings passed to the checkin program by
41\\[vc-register]."
42 :type '(choice (const :tag "None" nil)
43 (string :tag "Argument String")
44 (repeat :tag "Argument List"
45 :value ("")
46 string))
0d685c4f 47 :version "21.1"
c1b25099
GM
48 :group 'vc)
49
633147f5
AS
50(defcustom vc-cvs-diff-switches nil
51 "*A string or list of strings specifying extra switches for cvs diff under VC."
52 :type '(choice (const :tag "None" nil)
53 (string :tag "Argument String")
54 (repeat :tag "Argument List"
55 :value ("")
56 string))
57 :version "21.1"
58 :group 'vc)
59
c1b25099
GM
60(defcustom vc-cvs-header (or (cdr (assoc 'CVS vc-header-alist)) '("\$Id\$"))
61 "*Header keywords to be inserted by `vc-insert-headers'."
0d685c4f 62 :version "21.1"
ee3275df 63 :type '(repeat string)
c1b25099
GM
64 :group 'vc)
65
66(defcustom vc-cvs-use-edit t
67 "*Non-nil means to use `cvs edit' to \"check out\" a file.
68This is only meaningful if you don't use the implicit checkout model
69\(i.e. if you have $CVSREAD set)."
70 :type 'boolean
0d685c4f 71 :version "21.1"
c1b25099
GM
72 :group 'vc)
73
74(defcustom vc-cvs-stay-local t
0d685c4f 75 "*Non-nil means use local operations when possible for remote repositories.
c6a84d17
SM
76This avoids slow queries over the network and instead uses heuristics
77and past information to determine the current status of a file.
78The value can also be a regular expression to match against the host name
79of a repository; then VC only stays local for hosts that match it."
c1b25099
GM
80 :type '(choice (const :tag "Always stay local" t)
81 (string :tag "Host regexp")
82 (const :tag "Don't stay local" nil))
0d685c4f 83 :version "21.1"
c1b25099
GM
84 :group 'vc)
85
51af12fc
AS
86(defcustom vc-cvs-sticky-date-format-string "%c"
87 "*Format string for mode-line display of sticky date.
88Format is according to `format-time-string'. Only used if
89`vc-cvs-sticky-tag-display' is t."
90 :type '(string)
91 :version "21.3"
92 :group 'vc)
93
94(defcustom vc-cvs-sticky-tag-display t
95 "*Specify the mode-line display of sticky tags.
96Value t means default display, nil means no display at all. If the
97value is a function or macro, it is called with the sticky tag and
98its' type as parameters, in that order. TYPE can have three different
99values: `symbolic-name' (TAG is a string), `revision-number' (TAG is a
100string) and `date' (TAG is a date as returned by `encode-time'). The
101return value of the function or macro will be displayed as a string.
102
103Here's an example that will display the formatted date for sticky
104dates and the word \"Sticky\" for sticky tag names and revisions.
105
106 (lambda (tag type)
107 (cond ((eq type 'date) (format-time-string
108 vc-cvs-sticky-date-format-string tag))
109 ((eq type 'revision-number) \"Sticky\")
110 ((eq type 'symbolic-name) \"Sticky\")))
111
112Here's an example that will abbreviate to the first character only,
113any text before the first occurence of `-' for sticky symbolic tags.
114If the sticky tag is a revision number, the word \"Sticky\" is
115displayed. Date and time is displayed for sticky dates.
116
117 (lambda (tag type)
118 (cond ((eq type 'date) (format-time-string \"%Y%m%d %H:%M\" tag))
119 ((eq type 'revision-number) \"Sticky\")
120 ((eq type 'symbolic-name)
121 (condition-case nil
122 (progn
123 (string-match \"\\\\([^-]*\\\\)\\\\(.*\\\\)\" tag)
124 (concat (substring (match-string 1 tag) 0 1) \":\"
125 (substring (match-string 2 tag) 1 nil)))
126 (error tag))))) ; Fall-back to given tag name.
127
128See also variable `vc-cvs-sticky-date-format-string'."
129 :type '(choice boolean function)
130 :version "21.3"
131 :group 'vc)
9b0630e5 132
8f98485f
AS
133;;;
134;;; Internal variables
135;;;
136
137(defvar vc-cvs-local-month-numbers
138 '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4)
139 ("May" . 5) ("Jun" . 6) ("Jul" . 7) ("Aug" . 8)
140 ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))
141 "Local association list of month numbers.")
142
9b0630e5 143
8f98485f 144;;;
9b0630e5 145;;; State-querying functions
8f98485f
AS
146;;;
147
c1b25099
GM
148;;;###autoload (defun vc-cvs-registered (f)
149;;;###autoload (when (file-readable-p (expand-file-name
150;;;###autoload "CVS/Entries" (file-name-directory f)))
151;;;###autoload (require 'vc-cvs)
152;;;###autoload (vc-cvs-registered f)))
153
154(defun vc-cvs-registered (file)
155 "Check if FILE is CVS registered."
156 (let ((dirname (or (file-name-directory file) ""))
157 (basename (file-name-nondirectory file))
158 ;; make sure that the file name is searched case-sensitively
159 (case-fold-search nil))
160 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
161 (with-temp-buffer
162 (vc-insert-file (expand-file-name "CVS/Entries" dirname))
163 (goto-char (point-min))
0d685c4f
DL
164 (cond
165 ((re-search-forward
c1b25099
GM
166 (concat "^/" (regexp-quote basename) "/") nil t)
167 (beginning-of-line)
168 (vc-cvs-parse-entry file)
169 t)
170 (t nil)))
171 nil)))
172
c1b25099
GM
173(defun vc-cvs-state (file)
174 "CVS-specific version of `vc-state'."
175 (if (vc-cvs-stay-local-p file)
176 (let ((state (vc-file-getprop file 'vc-state)))
177 ;; If we should stay local, use the heuristic but only if
178 ;; we don't have a more precise state already available.
179 (if (memq state '(up-to-date edited))
180 (vc-cvs-state-heuristic file)
181 state))
182 (with-temp-buffer
183 (cd (file-name-directory file))
184 (vc-do-command t 0 "cvs" file "status")
185 (vc-cvs-parse-status t))))
186
187(defun vc-cvs-state-heuristic (file)
188 "CVS-specific state heuristic."
189 ;; If the file has not changed since checkout, consider it `up-to-date'.
190 ;; Otherwise consider it `edited'.
191 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
192 (lastmod (nth 5 (file-attributes file))))
193 (if (equal checkout-time lastmod)
194 'up-to-date
195 'edited)))
196
8f98485f
AS
197(defun vc-cvs-dir-state (dir)
198 "Find the CVS state of all files in DIR."
199 (if (vc-cvs-stay-local-p dir)
200 (vc-cvs-dir-state-heuristic dir)
201 (let ((default-directory dir))
202 ;; Don't specify DIR in this command, the default-directory is
203 ;; enough. Otherwise it might fail with remote repositories.
204 (with-temp-buffer
205 (vc-do-command t 0 "cvs" nil "status" "-l")
206 (goto-char (point-min))
207 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
208 (narrow-to-region (match-beginning 0) (match-end 0))
209 (vc-cvs-parse-status)
210 (goto-char (point-max))
211 (widen))))))
212
213(defun vc-cvs-workfile-version (file)
214 "CVS-specific version of `vc-workfile-version'."
215 ;; There is no need to consult RCS headers under CVS, because we
216 ;; get the workfile version for free when we recognize that a file
217 ;; is registered in CVS.
218 (vc-cvs-registered file)
219 (vc-file-getprop file 'vc-workfile-version))
220
8f98485f
AS
221(defun vc-cvs-checkout-model (file)
222 "CVS-specific version of `vc-checkout-model'."
223 (if (or (getenv "CVSREAD")
224 ;; If the file is not writable (despite CVSREAD being
225 ;; undefined), this is probably because the file is being
226 ;; "watched" by other developers.
227 ;; (If vc-mistrust-permissions was t, we actually shouldn't
228 ;; trust this, but there is no other way to learn this from CVS
229 ;; at the moment (version 1.9).)
230 (string-match "r-..-..-." (nth 8 (file-attributes file))))
231 'announce
232 'implicit))
233
099bd78a
SM
234(defun vc-cvs-mode-line-string (file)
235 "Return string for placement into the modeline for FILE.
51af12fc
AS
236Compared to the default implementation, this function does two things:
237Handle the special case of a CVS file that is added but not yet
238committed and support display of sticky tags."
239 (let* ((state (vc-state file))
240 (rev (vc-workfile-version file))
241 (sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
242 (sticky-tag-printable (and sticky-tag
243 (not (string= sticky-tag ""))
244 (concat "[" sticky-tag "]"))))
099bd78a 245 (cond ((string= rev "0")
9b0630e5 246 ;; A file that is added but not yet committed.
099bd78a
SM
247 "CVS @@")
248 ((or (eq state 'up-to-date)
249 (eq state 'needs-patch))
51af12fc 250 (concat "CVS-" rev sticky-tag-printable))
099bd78a 251 ((stringp state)
51af12fc 252 (concat "CVS:" state ":" rev sticky-tag-printable))
099bd78a
SM
253 (t
254 ;; Not just for the 'edited state, but also a fallback
255 ;; for all other states. Think about different symbols
256 ;; for 'needs-patch and 'needs-merge.
51af12fc 257 (concat "CVS:" rev sticky-tag-printable)))))
099bd78a 258
8f98485f
AS
259(defun vc-cvs-dired-state-info (file)
260 "CVS-specific version of `vc-dired-state-info'."
261 (let* ((cvs-state (vc-state file))
c6a84d17
SM
262 (state (cond ((eq cvs-state 'edited) "modified")
263 ((eq cvs-state 'needs-patch) "patch")
264 ((eq cvs-state 'needs-merge) "merge")
8f98485f 265 ;; FIXME: those two states cannot occur right now
c6a84d17
SM
266 ((eq cvs-state 'unlocked-changes) "conflict")
267 ((eq cvs-state 'locally-added) "added")
8f98485f
AS
268 )))
269 (if state (concat "(" state ")"))))
c1b25099 270
9b0630e5 271
8f98485f
AS
272;;;
273;;; State-changing functions
274;;;
c1b25099 275
8f98485f
AS
276(defun vc-cvs-register (file &optional rev comment)
277 "Register FILE into the CVS version-control system.
278COMMENT can be used to provide an initial description of FILE.
c1b25099 279
8f98485f
AS
280`vc-register-switches' and `vc-cvs-register-switches' are passed to
281the CVS command (in that order)."
282 (let ((switches (list
283 (if (stringp vc-register-switches)
284 (list vc-register-switches)
285 vc-register-switches)
286 (if (stringp vc-cvs-register-switches)
287 (list vc-cvs-register-switches)
288 vc-cvs-register-switches))))
9b0630e5 289
8f98485f
AS
290 (apply 'vc-do-command nil 0 "cvs" file
291 "add"
292 (and comment (string-match "[^\t\n ]" comment)
293 (concat "-m" comment))
294 switches)))
c1b25099 295
8f98485f
AS
296(defun vc-cvs-responsible-p (file)
297 "Return non-nil if CVS thinks it is responsible for FILE."
298 (file-directory-p (expand-file-name "CVS"
299 (if (file-directory-p file)
300 file
301 (file-name-directory file)))))
c1b25099 302
8f98485f
AS
303(defun vc-cvs-could-register (file)
304 "Return non-nil if FILE could be registered in CVS.
305This is only possible if CVS is responsible for FILE's directory."
306 (vc-cvs-responsible-p file))
c1b25099
GM
307
308(defun vc-cvs-checkin (file rev comment)
309 "CVS-specific version of `vc-backend-checkin'."
310 (let ((switches (if (stringp vc-checkin-switches)
311 (list vc-checkin-switches)
312 vc-checkin-switches))
313 status)
51af12fc
AS
314 (if (not rev)
315 (setq status (apply 'vc-do-command nil 1 "cvs" file
316 "ci" (if rev (concat "-r" rev))
317 (concat "-m" comment)
318 switches))
319 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
320 (error "%s is not a valid symbolic tag name")
321 ;; If the input revison is a valid symbolic tag name, we create it
322 ;; as a branch, commit and switch to it.
323 (apply 'vc-do-command nil 0 "cvs" file "tag" "-b" (list rev))
324 (apply 'vc-do-command nil 0 "cvs" file "update" "-r" (list rev))
325 (setq status (apply 'vc-do-command nil 1 "cvs" file
326 "ci"
327 (concat "-m" comment)
328 switches))
329 (vc-file-setprop file 'vc-cvs-sticky-tag rev)))
c1b25099
GM
330 (set-buffer "*vc*")
331 (goto-char (point-min))
bee48f25
AS
332 (when (not (zerop status))
333 ;; Check checkin problem.
334 (cond
335 ((re-search-forward "Up-to-date check failed" nil t)
336 (vc-file-setprop file 'vc-state 'needs-merge)
337 (error (substitute-command-keys
338 (concat "Up-to-date check failed: "
339 "type \\[vc-next-action] to merge in changes"))))
340 (t
341 (pop-to-buffer (current-buffer))
342 (goto-char (point-min))
343 (shrink-window-if-larger-than-buffer)
344 (error "Check-in failed"))))
c1b25099
GM
345 ;; Update file properties
346 (vc-file-setprop
347 file 'vc-workfile-version
348 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
349 ;; Forget the checkout model of the file, because we might have
350 ;; guessed wrong when we found the file. After commit, we can
351 ;; tell it from the permissions of the file (see
352 ;; vc-cvs-checkout-model).
353 (vc-file-setprop file 'vc-checkout-model nil)
51af12fc
AS
354
355 ;; if this was an explicit check-in (does not include creation of
356 ;; a branch), remove the sticky tag.
357 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
358 (vc-do-command nil 0 "cvs" file "update" "-A"))))
c1b25099 359
34abd98e 360(defun vc-cvs-checkout (file &optional editable rev workfile)
c1b25099 361 "Retrieve a revision of FILE into a WORKFILE.
34abd98e 362EDITABLE non-nil means that the file should be writable.
c1b25099
GM
363REV is the revision to check out into WORKFILE."
364 (let ((filename (or workfile file))
365 (file-buffer (get-file-buffer file))
366 switches)
367 (message "Checking out %s..." filename)
368 (save-excursion
369 ;; Change buffers to get local value of vc-checkout-switches.
370 (if file-buffer (set-buffer file-buffer))
371 (setq switches (if (stringp vc-checkout-switches)
372 (list vc-checkout-switches)
373 vc-checkout-switches))
374 ;; Save this buffer's default-directory
375 ;; and use save-excursion to make sure it is restored
376 ;; in the same buffer it was saved in.
377 (let ((default-directory default-directory))
378 (save-excursion
379 ;; Adjust the default-directory so that the check-out creates
380 ;; the file in the right place.
381 (setq default-directory (file-name-directory filename))
382 (if workfile
9b0630e5 383 (let ((failed t)
81d995bb 384 (backup-name (if (string= file workfile)
a8b6979f
AS
385 (car (find-backup-file-name filename)))))
386 (when backup-name
9b0630e5 387 (copy-file filename backup-name
a8b6979f
AS
388 'ok-if-already-exists 'keep-date)
389 (unless (file-writable-p filename)
390 (set-file-modes filename
391 (logior (file-modes filename) 128))))
c1b25099
GM
392 (unwind-protect
393 (progn
394 (let ((coding-system-for-read 'no-conversion)
395 (coding-system-for-write 'no-conversion))
396 (with-temp-file filename
397 (apply 'vc-do-command
398 (current-buffer) 0 "cvs" file
399 "-Q" ; suppress diagnostic output
c6a84d17 400 "update"
c1b25099
GM
401 (and rev (not (string= rev ""))
402 (concat "-r" rev))
403 "-p"
404 switches)))
405 (setq failed nil))
9b0630e5 406 (if failed
81d995bb 407 (if backup-name
9b0630e5 408 (rename-file backup-name filename
81d995bb
AS
409 'ok-if-already-exists)
410 (if (file-exists-p filename)
411 (delete-file filename)))
a8b6979f
AS
412 (and backup-name
413 (not vc-make-backup-files)
414 (delete-file backup-name)))))
c1b25099
GM
415 (if (and (file-exists-p file) (not rev))
416 ;; If no revision was specified, just make the file writable
417 ;; if necessary (using `cvs-edit' if requested).
c6a84d17 418 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
c1b25099
GM
419 (if vc-cvs-use-edit
420 (vc-do-command nil 0 "cvs" file "edit")
421 (set-file-modes file (logior (file-modes file) 128))
422 (if file-buffer (toggle-read-only -1))))
423 ;; Check out a particular version (or recreate the file).
424 (vc-file-setprop file 'vc-workfile-version nil)
425 (apply 'vc-do-command nil 0 "cvs" file
c6a84d17
SM
426 (and editable
427 (or (not (file-exists-p file))
428 (not (eq (vc-cvs-checkout-model file)
429 'implicit)))
430 "-w")
431 "update"
432 ;; default for verbose checkout: clear the sticky tag so
433 ;; that the actual update will get the head of the trunk
0d685c4f
DL
434 (if (or (not rev) (string= rev ""))
435 "-A"
c1b25099 436 (concat "-r" rev))
099bd78a 437 switches))))
c1b25099
GM
438 (vc-mode-line file)
439 (message "Checking out %s...done" filename)))))
440
99739bbf 441(defun vc-cvs-revert (file &optional contents-done)
8f98485f 442 "Revert FILE to the version it was based on."
99739bbf
AS
443 (unless contents-done
444 ;; Check out via standard output (caused by the final argument
445 ;; FILE below), so that no sticky tag is set.
446 (vc-cvs-checkout file nil (vc-workfile-version file) file))
447 (unless (eq (vc-checkout-model file) 'implicit)
448 (if vc-cvs-use-edit
449 (vc-do-command nil 0 "cvs" file "unedit")
450 ;; Make the file read-only by switching off all w-bits
451 (set-file-modes file (logand (file-modes file) 3950)))))
8f98485f
AS
452
453(defun vc-cvs-merge (file first-version &optional second-version)
454 "Merge changes into current working copy of FILE.
455The changes are between FIRST-VERSION and SECOND-VERSION."
456 (vc-do-command nil 0 "cvs" file
457 "update" "-kk"
458 (concat "-j" first-version)
459 (concat "-j" second-version))
460 (vc-file-setprop file 'vc-state 'edited)
461 (save-excursion
462 (set-buffer (get-buffer "*vc*"))
463 (goto-char (point-min))
464 (if (re-search-forward "conflicts during merge" nil t)
465 1 ; signal error
466 0))) ; signal success
467
468(defun vc-cvs-merge-news (file)
469 "Merge in any new changes made to FILE."
470 (message "Merging changes into %s..." file)
471 (save-excursion
472 ;; (vc-file-setprop file 'vc-workfile-version nil)
473 (vc-file-setprop file 'vc-checkout-time 0)
474 (vc-do-command nil 0 "cvs" file "update")
475 ;; Analyze the merge result reported by CVS, and set
476 ;; file properties accordingly.
477 (set-buffer (get-buffer "*vc*"))
478 (goto-char (point-min))
479 ;; get new workfile version
480 (if (re-search-forward (concat "^Merging differences between "
481 "[01234567890.]* and "
482 "\\([01234567890.]*\\) into")
483 nil t)
484 (vc-file-setprop file 'vc-workfile-version (match-string 1))
485 (vc-file-setprop file 'vc-workfile-version nil))
486 ;; get file status
487 (prog1
488 (if (eq (buffer-size) 0)
489 0 ;; there were no news; indicate success
490 (if (re-search-forward
491 (concat "^\\([CMUP] \\)?"
492 (regexp-quote (file-name-nondirectory file))
493 "\\( already contains the differences between \\)?")
494 nil t)
495 (cond
496 ;; Merge successful, we are in sync with repository now
497 ((or (match-string 2)
498 (string= (match-string 1) "U ")
499 (string= (match-string 1) "P "))
500 (vc-file-setprop file 'vc-state 'up-to-date)
501 (vc-file-setprop file 'vc-checkout-time
502 (nth 5 (file-attributes file)))
503 0);; indicate success to the caller
504 ;; Merge successful, but our own changes are still in the file
505 ((string= (match-string 1) "M ")
506 (vc-file-setprop file 'vc-state 'edited)
507 0);; indicate success to the caller
508 ;; Conflicts detected!
509 (t
510 (vc-file-setprop file 'vc-state 'edited)
511 1);; signal the error to the caller
512 )
513 (pop-to-buffer "*vc*")
514 (error "Couldn't analyze cvs update result")))
515 (message "Merging changes into %s...done" file))))
516
9b0630e5 517
8f98485f
AS
518;;;
519;;; History functions
520;;;
521
522(defun vc-cvs-print-log (file)
523 "Get change log associated with FILE."
8f9ab403
EZ
524 (vc-do-command
525 nil
526 (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
527 "cvs" file "log"))
8f98485f
AS
528
529(defun vc-cvs-show-log-entry (version)
530 (when (re-search-forward
531 ;; also match some context, for safety
532 (concat "----\nrevision " version
533 "\\(\tlocked by:.*\n\\|\n\\)date: ") nil t)
534 ;; set the display window so that
535 ;; the whole log entry is displayed
536 (let (start end lines)
537 (beginning-of-line) (forward-line -1) (setq start (point))
538 (if (not (re-search-forward "^----*\nrevision" nil t))
539 (setq end (point-max))
540 (beginning-of-line) (forward-line -1) (setq end (point)))
541 (setq lines (count-lines start end))
542 (cond
543 ;; if the global information and this log entry fit
544 ;; into the window, display from the beginning
545 ((< (count-lines (point-min) end) (window-height))
546 (goto-char (point-min))
547 (recenter 0)
548 (goto-char start))
549 ;; if the whole entry fits into the window,
550 ;; display it centered
551 ((< (1+ lines) (window-height))
552 (goto-char start)
553 (recenter (1- (- (/ (window-height) 2) (/ lines 2)))))
554 ;; otherwise (the entry is too large for the window),
555 ;; display from the start
556 (t
557 (goto-char start)
558 (recenter 0))))))
559
560(defun vc-cvs-diff (file &optional oldvers newvers)
561 "Get a difference report using CVS between two versions of FILE."
df8e0a91 562 (let (options status (diff-switches-list (vc-diff-switches-list 'CVS)))
8f98485f
AS
563 (if (string= (vc-workfile-version file) "0")
564 ;; This file is added but not yet committed; there is no master file.
565 (if (or oldvers newvers)
566 (error "No revisions of %s exist" file)
567 ;; we regard this as "changed".
568 ;; diff it against /dev/null.
6e024fc8 569 (apply 'vc-do-command "*vc-diff*"
8f98485f
AS
570 1 "diff" file
571 (append diff-switches-list '("/dev/null"))))
572 (setq status
6e024fc8 573 (apply 'vc-do-command "*vc-diff*"
8f9ab403
EZ
574 (if (and (vc-cvs-stay-local-p file)
575 (fboundp 'start-process))
576 'async
577 1)
8f98485f
AS
578 "cvs" file "diff"
579 (and oldvers (concat "-r" oldvers))
580 (and newvers (concat "-r" newvers))
581 diff-switches-list))
9b0630e5
SS
582 (if (vc-cvs-stay-local-p file)
583 1 ;; async diff, pessimistic assumption
8f98485f
AS
584 status))))
585
5e0fdc5a
AS
586(defun vc-cvs-diff-tree (dir &optional rev1 rev2)
587 "Diff all files at and below DIR."
588 (with-current-buffer "*vc-diff*"
589 (setq default-directory dir)
590 (if (vc-cvs-stay-local-p dir)
591 ;; local diff: do it filewise, and only for files that are modified
592 (vc-file-tree-walk
593 dir
594 (lambda (f)
595 (vc-exec-after
596 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
597 ;; possible optimization: fetch the state of all files
598 ;; in the tree via vc-cvs-dir-state-heuristic
599 (unless (vc-up-to-date-p ',f)
600 (message "Looking at %s" ',f)
601 (vc-diff-internal ',f ',rel1 ',rel2))))))
602 ;; cvs diff: use a single call for the entire tree
603 (let ((coding-system-for-read
604 (or coding-system-for-read 'undecided)))
605 (apply 'vc-do-command "*vc-diff*" 1 "cvs" nil "diff"
606 (and rel1 (concat "-r" rel1))
607 (and rel2 (concat "-r" rel2))
df8e0a91 608 (vc-diff-switches-list 'CVS))))))
5e0fdc5a 609
8f98485f
AS
610(defun vc-cvs-annotate-command (file buffer &optional version)
611 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
612Optional arg VERSION is a version to annotate from."
613 (vc-do-command buffer 0 "cvs" file "annotate" (if version
614 (concat "-r" version))))
c1b25099 615
8ba2df32
AS
616(defun vc-cvs-annotate-current-time ()
617 "Return the current time, based at midnight of the current day, and
618encoded as fractional days."
619 (vc-annotate-convert-time
620 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
621
622(defun vc-cvs-annotate-time ()
623 "Return the time of the next annotation (as fraction of days)
f0529b5b 624systime, or nil if there is none."
8ba2df32
AS
625 (let ((time-stamp
626 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): "))
627 (if (looking-at time-stamp)
c1b25099
GM
628 (progn
629 (let* ((day (string-to-number (match-string 1)))
8ba2df32
AS
630 (month (cdr (assoc (match-string 2)
631 vc-cvs-local-month-numbers)))
c1b25099
GM
632 (year-tmp (string-to-number (match-string 3)))
633 ;; Years 0..68 are 2000..2068.
634 ;; Years 69..99 are 1969..1999.
635 (year (+ (cond ((> 69 year-tmp) 2000)
636 ((> 100 year-tmp) 1900)
637 (t 0))
638 year-tmp)))
639 (goto-char (match-end 0)) ; Position at end makes for nicer overlay result
8ba2df32 640 (vc-annotate-convert-time (encode-time 0 0 0 day month year))))
c1b25099
GM
641 ;; If we did not look directly at an annotation, there might be
642 ;; some further down. This is the case if we are positioned at
643 ;; the very top of the buffer, for instance.
8ba2df32 644 (if (re-search-forward time-stamp nil t)
c1b25099
GM
645 (progn
646 (beginning-of-line nil)
8ba2df32 647 (vc-cvs-annotate-time))))))
9b0630e5 648
8f98485f
AS
649;;;
650;;; Snapshot system
651;;;
652
653(defun vc-cvs-create-snapshot (dir name branchp)
654 "Assign to DIR's current version a given NAME.
655If BRANCHP is non-nil, the name is created as a branch (and the current
656workspace is immediately moved to that new branch)."
657 (vc-do-command nil 0 "cvs" dir "tag" "-c" (if branchp "-b") name)
658 (when branchp (vc-do-command nil 0 "cvs" dir "update" "-r" name)))
659
660(defun vc-cvs-retrieve-snapshot (dir name update)
661 "Retrieve a snapshot at and below DIR.
662NAME is the name of the snapshot; if it is empty, do a `cvs update'.
663If UPDATE is non-nil, then update (resynch) any affected buffers."
664 (with-current-buffer (get-buffer-create "*vc*")
51af12fc
AS
665 (let ((default-directory dir)
666 (sticky-tag))
8f98485f
AS
667 (erase-buffer)
668 (if (or (not name) (string= name ""))
669 (vc-do-command t 0 "cvs" nil "update")
51af12fc
AS
670 (vc-do-command t 0 "cvs" nil "update" "-r" name)
671 (setq sticky-tag name))
8f98485f
AS
672 (when update
673 (goto-char (point-min))
674 (while (not (eobp))
675 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
676 (let* ((file (expand-file-name (match-string 2) dir))
677 (state (match-string 1))
678 (buffer (find-buffer-visiting file)))
679 (when buffer
680 (cond
681 ((or (string= state "U")
682 (string= state "P"))
683 (vc-file-setprop file 'vc-state 'up-to-date)
684 (vc-file-setprop file 'vc-workfile-version nil)
685 (vc-file-setprop file 'vc-checkout-time
686 (nth 5 (file-attributes file))))
687 ((or (string= state "M")
688 (string= state "C"))
689 (vc-file-setprop file 'vc-state 'edited)
690 (vc-file-setprop file 'vc-workfile-version nil)
691 (vc-file-setprop file 'vc-checkout-time 0)))
51af12fc 692 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
8f98485f
AS
693 (vc-resynch-buffer file t t))))
694 (forward-line 1))))))
695
9b0630e5 696
8f98485f
AS
697;;;
698;;; Miscellaneous
699;;;
700
701(defun vc-cvs-make-version-backups-p (file)
702 "Return non-nil if version backups should be made for FILE."
703 (vc-cvs-stay-local-p file))
704
705(defun vc-cvs-check-headers ()
706 "Check if the current file has any headers in it."
707 (save-excursion
708 (goto-char (point-min))
709 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
710\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
711
9b0630e5 712
8f98485f
AS
713;;;
714;;; Internal functions
715;;;
716
717(defun vc-cvs-stay-local-p (file)
718 "Return non-nil if VC should stay local when handling FILE."
719 (if vc-cvs-stay-local
720 (let* ((dirname (if (file-directory-p file)
721 (directory-file-name file)
722 (file-name-directory file)))
723 (prop
724 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
725 (let ((rootname (expand-file-name "CVS/Root" dirname)))
726 (vc-file-setprop
727 dirname 'vc-cvs-stay-local-p
728 (when (file-readable-p rootname)
729 (with-temp-buffer
730 (vc-insert-file rootname)
731 (goto-char (point-min))
732 (if (looking-at "\\([^:]*\\):")
733 (if (not (stringp vc-cvs-stay-local))
734 'yes
735 (let ((hostname (match-string 1)))
736 (if (string-match vc-cvs-stay-local hostname)
737 'yes
738 'no)))
739 'no))))))))
740 (if (eq prop 'yes) t nil))))
741
742(defun vc-cvs-parse-status (&optional full)
743 "Parse output of \"cvs status\" command in the current buffer.
744Set file properties accordingly. Unless FULL is t, parse only
745essential information."
746 (let (file status)
747 (goto-char (point-min))
748 (if (re-search-forward "^File: " nil t)
749 (cond
750 ((looking-at "no file") nil)
751 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
752 (setq file (expand-file-name (match-string 1)))
753 (vc-file-setprop file 'vc-backend 'CVS)
754 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
755 (setq status "Unknown")
756 (setq status (match-string 1)))
757 (if (and full
758 (re-search-forward
44f663a4 759 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
8f98485f
AS
760\[\t ]+\\([0-9.]+\\)"
761 nil t))
762 (vc-file-setprop file 'vc-latest-version (match-string 2)))
44f663a4
AS
763 (vc-file-setprop
764 file 'vc-state
765 (cond
766 ((string-match "Up-to-date" status)
767 (vc-file-setprop file 'vc-checkout-time
768 (nth 5 (file-attributes file)))
769 'up-to-date)
770 ((string-match "Locally Modified" status) 'edited)
771 ((string-match "Needs Merge" status) 'needs-merge)
772 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
773 (t 'edited))))))))
8f98485f
AS
774
775(defun vc-cvs-dir-state-heuristic (dir)
776 "Find the CVS state of all files in DIR, using only local information."
777 (with-temp-buffer
778 (vc-insert-file (expand-file-name "CVS/Entries" dir))
779 (goto-char (point-min))
780 (while (not (eobp))
781 (when (looking-at "/\\([^/]*\\)/")
782 (let ((file (expand-file-name (match-string 1) dir)))
783 (unless (vc-file-getprop file 'vc-state)
784 (vc-cvs-parse-entry file t))))
785 (forward-line 1))))
786
51af12fc
AS
787
788(defun vc-cvs-valid-symbolic-tag-name-p (tag)
789 "Return non-nil if TAG is a valid symbolic tag name."
790 ;; According to the CVS manual, a valid symbolic tag must start with
791 ;; an uppercase or lowercase letter and can contain uppercase and
792 ;; lowercase letters, digits, `-', and `_'.
793 (and (string-match "^[a-zA-Z]" tag)
794 (not (string-match "[^a-z0-9A-Z-_]" tag))))
795
796
797(defun vc-cvs-parse-sticky-tag (match-type match-tag)
798 "Parse and return the sticky tag as a string.
799`match-data' is protected."
800 (let ((data (match-data))
801 (tag)
802 (type (cond ((string= match-type "D") 'date)
803 ((string= match-type "T")
804 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
805 'symbolic-name
806 'revision-number))
807 (t nil))))
808 (unwind-protect
809 (progn
810 (cond
811 ;; Sticky Date tag. Convert to to a proper date value (`encode-time')
812 ((eq type 'date)
813 (string-match
814 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
815 match-tag)
816 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
817 (month (string-to-number (match-string 2 match-tag)))
818 (day (string-to-number (match-string 3 match-tag)))
819 (hour (string-to-number (match-string 4 match-tag)))
820 (min (string-to-number (match-string 5 match-tag)))
821 (sec (string-to-number (match-string 6 match-tag)))
822 ;; Years 0..68 are 2000..2068.
823 ;; Years 69..99 are 1969..1999.
824 (year (+ (cond ((> 69 year-tmp) 2000)
825 ((> 100 year-tmp) 1900)
826 (t 0))
827 year-tmp)))
828 (setq tag (encode-time sec min hour day month year))))
829 ;; Sticky Tag name or revision number
830 ((eq type 'symbolic-name) (setq tag match-tag))
831 ((eq type 'revision-number) (setq tag match-tag))
832 ;; Default is no sticky tag at all
833 (t nil))
834 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
835 ((eq vc-cvs-sticky-tag-display t)
836 (cond ((eq type 'date) (format-time-string
837 vc-cvs-sticky-date-format-string
838 tag))
839 ((eq type 'symbolic-name) tag)
840 ((eq type 'revision-number) tag)
841 (t nil)))
842 ((functionp vc-cvs-sticky-tag-display)
843 (funcall vc-cvs-sticky-tag-display tag type))
844 (t nil)))
845
846 (set-match-data data))))
847
8f98485f
AS
848(defun vc-cvs-parse-entry (file &optional set-state)
849 "Parse a line from CVS/Entries.
850Compare modification time to that of the FILE, set file properties
851accordingly. However, `vc-state' is set only if optional arg SET-STATE
852is non-nil."
853 (cond
854 ;; entry for a "locally added" file (not yet committed)
855 ((looking-at "/[^/]+/0/")
856 (vc-file-setprop file 'vc-checkout-time 0)
857 (vc-file-setprop file 'vc-workfile-version "0")
858 (if set-state (vc-file-setprop file 'vc-state 'edited)))
859 ;; normal entry
860 ((looking-at
861 (concat "/[^/]+"
862 ;; revision
863 "/\\([^/]*\\)"
864 ;; timestamp
51af12fc
AS
865 "/\\([^/]*\\)"
866 ;; optional conflict field
867 "\\(+[^/]*\\)?/"
868 ;; options
869 "\\([^/]*\\)/"
870 ;; sticky tag
871 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
872 "\\(.*\\)")) ;Sticky tag
8f98485f 873 (vc-file-setprop file 'vc-workfile-version (match-string 1))
51af12fc
AS
874 (vc-file-setprop file 'vc-cvs-sticky-tag
875 (vc-cvs-parse-sticky-tag (match-string 5) (match-string 6)))
8f98485f 876 ;; compare checkout time and modification time
4a9b4f30
SM
877 (let ((mtime (nth 5 (file-attributes file)))
878 (system-time-locale "C"))
879 (cond ((equal (format-time-string "%c" mtime 'utc) (match-string 2))
8f98485f
AS
880 (vc-file-setprop file 'vc-checkout-time mtime)
881 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
882 (t
883 (vc-file-setprop file 'vc-checkout-time 0)
4a9b4f30
SM
884 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
885
c1b25099
GM
886(provide 'vc-cvs)
887
888;;; vc-cvs.el ends here