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)
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.
27 ;; Sync'd with Subversion's vc-svn.el as of revision 5801. but this version
28 ;; has been extensively modified since to handle filesets.
36 ;;; Customization options
39 (defcustom vc-svn-global-switches nil
40 "*Global switches to pass to any SVN command."
41 :type
'(choice (const :tag
"None" nil
)
42 (string :tag
"Argument String")
43 (repeat :tag
"Argument List"
49 (defcustom vc-svn-register-switches nil
50 "*Extra switches for registering a file into SVN.
51 A string or list of strings passed to the checkin program by
53 :type
'(choice (const :tag
"None" nil
)
54 (string :tag
"Argument String")
55 (repeat :tag
"Argument List"
61 (defcustom vc-svn-diff-switches
62 t
;`svn' doesn't support common args like -c or -b.
63 "String or list of strings specifying extra switches for svn diff under VC.
64 If nil, use the value of `vc-diff-switches'.
65 If you want to force an empty list of arguments, use t."
66 :type
'(choice (const :tag
"Unspecified" nil
)
68 (string :tag
"Argument String")
69 (repeat :tag
"Argument List"
75 (defcustom vc-svn-header
(or (cdr (assoc 'SVN vc-header-alist
)) '("\$Id\$"))
76 "*Header keywords to be inserted by `vc-insert-headers'."
78 :type
'(repeat string
)
81 ;; We want to autoload it for use by the autoloaded version of
82 ;; vc-svn-registered, but we want the value to be compiled at startup, not
85 (defconst vc-svn-admin-directory
86 (cond ((and (memq system-type
'(cygwin windows-nt ms-dos
))
87 (getenv "SVN_ASP_DOT_NET_HACK"))
90 "The name of the \".svn\" subdirectory or its equivalent.")
92 ;;; Properties of the backend
94 (defun vc-svn-revision-granularity ()
97 ;;; State-querying functions
100 ;;; vc-svn-admin-directory is generally not defined when the
101 ;;; autoloaded function is called.
103 ;;;###autoload (defun vc-svn-registered (f)
104 ;;;###autoload (let ((admin-dir (cond ((and (eq system-type 'windows-nt)
105 ;;;###autoload (getenv "SVN_ASP_DOT_NET_HACK"))
106 ;;;###autoload "_svn")
107 ;;;###autoload (t ".svn"))))
108 ;;;###autoload (when (file-readable-p (expand-file-name
109 ;;;###autoload (concat admin-dir "/entries")
110 ;;;###autoload (file-name-directory f)))
111 ;;;###autoload (load "vc-svn")
112 ;;;###autoload (vc-svn-registered f))))
115 (add-to-list 'completion-ignored-extensions
".svn/")
117 (defun vc-svn-registered (file)
118 "Check if FILE is SVN registered."
119 (when (file-readable-p (expand-file-name (concat vc-svn-admin-directory
121 (file-name-directory file
)))
123 (cd (file-name-directory file
))
126 ;; Ignore all errors.
127 (vc-svn-command t t file
"status" "-v")
128 ;; Some problem happened. E.g. We can't find an `svn'
129 ;; executable. We used to only catch `file-error' but when
130 ;; the process is run on a remote host via Tramp, the error
131 ;; is only reported via the exit status which is turned into
132 ;; an `error' by vc-do-command.
135 (vc-svn-parse-status file
))))))
137 (defun vc-svn-state (file &optional localp
)
138 "SVN-specific version of `vc-state'."
139 (setq localp
(or localp
(vc-stay-local-p file
)))
141 (cd (file-name-directory file
))
142 (vc-svn-command t
0 file
"status" (if localp
"-v" "-u"))
143 (vc-svn-parse-status file
)))
145 (defun vc-svn-state-heuristic (file)
146 "SVN-specific state heuristic."
147 (vc-svn-state file
'local
))
149 (defun vc-svn-dir-state (dir &optional localp
)
150 "Find the SVN state of all files in DIR and its subdirectories."
151 (setq localp
(or localp
(vc-stay-local-p dir
)))
152 (let ((default-directory dir
))
153 ;; Don't specify DIR in this command, the default-directory is
154 ;; enough. Otherwise it might fail with remote repositories.
156 (buffer-disable-undo) ;; Because these buffers can get huge
157 (vc-svn-command t
0 nil
"status" (if localp
"-v" "-u"))
158 (vc-svn-parse-status))))
160 (defun vc-svn-working-revision (file)
161 "SVN-specific version of `vc-working-revision'."
162 ;; There is no need to consult RCS headers under SVN, because we
163 ;; get the workfile version for free when we recognize that a file
164 ;; is registered in SVN.
165 (vc-svn-registered file
)
166 (vc-file-getprop file
'vc-working-revision
))
168 (defun vc-svn-checkout-model (file)
169 "SVN-specific version of `vc-checkout-model'."
170 ;; It looks like Subversion has no equivalent of CVSREAD.
173 ;; vc-svn-mode-line-string doesn't exist because the default implementation
176 (defun vc-svn-dired-state-info (file)
177 "SVN-specific version of `vc-dired-state-info'."
178 (let ((svn-state (vc-state file
)))
179 (cond ((eq svn-state
'edited
)
180 (if (equal (vc-working-revision file
) "0")
181 "(added)" "(modified)"))
183 ;; fall back to the default VC representation
184 (vc-default-dired-state-info 'SVN file
)))))
187 (defun vc-svn-previous-revision (file rev
)
188 (let ((newrev (1- (string-to-number rev
))))
190 (number-to-string newrev
))))
192 (defun vc-svn-next-revision (file rev
)
193 (let ((newrev (1+ (string-to-number rev
))))
194 ;; The "working revision" is an uneasy conceptual fit under Subversion;
195 ;; we use it as the upper bound until a better idea comes along. If the
196 ;; workfile version W coincides with the tree's latest revision R, then
197 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
198 ;; inhibits showing of W+1 through R, which could be considered anywhere
199 ;; from gracious to impolite.
200 (unless (< (string-to-number (vc-file-getprop file
'vc-working-revision
))
202 (number-to-string newrev
))))
206 ;;; State-changing functions
209 (defun vc-svn-create-repo ()
210 "Create a new SVN repository."
211 (vc-do-command nil
0 "svnadmin" '("create" "SVN"))
212 (vc-do-command nil
0 "svn" '(".")
213 "checkout" (concat "file://" default-directory
"SVN")))
215 (defun vc-svn-register (files &optional rev comment
)
216 "Register FILES into the SVN version-control system.
217 The COMMENT argument is ignored This does an add but not a commit.
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 files
"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
)
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 (files rev comment
)
235 "SVN-specific version of `vc-backend-checkin'."
236 (if rev
(error "Committing to a specific revision is unsupported in SVN."))
238 'vc-svn-command nil
1 files
"ci"
239 (nconc (list "-m" comment
) (vc-switches 'SVN
'checkin
)))))
241 (goto-char (point-min))
242 (unless (equal status
0)
243 ;; Check checkin problem.
245 ((search-forward "Transaction is out of date" nil t
)
246 (mapc (lambda (file) (vc-file-setprop file
'vc-state
'needs-merge
))
248 (error (substitute-command-keys
249 (concat "Up-to-date check failed: "
250 "type \\[vc-next-action] to merge in changes"))))
252 (pop-to-buffer (current-buffer))
253 (goto-char (point-min))
254 (shrink-window-if-larger-than-buffer)
255 (error "Check-in failed"))))
256 ;; Update file properties
258 ;; file 'vc-working-revision
259 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
262 (defun vc-svn-find-revision (file rev buffer
)
263 "SVN-specific retrieval of a specified version into a buffer."
264 (apply 'vc-svn-command
267 (and rev
(not (string= rev
""))
269 (vc-switches 'SVN
'checkout
)))
271 (defun vc-svn-checkout (file &optional editable rev
)
272 (message "Checking out %s..." file
)
273 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
274 (vc-call update file editable rev
(vc-switches 'SVN
'checkout
)))
276 (message "Checking out %s...done" file
))
278 (defun vc-svn-update (file editable rev switches
)
279 (if (and (file-exists-p file
) (not rev
))
280 ;; If no revision was specified, there's nothing to do.
282 ;; Check out a particular version (or recreate the file).
283 (vc-file-setprop file
'vc-working-revision nil
)
284 (apply 'vc-svn-command nil
0 file
286 ;; default for verbose checkout: clear the sticky tag so
287 ;; that the actual update will get the head of the trunk
289 ((null rev
) "-rBASE")
290 ((or (eq rev t
) (equal rev
"")) nil
)
291 (t (concat "-r" rev
)))
294 (defun vc-svn-delete-file (file)
295 (vc-svn-command nil
0 file
"remove"))
297 (defun vc-svn-rename-file (old new
)
298 (vc-svn-command nil
0 new
"move" (file-relative-name old
)))
300 (defun vc-svn-revert (file &optional contents-done
)
301 "Revert FILE to the version it was based on."
302 (unless contents-done
303 (vc-svn-command nil
0 file
"revert")))
305 (defun vc-svn-merge (file first-version
&optional second-version
)
306 "Merge changes into current working copy of FILE.
307 The changes are between FIRST-VERSION and SECOND-VERSION."
308 (vc-svn-command nil
0 file
310 "-r" (if second-version
311 (concat first-version
":" second-version
)
313 (vc-file-setprop file
'vc-state
'edited
)
314 (with-current-buffer (get-buffer "*vc*")
315 (goto-char (point-min))
316 (if (looking-at "C ")
318 0))) ; signal success
320 (defun vc-svn-merge-news (file)
321 "Merge in any new changes made to FILE."
322 (message "Merging changes into %s..." file
)
323 ;; (vc-file-setprop file 'vc-working-revision nil)
324 (vc-file-setprop file
'vc-checkout-time
0)
325 (vc-svn-command nil
0 file
"update")
326 ;; Analyze the merge result reported by SVN, and set
327 ;; file properties accordingly.
328 (with-current-buffer (get-buffer "*vc*")
329 (goto-char (point-min))
330 ;; get new working revision
331 (if (re-search-forward
332 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t
)
333 (vc-file-setprop file
'vc-working-revision
(match-string 2))
334 (vc-file-setprop file
'vc-working-revision nil
))
336 (goto-char (point-min))
338 (if (looking-at "At revision")
339 0 ;; there were no news; indicate success
340 (if (re-search-forward
341 ;; Newer SVN clients have 3 columns of chars (one for the
342 ;; file's contents, then second for its properties, and the
343 ;; third for lock-grabbing info), before the 2 spaces.
344 ;; We also used to match the filename in column 0 without any
345 ;; meta-info before it, but I believe this can never happen.
346 (concat "^\\(\\([ACGDU]\\)\\(.[B ]\\)? \\)"
347 (regexp-quote (file-name-nondirectory file
)))
350 ;; Merge successful, we are in sync with repository now
351 ((string= (match-string 2) "U")
352 (vc-file-setprop file
'vc-state
'up-to-date
)
353 (vc-file-setprop file
'vc-checkout-time
354 (nth 5 (file-attributes file
)))
355 0);; indicate success to the caller
356 ;; Merge successful, but our own changes are still in the file
357 ((string= (match-string 2) "G")
358 (vc-file-setprop file
'vc-state
'edited
)
359 0);; indicate success to the caller
360 ;; Conflicts detected!
362 (vc-file-setprop file
'vc-state
'edited
)
363 1);; signal the error to the caller
365 (pop-to-buffer "*vc*")
366 (error "Couldn't analyze svn update result")))
367 (message "Merging changes into %s...done" file
))))
371 ;;; History functions
374 (defun vc-svn-print-log (files &optional buffer
)
375 "Get change log(s) associated with FILES."
377 (vc-setup-buffer buffer
)
378 (let ((inhibit-read-only t
))
379 (goto-char (point-min))
382 (insert "Working file: " file
"\n")
386 ;; (if (and (= (length files) 1) (vc-stay-local-p file)) 'async 0)
389 ;; By default Subversion only shows the log up to the
390 ;; working revision, whereas we also want the log of the
391 ;; subsequent commits. At least that's what the
392 ;; vc-cvs.el code does.
394 ;; Dump log for the entire directory.
395 (vc-svn-command buffer
0 nil
"log" "-rHEAD:0")))))
397 (defun vc-svn-wash-log ()
398 "Remove all non-comment information from log output."
399 ;; FIXME: not implemented for SVN
402 (defun vc-svn-diff (files &optional oldvers newvers buffer
)
403 "Get a difference report using SVN between two revisions of fileset FILES."
407 (or (equal oldvers
(vc-working-revision f
))
410 ;; Use nil rather than the current revision because svn handles
411 ;; it better (i.e. locally). Note that if _any_ of the files
412 ;; has a different revision, we fetch the lot, which is
413 ;; obviously sub-optimal.
416 (if vc-svn-diff-switches
417 (vc-switches 'SVN
'diff
)
418 (list "-x" (mapconcat 'identity
(vc-switches nil
'diff
) " "))))
419 (async (and (not vc-disable-async-diff
)
420 (vc-stay-local-p files
)
421 (or oldvers newvers
)))) ; Svn diffs those locally.
422 (apply 'vc-svn-command buffer
428 (list "-r" (if newvers
(concat oldvers
":" newvers
)
430 (if async
1 ; async diff => pessimistic assumption
431 ;; For some reason `svn diff' does not return a useful
432 ;; status w.r.t whether the diff was empty or not.
433 (buffer-size (get-buffer buffer
)))))
439 (defun vc-svn-create-snapshot (dir name branchp
)
440 "Assign to DIR's current revision a given NAME.
441 If BRANCHP is non-nil, the name is created as a branch (and the current
442 workspace is immediately moved to that new branch).
443 NAME is assumed to be a URL."
444 (vc-svn-command nil
0 dir
"copy" name
)
445 (when branchp
(vc-svn-retrieve-snapshot dir name nil
)))
447 (defun vc-svn-retrieve-snapshot (dir name update
)
448 "Retrieve a snapshot at and below DIR.
449 NAME is the name of the snapshot; if it is empty, do a `svn update'.
450 If UPDATE is non-nil, then update (resynch) any affected buffers.
451 NAME is assumed to be a URL."
452 (vc-svn-command nil
0 dir
"switch" name
)
453 ;; FIXME: parse the output and obey `update'.
460 ;; Subversion makes backups for us, so don't bother.
461 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
462 ;; "Return non-nil if version backups should be made for FILE.")
464 (defun vc-svn-check-headers ()
465 "Check if the current file has any headers in it."
467 (goto-char (point-min))
468 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
469 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t
)))
473 ;;; Internal functions
476 (defcustom vc-svn-program
"svn"
477 "Name of the svn executable."
481 (defun vc-svn-command (buffer okstatus file-or-list
&rest flags
)
482 "A wrapper around `vc-do-command' for use in vc-svn.el.
483 The difference to vc-do-command is that this function always invokes `svn',
484 and that it passes `vc-svn-global-switches' to it before FLAGS."
485 (apply 'vc-do-command buffer okstatus vc-svn-program file-or-list
486 (if (stringp vc-svn-global-switches
)
487 (cons vc-svn-global-switches flags
)
488 (append vc-svn-global-switches
491 (defun vc-svn-repository-hostname (dirname)
493 (let ((coding-system-for-read
494 (or file-name-coding-system
495 default-file-name-coding-system
)))
496 (vc-insert-file (expand-file-name (concat vc-svn-admin-directory
499 (goto-char (point-min))
500 (when (re-search-forward
501 ;; Old `svn' used name="svn:this_dir", newer use just name="".
502 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
503 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
504 "url=\"\\(?1:[^\"]+\\)\""
505 ;; Yet newer ones don't use XML any more.
506 "\\|^\ndir\n[0-9]+\n\\(?1:.*\\)") nil t
)
507 ;; This is not a hostname but a URL. This may actually be considered
508 ;; as a feature since it allows vc-svn-stay-local to specify different
509 ;; behavior for different modules on the same server.
512 (defun vc-svn-resolve-when-done ()
513 "Call \"svn resolved\" if the conflict markers have been removed."
515 (goto-char (point-min))
516 (if (not (re-search-forward "^<<<<<<< " nil t
))
517 (vc-svn-command nil
0 buffer-file-name
"resolved"))))
519 ;; Inspired by vc-arch-find-file-hook.
520 (defun vc-svn-find-file-hook ()
521 (when (eq ?C
(vc-file-getprop buffer-file-name
'vc-svn-status
))
522 ;; If the file is marked as "conflicted", then we should try and call
523 ;; "svn resolved" when applicable.
525 (goto-char (point-min))
526 (re-search-forward "^<<<<<<< " nil t
))
527 ;; There are conflict markers.
530 (add-hook 'after-save-hook
'vc-svn-resolve-when-done nil t
))
531 ;; There are no conflict markers. This is problematic: maybe it means
532 ;; the conflict has been resolved and we should immediately call "svn
533 ;; resolved", or it means that the file's type does not allow Svn to
534 ;; use conflict markers in which case we don't really know what to do.
535 ;; So let's just punt for now.
537 (message "There are unresolved conflicts in this file")))
539 (defun vc-svn-parse-status (&optional filename
)
540 "Parse output of \"svn status\" command in the current buffer.
541 Set file properties accordingly. Unless FILENAME is non-nil, parse only
542 information about FILENAME and return its status."
544 (goto-char (point-min))
545 (while (re-search-forward
546 ;; Ignore the files with status in [IX?].
547 "^[ ACDGMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t
)
548 ;; If the username contains spaces, the output format is ambiguous,
549 ;; so don't trust the output's filename unless we have to.
550 (setq file
(or filename
552 (buffer-substring (point) (line-end-position)))))
553 (setq status
(char-after (line-beginning-position)))
554 (unless (eq status ??
)
555 ;; `vc-BACKEND-registered' must not set vc-backend,
556 ;; which is instead set in vc-registered.
557 (unless filename
(vc-file-setprop file
'vc-backend
'SVN
))
558 ;; Use the last-modified revision, so that searching in vc-print-log
560 (vc-file-setprop file
'vc-working-revision
(match-string 3))
561 ;; Remember Svn's own status.
562 (vc-file-setprop file
'vc-svn-status status
)
567 (if (eq (char-after (match-beginning 1)) ?
*)
569 (vc-file-setprop file
'vc-checkout-time
570 (nth 5 (file-attributes file
)))
573 ;; If the file was actually copied, (match-string 2) is "-".
574 (vc-file-setprop file
'vc-working-revision
"0")
575 (vc-file-setprop file
'vc-checkout-time
0)
577 ((memq status
'(?M ?C
))
578 (if (eq (char-after (match-beginning 1)) ?
*)
582 (if filename
(vc-file-getprop filename
'vc-state
))))
584 (defun vc-svn-dir-state-heuristic (dir)
585 "Find the SVN state of all files in DIR, using only local information."
586 (vc-svn-dir-state dir
'local
))
588 (defun vc-svn-valid-symbolic-tag-name-p (tag)
589 "Return non-nil if TAG is a valid symbolic tag name."
590 ;; According to the SVN manual, a valid symbolic tag must start with
591 ;; an uppercase or lowercase letter and can contain uppercase and
592 ;; lowercase letters, digits, `-', and `_'.
593 (and (string-match "^[a-zA-Z]" tag
)
594 (not (string-match "[^a-z0-9A-Z-_]" tag
))))
596 (defun vc-svn-valid-revision-number-p (tag)
597 "Return non-nil if TAG is a valid revision number."
598 (and (string-match "^[0-9]" tag
)
599 (not (string-match "[^0-9]" tag
))))
601 ;; Support for `svn annotate'
603 (defun vc-svn-annotate-command (file buf
&optional rev
)
604 (vc-svn-command buf
0 file
"annotate" (if rev
(concat "-r" rev
))))
606 (defun vc-svn-annotate-time-of-rev (rev)
607 ;; Arbitrarily assume 10 commmits per day.
608 (/ (string-to-number rev
) 10.0))
610 (defun vc-svn-annotate-current-time ()
611 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev
))
613 (defconst vc-svn-annotate-re
"[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
615 (defun vc-svn-annotate-time ()
616 (when (looking-at vc-svn-annotate-re
)
617 (goto-char (match-end 0))
618 (vc-svn-annotate-time-of-rev (match-string 1))))
620 (defun vc-svn-annotate-extract-revision-at-line ()
623 (if (looking-at vc-svn-annotate-re
) (match-string 1))))
627 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
628 ;;; vc-svn.el ends here