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)
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 ;; 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.
35 ;; - VC-dired is (really) slow.
39 (require 'vc-xemacs) ; XEmacs compatiblity stuff
44 ;;; Customization options
47 (defcustom vc-svn-global-switches nil
48 "*Global switches to pass to any SVN command."
49 :type '(choice (const :tag "None" nil)
50 (string :tag "Argument String")
51 (repeat :tag "Argument List"
57 (defcustom vc-svn-register-switches nil
58 "*Extra switches for registering a file into SVN.
59 A string or list of strings passed to the checkin program by
61 :type '(choice (const :tag "None" nil)
62 (string :tag "Argument String")
63 (repeat :tag "Argument List"
69 (defcustom vc-svn-diff-switches
70 t ;`svn' doesn't support common args like -c or -b.
71 "String or list of strings specifying extra switches for svn diff under VC.
72 If nil, use the value of `vc-diff-switches'.
73 If you want to force an empty list of arguments, use t."
74 :type '(choice (const :tag "Unspecified" nil)
76 (string :tag "Argument String")
77 (repeat :tag "Argument List"
83 (defcustom vc-svn-header (or (cdr (assoc 'SVN vc-header-alist)) '("\$Id\$"))
84 "*Header keywords to be inserted by `vc-insert-headers'."
86 :type '(repeat string)
89 ;; We want to autoload it for use by the autoloaded version of
90 ;; vc-svn-registered, but we want the value to be compiled at startup, not
93 (defconst vc-svn-admin-directory
94 (cond ((and (memq system-type '(cygwin windows-nt ms-dos))
95 (getenv "SVN_ASP_DOT_NET_HACK"))
98 "The name of the \".svn\" subdirectory or its equivalent.")
101 ;;; State-querying functions
104 ;;; vc-svn-admin-directory is generally not defined when the
105 ;;; autoloaded function is called.
107 ;; XEmacs change: renamed to registered-lite due to 21.4 byte compiler or
108 ;; bytecode interpreter issue with redefining vc-svn-registered
109 ;;;###autoload (defun vc-svn-registered-lite (f)
110 ;;;###autoload (let ((admin-dir (cond ((and (eq system-type 'windows-nt)
111 ;;;###autoload (getenv "SVN_ASP_DOT_NET_HACK"))
112 ;;;###autoload "_svn")
113 ;;;###autoload (t ".svn"))))
114 ;;;###autoload (when (file-readable-p (expand-file-name
115 ;;;###autoload (concat admin-dir "/entries")
116 ;;;###autoload (file-name-directory f)))
117 ;;;###autoload (load "vc-svn")
118 ;;;###autoload (vc-svn-registered f))))
121 (add-to-list 'completion-ignored-extensions ".svn/")
123 (defun vc-svn-registered (file)
124 "Check if FILE is SVN registered."
125 (when (file-readable-p (expand-file-name (concat vc-svn-admin-directory
127 (file-name-directory file)))
129 (cd (file-name-directory file))
132 ;; Ignore all errors.
133 (vc-svn-command t t file "status" "-v")
134 ;; Some problem happened. E.g. We can't find an `svn'
135 ;; executable. We used to only catch `file-error' but when
136 ;; the process is run on a remote host via Tramp, the error
137 ;; is only reported via the exit status which is turned into
138 ;; an `error' by vc-do-command.
141 (vc-svn-parse-status file))))))
143 (defun vc-svn-state (file &optional localp)
144 "SVN-specific version of `vc-state'."
145 (setq localp (or localp (vc-stay-local-p file)))
147 (cd (file-name-directory file))
148 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
149 (vc-svn-parse-status file)))
151 (defun vc-svn-state-heuristic (file)
152 "SVN-specific state heuristic."
153 (vc-svn-state file 'local))
155 (defun vc-svn-dir-state (dir &optional localp)
156 "Find the SVN state of all files in DIR."
157 (setq localp (or localp (vc-stay-local-p dir)))
158 (let ((default-directory dir))
159 ;; Don't specify DIR in this command, the default-directory is
160 ;; enough. Otherwise it might fail with remote repositories.
162 (vc-svn-command t 0 nil "status" (if localp "-v" "-u"))
163 (vc-svn-parse-status))))
165 (defun vc-svn-workfile-version (file)
166 "SVN-specific version of `vc-workfile-version'."
167 ;; There is no need to consult RCS headers under SVN, because we
168 ;; get the workfile version for free when we recognize that a file
169 ;; is registered in SVN.
170 (vc-svn-registered file)
171 (vc-file-getprop file 'vc-workfile-version))
173 (defun vc-svn-checkout-model (file)
174 "SVN-specific version of `vc-checkout-model'."
175 ;; It looks like Subversion has no equivalent of CVSREAD.
178 ;; vc-svn-mode-line-string doesn't exist because the default implementation
181 (defun vc-svn-dired-state-info (file)
182 "SVN-specific version of `vc-dired-state-info'."
183 (let ((svn-state (vc-state file)))
184 (cond ((eq svn-state 'edited)
185 (if (equal (vc-workfile-version file) "0")
186 "(added)" "(modified)"))
187 ((eq svn-state 'needs-patch) "(patch)")
188 ((eq svn-state 'needs-merge) "(merge)"))))
190 (defun vc-svn-previous-version (file rev)
191 (let ((newrev (1- (string-to-number rev))))
193 (number-to-string newrev))))
195 (defun vc-svn-next-version (file rev)
196 (let ((newrev (1+ (string-to-number rev))))
197 ;; The "workfile version" is an uneasy conceptual fit under Subversion;
198 ;; we use it as the upper bound until a better idea comes along. If the
199 ;; workfile version W coincides with the tree's latest revision R, then
200 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
201 ;; inhibits showing of W+1 through R, which could be considered anywhere
202 ;; from gracious to impolite.
203 (unless (< (string-to-number (vc-file-getprop file 'vc-workfile-version))
205 (number-to-string newrev))))
209 ;;; State-changing functions
212 (defun vc-svn-register (file &optional rev comment)
213 "Register FILE into the SVN version-control system.
214 COMMENT can be used to provide an initial description of FILE.
216 `vc-register-switches' and `vc-svn-register-switches' are passed to
217 the SVN command (in that order)."
218 (apply 'vc-svn-command nil 0 file "add" (vc-switches 'SVN 'register)))
220 (defun vc-svn-responsible-p (file)
221 "Return non-nil if SVN thinks it is responsible for FILE."
222 (file-directory-p (expand-file-name vc-svn-admin-directory
223 (if (file-directory-p file)
225 (file-name-directory file)))))
227 (defalias 'vc-svn-could-register 'vc-svn-responsible-p
228 ;; XEmacs: we have only 2 args to defalias
229 ; "Return non-nil if FILE could be registered in SVN.
230 ;This is only possible if SVN is responsible for FILE's directory."
233 (defun vc-svn-checkin (file rev comment)
234 "SVN-specific version of `vc-backend-checkin'."
236 'vc-svn-command nil 1 file "ci"
237 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
239 (goto-char (point-min))
240 (unless (equal status 0)
241 ;; Check checkin problem.
243 ((search-forward "Transaction is out of date" nil t)
244 (vc-file-setprop file 'vc-state 'needs-merge)
245 (error (substitute-command-keys
246 (concat "Up-to-date check failed: "
247 "type \\[vc-next-action] to merge in changes"))))
249 (pop-to-buffer (current-buffer))
250 (goto-char (point-min))
251 (shrink-window-if-larger-than-buffer)
252 (error "Check-in failed"))))
253 ;; Update file properties
255 ;; file 'vc-workfile-version
256 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
259 (defun vc-svn-find-version (file rev buffer)
260 (apply 'vc-svn-command
263 (and rev (not (string= rev ""))
265 (vc-switches 'SVN 'checkout)))
267 (defun vc-svn-checkout (file &optional editable rev)
268 (message "Checking out %s..." file)
269 (with-current-buffer (or (get-file-buffer file) (current-buffer))
270 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
272 (message "Checking out %s...done" file))
274 (defun vc-svn-update (file editable rev switches)
275 (if (and (file-exists-p file) (not rev))
276 ;; If no revision was specified, there's nothing to do.
278 ;; Check out a particular version (or recreate the file).
279 (vc-file-setprop file 'vc-workfile-version nil)
280 (apply 'vc-svn-command nil 0 file
282 ;; default for verbose checkout: clear the sticky tag so
283 ;; that the actual update will get the head of the trunk
285 ((null rev) "-rBASE")
286 ((or (eq rev t) (equal rev "")) nil)
287 (t (concat "-r" rev)))
290 (defun vc-svn-delete-file (file)
291 (vc-svn-command nil 0 file "remove"))
293 (defun vc-svn-rename-file (old new)
294 (vc-svn-command nil 0 new "move" (file-relative-name old)))
296 (defun vc-svn-revert (file &optional contents-done)
297 "Revert FILE to the version it was based on."
298 (unless contents-done
299 (vc-svn-command nil 0 file "revert")))
301 (defun vc-svn-merge (file first-version &optional second-version)
302 "Merge changes into current working copy of FILE.
303 The changes are between FIRST-VERSION and SECOND-VERSION."
304 (vc-svn-command nil 0 file
306 "-r" (if second-version
307 (concat first-version ":" second-version)
309 (vc-file-setprop file 'vc-state 'edited)
310 (with-current-buffer (get-buffer "*vc*")
311 (goto-char (point-min))
312 (if (looking-at "C ")
314 0))) ; signal success
316 (defun vc-svn-merge-news (file)
317 "Merge in any new changes made to FILE."
318 (message "Merging changes into %s..." file)
319 ;; (vc-file-setprop file 'vc-workfile-version nil)
320 (vc-file-setprop file 'vc-checkout-time 0)
321 (vc-svn-command nil 0 file "update")
322 ;; Analyze the merge result reported by SVN, and set
323 ;; file properties accordingly.
324 (with-current-buffer (get-buffer "*vc*")
325 (goto-char (point-min))
326 ;; get new workfile version
327 (if (re-search-forward
328 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
329 (vc-file-setprop file 'vc-workfile-version (match-string 2))
330 (vc-file-setprop file 'vc-workfile-version nil))
332 (goto-char (point-min))
334 (if (looking-at "At revision")
335 0 ;; there were no news; indicate success
336 (if (re-search-forward
337 ;; Newer SVN clients have 3 columns of chars (one for the
338 ;; file's contents, then second for its properties, and the
339 ;; third for lock-grabbing info), before the 2 spaces.
340 ;; We also used to match the filename in column 0 without any
341 ;; meta-info before it, but I believe this can never happen.
342 (concat "^\\(\\([ACGDU]\\)\\(.[B ]\\)? \\)"
343 (regexp-quote (file-name-nondirectory file)))
346 ;; Merge successful, we are in sync with repository now
347 ((string= (match-string 2) "U")
348 (vc-file-setprop file 'vc-state 'up-to-date)
349 (vc-file-setprop file 'vc-checkout-time
350 (nth 5 (file-attributes file)))
351 0);; indicate success to the caller
352 ;; Merge successful, but our own changes are still in the file
353 ((string= (match-string 2) "G")
354 (vc-file-setprop file 'vc-state 'edited)
355 0);; indicate success to the caller
356 ;; Conflicts detected!
358 (vc-file-setprop file 'vc-state 'edited)
359 1);; signal the error to the caller
361 (pop-to-buffer "*vc*")
362 (error "Couldn't analyze svn update result")))
363 (message "Merging changes into %s...done" file))))
367 ;;; History functions
370 (defun vc-svn-print-log (file &optional buffer)
371 "Get change log associated with FILE."
373 (vc-setup-buffer buffer)
374 (let ((inhibit-read-only t))
375 (goto-char (point-min))
376 ;; Add a line to tell log-view-mode what file this is.
377 (insert "Working file: " (file-relative-name file) "\n"))
380 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
382 ;; By default Subversion only shows the log upto the working version,
383 ;; whereas we also want the log of the subsequent commits. At least
384 ;; that's what the vc-cvs.el code does.
387 (defun vc-svn-diff (file &optional oldvers newvers buffer)
388 "Get a difference report using SVN between two versions of FILE."
389 (unless buffer (setq buffer "*vc-diff*"))
390 (if (and oldvers (equal oldvers (vc-workfile-version file)))
391 ;; Use nil rather than the current revision because svn handles it
392 ;; better (i.e. locally).
394 (if (string= (vc-workfile-version file) "0")
395 ;; This file is added but not yet committed; there is no master file.
396 (if (or oldvers newvers)
397 (error "No revisions of %s exist" file)
398 ;; We regard this as "changed".
399 ;; Diff it against /dev/null.
400 ;; Note: this is NOT a "svn diff".
401 (apply 'vc-do-command buffer
403 (append (vc-switches nil 'diff) '("/dev/null")))
404 ;; Even if it's empty, it's locally modified.
407 (if vc-svn-diff-switches
408 (vc-switches 'SVN 'diff)
409 (list "-x" (mapconcat 'identity (vc-switches nil 'diff) " "))))
410 (async (and (not vc-disable-async-diff)
411 (vc-stay-local-p file)
412 (or oldvers newvers) ; Svn diffs those locally.
413 (fboundp 'start-process))))
414 (apply 'vc-svn-command buffer
420 (list "-r" (if newvers (concat oldvers ":" newvers)
422 (if async 1 ; async diff => pessimistic assumption
423 ;; For some reason `svn diff' does not return a useful
424 ;; status w.r.t whether the diff was empty or not.
425 (buffer-size (get-buffer buffer))))))
427 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
428 "Diff all files at and below DIR."
429 (vc-svn-diff (file-name-as-directory dir) rev1 rev2))
435 (defun vc-svn-create-snapshot (dir name branchp)
436 "Assign to DIR's current version a given NAME.
437 If BRANCHP is non-nil, the name is created as a branch (and the current
438 workspace is immediately moved to that new branch).
439 NAME is assumed to be a URL."
440 (vc-svn-command nil 0 dir "copy" name)
441 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
443 (defun vc-svn-retrieve-snapshot (dir name update)
444 "Retrieve a snapshot at and below DIR.
445 NAME is the name of the snapshot; if it is empty, do a `svn update'.
446 If UPDATE is non-nil, then update (resynch) any affected buffers.
447 NAME is assumed to be a URL."
448 (vc-svn-command nil 0 dir "switch" name)
449 ;; FIXME: parse the output and obey `update'.
456 ;; Subversion makes backups for us, so don't bother.
457 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
458 ;; "Return non-nil if version backups should be made for FILE.")
460 (defun vc-svn-check-headers ()
461 "Check if the current file has any headers in it."
463 (goto-char (point-min))
464 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
465 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
469 ;;; Internal functions
472 (defcustom vc-svn-program "svn"
473 "Name of the svn executable."
477 (defun vc-svn-command (buffer okstatus file &rest flags)
478 "A wrapper around `vc-do-command' for use in vc-svn.el.
479 The difference to vc-do-command is that this function always invokes `svn',
480 and that it passes `vc-svn-global-switches' to it before FLAGS."
481 (apply 'vc-do-command buffer okstatus vc-svn-program file
482 (if (stringp vc-svn-global-switches)
483 (cons vc-svn-global-switches flags)
484 (append vc-svn-global-switches
487 (defun vc-svn-repository-hostname (dirname)
489 ;; XEmacs change: use vc-xemacs-file-name-coding-system
490 (let ((coding-system-for-read (vc-xemacs-file-name-coding-system)))
491 (vc-insert-file (expand-file-name (concat vc-svn-admin-directory
494 (goto-char (point-min))
495 (when (re-search-forward
496 ;; Old `svn' used name="svn:this_dir", newer use just name="".
497 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
498 ;; XEmacs: this regexp hunk simplified; the non-greedy
499 ;; match at the end doesn't seem to work -> XEmacs bug ???
500 ;;"\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
502 ;; XEmacs change: (?<number>:...) syntax not supported.
503 "url=\"\\([^\"]+\\)\""
504 ;; Yet newer ones don't use XML any more.
505 "\\|^\ndir\n[0-9]+\n\\(.*\\)") nil t)
506 ;; This is not a hostname but a URL. This may actually be considered
507 ;; as a feature since it allows vc-svn-stay-local to specify different
508 ;; behavior for different modules on the same server.
509 ;; XEmacs change: match-string 1 or 2 for the (?<number>:) change above.
510 (or (match-string 1) (match-string 2)))))
512 (defun vc-svn-parse-status (&optional filename)
513 "Parse output of \"svn status\" command in the current buffer.
514 Set file properties accordingly. Unless FILENAME is non-nil, parse only
515 information about FILENAME and return its status."
517 (goto-char (point-min))
518 (while (re-search-forward
519 ;; Ignore the files with status in [IX?].
520 "^[ ACDGMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
521 ;; If the username contains spaces, the output format is ambiguous,
522 ;; so don't trust the output's filename unless we have to.
523 (setq file (or filename
525 (buffer-substring (point) (line-end-position)))))
526 (setq status (char-after (line-beginning-position)))
527 (unless (eq status ??)
528 ;; `vc-BACKEND-registered' must not set vc-backend,
529 ;; which is instead set in vc-registered.
530 (unless filename (vc-file-setprop file 'vc-backend 'SVN))
531 ;; Use the last-modified revision, so that searching in vc-print-log
533 (vc-file-setprop file 'vc-workfile-version (match-string 3))
538 (if (eq (char-after (match-beginning 1)) ?*)
540 (vc-file-setprop file 'vc-checkout-time
541 (nth 5 (file-attributes file)))
544 ;; If the file was actually copied, (match-string 2) is "-".
545 (vc-file-setprop file 'vc-workfile-version "0")
546 (vc-file-setprop file 'vc-checkout-time 0)
548 ((memq status '(?M ?C))
549 (if (eq (char-after (match-beginning 1)) ?*)
553 (if filename (vc-file-getprop filename 'vc-state))))
555 (defun vc-svn-dir-state-heuristic (dir)
556 "Find the SVN state of all files in DIR, using only local information."
557 (vc-svn-dir-state dir 'local))
559 (defun vc-svn-valid-symbolic-tag-name-p (tag)
560 "Return non-nil if TAG is a valid symbolic tag name."
561 ;; According to the SVN manual, a valid symbolic tag must start with
562 ;; an uppercase or lowercase letter and can contain uppercase and
563 ;; lowercase letters, digits, `-', and `_'.
564 (and (string-match "^[a-zA-Z]" tag)
565 (not (string-match "[^a-z0-9A-Z-_]" tag))))
567 (defun vc-svn-valid-version-number-p (tag)
568 "Return non-nil if TAG is a valid version number."
569 (and (string-match "^[0-9]" tag)
570 (not (string-match "[^0-9]" tag))))
572 ;; Support for `svn annotate'
574 (defun vc-svn-annotate-command (file buf &optional rev)
575 (vc-svn-command buf 0 file "annotate" (if rev (concat "-r" rev))))
577 (defun vc-svn-annotate-time-of-rev (rev)
578 ;; Arbitrarily assume 10 commmits per day.
579 (/ (string-to-number rev) 10.0))
581 (defun vc-svn-annotate-current-time ()
582 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
584 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
586 (defun vc-svn-annotate-time ()
587 (when (looking-at vc-svn-annotate-re)
588 (goto-char (match-end 0))
589 (vc-svn-annotate-time-of-rev (match-string 1))))
591 (defun vc-svn-annotate-extract-revision-at-line ()
594 (if (looking-at vc-svn-annotate-re) (match-string 1))))
598 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
599 ;;; vc-svn.el ends here