1 ;;; vc-svn.el --- non-resident support for Subversion version-control
3 ;; Copyright (C) 2003, 2004, 2005, 2006 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.
43 ;;; Customization options
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"
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
60 :type
'(choice (const :tag
"None" nil
)
61 (string :tag
"Argument String")
62 (repeat :tag
"Argument List"
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
)
75 (string :tag
"Argument String")
76 (repeat :tag
"Argument List"
82 (defcustom vc-svn-header
(or (cdr (assoc 'SVN vc-header-alist
)) '("\$Id\$"))
83 "*Header keywords to be inserted by `vc-insert-headers'."
85 :type
'(repeat string
)
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)."
99 ;;; State-querying functions
102 ;;;###autoload (defun vc-svn-registered (f)
103 ;;;###autoload (when (file-readable-p (expand-file-name
104 ;;;###autoload ".svn/entries" (file-name-directory f)))
105 ;;;###autoload (load "vc-svn")
106 ;;;###autoload (vc-svn-registered f)))
109 (add-to-list 'completion-ignored-extensions
".svn/")
111 (defun vc-svn-registered (file)
112 "Check if FILE is SVN registered."
113 (when (file-readable-p (expand-file-name ".svn/entries"
114 (file-name-directory file
)))
116 (cd (file-name-directory file
))
119 ;; Ignore all errors.
120 (vc-svn-command t t file
"status" "-v")
121 ;; Some problem happened. E.g. We can't find an `svn'
122 ;; executable. We used to only catch `file-error' but when
123 ;; the process is run on a remote host via Tramp, the error
124 ;; is only reported via the exit status which is turned into
125 ;; an `error' by vc-do-command.
128 (vc-svn-parse-status t
)
129 (eq 'SVN
(vc-file-getprop file
'vc-backend
)))))))
131 (defun vc-svn-state (file &optional localp
)
132 "SVN-specific version of `vc-state'."
133 (setq localp
(or localp
(vc-stay-local-p file
)))
135 (cd (file-name-directory file
))
136 (vc-svn-command t
0 file
"status" (if localp
"-v" "-u"))
137 (vc-svn-parse-status localp
)
138 (vc-file-getprop file
'vc-state
)))
140 (defun vc-svn-state-heuristic (file)
141 "SVN-specific state heuristic."
142 (vc-svn-state file
'local
))
144 (defun vc-svn-dir-state (dir &optional localp
)
145 "Find the SVN state of all files in DIR."
146 (setq localp
(or localp
(vc-stay-local-p dir
)))
147 (let ((default-directory dir
))
148 ;; Don't specify DIR in this command, the default-directory is
149 ;; enough. Otherwise it might fail with remote repositories.
151 (vc-svn-command t
0 nil
"status" (if localp
"-v" "-u"))
152 (vc-svn-parse-status localp
))))
154 (defun vc-svn-workfile-version (file)
155 "SVN-specific version of `vc-workfile-version'."
156 ;; There is no need to consult RCS headers under SVN, because we
157 ;; get the workfile version for free when we recognize that a file
158 ;; is registered in SVN.
159 (vc-svn-registered file
)
160 (vc-file-getprop file
'vc-workfile-version
))
162 (defun vc-svn-checkout-model (file)
163 "SVN-specific version of `vc-checkout-model'."
164 ;; It looks like Subversion has no equivalent of CVSREAD.
167 ;; vc-svn-mode-line-string doesn't exist because the default implementation
170 (defun vc-svn-dired-state-info (file)
171 "SVN-specific version of `vc-dired-state-info'."
172 (let ((svn-state (vc-state file
)))
173 (cond ((eq svn-state
'edited
)
174 (if (equal (vc-workfile-version file
) "0")
175 "(added)" "(modified)"))
176 ((eq svn-state
'needs-patch
) "(patch)")
177 ((eq svn-state
'needs-merge
) "(merge)"))))
179 (defun vc-svn-previous-version (file rev
)
180 (let ((newrev (1- (string-to-number rev
))))
182 (number-to-string newrev
))))
184 (defun vc-svn-next-version (file rev
)
185 (let ((newrev (1+ (string-to-number rev
))))
186 ;; The "workfile version" is an uneasy conceptual fit under Subversion;
187 ;; we use it as the upper bound until a better idea comes along. If the
188 ;; workfile version W coincides with the tree's latest revision R, then
189 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
190 ;; inhibits showing of W+1 through R, which could be considered anywhere
191 ;; from gracious to impolite.
192 (unless (< (string-to-number (vc-file-getprop file
'vc-workfile-version
))
194 (number-to-string newrev
))))
198 ;;; State-changing functions
201 (defun vc-svn-register (file &optional rev comment
)
202 "Register FILE into the SVN version-control system.
203 COMMENT can be used to provide an initial description of FILE.
205 `vc-register-switches' and `vc-svn-register-switches' are passed to
206 the SVN command (in that order)."
207 (apply 'vc-svn-command nil
0 file
"add" (vc-switches 'SVN
'register
)))
209 (defun vc-svn-responsible-p (file)
210 "Return non-nil if SVN thinks it is responsible for FILE."
211 (file-directory-p (expand-file-name ".svn"
212 (if (file-directory-p file
)
214 (file-name-directory file
)))))
216 (defalias 'vc-svn-could-register
'vc-svn-responsible-p
217 "Return non-nil if FILE could be registered in SVN.
218 This is only possible if SVN is responsible for FILE's directory.")
220 (defun vc-svn-checkin (file rev comment
)
221 "SVN-specific version of `vc-backend-checkin'."
223 'vc-svn-command nil
1 file
"ci"
224 (nconc (list "-m" comment
) (vc-switches 'SVN
'checkin
)))))
226 (goto-char (point-min))
227 (unless (equal status
0)
228 ;; Check checkin problem.
230 ((search-forward "Transaction is out of date" nil t
)
231 (vc-file-setprop file
'vc-state
'needs-merge
)
232 (error (substitute-command-keys
233 (concat "Up-to-date check failed: "
234 "type \\[vc-next-action] to merge in changes"))))
236 (pop-to-buffer (current-buffer))
237 (goto-char (point-min))
238 (shrink-window-if-larger-than-buffer)
239 (error "Check-in failed"))))
240 ;; Update file properties
242 ;; file 'vc-workfile-version
243 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
246 (defun vc-svn-find-version (file rev buffer
)
247 (apply 'vc-svn-command
250 (and rev
(not (string= rev
""))
252 (vc-switches 'SVN
'checkout
)))
254 (defun vc-svn-checkout (file &optional editable rev
)
255 (message "Checking out %s..." file
)
256 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
257 (vc-call update file editable rev
(vc-switches 'SVN
'checkout
)))
259 (message "Checking out %s...done" file
))
261 (defun vc-svn-update (file editable rev switches
)
262 (if (and (file-exists-p file
) (not rev
))
263 ;; If no revision was specified, just make the file writable
264 ;; if necessary (using `svn-edit' if requested).
265 (and editable
(not (eq (vc-svn-checkout-model file
) 'implicit
))
267 (vc-svn-command nil
0 file
"edit")
268 (set-file-modes file
(logior (file-modes file
) 128))
269 (if (equal file buffer-file-name
) (toggle-read-only -
1))))
270 ;; Check out a particular version (or recreate the file).
271 (vc-file-setprop file
'vc-workfile-version nil
)
272 (apply 'vc-svn-command nil
0 file
274 ;; default for verbose checkout: clear the sticky tag so
275 ;; that the actual update will get the head of the trunk
277 ((null rev
) "-rBASE")
278 ((or (eq rev t
) (equal rev
"")) nil
)
279 (t (concat "-r" rev
)))
282 (defun vc-svn-delete-file (file)
283 (vc-svn-command nil
0 file
"remove"))
285 (defun vc-svn-rename-file (old new
)
286 (vc-svn-command nil
0 new
"move" (file-relative-name old
)))
288 (defun vc-svn-revert (file &optional contents-done
)
289 "Revert FILE to the version it was based on."
290 (unless contents-done
291 (vc-svn-command nil
0 file
"revert"))
292 (unless (eq (vc-checkout-model file
) 'implicit
)
294 (vc-svn-command nil
0 file
"unedit")
295 ;; Make the file read-only by switching off all w-bits
296 (set-file-modes file
(logand (file-modes file
) 3950)))))
298 (defun vc-svn-merge (file first-version
&optional second-version
)
299 "Merge changes into current working copy of FILE.
300 The changes are between FIRST-VERSION and SECOND-VERSION."
301 (vc-svn-command nil
0 file
303 "-r" (if second-version
304 (concat first-version
":" second-version
)
306 (vc-file-setprop file
'vc-state
'edited
)
307 (with-current-buffer (get-buffer "*vc*")
308 (goto-char (point-min))
309 (if (looking-at "C ")
311 0))) ; signal success
313 (defun vc-svn-merge-news (file)
314 "Merge in any new changes made to FILE."
315 (message "Merging changes into %s..." file
)
316 ;; (vc-file-setprop file 'vc-workfile-version nil)
317 (vc-file-setprop file
'vc-checkout-time
0)
318 (vc-svn-command nil
0 file
"update")
319 ;; Analyze the merge result reported by SVN, and set
320 ;; file properties accordingly.
321 (with-current-buffer (get-buffer "*vc*")
322 (goto-char (point-min))
323 ;; get new workfile version
324 (if (re-search-forward
325 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t
)
326 (vc-file-setprop file
'vc-workfile-version
(match-string 2))
327 (vc-file-setprop file
'vc-workfile-version nil
))
329 (goto-char (point-min))
331 (if (looking-at "At revision")
332 0 ;; there were no news; indicate success
333 (if (re-search-forward
334 (concat "^\\([CGDU] \\)?"
335 (regexp-quote (file-name-nondirectory file
)))
338 ;; Merge successful, we are in sync with repository now
339 ((string= (match-string 1) "U ")
340 (vc-file-setprop file
'vc-state
'up-to-date
)
341 (vc-file-setprop file
'vc-checkout-time
342 (nth 5 (file-attributes file
)))
343 0);; indicate success to the caller
344 ;; Merge successful, but our own changes are still in the file
345 ((string= (match-string 1) "G ")
346 (vc-file-setprop file
'vc-state
'edited
)
347 0);; indicate success to the caller
348 ;; Conflicts detected!
350 (vc-file-setprop file
'vc-state
'edited
)
351 1);; signal the error to the caller
353 (pop-to-buffer "*vc*")
354 (error "Couldn't analyze svn update result")))
355 (message "Merging changes into %s...done" file
))))
359 ;;; History functions
362 (defun vc-svn-print-log (file &optional buffer
)
363 "Get change log associated with FILE."
365 (vc-setup-buffer buffer
)
366 (let ((inhibit-read-only t
))
367 (goto-char (point-min))
368 ;; Add a line to tell log-view-mode what file this is.
369 (insert "Working file: " (file-relative-name file
) "\n"))
372 (if (and (vc-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
374 ;; By default Subversion only shows the log upto the working version,
375 ;; whereas we also want the log of the subsequent commits. At least
376 ;; that's what the vc-cvs.el code does.
379 (defun vc-svn-diff (file &optional oldvers newvers buffer
)
380 "Get a difference report using SVN between two versions of FILE."
381 (unless buffer
(setq buffer
"*vc-diff*"))
382 (if (and oldvers
(equal oldvers
(vc-workfile-version file
)))
383 ;; Use nil rather than the current revision because svn handles it
384 ;; better (i.e. locally).
386 (if (string= (vc-workfile-version file
) "0")
387 ;; This file is added but not yet committed; there is no master file.
388 (if (or oldvers newvers
)
389 (error "No revisions of %s exist" file
)
390 ;; We regard this as "changed".
391 ;; Diff it against /dev/null.
392 ;; Note: this is NOT a "svn diff".
393 (apply 'vc-do-command buffer
395 (append (vc-switches nil
'diff
) '("/dev/null")))
396 ;; Even if it's empty, it's locally modified.
399 (if vc-svn-diff-switches
400 (vc-switches 'SVN
'diff
)
401 (list "-x" (mapconcat 'identity
(vc-switches nil
'diff
) " "))))
402 (async (and (not vc-disable-async-diff
)
403 (vc-stay-local-p file
)
404 (or oldvers newvers
) ; Svn diffs those locally.
405 (fboundp 'start-process
))))
406 (apply 'vc-svn-command buffer
412 (list "-r" (if newvers
(concat oldvers
":" newvers
)
414 (if async
1 ; async diff => pessimistic assumption
415 ;; For some reason `svn diff' does not return a useful
416 ;; status w.r.t whether the diff was empty or not.
417 (buffer-size (get-buffer buffer
))))))
419 (defun vc-svn-diff-tree (dir &optional rev1 rev2
)
420 "Diff all files at and below DIR."
421 (vc-svn-diff (file-name-as-directory dir
) rev1 rev2
))
427 (defun vc-svn-create-snapshot (dir name branchp
)
428 "Assign to DIR's current version a given NAME.
429 If BRANCHP is non-nil, the name is created as a branch (and the current
430 workspace is immediately moved to that new branch).
431 NAME is assumed to be a URL."
432 (vc-svn-command nil
0 dir
"copy" name
)
433 (when branchp
(vc-svn-retrieve-snapshot dir name nil
)))
435 (defun vc-svn-retrieve-snapshot (dir name update
)
436 "Retrieve a snapshot at and below DIR.
437 NAME is the name of the snapshot; if it is empty, do a `svn update'.
438 If UPDATE is non-nil, then update (resynch) any affected buffers.
439 NAME is assumed to be a URL."
440 (vc-svn-command nil
0 dir
"switch" name
)
441 ;; FIXME: parse the output and obey `update'.
448 ;; Subversion makes backups for us, so don't bother.
449 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
450 ;; "Return non-nil if version backups should be made for FILE.")
452 (defun vc-svn-check-headers ()
453 "Check if the current file has any headers in it."
455 (goto-char (point-min))
456 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
457 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t
)))
461 ;;; Internal functions
464 (defun vc-svn-command (buffer okstatus file
&rest flags
)
465 "A wrapper around `vc-do-command' for use in vc-svn.el.
466 The difference to vc-do-command is that this function always invokes `svn',
467 and that it passes `vc-svn-global-switches' to it before FLAGS."
468 (apply 'vc-do-command buffer okstatus
"svn" file
469 (if (stringp vc-svn-global-switches
)
470 (cons vc-svn-global-switches flags
)
471 (append vc-svn-global-switches
474 (defun vc-svn-repository-hostname (dirname)
476 (let ((coding-system-for-read
477 (or file-name-coding-system
478 default-file-name-coding-system
)))
479 (vc-insert-file (expand-file-name ".svn/entries" dirname
)))
480 (goto-char (point-min))
481 (when (re-search-forward
482 ;; Old `svn' used name="svn:dir", newer use just name="".
483 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
484 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
485 "url=\"\\([^\"]+\\)\"") nil t
)
486 ;; This is not a hostname but a URL. This may actually be considered
487 ;; as a feature since it allows vc-svn-stay-local to specify different
488 ;; behavior for different modules on the same server.
491 (defun vc-svn-parse-status (localp)
492 "Parse output of \"svn status\" command in the current buffer.
493 Set file properties accordingly. Unless FULL is t, parse only
494 essential information."
496 (goto-char (point-min))
497 (while (re-search-forward
498 "^[ ADMCI?!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t
)
499 (setq file
(expand-file-name
500 (buffer-substring (point) (line-end-position))))
501 (setq status
(char-after (line-beginning-position)))
502 (unless (eq status ??
)
503 (vc-file-setprop file
'vc-backend
'SVN
)
504 ;; Use the last-modified revision, so that searching in vc-print-log
506 (vc-file-setprop file
'vc-workfile-version
(match-string 3))
511 (if (eq (char-after (match-beginning 1)) ?
*)
513 (vc-file-setprop file
'vc-checkout-time
514 (nth 5 (file-attributes file
)))
517 ;; If the file was actually copied, (match-string 2) is "-".
518 (vc-file-setprop file
'vc-workfile-version
"0")
519 (vc-file-setprop file
'vc-checkout-time
0)
521 ((memq status
'(?M ?C
))
522 (if (eq (char-after (match-beginning 1)) ?
*)
527 (defun vc-svn-dir-state-heuristic (dir)
528 "Find the SVN state of all files in DIR, using only local information."
529 (vc-svn-dir-state dir
'local
))
531 (defun vc-svn-valid-symbolic-tag-name-p (tag)
532 "Return non-nil if TAG is a valid symbolic tag name."
533 ;; According to the SVN manual, a valid symbolic tag must start with
534 ;; an uppercase or lowercase letter and can contain uppercase and
535 ;; lowercase letters, digits, `-', and `_'.
536 (and (string-match "^[a-zA-Z]" tag
)
537 (not (string-match "[^a-z0-9A-Z-_]" tag
))))
539 (defun vc-svn-valid-version-number-p (tag)
540 "Return non-nil if TAG is a valid version number."
541 (and (string-match "^[0-9]" tag
)
542 (not (string-match "[^0-9]" tag
))))
544 ;; Support for `svn annotate'
546 (defun vc-svn-annotate-command (file buf
&optional rev
)
547 (vc-svn-command buf
0 file
"annotate" (if rev
(concat "-r" rev
))))
549 (defun vc-svn-annotate-time-of-rev (rev)
550 ;; Arbitrarily assume 10 commmits per day.
551 (/ (string-to-number rev
) 10.0))
553 (defun vc-svn-annotate-current-time ()
554 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev
))
556 (defconst vc-svn-annotate-re
"[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
558 (defun vc-svn-annotate-time ()
559 (when (looking-at vc-svn-annotate-re
)
560 (goto-char (match-end 0))
561 (vc-svn-annotate-time-of-rev (match-string 1))))
563 (defun vc-svn-annotate-extract-revision-at-line ()
566 (if (looking-at vc-svn-annotate-re
) (match-string 1))))
570 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
571 ;;; vc-svn.el ends here