(read_minibuf): UNGCPRO before returning.
[emacs.git] / lisp / vc-svn.el
blob46d83685475f4c6e196e4f9408075dace869a3e0
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)
13 ;; any later version.
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.
25 ;;; Commentary:
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.
33 ;;; Bugs:
35 ;; - VC-dired is (really) slow.
37 ;;; Code:
39 (eval-when-compile
40 (require 'vc))
42 ;;;
43 ;;; Customization options
44 ;;;
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"
51 :value ("")
52 string))
53 :version "21.4"
54 :group 'vc)
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
59 \\[vc-register]."
60 :type '(choice (const :tag "None" nil)
61 (string :tag "Argument String")
62 (repeat :tag "Argument List"
63 :value ("")
64 string))
65 :version "21.4"
66 :group 'vc)
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"
73 :value ("")
74 string))
75 :version "21.4"
76 :group 'vc)
78 (defcustom vc-svn-header (or (cdr (assoc 'SVN vc-header-alist)) '("\$Id\$"))
79 "*Header keywords to be inserted by `vc-insert-headers'."
80 :version "21.4"
81 :type '(repeat string)
82 :group 'vc)
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)."
89 ;; :type 'boolean
90 ;; :version "21.4"
91 ;; :group 'vc
94 ;;;
95 ;;; State-querying functions
96 ;;;
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)))
104 ;;;###autoload
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)))
111 (with-temp-buffer
112 (cd (file-name-directory file))
113 (condition-case nil
114 (vc-svn-command t 0 file "status" "-v")
115 ;; We can't find an `svn' executable. We could also deregister SVN.
116 (file-error nil))
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)))
123 (with-temp-buffer
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.
139 (with-temp-buffer
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.
154 'implicit)
156 ;; vc-svn-mode-line-string doesn't exist because the default implementation
157 ;; works just fine.
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)
185 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)))))
196 (set-buffer "*vc*")
197 (goto-char (point-min))
198 (unless (equal status 0)
199 ;; Check checkin problem.
200 (cond
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
212 ;; (vc-file-setprop
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
219 buffer 0 file
220 "cat"
221 (and rev (not (string= rev ""))
222 (concat "-r" 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)))
229 (vc-mode-line file)
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))
237 (if vc-svn-use-edit
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
244 "update"
245 ;; default for verbose checkout: clear the sticky tag so
246 ;; that the actual update will get the head of the trunk
247 (cond
248 ((null rev) "-rBASE")
249 ((or (eq rev t) (equal rev "")) nil)
250 (t (concat "-r" rev)))
251 switches)))
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)
264 (if vc-svn-use-edit
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
273 "merge"
274 "-r" (if second-version
275 (concat first-version ":" second-version)
276 first-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 ")
281 1 ; signal conflict
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))
299 ;; get file status
300 (goto-char (point-min))
301 (prog1
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)))
307 nil t)
308 (cond
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."
335 (save-current-buffer
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"))
341 (vc-svn-command
343 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
344 file "log")))
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*"
356 1 "diff" file
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*"
365 (if async 'async 0)
366 file "diff"
367 (append
368 (when switches
369 (list "-x" (mapconcat 'identity switches " ")))
370 (when oldvers
371 (list "-r" (if newvers (concat oldvers ":" newvers)
372 oldvers)))))
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))
383 ;;; Snapshot system
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'.
404 ;;; Miscellaneous
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."
413 (save-excursion
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
431 flags))))
433 (defun vc-svn-repository-hostname (dirname)
434 (with-temp-buffer
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)
444 (match-string 2))))
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."
450 (let (file status)
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
460 ;; output works.
461 (vc-file-setprop file 'vc-workfile-version (match-string 3))
462 (vc-file-setprop
463 file 'vc-state
464 (cond
465 ((eq status ?\ )
466 (if (eq (char-after (match-beginning 1)) ?*)
467 'needs-patch
468 (vc-file-setprop file 'vc-checkout-time
469 (nth 5 (file-attributes file)))
470 'up-to-date))
471 ((eq status ?A)
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)
475 'edited)
476 ((memq status '(?M ?C))
477 (if (eq (char-after (match-beginning 1)) ?*)
478 'needs-merge
479 'edited))
480 (t 'edited)))))))
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))))
499 (provide 'vc-svn)
501 ;;; vc-svn.el ends here