Add arch tagline
[emacs.git] / lisp / vc-svn.el
blobc66461ff3a386f8b8349988e8cb2df40cd115d57
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 3, 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 ;; We want to autoload it for use by the autoloaded version of
89 ;; vc-svn-registered, but we want the value to be compiled at startup, not
90 ;; at dump time.
91 ;; ;;;###autoload
92 (defconst vc-svn-admin-directory
93 (cond ((and (memq system-type '(cygwin windows-nt ms-dos))
94 (getenv "SVN_ASP_DOT_NET_HACK"))
95 "_svn")
96 (t ".svn"))
97 "The name of the \".svn\" subdirectory or its equivalent.")
99 ;;; Properties of the backend
101 (defun vc-svn-revision-granularity ()
102 'repository)
104 ;;; State-querying functions
107 ;;; vc-svn-admin-directory is generally not defined when the
108 ;;; autoloaded function is called.
110 ;;;###autoload (defun vc-svn-registered (f)
111 ;;;###autoload (let ((admin-dir (cond ((and (eq system-type 'windows-nt)
112 ;;;###autoload (getenv "SVN_ASP_DOT_NET_HACK"))
113 ;;;###autoload "_svn")
114 ;;;###autoload (t ".svn"))))
115 ;;;###autoload (when (file-readable-p (expand-file-name
116 ;;;###autoload (concat admin-dir "/entries")
117 ;;;###autoload (file-name-directory f)))
118 ;;;###autoload (load "vc-svn")
119 ;;;###autoload (vc-svn-registered f))))
121 ;;;###autoload
122 (add-to-list 'completion-ignored-extensions ".svn/")
124 (defun vc-svn-registered (file)
125 "Check if FILE is SVN registered."
126 (when (file-readable-p (expand-file-name (concat vc-svn-admin-directory
127 "/entries")
128 (file-name-directory file)))
129 (with-temp-buffer
130 (cd (file-name-directory file))
131 (let ((status
132 (condition-case nil
133 ;; Ignore all errors.
134 (vc-svn-command t t file "status" "-v")
135 ;; Some problem happened. E.g. We can't find an `svn'
136 ;; executable. We used to only catch `file-error' but when
137 ;; the process is run on a remote host via Tramp, the error
138 ;; is only reported via the exit status which is turned into
139 ;; an `error' by vc-do-command.
140 (error nil))))
141 (when (eq 0 status)
142 (vc-svn-parse-status file))))))
144 (defun vc-svn-state (file &optional localp)
145 "SVN-specific version of `vc-state'."
146 (setq localp (or localp (vc-stay-local-p file)))
147 (with-temp-buffer
148 (cd (file-name-directory file))
149 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
150 (vc-svn-parse-status file)))
152 (defun vc-svn-state-heuristic (file)
153 "SVN-specific state heuristic."
154 (vc-svn-state file 'local))
156 (defun vc-svn-dir-state (dir &optional localp)
157 "Find the SVN state of all files in DIR."
158 (setq localp (or localp (vc-stay-local-p dir)))
159 (let ((default-directory dir))
160 ;; Don't specify DIR in this command, the default-directory is
161 ;; enough. Otherwise it might fail with remote repositories.
162 (with-temp-buffer
163 (vc-svn-command t 0 nil "status" (if localp "-v" "-u"))
164 (vc-svn-parse-status))))
166 (defun vc-svn-workfile-version (file)
167 "SVN-specific version of `vc-workfile-version'."
168 ;; There is no need to consult RCS headers under SVN, because we
169 ;; get the workfile version for free when we recognize that a file
170 ;; is registered in SVN.
171 (vc-svn-registered file)
172 (vc-file-getprop file 'vc-workfile-version))
174 (defun vc-svn-checkout-model (file)
175 "SVN-specific version of `vc-checkout-model'."
176 ;; It looks like Subversion has no equivalent of CVSREAD.
177 'implicit)
179 ;; vc-svn-mode-line-string doesn't exist because the default implementation
180 ;; works just fine.
182 (defun vc-svn-dired-state-info (file)
183 "SVN-specific version of `vc-dired-state-info'."
184 (let ((svn-state (vc-state file)))
185 (cond ((eq svn-state 'edited)
186 (if (equal (vc-workfile-version file) "0")
187 "(added)" "(modified)"))
188 ((eq svn-state 'needs-patch) "(patch)")
189 ((eq svn-state 'needs-merge) "(merge)"))))
191 (defun vc-svn-previous-version (file rev)
192 (let ((newrev (1- (string-to-number rev))))
193 (when (< 0 newrev)
194 (number-to-string newrev))))
196 (defun vc-svn-next-version (file rev)
197 (let ((newrev (1+ (string-to-number rev))))
198 ;; The "workfile version" is an uneasy conceptual fit under Subversion;
199 ;; we use it as the upper bound until a better idea comes along. If the
200 ;; workfile version W coincides with the tree's latest revision R, then
201 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
202 ;; inhibits showing of W+1 through R, which could be considered anywhere
203 ;; from gracious to impolite.
204 (unless (< (string-to-number (vc-file-getprop file 'vc-workfile-version))
205 newrev)
206 (number-to-string newrev))))
210 ;;; State-changing functions
213 (defun vc-svn-create-repo ()
214 "Create a new SVN repository."
215 (vc-do-command nil 0 "svnadmin" '("create" "SVN"))
216 (vc-do-command nil 0 "svn" '(".")
217 "checkout" (concat "file://" default-directory "SVN")))
219 (defun vc-svn-register (files &optional rev comment)
220 "Register FILES into the SVN version-control system.
221 The COMMENT argument is ignored This does an add but not a commit.
223 `vc-register-switches' and `vc-svn-register-switches' are passed to
224 the SVN command (in that order)."
225 (apply 'vc-svn-command nil 0 files "add" (vc-switches 'SVN 'register)))
227 (defun vc-svn-responsible-p (file)
228 "Return non-nil if SVN thinks it is responsible for FILE."
229 (file-directory-p (expand-file-name vc-svn-admin-directory
230 (if (file-directory-p file)
231 file
232 (file-name-directory file)))))
234 (defalias 'vc-svn-could-register 'vc-svn-responsible-p
235 "Return non-nil if FILE could be registered in SVN.
236 This is only possible if SVN is responsible for FILE's directory.")
238 (defun vc-svn-checkin (files rev comment)
239 "SVN-specific version of `vc-backend-checkin'."
240 (if rev (error "Committing to a specific revision is unsupported in SVN."))
241 (let ((status (apply
242 'vc-svn-command nil 1 files "ci"
243 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
244 (set-buffer "*vc*")
245 (goto-char (point-min))
246 (unless (equal status 0)
247 ;; Check checkin problem.
248 (cond
249 ((search-forward "Transaction is out of date" nil t)
250 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
251 files)
252 (error (substitute-command-keys
253 (concat "Up-to-date check failed: "
254 "type \\[vc-next-action] to merge in changes"))))
256 (pop-to-buffer (current-buffer))
257 (goto-char (point-min))
258 (shrink-window-if-larger-than-buffer)
259 (error "Check-in failed"))))
260 ;; Update file properties
261 ;; (vc-file-setprop
262 ;; file 'vc-workfile-version
263 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
266 (defun vc-svn-find-version (file rev buffer)
267 "SVN-specific retrieval of a specified version into a buffer."
268 (apply 'vc-svn-command
269 buffer 0 file
270 "cat"
271 (and rev (not (string= rev ""))
272 (concat "-r" rev))
273 (vc-switches 'SVN 'checkout)))
275 (defun vc-svn-checkout (file &optional editable rev)
276 (message "Checking out %s..." file)
277 (with-current-buffer (or (get-file-buffer file) (current-buffer))
278 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
279 (vc-mode-line file)
280 (message "Checking out %s...done" file))
282 (defun vc-svn-update (file editable rev switches)
283 (if (and (file-exists-p file) (not rev))
284 ;; If no revision was specified, there's nothing to do.
286 ;; Check out a particular version (or recreate the file).
287 (vc-file-setprop file 'vc-workfile-version nil)
288 (apply 'vc-svn-command nil 0 file
289 "update"
290 ;; default for verbose checkout: clear the sticky tag so
291 ;; that the actual update will get the head of the trunk
292 (cond
293 ((null rev) "-rBASE")
294 ((or (eq rev t) (equal rev "")) nil)
295 (t (concat "-r" rev)))
296 switches)))
298 (defun vc-svn-delete-file (file)
299 (vc-svn-command nil 0 file "remove"))
301 (defun vc-svn-rename-file (old new)
302 (vc-svn-command nil 0 new "move" (file-relative-name old)))
304 (defun vc-svn-revert (file &optional contents-done)
305 "Revert FILE to the version it was based on."
306 (unless contents-done
307 (vc-svn-command nil 0 file "revert")))
309 (defun vc-svn-merge (file first-version &optional second-version)
310 "Merge changes into current working copy of FILE.
311 The changes are between FIRST-VERSION and SECOND-VERSION."
312 (vc-svn-command nil 0 file
313 "merge"
314 "-r" (if second-version
315 (concat first-version ":" second-version)
316 first-version))
317 (vc-file-setprop file 'vc-state 'edited)
318 (with-current-buffer (get-buffer "*vc*")
319 (goto-char (point-min))
320 (if (looking-at "C ")
321 1 ; signal conflict
322 0))) ; signal success
324 (defun vc-svn-merge-news (file)
325 "Merge in any new changes made to FILE."
326 (message "Merging changes into %s..." file)
327 ;; (vc-file-setprop file 'vc-workfile-version nil)
328 (vc-file-setprop file 'vc-checkout-time 0)
329 (vc-svn-command nil 0 file "update")
330 ;; Analyze the merge result reported by SVN, and set
331 ;; file properties accordingly.
332 (with-current-buffer (get-buffer "*vc*")
333 (goto-char (point-min))
334 ;; get new workfile version
335 (if (re-search-forward
336 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
337 (vc-file-setprop file 'vc-workfile-version (match-string 2))
338 (vc-file-setprop file 'vc-workfile-version nil))
339 ;; get file status
340 (goto-char (point-min))
341 (prog1
342 (if (looking-at "At revision")
343 0 ;; there were no news; indicate success
344 (if (re-search-forward
345 ;; Newer SVN clients have 3 columns of chars (one for the
346 ;; file's contents, then second for its properties, and the
347 ;; third for lock-grabbing info), before the 2 spaces.
348 ;; We also used to match the filename in column 0 without any
349 ;; meta-info before it, but I believe this can never happen.
350 (concat "^\\(\\([ACGDU]\\)\\(.[B ]\\)? \\)"
351 (regexp-quote (file-name-nondirectory file)))
352 nil t)
353 (cond
354 ;; Merge successful, we are in sync with repository now
355 ((string= (match-string 2) "U")
356 (vc-file-setprop file 'vc-state 'up-to-date)
357 (vc-file-setprop file 'vc-checkout-time
358 (nth 5 (file-attributes file)))
359 0);; indicate success to the caller
360 ;; Merge successful, but our own changes are still in the file
361 ((string= (match-string 2) "G")
362 (vc-file-setprop file 'vc-state 'edited)
363 0);; indicate success to the caller
364 ;; Conflicts detected!
366 (vc-file-setprop file 'vc-state 'edited)
367 1);; signal the error to the caller
369 (pop-to-buffer "*vc*")
370 (error "Couldn't analyze svn update result")))
371 (message "Merging changes into %s...done" file))))
375 ;;; History functions
378 (defun vc-svn-print-log (files &optional buffer)
379 "Get change log(s) associated with FILES."
380 (save-current-buffer
381 (vc-setup-buffer buffer)
382 (let ((inhibit-read-only t))
383 (goto-char (point-min))
384 ;; Add a line to tell log-view-mode what file this is.
385 ;; FIXME if there are multiple files, log-view-current-file
386 ;; breaks. It's trivial to adapt log-view-file-re for the
387 ;; changed prefix, but less trivial to make
388 ;; log-view-current-file actually do the right thing in the
389 ;; multiple file case.
390 (insert (format "Working file%s: "
391 (if (= (length files) 1)
393 "s"))
394 (vc-delistify (mapcar 'file-relative-name files)) "\n"))
395 (vc-svn-command
396 buffer
397 (if (and (= (length files) 1) (vc-stay-local-p (car files)) (fboundp 'start-process)) 'async 0)
398 files "log"
399 ;; By default Subversion only shows the log upto the working version,
400 ;; whereas we also want the log of the subsequent commits. At least
401 ;; that's what the vc-cvs.el code does.
402 "-rHEAD:0")))
404 (defun vc-svn-wash-log ()
405 "Remove all non-comment information from log output."
406 ;; FIXME: not implemented for SVN
407 nil)
409 (defun vc-svn-diff (files &optional oldvers newvers buffer)
410 "Get a difference report using SVN between two versions of fileset FILES."
411 (and oldvers
412 (catch 'no
413 (dolist (f files)
414 (or (equal oldvers (vc-workfile-version f))
415 (throw 'no nil)))
417 ;; Use nil rather than the current revision because svn handles
418 ;; it better (i.e. locally). Note that if _any_ of the files
419 ;; has a different revision, we fetch the lot, which is
420 ;; obviously sub-optimal.
421 (setq oldvers nil))
422 (let* ((switches
423 (if vc-svn-diff-switches
424 (vc-switches 'SVN 'diff)
425 (list "-x" (mapconcat 'identity (vc-switches nil 'diff) " "))))
426 (async (and (not vc-disable-async-diff)
427 (vc-stay-local-p files)
428 (or oldvers newvers) ; Svn diffs those locally.
429 (fboundp 'start-process))))
430 (apply 'vc-svn-command buffer
431 (if async 'async 0)
432 files "diff"
433 (append
434 switches
435 (when oldvers
436 (list "-r" (if newvers (concat oldvers ":" newvers)
437 oldvers)))))
438 (if async 1 ; async diff => pessimistic assumption
439 ;; For some reason `svn diff' does not return a useful
440 ;; status w.r.t whether the diff was empty or not.
441 (buffer-size (get-buffer buffer)))))
443 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
444 "Diff all files at and below DIR."
445 (vc-svn-diff (list (file-name-as-directory dir)) rev1 rev2))
448 ;;; Snapshot system
451 (defun vc-svn-create-snapshot (dir name branchp)
452 "Assign to DIR's current version a given NAME.
453 If BRANCHP is non-nil, the name is created as a branch (and the current
454 workspace is immediately moved to that new branch).
455 NAME is assumed to be a URL."
456 (vc-svn-command nil 0 dir "copy" name)
457 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
459 (defun vc-svn-retrieve-snapshot (dir name update)
460 "Retrieve a snapshot at and below DIR.
461 NAME is the name of the snapshot; if it is empty, do a `svn update'.
462 If UPDATE is non-nil, then update (resynch) any affected buffers.
463 NAME is assumed to be a URL."
464 (vc-svn-command nil 0 dir "switch" name)
465 ;; FIXME: parse the output and obey `update'.
469 ;;; Miscellaneous
472 ;; Subversion makes backups for us, so don't bother.
473 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
474 ;; "Return non-nil if version backups should be made for FILE.")
476 (defun vc-svn-check-headers ()
477 "Check if the current file has any headers in it."
478 (save-excursion
479 (goto-char (point-min))
480 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
481 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
485 ;;; Internal functions
488 (defcustom vc-svn-program "svn"
489 "Name of the svn executable."
490 :type 'string
491 :group 'vc)
493 (defun vc-svn-command (buffer okstatus file-or-list &rest flags)
494 "A wrapper around `vc-do-command' for use in vc-svn.el.
495 The difference to vc-do-command is that this function always invokes `svn',
496 and that it passes `vc-svn-global-switches' to it before FLAGS."
497 (apply 'vc-do-command buffer okstatus vc-svn-program file-or-list
498 (if (stringp vc-svn-global-switches)
499 (cons vc-svn-global-switches flags)
500 (append vc-svn-global-switches
501 flags))))
503 (defun vc-svn-repository-hostname (dirname)
504 (with-temp-buffer
505 (let ((coding-system-for-read
506 (or file-name-coding-system
507 default-file-name-coding-system)))
508 (vc-insert-file (expand-file-name (concat vc-svn-admin-directory
509 "/entries")
510 dirname)))
511 (goto-char (point-min))
512 (when (re-search-forward
513 ;; Old `svn' used name="svn:this_dir", newer use just name="".
514 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
515 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
516 "url=\"\\(?1:[^\"]+\\)\""
517 ;; Yet newer ones don't use XML any more.
518 "\\|^\ndir\n[0-9]+\n\\(?1:.*\\)") nil t)
519 ;; This is not a hostname but a URL. This may actually be considered
520 ;; as a feature since it allows vc-svn-stay-local to specify different
521 ;; behavior for different modules on the same server.
522 (match-string 1))))
524 (defun vc-svn-parse-status (&optional filename)
525 "Parse output of \"svn status\" command in the current buffer.
526 Set file properties accordingly. Unless FILENAME is non-nil, parse only
527 information about FILENAME and return its status."
528 (let (file status)
529 (goto-char (point-min))
530 (while (re-search-forward
531 ;; Ignore the files with status in [IX?].
532 "^[ ACDGMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
533 ;; If the username contains spaces, the output format is ambiguous,
534 ;; so don't trust the output's filename unless we have to.
535 (setq file (or filename
536 (expand-file-name
537 (buffer-substring (point) (line-end-position)))))
538 (setq status (char-after (line-beginning-position)))
539 (unless (eq status ??)
540 ;; `vc-BACKEND-registered' must not set vc-backend,
541 ;; which is instead set in vc-registered.
542 (unless filename (vc-file-setprop file 'vc-backend 'SVN))
543 ;; Use the last-modified revision, so that searching in vc-print-log
544 ;; output works.
545 (vc-file-setprop file 'vc-workfile-version (match-string 3))
546 (vc-file-setprop
547 file 'vc-state
548 (cond
549 ((eq status ?\ )
550 (if (eq (char-after (match-beginning 1)) ?*)
551 'needs-patch
552 (vc-file-setprop file 'vc-checkout-time
553 (nth 5 (file-attributes file)))
554 'up-to-date))
555 ((eq status ?A)
556 ;; If the file was actually copied, (match-string 2) is "-".
557 (vc-file-setprop file 'vc-workfile-version "0")
558 (vc-file-setprop file 'vc-checkout-time 0)
559 'edited)
560 ((memq status '(?M ?C))
561 (if (eq (char-after (match-beginning 1)) ?*)
562 'needs-merge
563 'edited))
564 (t 'edited)))))
565 (if filename (vc-file-getprop filename 'vc-state))))
567 (defun vc-svn-dir-state-heuristic (dir)
568 "Find the SVN state of all files in DIR, using only local information."
569 (vc-svn-dir-state dir 'local))
571 (defun vc-svn-valid-symbolic-tag-name-p (tag)
572 "Return non-nil if TAG is a valid symbolic tag name."
573 ;; According to the SVN manual, a valid symbolic tag must start with
574 ;; an uppercase or lowercase letter and can contain uppercase and
575 ;; lowercase letters, digits, `-', and `_'.
576 (and (string-match "^[a-zA-Z]" tag)
577 (not (string-match "[^a-z0-9A-Z-_]" tag))))
579 (defun vc-svn-valid-version-number-p (tag)
580 "Return non-nil if TAG is a valid version number."
581 (and (string-match "^[0-9]" tag)
582 (not (string-match "[^0-9]" tag))))
584 ;; Support for `svn annotate'
586 (defun vc-svn-annotate-command (file buf &optional rev)
587 (vc-svn-command buf 0 file "annotate" (if rev (concat "-r" rev))))
589 (defun vc-svn-annotate-time-of-rev (rev)
590 ;; Arbitrarily assume 10 commmits per day.
591 (/ (string-to-number rev) 10.0))
593 (defun vc-svn-annotate-current-time ()
594 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
596 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
598 (defun vc-svn-annotate-time ()
599 (when (looking-at vc-svn-annotate-re)
600 (goto-char (match-end 0))
601 (vc-svn-annotate-time-of-rev (match-string 1))))
603 (defun vc-svn-annotate-extract-revision-at-line ()
604 (save-excursion
605 (beginning-of-line)
606 (if (looking-at vc-svn-annotate-re) (match-string 1))))
608 (provide 'vc-svn)
610 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
611 ;;; vc-svn.el ends here