1 ;;; vc-svn.el --- non-resident support for Subversion version-control
3 ;; Copyright (C) 1995,98,99,2000,2001,02,2003 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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, 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 nil
69 "*A string or list of strings specifying extra switches for svn diff under VC."
70 :type
'(choice (const :tag
"None" nil
)
71 (string :tag
"Argument String")
72 (repeat :tag
"Argument List"
78 (defcustom vc-svn-header
(or (cdr (assoc 'SVN vc-header-alist
)) '("\$Id\$"))
79 "*Header keywords to be inserted by `vc-insert-headers'."
81 :type
'(repeat string
)
84 (defconst vc-svn-use-edit nil
85 ;; Subversion does not provide this feature (yet).
86 "*Non-nil means to use `svn edit' to \"check out\" a file.
87 This is only meaningful if you don't use the implicit checkout model
88 \(i.e. if you have $SVNREAD set)."
95 ;;; State-querying functions
98 ;;;###autoload (defun vc-svn-registered (f)
99 ;;;###autoload (when (file-readable-p (expand-file-name
100 ;;;###autoload ".svn/entries" (file-name-directory f)))
101 ;;;###autoload (load "vc-svn")
102 ;;;###autoload (vc-svn-registered f)))
105 (add-to-list 'completion-ignored-extensions
".svn/")
107 (defun vc-svn-registered (file)
108 "Check if FILE is SVN registered."
109 (when (file-readable-p (expand-file-name ".svn/entries"
110 (file-name-directory file
)))
112 (cd (file-name-directory file
))
114 (vc-svn-command t
0 file
"status" "-v")
115 ;; We can't find an `svn' executable. We could also deregister SVN.
117 (vc-svn-parse-status t
)
118 (eq 'SVN
(vc-file-getprop file
'vc-backend
)))))
120 (defun vc-svn-state (file &optional localp
)
121 "SVN-specific version of `vc-state'."
122 (setq localp
(or localp
(vc-stay-local-p file
)))
124 (cd (file-name-directory file
))
125 (vc-svn-command t
0 file
"status" (if localp
"-v" "-u"))
126 (vc-svn-parse-status localp
)
127 (vc-file-getprop file
'vc-state
)))
129 (defun vc-svn-state-heuristic (file)
130 "SVN-specific state heuristic."
131 (vc-svn-state file
'local
))
133 (defun vc-svn-dir-state (dir &optional localp
)
134 "Find the SVN state of all files in DIR."
135 (setq localp
(or localp
(vc-stay-local-p dir
)))
136 (let ((default-directory dir
))
137 ;; Don't specify DIR in this command, the default-directory is
138 ;; enough. Otherwise it might fail with remote repositories.
140 (vc-svn-command t
0 nil
"status" (if localp
"-v" "-u"))
141 (vc-svn-parse-status localp
))))
143 (defun vc-svn-workfile-version (file)
144 "SVN-specific version of `vc-workfile-version'."
145 ;; There is no need to consult RCS headers under SVN, because we
146 ;; get the workfile version for free when we recognize that a file
147 ;; is registered in SVN.
148 (vc-svn-registered file
)
149 (vc-file-getprop file
'vc-workfile-version
))
151 (defun vc-svn-checkout-model (file)
152 "SVN-specific version of `vc-checkout-model'."
153 ;; It looks like Subversion has no equivalent of CVSREAD.
156 ;; vc-svn-mode-line-string doesn't exist because the default implementation
159 (defun vc-svn-dired-state-info (file)
160 "SVN-specific version of `vc-dired-state-info'."
161 (let ((svn-state (vc-state file
)))
162 (cond ((eq svn-state
'edited
)
163 (if (equal (vc-workfile-version file
) "0")
164 "(added)" "(modified)"))
165 ((eq svn-state
'needs-patch
) "(patch)")
166 ((eq svn-state
'needs-merge
) "(merge)"))))
170 ;;; State-changing functions
173 (defun vc-svn-register (file &optional rev comment
)
174 "Register FILE into the SVN version-control system.
175 COMMENT can be used to provide an initial description of FILE.
177 `vc-register-switches' and `vc-svn-register-switches' are passed to
178 the SVN command (in that order)."
179 (apply 'vc-svn-command nil
0 file
"add" (vc-switches 'SVN
'register
)))
181 (defun vc-svn-responsible-p (file)
182 "Return non-nil if SVN thinks it is responsible for FILE."
183 (file-directory-p (expand-file-name ".svn"
184 (if (file-directory-p file
)
186 (file-name-directory file
)))))
188 (defalias 'vc-svn-could-register
'vc-svn-responsible-p
189 "Return non-nil if FILE could be registered in SVN.
190 This is only possible if SVN is responsible for FILE's directory.")
192 (defun vc-svn-checkin (file rev comment
)
193 "SVN-specific version of `vc-backend-checkin'."
194 (let ((status (apply 'vc-svn-command nil
1 file
195 "ci" (list* "-m" comment
(vc-switches 'SVN
'checkin
)))))
197 (goto-char (point-min))
198 (unless (equal status
0)
199 ;; Check checkin problem.
201 ((search-forward "Transaction is out of date" nil t
)
202 (vc-file-setprop file
'vc-state
'needs-merge
)
203 (error (substitute-command-keys
204 (concat "Up-to-date check failed: "
205 "type \\[vc-next-action] to merge in changes"))))
207 (pop-to-buffer (current-buffer))
208 (goto-char (point-min))
209 (shrink-window-if-larger-than-buffer)
210 (error "Check-in failed"))))
211 ;; Update file properties
213 ;; file 'vc-workfile-version
214 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
217 (defun vc-svn-find-version (file rev buffer
)
218 (apply 'vc-svn-command
221 (and rev
(not (string= rev
""))
223 (vc-switches 'SVN
'checkout
)))
225 (defun vc-svn-checkout (file &optional editable rev
)
226 (message "Checking out %s..." file
)
227 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
228 (vc-call update file editable rev
(vc-switches 'SVN
'checkout
)))
230 (message "Checking out %s...done" file
))
232 (defun vc-svn-update (file editable rev switches
)
233 (if (and (file-exists-p file
) (not rev
))
234 ;; If no revision was specified, just make the file writable
235 ;; if necessary (using `svn-edit' if requested).
236 (and editable
(not (eq (vc-svn-checkout-model file
) 'implicit
))
238 (vc-svn-command nil
0 file
"edit")
239 (set-file-modes file
(logior (file-modes file
) 128))
240 (if (equal file buffer-file-name
) (toggle-read-only -
1))))
241 ;; Check out a particular version (or recreate the file).
242 (vc-file-setprop file
'vc-workfile-version nil
)
243 (apply 'vc-svn-command nil
0 file
245 ;; default for verbose checkout: clear the sticky tag so
246 ;; that the actual update will get the head of the trunk
248 ((null rev
) "-rBASE")
249 ((or (eq rev t
) (equal rev
"")) nil
)
250 (t (concat "-r" rev
)))
253 (defun vc-svn-delete-file (file)
254 (vc-svn-command nil
0 file
"remove"))
256 (defun vc-svn-rename-file (old new
)
257 (vc-svn-command nil
0 new
"move" (file-relative-name old
)))
259 (defun vc-svn-revert (file &optional contents-done
)
260 "Revert FILE to the version it was based on."
261 (unless contents-done
262 (vc-svn-command nil
0 file
"revert"))
263 (unless (eq (vc-checkout-model file
) 'implicit
)
265 (vc-svn-command nil
0 file
"unedit")
266 ;; Make the file read-only by switching off all w-bits
267 (set-file-modes file
(logand (file-modes file
) 3950)))))
269 (defun vc-svn-merge (file first-version
&optional second-version
)
270 "Merge changes into current working copy of FILE.
271 The changes are between FIRST-VERSION and SECOND-VERSION."
272 (vc-svn-command nil
0 file
274 "-r" (if second-version
275 (concat first-version
":" second-version
)
277 (vc-file-setprop file
'vc-state
'edited
)
278 (with-current-buffer (get-buffer "*vc*")
279 (goto-char (point-min))
280 (if (looking-at "C ")
282 0))) ; signal success
284 (defun vc-svn-merge-news (file)
285 "Merge in any new changes made to FILE."
286 (message "Merging changes into %s..." file
)
287 ;; (vc-file-setprop file 'vc-workfile-version nil)
288 (vc-file-setprop file
'vc-checkout-time
0)
289 (vc-svn-command nil
0 file
"update")
290 ;; Analyze the merge result reported by SVN, and set
291 ;; file properties accordingly.
292 (with-current-buffer (get-buffer "*vc*")
293 (goto-char (point-min))
294 ;; get new workfile version
295 (if (re-search-forward
296 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t
)
297 (vc-file-setprop file
'vc-workfile-version
(match-string 2))
298 (vc-file-setprop file
'vc-workfile-version nil
))
300 (goto-char (point-min))
302 (if (looking-at "At revision")
303 0 ;; there were no news; indicate success
304 (if (re-search-forward
305 (concat "^\\([CGDU] \\)?"
306 (regexp-quote (file-name-nondirectory file
)))
309 ;; Merge successful, we are in sync with repository now
310 ((string= (match-string 1) "U ")
311 (vc-file-setprop file
'vc-state
'up-to-date
)
312 (vc-file-setprop file
'vc-checkout-time
313 (nth 5 (file-attributes file
)))
314 0);; indicate success to the caller
315 ;; Merge successful, but our own changes are still in the file
316 ((string= (match-string 1) "G ")
317 (vc-file-setprop file
'vc-state
'edited
)
318 0);; indicate success to the caller
319 ;; Conflicts detected!
321 (vc-file-setprop file
'vc-state
'edited
)
322 1);; signal the error to the caller
324 (pop-to-buffer "*vc*")
325 (error "Couldn't analyze svn update result")))
326 (message "Merging changes into %s...done" file
))))
330 ;;; History functions
333 (defun vc-svn-print-log (file)
334 "Get change log associated with FILE."
336 (vc-setup-buffer nil
)
337 (let ((inhibit-read-only t
))
338 (goto-char (point-min))
339 ;; Add a line to tell log-view-mode what file this is.
340 (insert "Working file: " (file-relative-name file
) "\n"))
343 (if (and (vc-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
346 (defun vc-svn-diff (file &optional oldvers newvers
)
347 "Get a difference report using SVN between two versions of FILE."
348 (if (string= (vc-workfile-version file
) "0")
349 ;; This file is added but not yet committed; there is no master file.
350 (if (or oldvers newvers
)
351 (error "No revisions of %s exist" file
)
352 ;; We regard this as "changed".
353 ;; Diff it against /dev/null.
354 ;; Note: this is NOT a "svn diff".
355 (apply 'vc-do-command
"*vc-diff*"
357 (append (vc-switches nil
'diff
) '("/dev/null")))
358 ;; Even if it's empty, it's locally modified.
360 (let* ((switches (vc-switches 'SVN
'diff
))
361 (async (and (vc-stay-local-p file
)
362 (or oldvers newvers
) ; Svn diffs those locally.
363 (fboundp 'start-process
))))
364 (apply 'vc-svn-command
"*vc-diff*"
369 (list "-x" (mapconcat 'identity switches
" ")))
371 (list "-r" (if newvers
(concat oldvers
":" newvers
)
373 (if async
1 ; async diff => pessimistic assumption
374 ;; For some reason `svn diff' does not return a useful
375 ;; status w.r.t whether the diff was empty or not.
376 (buffer-size (get-buffer "*vc-diff*"))))))
378 (defun vc-svn-diff-tree (dir &optional rev1 rev2
)
379 "Diff all files at and below DIR."
380 (vc-svn-diff (file-name-as-directory dir
) rev1 rev2
))
386 (defun vc-svn-create-snapshot (dir name branchp
)
387 "Assign to DIR's current version a given NAME.
388 If BRANCHP is non-nil, the name is created as a branch (and the current
389 workspace is immediately moved to that new branch).
390 NAME is assumed to be a URL."
391 (vc-svn-command nil
0 dir
"copy" name
)
392 (when branchp
(vc-svn-retrieve-snapshot dir name nil
)))
394 (defun vc-svn-retrieve-snapshot (dir name update
)
395 "Retrieve a snapshot at and below DIR.
396 NAME is the name of the snapshot; if it is empty, do a `svn update'.
397 If UPDATE is non-nil, then update (resynch) any affected buffers.
398 NAME is assumed to be a URL."
399 (vc-svn-command nil
0 dir
"switch" name
)
400 ;; FIXME: parse the output and obey `update'.
407 ;; Subversion makes backups for us, so don't bother.
408 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
409 ;; "Return non-nil if version backups should be made for FILE.")
411 (defun vc-svn-check-headers ()
412 "Check if the current file has any headers in it."
414 (goto-char (point-min))
415 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
416 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t
)))
420 ;;; Internal functions
423 (defun vc-svn-command (buffer okstatus file
&rest flags
)
424 "A wrapper around `vc-do-command' for use in vc-svn.el.
425 The difference to vc-do-command is that this function always invokes `svn',
426 and that it passes `vc-svn-global-switches' to it before FLAGS."
427 (apply 'vc-do-command buffer okstatus
"svn" file
428 (if (stringp vc-svn-global-switches
)
429 (cons vc-svn-global-switches flags
)
430 (append vc-svn-global-switches
433 (defun vc-svn-repository-hostname (dirname)
435 (let ((coding-system-for-read
436 (or file-name-coding-system
437 default-file-name-coding-system
)))
438 (vc-insert-file (expand-file-name ".svn/entries" dirname
)))
439 (goto-char (point-min))
440 (when (re-search-forward
441 (concat "name=\"svn:this_dir\"[\n\t ]*"
442 "\\([-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
443 "url=\"\\([^\"]+\\)\"") nil t
)
446 (defun vc-svn-parse-status (localp)
447 "Parse output of \"svn status\" command in the current buffer.
448 Set file properties accordingly. Unless FULL is t, parse only
449 essential information."
451 (goto-char (point-min))
452 (while (re-search-forward
453 "^[ ADMCI?!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t
)
454 (setq file
(expand-file-name
455 (buffer-substring (point) (line-end-position))))
456 (setq status
(char-after (line-beginning-position)))
457 (unless (eq status ??
)
458 (vc-file-setprop file
'vc-backend
'SVN
)
459 ;; Use the last-modified revision, so that searching in vc-print-log
461 (vc-file-setprop file
'vc-workfile-version
(match-string 3))
466 (if (eq (char-after (match-beginning 1)) ?
*)
468 (vc-file-setprop file
'vc-checkout-time
469 (nth 5 (file-attributes file
)))
472 ;; If the file was actually copied, (match-string 2) is "-".
473 (vc-file-setprop file
'vc-workfile-version
"0")
474 (vc-file-setprop file
'vc-checkout-time
0)
476 ((memq status
'(?M ?C
))
477 (if (eq (char-after (match-beginning 1)) ?
*)
482 (defun vc-svn-dir-state-heuristic (dir)
483 "Find the SVN state of all files in DIR, using only local information."
484 (vc-svn-dir-state dir
'local
))
486 (defun vc-svn-valid-symbolic-tag-name-p (tag)
487 "Return non-nil if TAG is a valid symbolic tag name."
488 ;; According to the SVN manual, a valid symbolic tag must start with
489 ;; an uppercase or lowercase letter and can contain uppercase and
490 ;; lowercase letters, digits, `-', and `_'.
491 (and (string-match "^[a-zA-Z]" tag
)
492 (not (string-match "[^a-z0-9A-Z-_]" tag
))))
494 (defun vc-svn-valid-version-number-p (tag)
495 "Return non-nil if TAG is a valid version number."
496 (and (string-match "^[0-9]" tag
)
497 (not (string-match "[^0-9]" tag
))))
501 ;;; vc-svn.el ends here