Update years in copyright notice; nfc.
[bpt/emacs.git] / lisp / vc-svn.el
1 ;;; vc-svn.el --- non-resident support for Subversion version-control
2
3 ;; Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Commentary:
26
27 ;; This is preliminary support for Subversion (http://subversion.tigris.org/).
28 ;; It started as `sed s/cvs/svn/ vc.cvs.el' (from version 1.56)
29 ;; and hasn't been completely fixed since.
30
31 ;; Sync'd with Subversion's vc-svn.el as of revision 5801.
32
33 ;;; Bugs:
34
35 ;; - VC-dired is (really) slow.
36
37 ;;; Code:
38
39 (eval-when-compile
40 (require 'vc))
41
42 ;;;
43 ;;; Customization options
44 ;;;
45
46 (defcustom vc-svn-global-switches nil
47 "*Global switches to pass to any SVN command."
48 :type '(choice (const :tag "None" nil)
49 (string :tag "Argument String")
50 (repeat :tag "Argument List"
51 :value ("")
52 string))
53 :version "22.1"
54 :group 'vc)
55
56 (defcustom vc-svn-register-switches nil
57 "*Extra switches for registering a file into SVN.
58 A string or list of strings passed to the checkin program by
59 \\[vc-register]."
60 :type '(choice (const :tag "None" nil)
61 (string :tag "Argument String")
62 (repeat :tag "Argument List"
63 :value ("")
64 string))
65 :version "22.1"
66 :group 'vc)
67
68 (defcustom vc-svn-diff-switches
69 t ;`svn' doesn't support common args like -c or -b.
70 "String or list of strings specifying extra switches for svn diff under VC.
71 If nil, use the value of `vc-diff-switches'.
72 If you want to force an empty list of arguments, use t."
73 :type '(choice (const :tag "Unspecified" nil)
74 (const :tag "None" t)
75 (string :tag "Argument String")
76 (repeat :tag "Argument List"
77 :value ("")
78 string))
79 :version "22.1"
80 :group 'vc)
81
82 (defcustom vc-svn-header (or (cdr (assoc 'SVN vc-header-alist)) '("\$Id\$"))
83 "*Header keywords to be inserted by `vc-insert-headers'."
84 :version "22.1"
85 :type '(repeat string)
86 :group 'vc)
87
88 (defconst vc-svn-use-edit nil
89 ;; Subversion does not provide this feature (yet).
90 "*Non-nil means to use `svn edit' to \"check out\" a file.
91 This is only meaningful if you don't use the implicit checkout model
92 \(i.e. if you have $SVNREAD set)."
93 ;; :type 'boolean
94 ;; :version "22.1"
95 ;; :group 'vc
96 )
97
98 ;;;
99 ;;; State-querying functions
100 ;;;
101
102 ;;;###autoload (defun vc-svn-registered (f)
103 ;;;###autoload (when (file-readable-p (expand-file-name
104 ;;;###autoload ".svn/entries" (file-name-directory f)))
105 ;;;###autoload (load "vc-svn")
106 ;;;###autoload (vc-svn-registered f)))
107
108 ;;;###autoload
109 (add-to-list 'completion-ignored-extensions ".svn/")
110
111 (defun vc-svn-registered (file)
112 "Check if FILE is SVN registered."
113 (when (file-readable-p (expand-file-name ".svn/entries"
114 (file-name-directory file)))
115 (with-temp-buffer
116 (cd (file-name-directory file))
117 (let ((status
118 (condition-case nil
119 ;; Ignore all errors.
120 (vc-svn-command t t file "status" "-v")
121 ;; Some problem happened. E.g. We can't find an `svn'
122 ;; executable. We used to only catch `file-error' but when
123 ;; the process is run on a remote host via Tramp, the error
124 ;; is only reported via the exit status which is turned into
125 ;; an `error' by vc-do-command.
126 (error nil))))
127 (when (eq 0 status)
128 (vc-svn-parse-status t)
129 (eq 'SVN (vc-file-getprop file 'vc-backend)))))))
130
131 (defun vc-svn-state (file &optional localp)
132 "SVN-specific version of `vc-state'."
133 (setq localp (or localp (vc-stay-local-p file)))
134 (with-temp-buffer
135 (cd (file-name-directory file))
136 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
137 (vc-svn-parse-status localp)
138 (vc-file-getprop file 'vc-state)))
139
140 (defun vc-svn-state-heuristic (file)
141 "SVN-specific state heuristic."
142 (vc-svn-state file 'local))
143
144 (defun vc-svn-dir-state (dir &optional localp)
145 "Find the SVN state of all files in DIR."
146 (setq localp (or localp (vc-stay-local-p dir)))
147 (let ((default-directory dir))
148 ;; Don't specify DIR in this command, the default-directory is
149 ;; enough. Otherwise it might fail with remote repositories.
150 (with-temp-buffer
151 (vc-svn-command t 0 nil "status" (if localp "-v" "-u"))
152 (vc-svn-parse-status localp))))
153
154 (defun vc-svn-workfile-version (file)
155 "SVN-specific version of `vc-workfile-version'."
156 ;; There is no need to consult RCS headers under SVN, because we
157 ;; get the workfile version for free when we recognize that a file
158 ;; is registered in SVN.
159 (vc-svn-registered file)
160 (vc-file-getprop file 'vc-workfile-version))
161
162 (defun vc-svn-checkout-model (file)
163 "SVN-specific version of `vc-checkout-model'."
164 ;; It looks like Subversion has no equivalent of CVSREAD.
165 'implicit)
166
167 ;; vc-svn-mode-line-string doesn't exist because the default implementation
168 ;; works just fine.
169
170 (defun vc-svn-dired-state-info (file)
171 "SVN-specific version of `vc-dired-state-info'."
172 (let ((svn-state (vc-state file)))
173 (cond ((eq svn-state 'edited)
174 (if (equal (vc-workfile-version file) "0")
175 "(added)" "(modified)"))
176 ((eq svn-state 'needs-patch) "(patch)")
177 ((eq svn-state 'needs-merge) "(merge)"))))
178
179
180 ;;;
181 ;;; State-changing functions
182 ;;;
183
184 (defun vc-svn-register (file &optional rev comment)
185 "Register FILE into the SVN version-control system.
186 COMMENT can be used to provide an initial description of FILE.
187
188 `vc-register-switches' and `vc-svn-register-switches' are passed to
189 the SVN command (in that order)."
190 (apply 'vc-svn-command nil 0 file "add" (vc-switches 'SVN 'register)))
191
192 (defun vc-svn-responsible-p (file)
193 "Return non-nil if SVN thinks it is responsible for FILE."
194 (file-directory-p (expand-file-name ".svn"
195 (if (file-directory-p file)
196 file
197 (file-name-directory file)))))
198
199 (defalias 'vc-svn-could-register 'vc-svn-responsible-p
200 "Return non-nil if FILE could be registered in SVN.
201 This is only possible if SVN is responsible for FILE's directory.")
202
203 (defun vc-svn-checkin (file rev comment)
204 "SVN-specific version of `vc-backend-checkin'."
205 (let ((status (apply
206 'vc-svn-command nil 1 file "ci"
207 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
208 (set-buffer "*vc*")
209 (goto-char (point-min))
210 (unless (equal status 0)
211 ;; Check checkin problem.
212 (cond
213 ((search-forward "Transaction is out of date" nil t)
214 (vc-file-setprop file 'vc-state 'needs-merge)
215 (error (substitute-command-keys
216 (concat "Up-to-date check failed: "
217 "type \\[vc-next-action] to merge in changes"))))
218 (t
219 (pop-to-buffer (current-buffer))
220 (goto-char (point-min))
221 (shrink-window-if-larger-than-buffer)
222 (error "Check-in failed"))))
223 ;; Update file properties
224 ;; (vc-file-setprop
225 ;; file 'vc-workfile-version
226 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
227 ))
228
229 (defun vc-svn-find-version (file rev buffer)
230 (apply 'vc-svn-command
231 buffer 0 file
232 "cat"
233 (and rev (not (string= rev ""))
234 (concat "-r" rev))
235 (vc-switches 'SVN 'checkout)))
236
237 (defun vc-svn-checkout (file &optional editable rev)
238 (message "Checking out %s..." file)
239 (with-current-buffer (or (get-file-buffer file) (current-buffer))
240 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
241 (vc-mode-line file)
242 (message "Checking out %s...done" file))
243
244 (defun vc-svn-update (file editable rev switches)
245 (if (and (file-exists-p file) (not rev))
246 ;; If no revision was specified, just make the file writable
247 ;; if necessary (using `svn-edit' if requested).
248 (and editable (not (eq (vc-svn-checkout-model file) 'implicit))
249 (if vc-svn-use-edit
250 (vc-svn-command nil 0 file "edit")
251 (set-file-modes file (logior (file-modes file) 128))
252 (if (equal file buffer-file-name) (toggle-read-only -1))))
253 ;; Check out a particular version (or recreate the file).
254 (vc-file-setprop file 'vc-workfile-version nil)
255 (apply 'vc-svn-command nil 0 file
256 "update"
257 ;; default for verbose checkout: clear the sticky tag so
258 ;; that the actual update will get the head of the trunk
259 (cond
260 ((null rev) "-rBASE")
261 ((or (eq rev t) (equal rev "")) nil)
262 (t (concat "-r" rev)))
263 switches)))
264
265 (defun vc-svn-delete-file (file)
266 (vc-svn-command nil 0 file "remove"))
267
268 (defun vc-svn-rename-file (old new)
269 (vc-svn-command nil 0 new "move" (file-relative-name old)))
270
271 (defun vc-svn-revert (file &optional contents-done)
272 "Revert FILE to the version it was based on."
273 (unless contents-done
274 (vc-svn-command nil 0 file "revert"))
275 (unless (eq (vc-checkout-model file) 'implicit)
276 (if vc-svn-use-edit
277 (vc-svn-command nil 0 file "unedit")
278 ;; Make the file read-only by switching off all w-bits
279 (set-file-modes file (logand (file-modes file) 3950)))))
280
281 (defun vc-svn-merge (file first-version &optional second-version)
282 "Merge changes into current working copy of FILE.
283 The changes are between FIRST-VERSION and SECOND-VERSION."
284 (vc-svn-command nil 0 file
285 "merge"
286 "-r" (if second-version
287 (concat first-version ":" second-version)
288 first-version))
289 (vc-file-setprop file 'vc-state 'edited)
290 (with-current-buffer (get-buffer "*vc*")
291 (goto-char (point-min))
292 (if (looking-at "C ")
293 1 ; signal conflict
294 0))) ; signal success
295
296 (defun vc-svn-merge-news (file)
297 "Merge in any new changes made to FILE."
298 (message "Merging changes into %s..." file)
299 ;; (vc-file-setprop file 'vc-workfile-version nil)
300 (vc-file-setprop file 'vc-checkout-time 0)
301 (vc-svn-command nil 0 file "update")
302 ;; Analyze the merge result reported by SVN, and set
303 ;; file properties accordingly.
304 (with-current-buffer (get-buffer "*vc*")
305 (goto-char (point-min))
306 ;; get new workfile version
307 (if (re-search-forward
308 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
309 (vc-file-setprop file 'vc-workfile-version (match-string 2))
310 (vc-file-setprop file 'vc-workfile-version nil))
311 ;; get file status
312 (goto-char (point-min))
313 (prog1
314 (if (looking-at "At revision")
315 0 ;; there were no news; indicate success
316 (if (re-search-forward
317 (concat "^\\([CGDU] \\)?"
318 (regexp-quote (file-name-nondirectory file)))
319 nil t)
320 (cond
321 ;; Merge successful, we are in sync with repository now
322 ((string= (match-string 1) "U ")
323 (vc-file-setprop file 'vc-state 'up-to-date)
324 (vc-file-setprop file 'vc-checkout-time
325 (nth 5 (file-attributes file)))
326 0);; indicate success to the caller
327 ;; Merge successful, but our own changes are still in the file
328 ((string= (match-string 1) "G ")
329 (vc-file-setprop file 'vc-state 'edited)
330 0);; indicate success to the caller
331 ;; Conflicts detected!
332 (t
333 (vc-file-setprop file 'vc-state 'edited)
334 1);; signal the error to the caller
335 )
336 (pop-to-buffer "*vc*")
337 (error "Couldn't analyze svn update result")))
338 (message "Merging changes into %s...done" file))))
339
340
341 ;;;
342 ;;; History functions
343 ;;;
344
345 (defun vc-svn-print-log (file &optional buffer)
346 "Get change log associated with FILE."
347 (save-current-buffer
348 (vc-setup-buffer buffer)
349 (let ((inhibit-read-only t))
350 (goto-char (point-min))
351 ;; Add a line to tell log-view-mode what file this is.
352 (insert "Working file: " (file-relative-name file) "\n"))
353 (vc-svn-command
354 buffer
355 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
356 file "log")))
357
358 (defun vc-svn-diff (file &optional oldvers newvers buffer)
359 "Get a difference report using SVN between two versions of FILE."
360 (unless buffer (setq buffer "*vc-diff*"))
361 (if (and oldvers (equal oldvers (vc-workfile-version file)))
362 ;; Use nil rather than the current revision because svn handles it
363 ;; better (i.e. locally).
364 (setq oldvers nil))
365 (if (string= (vc-workfile-version file) "0")
366 ;; This file is added but not yet committed; there is no master file.
367 (if (or oldvers newvers)
368 (error "No revisions of %s exist" file)
369 ;; We regard this as "changed".
370 ;; Diff it against /dev/null.
371 ;; Note: this is NOT a "svn diff".
372 (apply 'vc-do-command buffer
373 1 "diff" file
374 (append (vc-switches nil 'diff) '("/dev/null")))
375 ;; Even if it's empty, it's locally modified.
376 1)
377 (let* ((switches
378 (if vc-svn-diff-switches
379 (vc-switches 'SVN 'diff)
380 (list "-x" (mapconcat 'identity (vc-switches nil 'diff) " "))))
381 (async (and (not vc-disable-async-diff)
382 (vc-stay-local-p file)
383 (or oldvers newvers) ; Svn diffs those locally.
384 (fboundp 'start-process))))
385 (apply 'vc-svn-command buffer
386 (if async 'async 0)
387 file "diff"
388 (append
389 switches
390 (when oldvers
391 (list "-r" (if newvers (concat oldvers ":" newvers)
392 oldvers)))))
393 (if async 1 ; async diff => pessimistic assumption
394 ;; For some reason `svn diff' does not return a useful
395 ;; status w.r.t whether the diff was empty or not.
396 (buffer-size (get-buffer buffer))))))
397
398 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
399 "Diff all files at and below DIR."
400 (vc-svn-diff (file-name-as-directory dir) rev1 rev2))
401
402 ;;;
403 ;;; Snapshot system
404 ;;;
405
406 (defun vc-svn-create-snapshot (dir name branchp)
407 "Assign to DIR's current version a given NAME.
408 If BRANCHP is non-nil, the name is created as a branch (and the current
409 workspace is immediately moved to that new branch).
410 NAME is assumed to be a URL."
411 (vc-svn-command nil 0 dir "copy" name)
412 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
413
414 (defun vc-svn-retrieve-snapshot (dir name update)
415 "Retrieve a snapshot at and below DIR.
416 NAME is the name of the snapshot; if it is empty, do a `svn update'.
417 If UPDATE is non-nil, then update (resynch) any affected buffers.
418 NAME is assumed to be a URL."
419 (vc-svn-command nil 0 dir "switch" name)
420 ;; FIXME: parse the output and obey `update'.
421 )
422
423 ;;;
424 ;;; Miscellaneous
425 ;;;
426
427 ;; Subversion makes backups for us, so don't bother.
428 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
429 ;; "Return non-nil if version backups should be made for FILE.")
430
431 (defun vc-svn-check-headers ()
432 "Check if the current file has any headers in it."
433 (save-excursion
434 (goto-char (point-min))
435 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
436 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
437
438
439 ;;;
440 ;;; Internal functions
441 ;;;
442
443 (defun vc-svn-command (buffer okstatus file &rest flags)
444 "A wrapper around `vc-do-command' for use in vc-svn.el.
445 The difference to vc-do-command is that this function always invokes `svn',
446 and that it passes `vc-svn-global-switches' to it before FLAGS."
447 (apply 'vc-do-command buffer okstatus "svn" file
448 (if (stringp vc-svn-global-switches)
449 (cons vc-svn-global-switches flags)
450 (append vc-svn-global-switches
451 flags))))
452
453 (defun vc-svn-repository-hostname (dirname)
454 (with-temp-buffer
455 (let ((coding-system-for-read
456 (or file-name-coding-system
457 default-file-name-coding-system)))
458 (vc-insert-file (expand-file-name ".svn/entries" dirname)))
459 (goto-char (point-min))
460 (when (re-search-forward
461 ;; Old `svn' used name="svn:dir", newer use just name="".
462 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
463 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
464 "url=\"\\([^\"]+\\)\"") nil t)
465 ;; This is not a hostname but a URL. This may actually be considered
466 ;; as a feature since it allows vc-svn-stay-local to specify different
467 ;; behavior for different modules on the same server.
468 (match-string 1))))
469
470 (defun vc-svn-parse-status (localp)
471 "Parse output of \"svn status\" command in the current buffer.
472 Set file properties accordingly. Unless FULL is t, parse only
473 essential information."
474 (let (file status)
475 (goto-char (point-min))
476 (while (re-search-forward
477 "^[ ADMCI?!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
478 (setq file (expand-file-name
479 (buffer-substring (point) (line-end-position))))
480 (setq status (char-after (line-beginning-position)))
481 (unless (eq status ??)
482 (vc-file-setprop file 'vc-backend 'SVN)
483 ;; Use the last-modified revision, so that searching in vc-print-log
484 ;; output works.
485 (vc-file-setprop file 'vc-workfile-version (match-string 3))
486 (vc-file-setprop
487 file 'vc-state
488 (cond
489 ((eq status ?\ )
490 (if (eq (char-after (match-beginning 1)) ?*)
491 'needs-patch
492 (vc-file-setprop file 'vc-checkout-time
493 (nth 5 (file-attributes file)))
494 'up-to-date))
495 ((eq status ?A)
496 ;; If the file was actually copied, (match-string 2) is "-".
497 (vc-file-setprop file 'vc-workfile-version "0")
498 (vc-file-setprop file 'vc-checkout-time 0)
499 'edited)
500 ((memq status '(?M ?C))
501 (if (eq (char-after (match-beginning 1)) ?*)
502 'needs-merge
503 'edited))
504 (t 'edited)))))))
505
506 (defun vc-svn-dir-state-heuristic (dir)
507 "Find the SVN state of all files in DIR, using only local information."
508 (vc-svn-dir-state dir 'local))
509
510 (defun vc-svn-valid-symbolic-tag-name-p (tag)
511 "Return non-nil if TAG is a valid symbolic tag name."
512 ;; According to the SVN manual, a valid symbolic tag must start with
513 ;; an uppercase or lowercase letter and can contain uppercase and
514 ;; lowercase letters, digits, `-', and `_'.
515 (and (string-match "^[a-zA-Z]" tag)
516 (not (string-match "[^a-z0-9A-Z-_]" tag))))
517
518 (defun vc-svn-valid-version-number-p (tag)
519 "Return non-nil if TAG is a valid version number."
520 (and (string-match "^[0-9]" tag)
521 (not (string-match "[^0-9]" tag))))
522
523 ;; Support for `svn annotate'
524
525 (defun vc-svn-annotate-command (file buf &optional rev)
526 (vc-svn-command buf 0 file "annotate" (if rev (concat "-r" rev))))
527
528 (defun vc-svn-annotate-time-of-rev (rev)
529 ;; Arbitrarily assume 10 commmits per day.
530 (/ (string-to-number rev) 10.0))
531
532 (defun vc-svn-annotate-current-time ()
533 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
534
535 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
536
537 (defun vc-svn-annotate-time ()
538 (when (looking-at vc-svn-annotate-re)
539 (goto-char (match-end 0))
540 (vc-svn-annotate-time-of-rev (match-string 1))))
541
542 (defun vc-svn-annotate-extract-revision-at-line ()
543 (save-excursion
544 (beginning-of-line)
545 (if (looking-at vc-svn-annotate-re) (match-string 1))))
546
547 (provide 'vc-svn)
548
549 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
550 ;;; vc-svn.el ends here