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