1 ;;; vc-svn.el --- non-resident support for Subversion version-control
3 ;; Copyright (C) 2003, 2004, 2005 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))
118 (vc-svn-command t 0 file "status" "-v")
119 ;; Some problem happened. E.g. We can't find an `svn' executable.
120 ;; We used to only catch `file-error' but when the process is run on
121 ;; a remote host via Tramp, the error is only reported via the
122 ;; exit status which is turned into an `error' by vc-do-command.
124 (vc-svn-parse-status t)
125 (eq 'SVN (vc-file-getprop file 'vc-backend)))))
127 (defun vc-svn-state (file &optional localp)
128 "SVN-specific version of `vc-state'."
129 (setq localp (or localp (vc-stay-local-p file)))
131 (cd (file-name-directory file))
132 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
133 (vc-svn-parse-status localp)
134 (vc-file-getprop file 'vc-state)))
136 (defun vc-svn-state-heuristic (file)
137 "SVN-specific state heuristic."
138 (vc-svn-state file 'local))
140 (defun vc-svn-dir-state (dir &optional localp)
141 "Find the SVN state of all files in DIR."
142 (setq localp (or localp (vc-stay-local-p dir)))
143 (let ((default-directory dir))
144 ;; Don't specify DIR in this command, the default-directory is
145 ;; enough. Otherwise it might fail with remote repositories.
147 (vc-svn-command t 0 nil "status" (if localp "-v" "-u"))
148 (vc-svn-parse-status localp))))
150 (defun vc-svn-workfile-version (file)
151 "SVN-specific version of `vc-workfile-version'."
152 ;; There is no need to consult RCS headers under SVN, because we
153 ;; get the workfile version for free when we recognize that a file
154 ;; is registered in SVN.
155 (vc-svn-registered file)
156 (vc-file-getprop file 'vc-workfile-version))
158 (defun vc-svn-checkout-model (file)
159 "SVN-specific version of `vc-checkout-model'."
160 ;; It looks like Subversion has no equivalent of CVSREAD.
163 ;; vc-svn-mode-line-string doesn't exist because the default implementation
166 (defun vc-svn-dired-state-info (file)
167 "SVN-specific version of `vc-dired-state-info'."
168 (let ((svn-state (vc-state file)))
169 (cond ((eq svn-state 'edited)
170 (if (equal (vc-workfile-version file) "0")
171 "(added)" "(modified)"))
172 ((eq svn-state 'needs-patch) "(patch)")
173 ((eq svn-state 'needs-merge) "(merge)"))))
177 ;;; State-changing functions
180 (defun vc-svn-register (file &optional rev comment)
181 "Register FILE into the SVN version-control system.
182 COMMENT can be used to provide an initial description of FILE.
184 `vc-register-switches' and `vc-svn-register-switches' are passed to
185 the SVN command (in that order)."
186 (apply 'vc-svn-command nil 0 file "add" (vc-switches 'SVN 'register)))
188 (defun vc-svn-responsible-p (file)
189 "Return non-nil if SVN thinks it is responsible for FILE."
190 (file-directory-p (expand-file-name ".svn"
191 (if (file-directory-p file)
193 (file-name-directory file)))))
195 (defalias 'vc-svn-could-register 'vc-svn-responsible-p
196 "Return non-nil if FILE could be registered in SVN.
197 This is only possible if SVN is responsible for FILE's directory.")
199 (defun vc-svn-checkin (file rev comment)
200 "SVN-specific version of `vc-backend-checkin'."
202 'vc-svn-command nil 1 file "ci"
203 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
205 (goto-char (point-min))
206 (unless (equal status 0)
207 ;; Check checkin problem.
209 ((search-forward "Transaction is out of date" nil t)
210 (vc-file-setprop file 'vc-state 'needs-merge)
211 (error (substitute-command-keys
212 (concat "Up-to-date check failed: "
213 "type \\[vc-next-action] to merge in changes"))))
215 (pop-to-buffer (current-buffer))
216 (goto-char (point-min))
217 (shrink-window-if-larger-than-buffer)
218 (error "Check-in failed"))))
219 ;; Update file properties
221 ;; file 'vc-workfile-version
222 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
225 (defun vc-svn-find-version (file rev buffer)
226 (apply 'vc-svn-command
229 (and rev (not (string= rev ""))
231 (vc-switches 'SVN 'checkout)))
233 (defun vc-svn-checkout (file &optional editable rev)
234 (message "Checking out %s..." file)
235 (with-current-buffer (or (get-file-buffer file) (current-buffer))
236 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
238 (message "Checking out %s...done" file))
240 (defun vc-svn-update (file editable rev switches)
241 (if (and (file-exists-p file) (not rev))
242 ;; If no revision was specified, just make the file writable
243 ;; if necessary (using `svn-edit' if requested).
244 (and editable (not (eq (vc-svn-checkout-model file) 'implicit))
246 (vc-svn-command nil 0 file "edit")
247 (set-file-modes file (logior (file-modes file) 128))
248 (if (equal file buffer-file-name) (toggle-read-only -1))))
249 ;; Check out a particular version (or recreate the file).
250 (vc-file-setprop file 'vc-workfile-version nil)
251 (apply 'vc-svn-command nil 0 file
253 ;; default for verbose checkout: clear the sticky tag so
254 ;; that the actual update will get the head of the trunk
256 ((null rev) "-rBASE")
257 ((or (eq rev t) (equal rev "")) nil)
258 (t (concat "-r" rev)))
261 (defun vc-svn-delete-file (file)
262 (vc-svn-command nil 0 file "remove"))
264 (defun vc-svn-rename-file (old new)
265 (vc-svn-command nil 0 new "move" (file-relative-name old)))
267 (defun vc-svn-revert (file &optional contents-done)
268 "Revert FILE to the version it was based on."
269 (unless contents-done
270 (vc-svn-command nil 0 file "revert"))
271 (unless (eq (vc-checkout-model file) 'implicit)
273 (vc-svn-command nil 0 file "unedit")
274 ;; Make the file read-only by switching off all w-bits
275 (set-file-modes file (logand (file-modes file) 3950)))))
277 (defun vc-svn-merge (file first-version &optional second-version)
278 "Merge changes into current working copy of FILE.
279 The changes are between FIRST-VERSION and SECOND-VERSION."
280 (vc-svn-command nil 0 file
282 "-r" (if second-version
283 (concat first-version ":" second-version)
285 (vc-file-setprop file 'vc-state 'edited)
286 (with-current-buffer (get-buffer "*vc*")
287 (goto-char (point-min))
288 (if (looking-at "C ")
290 0))) ; signal success
292 (defun vc-svn-merge-news (file)
293 "Merge in any new changes made to FILE."
294 (message "Merging changes into %s..." file)
295 ;; (vc-file-setprop file 'vc-workfile-version nil)
296 (vc-file-setprop file 'vc-checkout-time 0)
297 (vc-svn-command nil 0 file "update")
298 ;; Analyze the merge result reported by SVN, and set
299 ;; file properties accordingly.
300 (with-current-buffer (get-buffer "*vc*")
301 (goto-char (point-min))
302 ;; get new workfile version
303 (if (re-search-forward
304 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
305 (vc-file-setprop file 'vc-workfile-version (match-string 2))
306 (vc-file-setprop file 'vc-workfile-version nil))
308 (goto-char (point-min))
310 (if (looking-at "At revision")
311 0 ;; there were no news; indicate success
312 (if (re-search-forward
313 (concat "^\\([CGDU] \\)?"
314 (regexp-quote (file-name-nondirectory file)))
317 ;; Merge successful, we are in sync with repository now
318 ((string= (match-string 1) "U ")
319 (vc-file-setprop file 'vc-state 'up-to-date)
320 (vc-file-setprop file 'vc-checkout-time
321 (nth 5 (file-attributes file)))
322 0);; indicate success to the caller
323 ;; Merge successful, but our own changes are still in the file
324 ((string= (match-string 1) "G ")
325 (vc-file-setprop file 'vc-state 'edited)
326 0);; indicate success to the caller
327 ;; Conflicts detected!
329 (vc-file-setprop file 'vc-state 'edited)
330 1);; signal the error to the caller
332 (pop-to-buffer "*vc*")
333 (error "Couldn't analyze svn update result")))
334 (message "Merging changes into %s...done" file))))
338 ;;; History functions
341 (defun vc-svn-print-log (file &optional buffer)
342 "Get change log associated with FILE."
344 (vc-setup-buffer buffer)
345 (let ((inhibit-read-only t))
346 (goto-char (point-min))
347 ;; Add a line to tell log-view-mode what file this is.
348 (insert "Working file: " (file-relative-name file) "\n"))
351 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
354 (defun vc-svn-diff (file &optional oldvers newvers buffer)
355 "Get a difference report using SVN between two versions of FILE."
356 (unless buffer (setq buffer "*vc-diff*"))
357 (if (and oldvers (equal oldvers (vc-workfile-version file)))
358 ;; Use nil rather than the current revision because svn handles it
359 ;; better (i.e. locally).
361 (if (string= (vc-workfile-version file) "0")
362 ;; This file is added but not yet committed; there is no master file.
363 (if (or oldvers newvers)
364 (error "No revisions of %s exist" file)
365 ;; We regard this as "changed".
366 ;; Diff it against /dev/null.
367 ;; Note: this is NOT a "svn diff".
368 (apply 'vc-do-command buffer
370 (append (vc-switches nil 'diff) '("/dev/null")))
371 ;; Even if it's empty, it's locally modified.
374 (if vc-svn-diff-switches
375 (vc-switches 'SVN 'diff)
376 (list "-x" (mapconcat 'identity (vc-switches nil 'diff) " "))))
377 (async (and (not vc-disable-async-diff)
378 (vc-stay-local-p file)
379 (or oldvers newvers) ; Svn diffs those locally.
380 (fboundp 'start-process))))
381 (apply 'vc-svn-command buffer
387 (list "-r" (if newvers (concat oldvers ":" newvers)
389 (if async 1 ; async diff => pessimistic assumption
390 ;; For some reason `svn diff' does not return a useful
391 ;; status w.r.t whether the diff was empty or not.
392 (buffer-size (get-buffer buffer))))))
394 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
395 "Diff all files at and below DIR."
396 (vc-svn-diff (file-name-as-directory dir) rev1 rev2))
402 (defun vc-svn-create-snapshot (dir name branchp)
403 "Assign to DIR's current version a given NAME.
404 If BRANCHP is non-nil, the name is created as a branch (and the current
405 workspace is immediately moved to that new branch).
406 NAME is assumed to be a URL."
407 (vc-svn-command nil 0 dir "copy" name)
408 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
410 (defun vc-svn-retrieve-snapshot (dir name update)
411 "Retrieve a snapshot at and below DIR.
412 NAME is the name of the snapshot; if it is empty, do a `svn update'.
413 If UPDATE is non-nil, then update (resynch) any affected buffers.
414 NAME is assumed to be a URL."
415 (vc-svn-command nil 0 dir "switch" name)
416 ;; FIXME: parse the output and obey `update'.
423 ;; Subversion makes backups for us, so don't bother.
424 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
425 ;; "Return non-nil if version backups should be made for FILE.")
427 (defun vc-svn-check-headers ()
428 "Check if the current file has any headers in it."
430 (goto-char (point-min))
431 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
432 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
436 ;;; Internal functions
439 (defun vc-svn-command (buffer okstatus file &rest flags)
440 "A wrapper around `vc-do-command' for use in vc-svn.el.
441 The difference to vc-do-command is that this function always invokes `svn',
442 and that it passes `vc-svn-global-switches' to it before FLAGS."
443 (apply 'vc-do-command buffer okstatus "svn" file
444 (if (stringp vc-svn-global-switches)
445 (cons vc-svn-global-switches flags)
446 (append vc-svn-global-switches
449 (defun vc-svn-repository-hostname (dirname)
451 (let ((coding-system-for-read
452 (or file-name-coding-system
453 default-file-name-coding-system)))
454 (vc-insert-file (expand-file-name ".svn/entries" dirname)))
455 (goto-char (point-min))
456 (when (re-search-forward
457 ;; Old `svn' used name="svn:dir", newer use just name="".
458 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
459 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
460 "url=\"\\([^\"]+\\)\"") nil t)
461 ;; This is not a hostname but a URL. This may actually be considered
462 ;; as a feature since it allows vc-svn-stay-local to specify different
463 ;; behavior for different modules on the same server.
466 (defun vc-svn-parse-status (localp)
467 "Parse output of \"svn status\" command in the current buffer.
468 Set file properties accordingly. Unless FULL is t, parse only
469 essential information."
471 (goto-char (point-min))
472 (while (re-search-forward
473 "^[ ADMCI?!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
474 (setq file (expand-file-name
475 (buffer-substring (point) (line-end-position))))
476 (setq status (char-after (line-beginning-position)))
477 (unless (eq status ??)
478 (vc-file-setprop file 'vc-backend 'SVN)
479 ;; Use the last-modified revision, so that searching in vc-print-log
481 (vc-file-setprop file 'vc-workfile-version (match-string 3))
486 (if (eq (char-after (match-beginning 1)) ?*)
488 (vc-file-setprop file 'vc-checkout-time
489 (nth 5 (file-attributes file)))
492 ;; If the file was actually copied, (match-string 2) is "-".
493 (vc-file-setprop file 'vc-workfile-version "0")
494 (vc-file-setprop file 'vc-checkout-time 0)
496 ((memq status '(?M ?C))
497 (if (eq (char-after (match-beginning 1)) ?*)
502 (defun vc-svn-dir-state-heuristic (dir)
503 "Find the SVN state of all files in DIR, using only local information."
504 (vc-svn-dir-state dir 'local))
506 (defun vc-svn-valid-symbolic-tag-name-p (tag)
507 "Return non-nil if TAG is a valid symbolic tag name."
508 ;; According to the SVN manual, a valid symbolic tag must start with
509 ;; an uppercase or lowercase letter and can contain uppercase and
510 ;; lowercase letters, digits, `-', and `_'.
511 (and (string-match "^[a-zA-Z]" tag)
512 (not (string-match "[^a-z0-9A-Z-_]" tag))))
514 (defun vc-svn-valid-version-number-p (tag)
515 "Return non-nil if TAG is a valid version number."
516 (and (string-match "^[0-9]" tag)
517 (not (string-match "[^0-9]" tag))))
519 ;; Support for `svn annotate'
521 (defun vc-svn-annotate-command (file buf &optional rev)
522 (vc-svn-command buf 0 file "annotate" (if rev (concat "-r" rev))))
524 (defun vc-svn-annotate-time-of-rev (rev)
525 ;; Arbitrarily assume 10 commmits per day.
526 (/ (string-to-number rev) 10.0))
528 (defun vc-svn-annotate-current-time ()
529 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
531 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
533 (defun vc-svn-annotate-time ()
534 (when (looking-at vc-svn-annotate-re)
535 (goto-char (match-end 0))
536 (vc-svn-annotate-time-of-rev (match-string 1))))
538 (defun vc-svn-annotate-extract-revision-at-line ()
541 (if (looking-at vc-svn-annotate-re) (match-string 1))))
545 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
546 ;;; vc-svn.el ends here