Merge from gnus--rel--5.10
[emacs.git] / lisp / vc-svn.el
blob916930ffde2774d588b36b6e0709f8cee75ac595
1 ;;; vc-svn.el --- non-resident support for Subversion version-control
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
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.
31 ;; Sync'd with Subversion's vc-svn.el as of revision 5801.
33 ;;; Bugs:
35 ;; - VC-dired is (really) slow.
37 ;;; Code:
39 (eval-when-compile
40 (require 'vc))
42 ;;;
43 ;;; Customization options
44 ;;;
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)
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)
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)
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)
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
98 (defvar vc-svn-admin-directory
99 (cond ((and (eq system-type 'windows-nt)
100 (getenv "SVN_ASP_DOT_NET_HACK"))
101 "_svn")
102 (t ".svn"))
103 "The name of the \".svn\" subdirectory or its equivalent.")
106 ;;; State-querying functions
109 ;;; vc-svn-admin-directory is generally not defined when the
110 ;;; autoloaded function is called.
112 ;;;###autoload (defun vc-svn-registered (f)
113 ;;;###autoload (let ((admin-dir (cond ((and (eq system-type 'windows-nt)
114 ;;;###autoload (getenv "SVN_ASP_DOT_NET_HACK"))
115 ;;;###autoload "_svn")
116 ;;;###autoload (t ".svn"))))
117 ;;;###autoload (when (file-readable-p (expand-file-name
118 ;;;###autoload (concat admin-dir "/entries")
119 ;;;###autoload (file-name-directory f)))
120 ;;;###autoload (load "vc-svn")
121 ;;;###autoload (vc-svn-registered f))))
123 ;;;###autoload
124 (add-to-list 'completion-ignored-extensions ".svn/")
126 (defun vc-svn-registered (file)
127 "Check if FILE is SVN registered."
128 (when (file-readable-p (expand-file-name (concat vc-svn-admin-directory
129 "/entries")
130 (file-name-directory file)))
131 (with-temp-buffer
132 (cd (file-name-directory file))
133 (let ((status
134 (condition-case nil
135 ;; Ignore all errors.
136 (vc-svn-command t t file "status" "-v")
137 ;; Some problem happened. E.g. We can't find an `svn'
138 ;; executable. We used to only catch `file-error' but when
139 ;; the process is run on a remote host via Tramp, the error
140 ;; is only reported via the exit status which is turned into
141 ;; an `error' by vc-do-command.
142 (error nil))))
143 (when (eq 0 status)
144 (vc-svn-parse-status file))))))
146 (defun vc-svn-state (file &optional localp)
147 "SVN-specific version of `vc-state'."
148 (setq localp (or localp (vc-stay-local-p file)))
149 (with-temp-buffer
150 (cd (file-name-directory file))
151 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
152 (vc-svn-parse-status file)))
154 (defun vc-svn-state-heuristic (file)
155 "SVN-specific state heuristic."
156 (vc-svn-state file 'local))
158 (defun vc-svn-dir-state (dir &optional localp)
159 "Find the SVN state of all files in DIR."
160 (setq localp (or localp (vc-stay-local-p dir)))
161 (let ((default-directory dir))
162 ;; Don't specify DIR in this command, the default-directory is
163 ;; enough. Otherwise it might fail with remote repositories.
164 (with-temp-buffer
165 (vc-svn-command t 0 nil "status" (if localp "-v" "-u"))
166 (vc-svn-parse-status))))
168 (defun vc-svn-workfile-version (file)
169 "SVN-specific version of `vc-workfile-version'."
170 ;; There is no need to consult RCS headers under SVN, because we
171 ;; get the workfile version for free when we recognize that a file
172 ;; is registered in SVN.
173 (vc-svn-registered file)
174 (vc-file-getprop file 'vc-workfile-version))
176 (defun vc-svn-checkout-model (file)
177 "SVN-specific version of `vc-checkout-model'."
178 ;; It looks like Subversion has no equivalent of CVSREAD.
179 'implicit)
181 ;; vc-svn-mode-line-string doesn't exist because the default implementation
182 ;; works just fine.
184 (defun vc-svn-dired-state-info (file)
185 "SVN-specific version of `vc-dired-state-info'."
186 (let ((svn-state (vc-state file)))
187 (cond ((eq svn-state 'edited)
188 (if (equal (vc-workfile-version file) "0")
189 "(added)" "(modified)"))
190 ((eq svn-state 'needs-patch) "(patch)")
191 ((eq svn-state 'needs-merge) "(merge)"))))
193 (defun vc-svn-previous-version (file rev)
194 (let ((newrev (1- (string-to-number rev))))
195 (when (< 0 newrev)
196 (number-to-string newrev))))
198 (defun vc-svn-next-version (file rev)
199 (let ((newrev (1+ (string-to-number rev))))
200 ;; The "workfile version" is an uneasy conceptual fit under Subversion;
201 ;; we use it as the upper bound until a better idea comes along. If the
202 ;; workfile version W coincides with the tree's latest revision R, then
203 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
204 ;; inhibits showing of W+1 through R, which could be considered anywhere
205 ;; from gracious to impolite.
206 (unless (< (string-to-number (vc-file-getprop file 'vc-workfile-version))
207 newrev)
208 (number-to-string newrev))))
212 ;;; State-changing functions
215 (defun vc-svn-register (file &optional rev comment)
216 "Register FILE into the SVN version-control system.
217 COMMENT can be used to provide an initial description of FILE.
219 `vc-register-switches' and `vc-svn-register-switches' are passed to
220 the SVN command (in that order)."
221 (apply 'vc-svn-command nil 0 file "add" (vc-switches 'SVN 'register)))
223 (defun vc-svn-responsible-p (file)
224 "Return non-nil if SVN thinks it is responsible for FILE."
225 (file-directory-p (expand-file-name vc-svn-admin-directory
226 (if (file-directory-p file)
227 file
228 (file-name-directory file)))))
230 (defalias 'vc-svn-could-register 'vc-svn-responsible-p
231 "Return non-nil if FILE could be registered in SVN.
232 This is only possible if SVN is responsible for FILE's directory.")
234 (defun vc-svn-checkin (file rev comment)
235 "SVN-specific version of `vc-backend-checkin'."
236 (let ((status (apply
237 'vc-svn-command nil 1 file "ci"
238 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
239 (set-buffer "*vc*")
240 (goto-char (point-min))
241 (unless (equal status 0)
242 ;; Check checkin problem.
243 (cond
244 ((search-forward "Transaction is out of date" nil t)
245 (vc-file-setprop file 'vc-state 'needs-merge)
246 (error (substitute-command-keys
247 (concat "Up-to-date check failed: "
248 "type \\[vc-next-action] to merge in changes"))))
250 (pop-to-buffer (current-buffer))
251 (goto-char (point-min))
252 (shrink-window-if-larger-than-buffer)
253 (error "Check-in failed"))))
254 ;; Update file properties
255 ;; (vc-file-setprop
256 ;; file 'vc-workfile-version
257 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
260 (defun vc-svn-find-version (file rev buffer)
261 (apply 'vc-svn-command
262 buffer 0 file
263 "cat"
264 (and rev (not (string= rev ""))
265 (concat "-r" rev))
266 (vc-switches 'SVN 'checkout)))
268 (defun vc-svn-checkout (file &optional editable rev)
269 (message "Checking out %s..." file)
270 (with-current-buffer (or (get-file-buffer file) (current-buffer))
271 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
272 (vc-mode-line file)
273 (message "Checking out %s...done" file))
275 (defun vc-svn-update (file editable rev switches)
276 (if (and (file-exists-p file) (not rev))
277 ;; If no revision was specified, just make the file writable
278 ;; if necessary (using `svn-edit' if requested).
279 (and editable (not (eq (vc-svn-checkout-model file) 'implicit))
280 (if vc-svn-use-edit
281 (vc-svn-command nil 0 file "edit")
282 (set-file-modes file (logior (file-modes file) 128))
283 (if (equal file buffer-file-name) (toggle-read-only -1))))
284 ;; Check out a particular version (or recreate the file).
285 (vc-file-setprop file 'vc-workfile-version nil)
286 (apply 'vc-svn-command nil 0 file
287 "update"
288 ;; default for verbose checkout: clear the sticky tag so
289 ;; that the actual update will get the head of the trunk
290 (cond
291 ((null rev) "-rBASE")
292 ((or (eq rev t) (equal rev "")) nil)
293 (t (concat "-r" rev)))
294 switches)))
296 (defun vc-svn-delete-file (file)
297 (vc-svn-command nil 0 file "remove"))
299 (defun vc-svn-rename-file (old new)
300 (vc-svn-command nil 0 new "move" (file-relative-name old)))
302 (defun vc-svn-revert (file &optional contents-done)
303 "Revert FILE to the version it was based on."
304 (unless contents-done
305 (vc-svn-command nil 0 file "revert"))
306 (unless (eq (vc-checkout-model file) 'implicit)
307 (if vc-svn-use-edit
308 (vc-svn-command nil 0 file "unedit")
309 ;; Make the file read-only by switching off all w-bits
310 (set-file-modes file (logand (file-modes file) 3950)))))
312 (defun vc-svn-merge (file first-version &optional second-version)
313 "Merge changes into current working copy of FILE.
314 The changes are between FIRST-VERSION and SECOND-VERSION."
315 (vc-svn-command nil 0 file
316 "merge"
317 "-r" (if second-version
318 (concat first-version ":" second-version)
319 first-version))
320 (vc-file-setprop file 'vc-state 'edited)
321 (with-current-buffer (get-buffer "*vc*")
322 (goto-char (point-min))
323 (if (looking-at "C ")
324 1 ; signal conflict
325 0))) ; signal success
327 (defun vc-svn-merge-news (file)
328 "Merge in any new changes made to FILE."
329 (message "Merging changes into %s..." file)
330 ;; (vc-file-setprop file 'vc-workfile-version nil)
331 (vc-file-setprop file 'vc-checkout-time 0)
332 (vc-svn-command nil 0 file "update")
333 ;; Analyze the merge result reported by SVN, and set
334 ;; file properties accordingly.
335 (with-current-buffer (get-buffer "*vc*")
336 (goto-char (point-min))
337 ;; get new workfile version
338 (if (re-search-forward
339 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
340 (vc-file-setprop file 'vc-workfile-version (match-string 2))
341 (vc-file-setprop file 'vc-workfile-version nil))
342 ;; get file status
343 (goto-char (point-min))
344 (prog1
345 (if (looking-at "At revision")
346 0 ;; there were no news; indicate success
347 (if (re-search-forward
348 (concat "^\\([CGDU] \\)?"
349 (regexp-quote (file-name-nondirectory file)))
350 nil t)
351 (cond
352 ;; Merge successful, we are in sync with repository now
353 ((string= (match-string 1) "U ")
354 (vc-file-setprop file 'vc-state 'up-to-date)
355 (vc-file-setprop file 'vc-checkout-time
356 (nth 5 (file-attributes file)))
357 0);; indicate success to the caller
358 ;; Merge successful, but our own changes are still in the file
359 ((string= (match-string 1) "G ")
360 (vc-file-setprop file 'vc-state 'edited)
361 0);; indicate success to the caller
362 ;; Conflicts detected!
364 (vc-file-setprop file 'vc-state 'edited)
365 1);; signal the error to the caller
367 (pop-to-buffer "*vc*")
368 (error "Couldn't analyze svn update result")))
369 (message "Merging changes into %s...done" file))))
373 ;;; History functions
376 (defun vc-svn-print-log (file &optional buffer)
377 "Get change log associated with FILE."
378 (save-current-buffer
379 (vc-setup-buffer buffer)
380 (let ((inhibit-read-only t))
381 (goto-char (point-min))
382 ;; Add a line to tell log-view-mode what file this is.
383 (insert "Working file: " (file-relative-name file) "\n"))
384 (vc-svn-command
385 buffer
386 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
387 file "log"
388 ;; By default Subversion only shows the log upto the working version,
389 ;; whereas we also want the log of the subsequent commits. At least
390 ;; that's what the vc-cvs.el code does.
391 "-rHEAD:0")))
393 (defun vc-svn-diff (file &optional oldvers newvers buffer)
394 "Get a difference report using SVN between two versions of FILE."
395 (unless buffer (setq buffer "*vc-diff*"))
396 (if (and oldvers (equal oldvers (vc-workfile-version file)))
397 ;; Use nil rather than the current revision because svn handles it
398 ;; better (i.e. locally).
399 (setq oldvers nil))
400 (if (string= (vc-workfile-version file) "0")
401 ;; This file is added but not yet committed; there is no master file.
402 (if (or oldvers newvers)
403 (error "No revisions of %s exist" file)
404 ;; We regard this as "changed".
405 ;; Diff it against /dev/null.
406 ;; Note: this is NOT a "svn diff".
407 (apply 'vc-do-command buffer
408 1 "diff" file
409 (append (vc-switches nil 'diff) '("/dev/null")))
410 ;; Even if it's empty, it's locally modified.
412 (let* ((switches
413 (if vc-svn-diff-switches
414 (vc-switches 'SVN 'diff)
415 (list "-x" (mapconcat 'identity (vc-switches nil 'diff) " "))))
416 (async (and (not vc-disable-async-diff)
417 (vc-stay-local-p file)
418 (or oldvers newvers) ; Svn diffs those locally.
419 (fboundp 'start-process))))
420 (apply 'vc-svn-command buffer
421 (if async 'async 0)
422 file "diff"
423 (append
424 switches
425 (when oldvers
426 (list "-r" (if newvers (concat oldvers ":" newvers)
427 oldvers)))))
428 (if async 1 ; async diff => pessimistic assumption
429 ;; For some reason `svn diff' does not return a useful
430 ;; status w.r.t whether the diff was empty or not.
431 (buffer-size (get-buffer buffer))))))
433 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
434 "Diff all files at and below DIR."
435 (vc-svn-diff (file-name-as-directory dir) rev1 rev2))
438 ;;; Snapshot system
441 (defun vc-svn-create-snapshot (dir name branchp)
442 "Assign to DIR's current version a given NAME.
443 If BRANCHP is non-nil, the name is created as a branch (and the current
444 workspace is immediately moved to that new branch).
445 NAME is assumed to be a URL."
446 (vc-svn-command nil 0 dir "copy" name)
447 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
449 (defun vc-svn-retrieve-snapshot (dir name update)
450 "Retrieve a snapshot at and below DIR.
451 NAME is the name of the snapshot; if it is empty, do a `svn update'.
452 If UPDATE is non-nil, then update (resynch) any affected buffers.
453 NAME is assumed to be a URL."
454 (vc-svn-command nil 0 dir "switch" name)
455 ;; FIXME: parse the output and obey `update'.
459 ;;; Miscellaneous
462 ;; Subversion makes backups for us, so don't bother.
463 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
464 ;; "Return non-nil if version backups should be made for FILE.")
466 (defun vc-svn-check-headers ()
467 "Check if the current file has any headers in it."
468 (save-excursion
469 (goto-char (point-min))
470 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
471 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
475 ;;; Internal functions
478 (defun vc-svn-command (buffer okstatus file &rest flags)
479 "A wrapper around `vc-do-command' for use in vc-svn.el.
480 The difference to vc-do-command is that this function always invokes `svn',
481 and that it passes `vc-svn-global-switches' to it before FLAGS."
482 (apply 'vc-do-command buffer okstatus "svn" file
483 (if (stringp vc-svn-global-switches)
484 (cons vc-svn-global-switches flags)
485 (append vc-svn-global-switches
486 flags))))
488 (defun vc-svn-repository-hostname (dirname)
489 (with-temp-buffer
490 (let ((coding-system-for-read
491 (or file-name-coding-system
492 default-file-name-coding-system)))
493 (vc-insert-file (expand-file-name (concat vc-svn-admin-directory
494 "/entries")
495 dirname)))
496 (goto-char (point-min))
497 (when (re-search-forward
498 ;; Old `svn' used name="svn:this_dir", newer use just name="".
499 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
500 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
501 "url=\"\\([^\"]+\\)\"") nil t)
502 ;; This is not a hostname but a URL. This may actually be considered
503 ;; as a feature since it allows vc-svn-stay-local to specify different
504 ;; behavior for different modules on the same server.
505 (match-string 1))))
507 (defun vc-svn-parse-status (&optional filename)
508 "Parse output of \"svn status\" command in the current buffer.
509 Set file properties accordingly. Unless FILENAME is non-nil, parse only
510 information about FILENAME and return its status."
511 (let (file status)
512 (goto-char (point-min))
513 (while (re-search-forward
514 ;; Ignore the files with status in [IX?].
515 "^[ ACDGMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
516 ;; If the username contains spaces, the output format is ambiguous,
517 ;; so don't trust the output's filename unless we have to.
518 (setq file (or filename
519 (expand-file-name
520 (buffer-substring (point) (line-end-position)))))
521 (setq status (char-after (line-beginning-position)))
522 (unless (eq status ??)
523 ;; `vc-BACKEND-registered' must not set vc-backend,
524 ;; which is instead set in vc-registered.
525 (unless filename (vc-file-setprop file 'vc-backend 'SVN))
526 ;; Use the last-modified revision, so that searching in vc-print-log
527 ;; output works.
528 (vc-file-setprop file 'vc-workfile-version (match-string 3))
529 (vc-file-setprop
530 file 'vc-state
531 (cond
532 ((eq status ?\ )
533 (if (eq (char-after (match-beginning 1)) ?*)
534 'needs-patch
535 (vc-file-setprop file 'vc-checkout-time
536 (nth 5 (file-attributes file)))
537 'up-to-date))
538 ((eq status ?A)
539 ;; If the file was actually copied, (match-string 2) is "-".
540 (vc-file-setprop file 'vc-workfile-version "0")
541 (vc-file-setprop file 'vc-checkout-time 0)
542 'edited)
543 ((memq status '(?M ?C))
544 (if (eq (char-after (match-beginning 1)) ?*)
545 'needs-merge
546 'edited))
547 (t 'edited)))))
548 (if filename (vc-file-getprop filename 'vc-state))))
550 (defun vc-svn-dir-state-heuristic (dir)
551 "Find the SVN state of all files in DIR, using only local information."
552 (vc-svn-dir-state dir 'local))
554 (defun vc-svn-valid-symbolic-tag-name-p (tag)
555 "Return non-nil if TAG is a valid symbolic tag name."
556 ;; According to the SVN manual, a valid symbolic tag must start with
557 ;; an uppercase or lowercase letter and can contain uppercase and
558 ;; lowercase letters, digits, `-', and `_'.
559 (and (string-match "^[a-zA-Z]" tag)
560 (not (string-match "[^a-z0-9A-Z-_]" tag))))
562 (defun vc-svn-valid-version-number-p (tag)
563 "Return non-nil if TAG is a valid version number."
564 (and (string-match "^[0-9]" tag)
565 (not (string-match "[^0-9]" tag))))
567 ;; Support for `svn annotate'
569 (defun vc-svn-annotate-command (file buf &optional rev)
570 (vc-svn-command buf 0 file "annotate" (if rev (concat "-r" rev))))
572 (defun vc-svn-annotate-time-of-rev (rev)
573 ;; Arbitrarily assume 10 commmits per day.
574 (/ (string-to-number rev) 10.0))
576 (defun vc-svn-annotate-current-time ()
577 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
579 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
581 (defun vc-svn-annotate-time ()
582 (when (looking-at vc-svn-annotate-re)
583 (goto-char (match-end 0))
584 (vc-svn-annotate-time-of-rev (match-string 1))))
586 (defun vc-svn-annotate-extract-revision-at-line ()
587 (save-excursion
588 (beginning-of-line)
589 (if (looking-at vc-svn-annotate-re) (match-string 1))))
591 (provide 'vc-svn)
593 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
594 ;;; vc-svn.el ends here