Put the lower half (the back-end) of NewVC in place. This commit
[emacs.git] / lisp / vc-svn.el
blob57bf5828a3f170e4971de6bf379bb6e0499e6f3c
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 ;; 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 (insert "Working file(s): " (vc-delistify (mapcar 'file-relative-name files)) "\n"))
386 (vc-svn-command
387 buffer
388 (if (and (= (length files) 1) (vc-stay-local-p (car files)) (fboundp 'start-process)) 'async 0)
389 files "log"
390 ;; By default Subversion only shows the log upto the working version,
391 ;; whereas we also want the log of the subsequent commits. At least
392 ;; that's what the vc-cvs.el code does.
393 "-rHEAD:0"))))
395 (defun vc-svn-wash-log ()
396 "Remove all non-comment information from log output."
397 ;; FIXME: not implemented for SVN
398 nil)
400 (defun vc-svn-diff (files &optional oldvers newvers buffer)
401 "Get a difference report using SVN between two versions of fileset FILES."
402 (let* ((switches
403 (if vc-svn-diff-switches
404 (vc-switches 'SVN 'diff)
405 (list "-x" (mapconcat 'identity (vc-switches nil 'diff) " "))))
406 (async (and (not vc-disable-async-diff)
407 (vc-stay-local-p files)
408 (or oldvers newvers) ; Svn diffs those locally.
409 (fboundp 'start-process))))
410 (apply 'vc-svn-command buffer
411 (if async 'async 0)
412 files "diff"
413 (append
414 switches
415 (when oldvers
416 (list "-r" (if newvers (concat oldvers ":" newvers)
417 oldvers)))))
418 (if async 1 ; async diff => pessimistic assumption
419 ;; For some reason `svn diff' does not return a useful
420 ;; status w.r.t whether the diff was empty or not.
421 (buffer-size (get-buffer buffer)))))
423 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
424 "Diff all files at and below DIR."
425 (vc-svn-diff (file-name-as-directory dir) rev1 rev2))
428 ;;; Snapshot system
431 (defun vc-svn-create-snapshot (dir name branchp)
432 "Assign to DIR's current version a given NAME.
433 If BRANCHP is non-nil, the name is created as a branch (and the current
434 workspace is immediately moved to that new branch).
435 NAME is assumed to be a URL."
436 (vc-svn-command nil 0 dir "copy" name)
437 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
439 (defun vc-svn-retrieve-snapshot (dir name update)
440 "Retrieve a snapshot at and below DIR.
441 NAME is the name of the snapshot; if it is empty, do a `svn update'.
442 If UPDATE is non-nil, then update (resynch) any affected buffers.
443 NAME is assumed to be a URL."
444 (vc-svn-command nil 0 dir "switch" name)
445 ;; FIXME: parse the output and obey `update'.
449 ;;; Miscellaneous
452 ;; Subversion makes backups for us, so don't bother.
453 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
454 ;; "Return non-nil if version backups should be made for FILE.")
456 (defun vc-svn-check-headers ()
457 "Check if the current file has any headers in it."
458 (save-excursion
459 (goto-char (point-min))
460 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
461 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
465 ;;; Internal functions
468 (defcustom vc-svn-program "svn"
469 "Name of the svn executable."
470 :type 'string
471 :group 'vc)
473 (defun vc-svn-command (buffer okstatus file-or-list &rest flags)
474 "A wrapper around `vc-do-command' for use in vc-svn.el.
475 The difference to vc-do-command is that this function always invokes `svn',
476 and that it passes `vc-svn-global-switches' to it before FLAGS."
477 (apply 'vc-do-command buffer okstatus vc-svn-program file-or-list
478 (if (stringp vc-svn-global-switches)
479 (cons vc-svn-global-switches flags)
480 (append vc-svn-global-switches
481 flags))))
483 (defun vc-svn-repository-hostname (dirname)
484 (with-temp-buffer
485 (let ((coding-system-for-read
486 (or file-name-coding-system
487 default-file-name-coding-system)))
488 (vc-insert-file (expand-file-name (concat vc-svn-admin-directory
489 "/entries")
490 dirname)))
491 (goto-char (point-min))
492 (when (re-search-forward
493 ;; Old `svn' used name="svn:this_dir", newer use just name="".
494 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
495 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
496 "url=\"\\(?1:[^\"]+\\)\""
497 ;; Yet newer ones don't use XML any more.
498 "\\|^\ndir\n[0-9]+\n\\(?1:.*\\)") nil t)
499 ;; This is not a hostname but a URL. This may actually be considered
500 ;; as a feature since it allows vc-svn-stay-local to specify different
501 ;; behavior for different modules on the same server.
502 (match-string 1))))
504 (defun vc-svn-parse-status (&optional filename)
505 "Parse output of \"svn status\" command in the current buffer.
506 Set file properties accordingly. Unless FILENAME is non-nil, parse only
507 information about FILENAME and return its status."
508 (let (file status)
509 (goto-char (point-min))
510 (while (re-search-forward
511 ;; Ignore the files with status in [IX?].
512 "^[ ACDGMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
513 ;; If the username contains spaces, the output format is ambiguous,
514 ;; so don't trust the output's filename unless we have to.
515 (setq file (or filename
516 (expand-file-name
517 (buffer-substring (point) (line-end-position)))))
518 (setq status (char-after (line-beginning-position)))
519 (unless (eq status ??)
520 ;; `vc-BACKEND-registered' must not set vc-backend,
521 ;; which is instead set in vc-registered.
522 (unless filename (vc-file-setprop file 'vc-backend 'SVN))
523 ;; Use the last-modified revision, so that searching in vc-print-log
524 ;; output works.
525 (vc-file-setprop file 'vc-workfile-version (match-string 3))
526 (vc-file-setprop
527 file 'vc-state
528 (cond
529 ((eq status ?\ )
530 (if (eq (char-after (match-beginning 1)) ?*)
531 'needs-patch
532 (vc-file-setprop file 'vc-checkout-time
533 (nth 5 (file-attributes file)))
534 'up-to-date))
535 ((eq status ?A)
536 ;; If the file was actually copied, (match-string 2) is "-".
537 (vc-file-setprop file 'vc-workfile-version "0")
538 (vc-file-setprop file 'vc-checkout-time 0)
539 'edited)
540 ((memq status '(?M ?C))
541 (if (eq (char-after (match-beginning 1)) ?*)
542 'needs-merge
543 'edited))
544 (t 'edited)))))
545 (if filename (vc-file-getprop filename 'vc-state))))
547 (defun vc-svn-dir-state-heuristic (dir)
548 "Find the SVN state of all files in DIR, using only local information."
549 (vc-svn-dir-state dir 'local))
551 (defun vc-svn-valid-symbolic-tag-name-p (tag)
552 "Return non-nil if TAG is a valid symbolic tag name."
553 ;; According to the SVN manual, a valid symbolic tag must start with
554 ;; an uppercase or lowercase letter and can contain uppercase and
555 ;; lowercase letters, digits, `-', and `_'.
556 (and (string-match "^[a-zA-Z]" tag)
557 (not (string-match "[^a-z0-9A-Z-_]" tag))))
559 (defun vc-svn-valid-version-number-p (tag)
560 "Return non-nil if TAG is a valid version number."
561 (and (string-match "^[0-9]" tag)
562 (not (string-match "[^0-9]" tag))))
564 ;; Support for `svn annotate'
566 (defun vc-svn-annotate-command (file buf &optional rev)
567 (vc-svn-command buf 0 file "annotate" (if rev (concat "-r" rev))))
569 (defun vc-svn-annotate-time-of-rev (rev)
570 ;; Arbitrarily assume 10 commmits per day.
571 (/ (string-to-number rev) 10.0))
573 (defun vc-svn-annotate-current-time ()
574 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
576 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
578 (defun vc-svn-annotate-time ()
579 (when (looking-at vc-svn-annotate-re)
580 (goto-char (match-end 0))
581 (vc-svn-annotate-time-of-rev (match-string 1))))
583 (defun vc-svn-annotate-extract-revision-at-line ()
584 (save-excursion
585 (beginning-of-line)
586 (if (looking-at vc-svn-annotate-re) (match-string 1))))
588 (provide 'vc-svn)
590 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
591 ;;; vc-svn.el ends here