Add vc-ignore.
[bpt/emacs.git] / lisp / vc / vc-cvs.el
CommitLineData
0d42eb3e 1;;; vc-cvs.el --- non-resident support for CVS version-control -*- lexical-binding: t -*-
c1b25099 2
ab422c4d 3;; Copyright (C) 1995, 1998-2013 Free Software Foundation, Inc.
c1b25099
GM
4
5;; Author: FSF (see vc.el for full credits)
6;; Maintainer: Andre Spiegel <spiegel@gnu.org>
bd78fa1d 7;; Package: vc
c1b25099 8
c1b25099
GM
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
c1b25099 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
c1b25099
GM
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
c1b25099
GM
23
24;;; Commentary:
25
26;;; Code:
27
a464a6c7 28(eval-when-compile (require 'vc))
89e65817 29
98ad325c
SM
30;; Clear up the cache to force vc-call to check again and discover
31;; new functions when we reload this file.
32(put 'CVS 'vc-functions nil)
33
70e2f6c7
ER
34;;; Properties of the backend.
35
36(defun vc-cvs-revision-granularity () 'file)
37
38(defun vc-cvs-checkout-model (files)
39 "CVS-specific version of `vc-checkout-model'."
40 (if (getenv "CVSREAD")
41 'announce
42 (let* ((file (if (consp files) (car files) files))
43 (attrib (file-attributes file)))
44 (or (vc-file-getprop file 'vc-checkout-model)
45 (vc-file-setprop
46 file 'vc-checkout-model
47 (if (and attrib ;; don't check further if FILE doesn't exist
48 ;; If the file is not writable (despite CVSREAD being
49 ;; undefined), this is probably because the file is being
50 ;; "watched" by other developers.
51 ;; (If vc-mistrust-permissions was t, we actually shouldn't
52 ;; trust this, but there is no other way to learn this from
53 ;; CVS at the moment (version 1.9).)
54 (string-match "r-..-..-." (nth 8 attrib)))
55 'announce
56 'implicit))))))
57
9b0630e5 58;;;
8f98485f
AS
59;;; Customization options
60;;;
61
67b0de11
CY
62(defgroup vc-cvs nil
63 "VC CVS backend."
64 :version "24.1"
65 :group 'vc)
66
bbce0417 67(defcustom vc-cvs-global-switches nil
9201cc28 68 "Global switches to pass to any CVS command."
bbce0417
AS
69 :type '(choice (const :tag "None" nil)
70 (string :tag "Argument String")
71 (repeat :tag "Argument List"
72 :value ("")
73 string))
bf247b6e 74 :version "22.1"
67b0de11 75 :group 'vc-cvs)
bbce0417 76
c1b25099 77(defcustom vc-cvs-register-switches nil
e5c741d9 78 "Switches for registering a file into CVS.
c1b25099 79A string or list of strings passed to the checkin program by
e5c741d9
GM
80\\[vc-register]. If nil, use the value of `vc-register-switches'.
81If t, use no switches."
82 :type '(choice (const :tag "Unspecified" nil)
83 (const :tag "None" t)
c1b25099 84 (string :tag "Argument String")
e5c741d9 85 (repeat :tag "Argument List" :value ("") string))
0d685c4f 86 :version "21.1"
67b0de11 87 :group 'vc-cvs)
c1b25099 88
633147f5 89(defcustom vc-cvs-diff-switches nil
e1f650f4
GM
90 "String or list of strings specifying switches for CVS diff under VC.
91If nil, use the value of `vc-diff-switches'. If t, use no switches."
92 :type '(choice (const :tag "Unspecified" nil)
93 (const :tag "None" t)
94 (string :tag "Argument String")
95 (repeat :tag "Argument List" :value ("") string))
633147f5 96 :version "21.1"
67b0de11 97 :group 'vc-cvs)
633147f5 98
67141a37 99(defcustom vc-cvs-header '("\$Id\$")
9201cc28 100 "Header keywords to be inserted by `vc-insert-headers'."
67141a37 101 :version "24.1" ; no longer consult the obsolete vc-header-alist
ee3275df 102 :type '(repeat string)
67b0de11 103 :group 'vc-cvs)
c1b25099
GM
104
105(defcustom vc-cvs-use-edit t
9201cc28 106 "Non-nil means to use `cvs edit' to \"check out\" a file.
c1b25099
GM
107This is only meaningful if you don't use the implicit checkout model
108\(i.e. if you have $CVSREAD set)."
109 :type 'boolean
0d685c4f 110 :version "21.1"
67b0de11 111 :group 'vc-cvs)
c1b25099 112
5870cb76 113(defcustom vc-cvs-stay-local 'only-file
9201cc28 114 "Non-nil means use local operations when possible for remote repositories.
c6a84d17
SM
115This avoids slow queries over the network and instead uses heuristics
116and past information to determine the current status of a file.
f354c160 117
8d0b26f6 118If value is the symbol `only-file', `vc-dir' will connect to the
5870cb76
DN
119server, but heuristics will be used to determine the status for
120all other VC operations.
121
d3ed06c6
AS
122The value can also be a regular expression or list of regular
123expressions to match against the host name of a repository; then VC
f354c160 124only stays local for hosts that match it. Alternatively, the value
bc99a968
TTN
125can be a list of regular expressions where the first element is the
126symbol `except'; then VC always stays local except for hosts matched
f354c160 127by these regular expressions."
c1b25099 128 :type '(choice (const :tag "Always stay local" t)
efe0da9c 129 (const :tag "Only for file operations" only-file)
5870cb76 130 (const :tag "Don't stay local" nil)
e1f650f4
GM
131 (list :format "\nExamine hostname and %v"
132 :tag "Examine hostname ..."
133 (set :format "%v" :inline t
134 (const :format "%t" :tag "don't" except))
135 (regexp :format " stay local,\n%t: %v"
136 :tag "if it matches")
d3ed06c6 137 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
264175a8 138 :version "23.1"
67b0de11 139 :group 'vc-cvs)
264175a8 140
51af12fc 141(defcustom vc-cvs-sticky-date-format-string "%c"
9201cc28 142 "Format string for mode-line display of sticky date.
51af12fc
AS
143Format is according to `format-time-string'. Only used if
144`vc-cvs-sticky-tag-display' is t."
145 :type '(string)
bf247b6e 146 :version "22.1"
67b0de11 147 :group 'vc-cvs)
51af12fc
AS
148
149(defcustom vc-cvs-sticky-tag-display t
9201cc28 150 "Specify the mode-line display of sticky tags.
51af12fc
AS
151Value t means default display, nil means no display at all. If the
152value is a function or macro, it is called with the sticky tag and
153its' type as parameters, in that order. TYPE can have three different
154values: `symbolic-name' (TAG is a string), `revision-number' (TAG is a
155string) and `date' (TAG is a date as returned by `encode-time'). The
156return value of the function or macro will be displayed as a string.
157
158Here's an example that will display the formatted date for sticky
159dates and the word \"Sticky\" for sticky tag names and revisions.
160
161 (lambda (tag type)
968b980c 162 (cond ((eq type 'date) (format-time-string
51af12fc
AS
163 vc-cvs-sticky-date-format-string tag))
164 ((eq type 'revision-number) \"Sticky\")
165 ((eq type 'symbolic-name) \"Sticky\")))
166
167Here's an example that will abbreviate to the first character only,
aa4af071 168any text before the first occurrence of `-' for sticky symbolic tags.
51af12fc
AS
169If the sticky tag is a revision number, the word \"Sticky\" is
170displayed. Date and time is displayed for sticky dates.
171
172 (lambda (tag type)
173 (cond ((eq type 'date) (format-time-string \"%Y%m%d %H:%M\" tag))
174 ((eq type 'revision-number) \"Sticky\")
968b980c 175 ((eq type 'symbolic-name)
51af12fc
AS
176 (condition-case nil
177 (progn
178 (string-match \"\\\\([^-]*\\\\)\\\\(.*\\\\)\" tag)
968b980c 179 (concat (substring (match-string 1 tag) 0 1) \":\"
51af12fc
AS
180 (substring (match-string 2 tag) 1 nil)))
181 (error tag))))) ; Fall-back to given tag name.
182
183See also variable `vc-cvs-sticky-date-format-string'."
184 :type '(choice boolean function)
bf247b6e 185 :version "22.1"
67b0de11 186 :group 'vc-cvs)
9b0630e5 187
8f98485f
AS
188;;;
189;;; Internal variables
190;;;
191
9b0630e5 192
8f98485f 193;;;
9b0630e5 194;;; State-querying functions
8f98485f
AS
195;;;
196
3adbe224
GM
197;;;###autoload(defun vc-cvs-registered (f)
198;;;###autoload "Return non-nil if file F is registered with CVS."
c1b25099
GM
199;;;###autoload (when (file-readable-p (expand-file-name
200;;;###autoload "CVS/Entries" (file-name-directory f)))
af314ba0 201;;;###autoload (load "vc-cvs" nil t)
c1b25099
GM
202;;;###autoload (vc-cvs-registered f)))
203
204(defun vc-cvs-registered (file)
205 "Check if FILE is CVS registered."
206 (let ((dirname (or (file-name-directory file) ""))
207 (basename (file-name-nondirectory file))
208 ;; make sure that the file name is searched case-sensitively
209 (case-fold-search nil))
210 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
1d1d90d6
SS
211 (or (string= basename "")
212 (with-temp-buffer
213 (vc-cvs-get-entries dirname)
214 (goto-char (point-min))
215 (cond ((re-search-forward
216 (concat "^/" (regexp-quote basename) "/[^/]") nil t)
217 (beginning-of-line)
218 (vc-cvs-parse-entry file)
219 t)
220 (t nil))))
c1b25099
GM
221 nil)))
222
c1b25099
GM
223(defun vc-cvs-state (file)
224 "CVS-specific version of `vc-state'."
77bf3f54 225 (if (vc-stay-local-p file 'CVS)
c1b25099
GM
226 (let ((state (vc-file-getprop file 'vc-state)))
227 ;; If we should stay local, use the heuristic but only if
228 ;; we don't have a more precise state already available.
5124dd38 229 (if (memq state '(up-to-date edited nil))
c1b25099
GM
230 (vc-cvs-state-heuristic file)
231 state))
232 (with-temp-buffer
233 (cd (file-name-directory file))
66f3089c
MA
234 (let (process-file-side-effects)
235 (vc-cvs-command t 0 file "status"))
c1b25099
GM
236 (vc-cvs-parse-status t))))
237
238(defun vc-cvs-state-heuristic (file)
239 "CVS-specific state heuristic."
240 ;; If the file has not changed since checkout, consider it `up-to-date'.
241 ;; Otherwise consider it `edited'.
242 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
243 (lastmod (nth 5 (file-attributes file))))
45b24b4d
SM
244 (cond
245 ((equal checkout-time lastmod) 'up-to-date)
246 ((string= (vc-working-revision file) "0") 'added)
47dd5958 247 ((null checkout-time) 'unregistered)
45b24b4d 248 (t 'edited))))
c1b25099 249
ac3f4c6f
ER
250(defun vc-cvs-working-revision (file)
251 "CVS-specific version of `vc-working-revision'."
8f98485f
AS
252 ;; There is no need to consult RCS headers under CVS, because we
253 ;; get the workfile version for free when we recognize that a file
254 ;; is registered in CVS.
255 (vc-cvs-registered file)
ac3f4c6f 256 (vc-file-getprop file 'vc-working-revision))
8f98485f 257
099bd78a 258(defun vc-cvs-mode-line-string (file)
37269466 259 "Return a string for `vc-mode-line' to put in the mode line for FILE.
51af12fc
AS
260Compared to the default implementation, this function does two things:
261Handle the special case of a CVS file that is added but not yet
262committed and support display of sticky tags."
3a12f9f8
DN
263 (let* ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
264 help-echo
45b24b4d
SM
265 (string
266 (let ((def-ml (vc-default-mode-line-string 'CVS file)))
bdb55eba 267 (setq help-echo
45b24b4d
SM
268 (get-text-property 0 'help-echo def-ml))
269 def-ml)))
bdb55eba 270 (propertize
3a12f9f8
DN
271 (if (zerop (length sticky-tag))
272 string
bdb55eba 273 (setq help-echo (format "%s on the '%s' branch"
3a12f9f8
DN
274 help-echo sticky-tag))
275 (concat string "[" sticky-tag "]"))
276 'help-echo help-echo)))
099bd78a 277
9b0630e5 278
8f98485f
AS
279;;;
280;;; State-changing functions
281;;;
c1b25099 282
712b9732
GM
283(autoload 'vc-switches "vc")
284
0d42eb3e 285(defun vc-cvs-register (files &optional _rev comment)
8cdd17b4
ER
286 "Register FILES into the CVS version-control system.
287COMMENT can be used to provide an initial description of FILES.
e5c741d9
GM
288Passes either `vc-cvs-register-switches' or `vc-register-switches'
289to the CVS command."
967bf297
GM
290 ;; Register the directories if needed.
291 (let (dirs)
292 (dolist (file files)
293 (and (not (vc-cvs-responsible-p file))
294 (vc-cvs-could-register file)
295 (push (directory-file-name (file-name-directory file)) dirs)))
296 (if dirs (vc-cvs-register dirs)))
297 (apply 'vc-cvs-command nil 0 files
298 "add"
299 (and comment (string-match "[^\t\n ]" comment)
300 (concat "-m" comment))
301 (vc-switches 'CVS 'register)))
c1b25099 302
8f98485f
AS
303(defun vc-cvs-responsible-p (file)
304 "Return non-nil if CVS thinks it is responsible for FILE."
305 (file-directory-p (expand-file-name "CVS"
306 (if (file-directory-p file)
307 file
308 (file-name-directory file)))))
c1b25099 309
e54faddb 310(defun vc-cvs-could-register (file)
8f98485f 311 "Return non-nil if FILE could be registered in CVS.
e54faddb
SM
312This is only possible if CVS is managing FILE's directory or one of
313its parents."
314 (let ((dir file))
315 (while (and (stringp dir)
316 (not (equal dir (setq dir (file-name-directory dir))))
317 dir)
fcb61bee 318 (setq dir (if (file-exists-p
e54faddb 319 (expand-file-name "CVS/Entries" dir))
fcb61bee
LK
320 t
321 (directory-file-name dir))))
e54faddb 322 (eq dir t)))
c1b25099 323
4624de78 324(defun vc-cvs-checkin (files rev comment)
c1b25099 325 "CVS-specific version of `vc-backend-checkin'."
ac3f4c6f 326 (unless (or (not rev) (vc-cvs-valid-revision-number-p rev))
f153cb52
SM
327 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
328 (error "%s is not a valid symbolic tag name" rev)
9858f6c3 329 ;; If the input revision is a valid symbolic tag name, we create it
f153cb52 330 ;; as a branch, commit and switch to it.
8cdd17b4
ER
331 (apply 'vc-cvs-command nil 0 files "tag" "-b" (list rev))
332 (apply 'vc-cvs-command nil 0 files "update" "-r" (list rev))
ea139d51
ER
333 (mapc (lambda (file) (vc-file-setprop file 'vc-cvs-sticky-tag rev))
334 files)))
335 (let ((status (apply 'vc-cvs-command nil 1 files
f153cb52
SM
336 "ci" (if rev (concat "-r" rev))
337 (concat "-m" comment)
338 (vc-switches 'CVS 'checkin))))
c1b25099
GM
339 (set-buffer "*vc*")
340 (goto-char (point-min))
bee48f25
AS
341 (when (not (zerop status))
342 ;; Check checkin problem.
343 (cond
344 ((re-search-forward "Up-to-date check failed" nil t)
7d8c4332
ER
345 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
346 files)
8c16bd8c 347 (error "%s" (substitute-command-keys
bee48f25
AS
348 (concat "Up-to-date check failed: "
349 "type \\[vc-next-action] to merge in changes"))))
350 (t
351 (pop-to-buffer (current-buffer))
352 (goto-char (point-min))
353 (shrink-window-if-larger-than-buffer)
354 (error "Check-in failed"))))
ac3f4c6f 355 ;; Single-file commit? Then update the revision by parsing the buffer.
8cdd17b4
ER
356 ;; Otherwise we can't necessarily tell what goes with what; clear
357 ;; its properties so they have to be refetched.
358 (if (= (length files) 1)
359 (vc-file-setprop
ac3f4c6f 360 (car files) 'vc-working-revision
8cdd17b4 361 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
e0607aaa 362 (mapc 'vc-file-clearprops files))
8cdd17b4 363 ;; Anyway, forget the checkout model of the file, because we might have
c1b25099
GM
364 ;; guessed wrong when we found the file. After commit, we can
365 ;; tell it from the permissions of the file (see
366 ;; vc-cvs-checkout-model).
8cdd17b4
ER
367 (mapc (lambda (file) (vc-file-setprop file 'vc-checkout-model nil))
368 files)
51af12fc
AS
369
370 ;; if this was an explicit check-in (does not include creation of
371 ;; a branch), remove the sticky tag.
372 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
8cdd17b4 373 (vc-cvs-command nil 0 files "update" "-A"))))
c1b25099 374
ac3f4c6f 375(defun vc-cvs-find-revision (file rev buffer)
ea05db01
SM
376 (apply 'vc-cvs-command
377 buffer 0 file
378 "-Q" ; suppress diagnostic output
379 "update"
380 (and rev (not (string= rev ""))
381 (concat "-r" rev))
382 "-p"
f153cb52 383 (vc-switches 'CVS 'checkout)))
ea05db01 384
f4b43eb3
SM
385(defun vc-cvs-checkout (file &optional editable rev)
386 "Checkout a revision of FILE into the working area.
34abd98e 387EDITABLE non-nil means that the file should be writable.
f4b43eb3
SM
388REV is the revision to check out."
389 (message "Checking out %s..." file)
390 ;; Change buffers to get local value of vc-checkout-switches.
391 (with-current-buffer (or (get-file-buffer file) (current-buffer))
392 (if (and (file-exists-p file) (not rev))
393 ;; If no revision was specified, just make the file writable
394 ;; if necessary (using `cvs-edit' if requested).
70e2f6c7 395 (and editable (not (eq (vc-cvs-checkout-model (list file)) 'implicit))
f4b43eb3
SM
396 (if vc-cvs-use-edit
397 (vc-cvs-command nil 0 file "edit")
398 (set-file-modes file (logior (file-modes file) 128))
35e62fc9 399 (if (equal file buffer-file-name) (read-only-mode -1))))
ac3f4c6f
ER
400 ;; Check out a particular revision (or recreate the file).
401 (vc-file-setprop file 'vc-working-revision nil)
f4b43eb3
SM
402 (apply 'vc-cvs-command nil 0 file
403 (and editable "-w")
404 "update"
405 (when rev
406 (unless (eq rev t)
407 ;; default for verbose checkout: clear the
408 ;; sticky tag so that the actual update will
409 ;; get the head of the trunk
410 (if (string= rev "")
411 "-A"
412 (concat "-r" rev))))
413 (vc-switches 'CVS 'checkout)))
77bf3f54 414 (vc-mode-line file 'CVS))
f4b43eb3 415 (message "Checking out %s...done" file))
c1b25099 416
a0688443 417(defun vc-cvs-delete-file (file)
105cac2d 418 (vc-cvs-command nil 0 file "remove" "-f"))
a0688443 419
712b9732
GM
420(autoload 'vc-default-revert "vc")
421
99739bbf 422(defun vc-cvs-revert (file &optional contents-done)
ac3f4c6f 423 "Revert FILE to the working revision on which it was based."
f4b43eb3 424 (vc-default-revert 'CVS file contents-done)
70e2f6c7 425 (unless (eq (vc-cvs-checkout-model (list file)) 'implicit)
99739bbf 426 (if vc-cvs-use-edit
bbce0417 427 (vc-cvs-command nil 0 file "unedit")
99739bbf
AS
428 ;; Make the file read-only by switching off all w-bits
429 (set-file-modes file (logand (file-modes file) 3950)))))
8f98485f 430
ac3f4c6f 431(defun vc-cvs-merge (file first-revision &optional second-revision)
8f98485f 432 "Merge changes into current working copy of FILE.
ac3f4c6f 433The changes are between FIRST-REVISION and SECOND-REVISION."
bbce0417 434 (vc-cvs-command nil 0 file
8f98485f 435 "update" "-kk"
ac3f4c6f
ER
436 (concat "-j" first-revision)
437 (concat "-j" second-revision))
8f98485f 438 (vc-file-setprop file 'vc-state 'edited)
d3ee404f 439 (with-current-buffer (get-buffer "*vc*")
8f98485f
AS
440 (goto-char (point-min))
441 (if (re-search-forward "conflicts during merge" nil t)
bdb55eba 442 (progn
7fbb4797
DN
443 (vc-file-setprop file 'vc-state 'conflict)
444 ;; signal error
445 1)
446 (vc-file-setprop file 'vc-state 'edited)
447 ;; signal success
448 0)))
8f98485f
AS
449
450(defun vc-cvs-merge-news (file)
451 "Merge in any new changes made to FILE."
452 (message "Merging changes into %s..." file)
ac3f4c6f 453 ;; (vc-file-setprop file 'vc-working-revision nil)
d3ee404f 454 (vc-file-setprop file 'vc-checkout-time 0)
c6a234ac 455 (vc-cvs-command nil nil file "update")
d3ee404f
SM
456 ;; Analyze the merge result reported by CVS, and set
457 ;; file properties accordingly.
458 (with-current-buffer (get-buffer "*vc*")
8f98485f 459 (goto-char (point-min))
ac3f4c6f 460 ;; get new working revision
d3ee404f
SM
461 (if (re-search-forward
462 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
ac3f4c6f
ER
463 (vc-file-setprop file 'vc-working-revision (match-string 1))
464 (vc-file-setprop file 'vc-working-revision nil))
8f98485f
AS
465 ;; get file status
466 (prog1
467 (if (eq (buffer-size) 0)
468 0 ;; there were no news; indicate success
469 (if (re-search-forward
470 (concat "^\\([CMUP] \\)?"
9ba562d3 471 (regexp-quote
ff6b00de
SS
472 (substring file (1+ (length (expand-file-name
473 "." default-directory)))))
8f98485f
AS
474 "\\( already contains the differences between \\)?")
475 nil t)
476 (cond
477 ;; Merge successful, we are in sync with repository now
478 ((or (match-string 2)
479 (string= (match-string 1) "U ")
480 (string= (match-string 1) "P "))
481 (vc-file-setprop file 'vc-state 'up-to-date)
482 (vc-file-setprop file 'vc-checkout-time
483 (nth 5 (file-attributes file)))
484 0);; indicate success to the caller
485 ;; Merge successful, but our own changes are still in the file
486 ((string= (match-string 1) "M ")
487 (vc-file-setprop file 'vc-state 'edited)
488 0);; indicate success to the caller
489 ;; Conflicts detected!
490 (t
7fbb4797 491 (vc-file-setprop file 'vc-state 'conflict)
8f98485f
AS
492 1);; signal the error to the caller
493 )
494 (pop-to-buffer "*vc*")
495 (error "Couldn't analyze cvs update result")))
496 (message "Merging changes into %s...done" file))))
497
9b64a7f0 498(defun vc-cvs-modify-change-comment (files rev comment)
bdb55eba 499 "Modify the change comments for FILES on a specified REV.
9b64a7f0 500Will fail unless you have administrative privileges on the repo."
031f1766 501 (vc-cvs-command nil 0 files "admin" (concat "-m" rev ":" comment)))
9b0630e5 502
8f98485f
AS
503;;;
504;;; History functions
505;;;
506
db167d28 507(declare-function vc-rcs-print-log-cleanup "vc-rcs" ())
712b9732
GM
508;; Follows vc-cvs-command, which uses vc-do-command from vc-dispatcher.
509(declare-function vc-exec-after "vc-dispatcher" (code))
db167d28 510
0d42eb3e 511(defun vc-cvs-print-log (files buffer &optional _shortlog _start-revision limit)
bb7cdf58
GM
512 "Print commit log associated with FILES into specified BUFFER.
513Remaining arguments are ignored."
db167d28 514 (require 'vc-rcs)
be01714b 515 ;; It's just the catenation of the individual logs.
bbce0417 516 (vc-cvs-command
a9817cc4 517 buffer
77bf3f54 518 (if (vc-stay-local-p files 'CVS) 'async 0)
db167d28
DN
519 files "log")
520 (with-current-buffer buffer
48b27575
DN
521 (vc-exec-after (vc-rcs-print-log-cleanup)))
522 (when limit 'limit-unsupported))
8cdd17b4 523
6aa5d910
ER
524(defun vc-cvs-comment-history (file)
525 "Get comment history of a file."
526 (vc-call-backend 'RCS 'comment-history file))
8f98485f 527
712b9732
GM
528(autoload 'vc-version-backup-file "vc")
529(declare-function vc-coding-system-for-diff "vc" (file))
530
8cdd17b4 531(defun vc-cvs-diff (files &optional oldvers newvers buffer)
ac3f4c6f 532 "Get a difference report using CVS between two revisions of FILE."
66f3089c
MA
533 (let* (process-file-side-effects
534 (async (and (not vc-disable-async-diff)
77bf3f54 535 (vc-stay-local-p files 'CVS)))
2e7a8a21
DN
536 (invoke-cvs-diff-list nil)
537 status)
538 ;; Look through the file list and see if any files have backups
539 ;; that can be used to do a plain "diff" instead of "cvs diff".
540 (dolist (file files)
541 (let ((ov oldvers)
542 (nv newvers))
543 (when (or (not ov) (string-equal ov ""))
544 (setq ov (vc-working-revision file)))
545 (when (string-equal nv "")
546 (setq nv nil))
547 (let ((file-oldvers (vc-version-backup-file file ov))
548 (file-newvers (if (not nv)
549 file
550 (vc-version-backup-file file nv)))
551 (coding-system-for-read (vc-coding-system-for-diff file)))
552 (if (and file-oldvers file-newvers)
553 (progn
e1f650f4
GM
554 ;; This used to append diff-switches and vc-diff-switches,
555 ;; which was consistent with the vc-diff-switches doc at that
556 ;; time, but not with the actual behavior of any other VC diff.
2e7a8a21 557 (apply 'vc-do-command (or buffer "*vc-diff*") 1 "diff" nil
e1f650f4
GM
558 ;; Not a CVS diff, does not use vc-cvs-diff-switches.
559 (append (vc-switches nil 'diff)
2e7a8a21
DN
560 (list (file-relative-name file-oldvers)
561 (file-relative-name file-newvers))))
562 (setq status 0))
563 (push file invoke-cvs-diff-list)))))
564 (when invoke-cvs-diff-list
565 (setq status (apply 'vc-cvs-command (or buffer "*vc-diff*")
f153cb52 566 (if async 'async 1)
2e7a8a21 567 invoke-cvs-diff-list "diff"
f153cb52
SM
568 (and oldvers (concat "-r" oldvers))
569 (and newvers (concat "-r" newvers))
570 (vc-switches 'CVS 'diff))))
2e7a8a21
DN
571 (if async 1 status))) ; async diff, pessimistic assumption
572
98ad325c
SM
573(defconst vc-cvs-annotate-first-line-re "^[0-9]")
574
bcd7a0a4 575(defun vc-cvs-annotate-process-filter (filter process string)
98ad325c
SM
576 (setq string (concat (process-get process 'output) string))
577 (if (not (string-match vc-cvs-annotate-first-line-re string))
578 ;; Still waiting for the first real line.
579 (process-put process 'output string)
bcd7a0a4
SM
580 (remove-function (process-filter process) #'vc-cvs-annotate-process-filter)
581 (funcall filter process (substring string (match-beginning 0)))))
98ad325c 582
ac3f4c6f 583(defun vc-cvs-annotate-command (file buffer &optional revision)
8f98485f 584 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
ac3f4c6f 585Optional arg REVISION is a revision to annotate from."
98ad325c 586 (vc-cvs-command buffer
77bf3f54 587 (if (vc-stay-local-p file 'CVS)
98ad325c
SM
588 'async 0)
589 file "annotate"
ac3f4c6f 590 (if revision (concat "-r" revision)))
98ad325c
SM
591 ;; Strip the leading few lines.
592 (let ((proc (get-buffer-process buffer)))
593 (if proc
594 ;; If running asynchronously, use a process filter.
bcd7a0a4
SM
595 (add-function :around (process-filter proc)
596 #'vc-cvs-annotate-process-filter)
98ad325c
SM
597 (with-current-buffer buffer
598 (goto-char (point-min))
599 (re-search-forward vc-cvs-annotate-first-line-re)
600 (delete-region (point-min) (1- (point)))))))
c1b25099 601
f8bd9ac6
DN
602(declare-function vc-annotate-convert-time "vc-annotate" (time))
603
8ba2df32
AS
604(defun vc-cvs-annotate-current-time ()
605 "Return the current time, based at midnight of the current day, and
606encoded as fractional days."
607 (vc-annotate-convert-time
608 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
609
610(defun vc-cvs-annotate-time ()
611 "Return the time of the next annotation (as fraction of days)
f0529b5b 612systime, or nil if there is none."
b44a1825
TTN
613 (let* ((bol (point))
614 (cache (get-text-property bol 'vc-cvs-annotate-time))
72d1ce61
SM
615 (inhibit-read-only t)
616 (inhibit-modification-hooks t))
b44a1825
TTN
617 (cond
618 (cache)
619 ((looking-at
620 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
621 (let ((day (string-to-number (match-string 1)))
622 (month (cdr (assq (intern (match-string 2))
623 '((Jan . 1) (Feb . 2) (Mar . 3)
624 (Apr . 4) (May . 5) (Jun . 6)
625 (Jul . 7) (Aug . 8) (Sep . 9)
626 (Oct . 10) (Nov . 11) (Dec . 12)))))
627 (year (let ((tmp (string-to-number (match-string 3))))
628 ;; Years 0..68 are 2000..2068.
629 ;; Years 69..99 are 1969..1999.
630 (+ (cond ((> 69 tmp) 2000)
631 ((> 100 tmp) 1900)
632 (t 0))
633 tmp))))
634 (put-text-property
635 bol (1+ bol) 'vc-cvs-annotate-time
636 (setq cache (cons
637 ;; Position at end makes for nicer overlay result.
d58107b0
SM
638 ;; Don't put actual buffer pos here, but only relative
639 ;; distance, so we don't ever move backward in the
640 ;; goto-char below, even if the text is moved.
641 (- (match-end 0) (match-beginning 0))
b44a1825
TTN
642 (vc-annotate-convert-time
643 (encode-time 0 0 0 day month year))))))))
644 (when cache
d58107b0 645 (goto-char (+ bol (car cache))) ; Fontify from here to eol.
b44a1825 646 (cdr cache)))) ; days (float)
9b0630e5 647
f2a2e61b
AS
648(defun vc-cvs-annotate-extract-revision-at-line ()
649 (save-excursion
650 (beginning-of-line)
651 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
652 (line-end-position) t)
653 (match-string-no-properties 1)
654 nil)))
655
b3cca6a6
DN
656(defun vc-cvs-previous-revision (file rev)
657 (vc-call-backend 'RCS 'previous-revision file rev))
658
659(defun vc-cvs-next-revision (file rev)
660 (vc-call-backend 'RCS 'next-revision file rev))
661
662;; FIXME: This should probably be replaced by code using cvs2cl.
663(defun vc-cvs-update-changelog (files)
664 (vc-call-backend 'RCS 'update-changelog files))
665
8f98485f 666;;;
370fded4 667;;; Tag system
8f98485f
AS
668;;;
669
370fded4 670(defun vc-cvs-create-tag (dir name branchp)
ac3f4c6f 671 "Assign to DIR's current revision a given NAME.
8f98485f
AS
672If BRANCHP is non-nil, the name is created as a branch (and the current
673workspace is immediately moved to that new branch)."
bbce0417
AS
674 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
675 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
8f98485f 676
712b9732
GM
677;; Follows vc-cvs-command, which uses vc-do-command from vc-dispatcher.
678(declare-function vc-resynch-buffer "vc-dispatcher"
679 (file &optional keep noquery reset-vc-info))
680
370fded4
ER
681(defun vc-cvs-retrieve-tag (dir name update)
682 "Retrieve a tag at and below DIR.
683NAME is the name of the tag; if it is empty, do a `cvs update'.
8f98485f
AS
684If UPDATE is non-nil, then update (resynch) any affected buffers."
685 (with-current-buffer (get-buffer-create "*vc*")
51af12fc
AS
686 (let ((default-directory dir)
687 (sticky-tag))
8f98485f
AS
688 (erase-buffer)
689 (if (or (not name) (string= name ""))
bbce0417
AS
690 (vc-cvs-command t 0 nil "update")
691 (vc-cvs-command t 0 nil "update" "-r" name)
51af12fc 692 (setq sticky-tag name))
8f98485f
AS
693 (when update
694 (goto-char (point-min))
695 (while (not (eobp))
696 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
697 (let* ((file (expand-file-name (match-string 2) dir))
698 (state (match-string 1))
699 (buffer (find-buffer-visiting file)))
700 (when buffer
701 (cond
702 ((or (string= state "U")
703 (string= state "P"))
704 (vc-file-setprop file 'vc-state 'up-to-date)
ac3f4c6f 705 (vc-file-setprop file 'vc-working-revision nil)
8f98485f
AS
706 (vc-file-setprop file 'vc-checkout-time
707 (nth 5 (file-attributes file))))
708 ((or (string= state "M")
709 (string= state "C"))
710 (vc-file-setprop file 'vc-state 'edited)
ac3f4c6f 711 (vc-file-setprop file 'vc-working-revision nil)
8f98485f 712 (vc-file-setprop file 'vc-checkout-time 0)))
51af12fc 713 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
8f98485f
AS
714 (vc-resynch-buffer file t t))))
715 (forward-line 1))))))
716
9b0630e5 717
8f98485f
AS
718;;;
719;;; Miscellaneous
720;;;
721
77bf3f54
DN
722(defun vc-cvs-make-version-backups-p (file)
723 "Return non-nil if version backups should be made for FILE."
724 (vc-stay-local-p file 'CVS))
8f98485f
AS
725
726(defun vc-cvs-check-headers ()
727 "Check if the current file has any headers in it."
728 (save-excursion
729 (goto-char (point-min))
730 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
731\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
732
9b0630e5 733
8f98485f
AS
734;;;
735;;; Internal functions
736;;;
737
8cdd17b4 738(defun vc-cvs-command (buffer okstatus files &rest flags)
bbce0417
AS
739 "A wrapper around `vc-do-command' for use in vc-cvs.el.
740The difference to vc-do-command is that this function always invokes `cvs',
741and that it passes `vc-cvs-global-switches' to it before FLAGS."
2888a97e 742 (apply 'vc-do-command (or buffer "*vc*") okstatus "cvs" files
968b980c 743 (if (stringp vc-cvs-global-switches)
bbce0417
AS
744 (cons vc-cvs-global-switches flags)
745 (append vc-cvs-global-switches
746 flags))))
747
77bf3f54
DN
748(defun vc-cvs-stay-local-p (file) ;Back-compatibility.
749 (vc-stay-local-p file 'CVS))
e54faddb
SM
750
751(defun vc-cvs-repository-hostname (dirname)
752 "Hostname of the CVS server associated to workarea DIRNAME."
753 (let ((rootname (expand-file-name "CVS/Root" dirname)))
754 (when (file-readable-p rootname)
755 (with-temp-buffer
756 (let ((coding-system-for-read
757 (or file-name-coding-system
758 default-file-name-coding-system)))
759 (vc-insert-file rootname))
760 (goto-char (point-min))
761 (nth 2 (vc-cvs-parse-root
762 (buffer-substring (point)
763 (line-end-position))))))))
78376474 764
05342dca
SS
765(defun vc-cvs-parse-uhp (path)
766 "parse user@host/path into (user@host /path)"
767 (if (string-match "\\([^/]+\\)\\(/.*\\)" path)
768 (list (match-string 1 path) (match-string 2 path))
769 (list nil path)))
770
78376474 771(defun vc-cvs-parse-root (root)
d3ed06c6
AS
772 "Split CVS ROOT specification string into a list of fields.
773A CVS root specification of the form
05342dca 774 [:METHOD:][[USER@]HOSTNAME]:?/path/to/repository
d3ed06c6
AS
775is converted to a normalized record with the following structure:
776 \(METHOD USER HOSTNAME CVS-ROOT).
777The default METHOD for a CVS root of the form
778 /path/to/repository
779is `local'.
780The default METHOD for a CVS root of the form
781 [USER@]HOSTNAME:/path/to/repository
782is `ext'.
aaed846c 783For an empty string, nil is returned (invalid CVS root)."
d3ed06c6
AS
784 ;; Split CVS root into colon separated fields (0-4).
785 ;; The `x:' makes sure, that leading colons are not lost;
786 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
787 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
788 (len (length root-list))
789 ;; All syntactic varieties will get a proper METHOD.
790 (root-list
791 (cond
792 ((= len 0)
793 ;; Invalid CVS root
794 nil)
795 ((= len 1)
05342dca
SS
796 (let ((uhp (vc-cvs-parse-uhp (car root-list))))
797 (cons (if (car uhp) "ext" "local") uhp)))
d3ed06c6
AS
798 ((= len 2)
799 ;; [USER@]HOST:PATH => method `ext'
800 (and (not (equal (car root-list) ""))
801 (cons "ext" root-list)))
802 ((= len 3)
05342dca 803 ;; :METHOD:PATH or :METHOD:USER@HOSTNAME/PATH
d3ed06c6 804 (cons (cadr root-list)
a464a6c7 805 (vc-cvs-parse-uhp (nth 2 root-list))))
d3ed06c6
AS
806 (t
807 ;; :METHOD:[USER@]HOST:PATH
808 (cdr root-list)))))
809 (if root-list
810 (let ((method (car root-list))
811 (uhost (or (cadr root-list) ""))
812 (root (nth 2 root-list))
813 user host)
814 ;; Split USER@HOST
815 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
816 (setq user (match-string 1 uhost)
817 host (match-string 2 uhost))
818 (setq host uhost))
819 ;; Remove empty HOST
820 (and (equal host "")
821 (setq host))
822 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
823 (and host
824 (equal method "local")
825 (setq root (concat host ":" root) host))
826 ;; Normalize CVS root record
827 (list method user host root)))))
8f98485f 828
4e383781
DN
829;; XXX: This does not work correctly for subdirectories. "cvs status"
830;; information is context sensitive, it contains lines like:
831;; cvs status: Examining DIRNAME
832;; and the file entries after that don't show the full path.
caf37b1f
ER
833;; Because of this VC directory listings only show changed files
834;; at the top level for CVS.
8f98485f
AS
835(defun vc-cvs-parse-status (&optional full)
836 "Parse output of \"cvs status\" command in the current buffer.
837Set file properties accordingly. Unless FULL is t, parse only
722f037f
ER
838essential information. Note that this can never set the 'ignored
839state."
920fb2b0 840 (let (file status missing)
8f98485f 841 (goto-char (point-min))
722f037f
ER
842 (while (looking-at "? \\(.*\\)")
843 (setq file (expand-file-name (match-string 1)))
844 (vc-file-setprop file 'vc-state 'unregistered)
845 (forward-line 1))
920fb2b0
DN
846 (when (re-search-forward "^File: " nil t)
847 (when (setq missing (looking-at "no file "))
848 (goto-char (match-end 0)))
849 (cond
850 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
851 (setq file (expand-file-name (match-string 1)))
47dd5958
SM
852 (setq status(if (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t)
853 (match-string 1) "Unknown"))
7fbb4797
DN
854 (when (and full
855 (re-search-forward
856 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
8f98485f 857\[\t ]+\\([0-9.]+\\)"
7fbb4797 858 nil t))
920fb2b0
DN
859 (vc-file-setprop file 'vc-latest-revision (match-string 2)))
860 (vc-file-setprop
861 file 'vc-state
862 (cond
863 ((string-match "Up-to-date" status)
864 (vc-file-setprop file 'vc-checkout-time
865 (nth 5 (file-attributes file)))
866 'up-to-date)
867 ((string-match "Locally Modified" status) 'edited)
868 ((string-match "Needs Merge" status) 'needs-merge)
869 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
3702367b 870 (if missing 'missing 'needs-update))
920fb2b0
DN
871 ((string-match "Locally Added" status) 'added)
872 ((string-match "Locally Removed" status) 'removed)
7fbb4797 873 ((string-match "File had conflicts " status) 'conflict)
47dd5958 874 ((string-match "Unknown" status) 'unregistered)
920fb2b0 875 (t 'edited))))))))
8f98485f 876
c1b51374 877(defun vc-cvs-after-dir-status (update-function)
798dafb4 878 ;; Heavily inspired by vc-cvs-parse-status. AKA a quick hack.
5a9de6d0 879 ;; This needs a lot of testing.
798dafb4
DN
880 (let ((status nil)
881 (status-str nil)
882 (file nil)
883 (result nil)
920fb2b0 884 (missing nil)
1a0cf619 885 (ignore-next nil)
798dafb4
DN
886 (subdir default-directory))
887 (goto-char (point-min))
888 (while
889 ;; Look for either a file entry, an unregistered file, or a
890 ;; directory change.
891 (re-search-forward
1a0cf619 892 "\\(^=+\n\\([^=c?\n].*\n\\|\n\\)+\\)\\|\\(\\(^?? .*\n\\)+\\)\\|\\(^cvs status: \\(Examining\\|nothing\\) .*\n\\)"
798dafb4 893 nil t)
9fc36123 894 ;; FIXME: get rid of narrowing here.
798dafb4
DN
895 (narrow-to-region (match-beginning 0) (match-end 0))
896 (goto-char (point-min))
897 ;; The subdir
898 (when (looking-at "cvs status: Examining \\(.+\\)")
899 (setq subdir (expand-file-name (match-string 1))))
900 ;; Unregistered files
901 (while (looking-at "? \\(.*\\)")
bdb55eba 902 (setq file (file-relative-name
798dafb4 903 (expand-file-name (match-string 1) subdir)))
1b3f2d4e 904 (push (list file 'unregistered) result)
798dafb4 905 (forward-line 1))
1a0cf619
DN
906 (when (looking-at "cvs status: nothing known about")
907 ;; We asked about a non existent file. The output looks like this:
908
909 ;; cvs status: nothing known about `lisp/v.diff'
910 ;; ===================================================================
911 ;; File: no file v.diff Status: Unknown
912 ;;
913 ;; Working revision: No entry for v.diff
914 ;; Repository revision: No revision control file
915 ;;
916
917 ;; Due to narrowing in this iteration we only see the "cvs
918 ;; status:" line, so just set a flag so that we can ignore the
919 ;; file in the next iteration.
920 (setq ignore-next t))
798dafb4 921 ;; A file entry.
9fc36123
DN
922 (when (re-search-forward "^File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: \\(.*\\)" nil t)
923 (setq missing (match-string 1))
924 (setq file (file-relative-name
925 (expand-file-name (match-string 2) subdir)))
926 (setq status-str (match-string 3))
927 (setq status
928 (cond
929 ((string-match "Up-to-date" status-str) 'up-to-date)
930 ((string-match "Locally Modified" status-str) 'edited)
931 ((string-match "Needs Merge" status-str) 'needs-merge)
932 ((string-match "Needs \\(Checkout\\|Patch\\)" status-str)
933 (if missing 'missing 'needs-update))
934 ((string-match "Locally Added" status-str) 'added)
935 ((string-match "Locally Removed" status-str) 'removed)
936 ((string-match "File had conflicts " status-str) 'conflict)
17996b53 937 ((string-match "Unknown" status-str) 'unregistered)
9fc36123 938 (t 'edited)))
1a0cf619
DN
939 (if ignore-next
940 (setq ignore-next nil)
941 (unless (eq status 'up-to-date)
942 (push (list file status) result))))
798dafb4
DN
943 (goto-char (point-max))
944 (widen))
40de1a6a 945 (funcall update-function result))
769303ae
DN
946 ;; Alternative implementation: use the "update" command instead of
947 ;; the "status" command.
948 ;; (let ((result nil)
949 ;; (translation '((?? . unregistered)
950 ;; (?A . added)
951 ;; (?C . conflict)
952 ;; (?M . edited)
953 ;; (?P . needs-merge)
954 ;; (?R . removed)
3702367b 955 ;; (?U . needs-update))))
769303ae
DN
956 ;; (goto-char (point-min))
957 ;; (while (not (eobp))
958 ;; (if (looking-at "^[ACMPRU?] \\(.*\\)$")
bdb55eba
SS
959 ;; (push (list (match-string 1)
960 ;; (cdr (assoc (char-after) translation)))
769303ae
DN
961 ;; result)
962 ;; (cond
963 ;; ((looking-at "cvs update: warning: \\(.*\\) was lost")
964 ;; ;; Format is:
965 ;; ;; cvs update: warning: FILENAME was lost
966 ;; ;; U FILENAME
967 ;; (push (list (match-string 1) 'missing) result)
968 ;; ;; Skip the "U" line
969 ;; (forward-line 1))
970 ;; ((looking-at "cvs update: New directory `\\(.*\\)' -- ignored")
971 ;; (push (list (match-string 1) 'unregistered) result))))
972 ;; (forward-line 1))
973 ;; (funcall update-function result)))
974 )
798dafb4 975
f3e6c9f3 976;; Based on vc-cvs-dir-state-heuristic from Emacs 22.
9515cdcc 977;; FIXME does not mention unregistered files.
f3e6c9f3
GM
978(defun vc-cvs-dir-status-heuristic (dir update-function &optional basedir)
979 "Find the CVS state of all files in DIR, using only local information."
980 (let (file basename status result dirlist)
981 (with-temp-buffer
982 (vc-cvs-get-entries dir)
983 (goto-char (point-min))
984 (while (not (eobp))
985 (if (looking-at "D/\\([^/]*\\)////")
986 (push (expand-file-name (match-string 1) dir) dirlist)
987 ;; CVS-removed files are not taken under VC control.
988 (when (looking-at "/\\([^/]*\\)/[^/-]")
989 (setq basename (match-string 1)
990 file (expand-file-name basename dir)
991 status (or (vc-file-getprop file 'vc-state)
992 (vc-cvs-parse-entry file t)))
993 (unless (eq status 'up-to-date)
994 (push (list (if basedir
995 (file-relative-name file basedir)
996 basename)
997 status) result))))
998 (forward-line 1)))
999 (dolist (subdir dirlist)
1000 (setq result (append result
1001 (vc-cvs-dir-status-heuristic subdir nil
1002 (or basedir dir)))))
1003 (if basedir result
1004 (funcall update-function result))))
1005
c1b51374 1006(defun vc-cvs-dir-status (dir update-function)
798dafb4 1007 "Create a list of conses (file . state) for DIR."
f3e6c9f3 1008 ;; FIXME check all files in DIR instead?
77bf3f54 1009 (let ((local (vc-stay-local-p dir 'CVS)))
5870cb76
DN
1010 (if (and local (not (eq local 'only-file)))
1011 (vc-cvs-dir-status-heuristic dir update-function)
1012 (vc-cvs-command (current-buffer) 'async dir "-f" "status")
1013 ;; Alternative implementation: use the "update" command instead of
1014 ;; the "status" command.
1015 ;; (vc-cvs-command (current-buffer) 'async
1016 ;; (file-relative-name dir)
1017 ;; "-f" "-n" "update" "-d" "-P")
1018 (vc-exec-after
1019 `(vc-cvs-after-dir-status (quote ,update-function))))))
efe0da9c 1020
0d42eb3e 1021(defun vc-cvs-dir-status-files (dir files _default-state update-function)
847fb889
DN
1022 "Create a list of conses (file . state) for DIR."
1023 (apply 'vc-cvs-command (current-buffer) 'async dir "-f" "status" files)
1024 (vc-exec-after
1025 `(vc-cvs-after-dir-status (quote ,update-function))))
1026
98712492
ER
1027(defun vc-cvs-file-to-string (file)
1028 "Read the content of FILE and return it as a string."
1029 (condition-case nil
1030 (with-temp-buffer
1031 (insert-file-contents file)
1032 (goto-char (point-min))
1033 (buffer-substring (point) (point-max)))
1034 (file-error nil)))
1035
0d42eb3e 1036(defun vc-cvs-dir-extra-headers (_dir)
98712492 1037 "Extract and represent per-directory properties of a CVS working copy."
4c61891a 1038 (let ((repo
98712492
ER
1039 (condition-case nil
1040 (with-temp-buffer
1041 (insert-file-contents "CVS/Root")
1042 (goto-char (point-min))
4c61891a 1043 (and (looking-at ":ext:") (delete-char 5))
644896e1 1044 (concat (buffer-substring (point) (1- (point-max))) "\n"))
98712492
ER
1045 (file-error nil)))
1046 (module
1047 (condition-case nil
1048 (with-temp-buffer
1049 (insert-file-contents "CVS/Repository")
1050 (goto-char (point-min))
7f5cd554
CY
1051 (skip-chars-forward "^\n")
1052 (concat (buffer-substring (point-min) (point)) "\n"))
98712492 1053 (file-error nil))))
4c61891a 1054 (concat
4c61891a 1055 (cond (repo
bdb55eba
SS
1056 (concat (propertize "Repository : " 'face 'font-lock-type-face)
1057 (propertize repo 'face 'font-lock-variable-name-face)))
4c61891a 1058 (t ""))
644896e1
NR
1059 (cond (module
1060 (concat (propertize "Module : " 'face 'font-lock-type-face)
1061 (propertize module 'face 'font-lock-variable-name-face)))
1062 (t ""))
1063 (if (file-readable-p "CVS/Tag")
1064 (let ((tag (vc-cvs-file-to-string "CVS/Tag")))
1065 (cond
1066 ((string-match "\\`T" tag)
1067 (concat (propertize "Tag : " 'face 'font-lock-type-face)
1068 (propertize (substring tag 1)
1069 'face 'font-lock-variable-name-face)))
1070 ((string-match "\\`D" tag)
1071 (concat (propertize "Date : " 'face 'font-lock-type-face)
1072 (propertize (substring tag 1)
1073 'face 'font-lock-variable-name-face)))
1074 (t ""))))
1075
bdb55eba
SS
1076 ;; In CVS, branch is a per-file property, not a per-directory property.
1077 ;; We can't really do this here without making dangerous assumptions.
98712492
ER
1078 ;;(propertize "Branch: " 'face 'font-lock-type-face)
1079 ;;(propertize "ADD CODE TO PRINT THE BRANCH NAME\n"
1080 ;; 'face 'font-lock-warning-face)
1081 )))
47302633 1082
666721a6
AS
1083(defun vc-cvs-get-entries (dir)
1084 "Insert the CVS/Entries file from below DIR into the current buffer.
1085This function ensures that the correct coding system is used for that,
1086which may not be the one that is used for the files' contents.
1087CVS/Entries should only be accessed through this function."
1088 (let ((coding-system-for-read (or file-name-coding-system
1089 default-file-name-coding-system)))
1090 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
bc99a968 1091
51af12fc
AS
1092(defun vc-cvs-valid-symbolic-tag-name-p (tag)
1093 "Return non-nil if TAG is a valid symbolic tag name."
1094 ;; According to the CVS manual, a valid symbolic tag must start with
1095 ;; an uppercase or lowercase letter and can contain uppercase and
1096 ;; lowercase letters, digits, `-', and `_'.
1097 (and (string-match "^[a-zA-Z]" tag)
1098 (not (string-match "[^a-z0-9A-Z-_]" tag))))
968b980c 1099
ac3f4c6f
ER
1100(defun vc-cvs-valid-revision-number-p (tag)
1101 "Return non-nil if TAG is a valid revision number."
93bcb353
SS
1102 (and (string-match "^[0-9]" tag)
1103 (not (string-match "[^0-9.]" tag))))
51af12fc
AS
1104
1105(defun vc-cvs-parse-sticky-tag (match-type match-tag)
968b980c 1106 "Parse and return the sticky tag as a string.
51af12fc
AS
1107`match-data' is protected."
1108 (let ((data (match-data))
1109 (tag)
1110 (type (cond ((string= match-type "D") 'date)
1111 ((string= match-type "T")
1112 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
1113 'symbolic-name
1114 'revision-number))
1115 (t nil))))
1116 (unwind-protect
1117 (progn
968b980c 1118 (cond
e6608c12 1119 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
51af12fc 1120 ((eq type 'date)
968b980c
SS
1121 (string-match
1122 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
51af12fc
AS
1123 match-tag)
1124 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
1125 (month (string-to-number (match-string 2 match-tag)))
1126 (day (string-to-number (match-string 3 match-tag)))
1127 (hour (string-to-number (match-string 4 match-tag)))
1128 (min (string-to-number (match-string 5 match-tag)))
1129 (sec (string-to-number (match-string 6 match-tag)))
1130 ;; Years 0..68 are 2000..2068.
1131 ;; Years 69..99 are 1969..1999.
1132 (year (+ (cond ((> 69 year-tmp) 2000)
1133 ((> 100 year-tmp) 1900)
1134 (t 0))
1135 year-tmp)))
1136 (setq tag (encode-time sec min hour day month year))))
1137 ;; Sticky Tag name or revision number
1138 ((eq type 'symbolic-name) (setq tag match-tag))
1139 ((eq type 'revision-number) (setq tag match-tag))
1140 ;; Default is no sticky tag at all
1141 (t nil))
1142 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
1143 ((eq vc-cvs-sticky-tag-display t)
968b980c 1144 (cond ((eq type 'date) (format-time-string
51af12fc
AS
1145 vc-cvs-sticky-date-format-string
1146 tag))
1147 ((eq type 'symbolic-name) tag)
1148 ((eq type 'revision-number) tag)
1149 (t nil)))
968b980c 1150 ((functionp vc-cvs-sticky-tag-display)
51af12fc
AS
1151 (funcall vc-cvs-sticky-tag-display tag type))
1152 (t nil)))
1153
1154 (set-match-data data))))
1155
8f98485f
AS
1156(defun vc-cvs-parse-entry (file &optional set-state)
1157 "Parse a line from CVS/Entries.
1158Compare modification time to that of the FILE, set file properties
1159accordingly. However, `vc-state' is set only if optional arg SET-STATE
1160is non-nil."
1161 (cond
1162 ;; entry for a "locally added" file (not yet committed)
1163 ((looking-at "/[^/]+/0/")
1164 (vc-file-setprop file 'vc-checkout-time 0)
ac3f4c6f 1165 (vc-file-setprop file 'vc-working-revision "0")
45b24b4d 1166 (if set-state (vc-file-setprop file 'vc-state 'added)))
8f98485f
AS
1167 ;; normal entry
1168 ((looking-at
1169 (concat "/[^/]+"
1170 ;; revision
1171 "/\\([^/]*\\)"
92788b3b
AS
1172 ;; timestamp and optional conflict field
1173 "/\\([^/]*\\)/"
51af12fc
AS
1174 ;; options
1175 "\\([^/]*\\)/"
1176 ;; sticky tag
1177 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
1178 "\\(.*\\)")) ;Sticky tag
ac3f4c6f 1179 (vc-file-setprop file 'vc-working-revision (match-string 1))
839dacca 1180 (vc-file-setprop file 'vc-cvs-sticky-tag
bc99a968 1181 (vc-cvs-parse-sticky-tag (match-string 4)
e5d9c9a2 1182 (match-string 5)))
ecfc2ba0
AS
1183 ;; Compare checkout time and modification time.
1184 ;; This is intentionally different from the algorithm that CVS uses
e5d9c9a2 1185 ;; (which is based on textual comparison), because there can be problems
ecfc2ba0 1186 ;; generating a time string that looks exactly like the one from CVS.
9515cdcc
GM
1187 (let* ((time (match-string 2))
1188 (mtime (nth 5 (file-attributes file)))
1189 (parsed-time (progn (require 'parse-time)
1190 (parse-time-string (concat time " +0000")))))
1191 (cond ((and (not (string-match "\\+" time))
1192 (car parsed-time)
ca5256ad
PE
1193 ;; Compare just the seconds part of the file time,
1194 ;; since CVS file time stamp resolution is just 1 second.
1195 (let ((ptime (apply 'encode-time parsed-time)))
1196 (and (eq (car mtime) (car ptime))
1197 (eq (cadr mtime) (cadr ptime)))))
9515cdcc
GM
1198 (vc-file-setprop file 'vc-checkout-time mtime)
1199 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
1200 (t
1201 (vc-file-setprop file 'vc-checkout-time 0)
1202 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
968b980c 1203
2346acf6
SM
1204;; Completion of revision names.
1205;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
1206;; `cvs log' so I can list all the revision numbers rather than only
1207;; tag names.
1208
1209(defun vc-cvs-revision-table (file)
66f3089c
MA
1210 (let (process-file-side-effects
1211 (default-directory (file-name-directory file))
2346acf6
SM
1212 (res nil))
1213 (with-temp-buffer
1214 (vc-cvs-command t nil file "log")
1215 (goto-char (point-min))
1216 (when (re-search-forward "^symbolic names:\n" nil t)
1217 (while (looking-at "^ \\(.*\\): \\(.*\\)")
1218 (push (cons (match-string 1) (match-string 2)) res)
1219 (forward-line 1)))
1220 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
1221 (push (match-string 1) res))
1222 res)))
1223
32c58c47 1224(defun vc-cvs-revision-completion-table (files)
0d42eb3e
SM
1225 (letrec ((table (lazy-completion-table
1226 table (lambda () (vc-cvs-revision-table (car files))))))
2346acf6 1227 table))
bdb55eba 1228
7aa7fff0
XF
1229(defun vc-cvs-ignore (file)
1230 "Ignore FILE under CVS."
1231 (interactive)
1232 (cvs-append-to-ignore (file-name-directory file) file))
1233
1234(defun cvs-append-to-ignore (dir str &optional old-dir)
1235 "In DIR, add STR to the .cvsignore file.
1236If OLD-DIR is non-nil, then this is a directory that we don't want
1237to hear about anymore."
1238 (with-current-buffer
1239 (find-file-noselect (expand-file-name ".cvsignore" dir))
1240 (when (ignore-errors
1241 (and buffer-read-only
1242 (eq 'CVS (vc-backend buffer-file-name))
1243 (not (vc-editable-p buffer-file-name))))
1244 ;; CVSREAD=on special case
1245 (vc-checkout buffer-file-name t))
1246 (goto-char (point-max))
1247 (unless (bolp) (insert "\n"))
1248 (insert str (if old-dir "/\n" "\n"))
1249 (if cvs-sort-ignore-file (sort-lines nil (point-min) (point-max)))
1250 (save-buffer)))
2346acf6 1251
c1b25099
GM
1252(provide 'vc-cvs)
1253
1254;;; vc-cvs.el ends here