(completion-ignored-extensions): Add .svn.
[bpt/emacs.git] / lisp / vc-svn.el
1 ;;; vc-svn.el --- non-resident support for Subversion version-control
2
3 ;; Copyright (C) 1995,98,99,2000,2001,02,2003 Free Software Foundation, Inc.
4
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
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.
30
31 ;; Sync'd with Subversion's vc-svn.el as of revision 5801.
32
33 ;;; Bugs:
34
35 ;; - VC-dired is (really) slow.
36
37 ;;; Code:
38
39 (eval-when-compile
40 (require 'vc))
41
42 ;;;
43 ;;; Customization options
44 ;;;
45
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)
55
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)
67
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)
77
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)
83
84 (defcustom vc-svn-use-edit nil
85 "*Non-nil means to use `svn edit' to \"check out\" a file.
86 This is only meaningful if you don't use the implicit checkout model
87 \(i.e. if you have $SVNREAD set)."
88 :type 'boolean
89 :version "21.4"
90 :group 'vc)
91
92 ;;;
93 ;;; State-querying functions
94 ;;;
95
96 ;;;###autoload (defun vc-svn-registered (f)
97 ;;;###autoload (when (file-readable-p (expand-file-name
98 ;;;###autoload ".svn/entries" (file-name-directory f)))
99 ;;;###autoload (load "vc-svn")
100 ;;;###autoload (vc-svn-registered f)))
101
102 ;;;###autoload
103 (add-to-list 'completion-ignored-extensions ".svn/")
104
105 (defun vc-svn-registered (file)
106 "Check if FILE is SVN registered."
107 (when (file-readable-p (expand-file-name ".svn/entries"
108 (file-name-directory file)))
109 (with-temp-buffer
110 (cd (file-name-directory file))
111 (condition-case nil
112 (vc-svn-command t 0 file "status" "-v")
113 ;; We can't find an `svn' executable. We could also deregister SVN.
114 (file-error nil))
115 (vc-svn-parse-status t)
116 (eq 'SVN (vc-file-getprop file 'vc-backend)))))
117
118 (defun vc-svn-state (file &optional localp)
119 "SVN-specific version of `vc-state'."
120 (setq localp (or localp (vc-stay-local-p file)))
121 (with-temp-buffer
122 (cd (file-name-directory file))
123 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
124 (vc-svn-parse-status localp)
125 (vc-file-getprop file 'vc-state)))
126
127 (defun vc-svn-state-heuristic (file)
128 "SVN-specific state heuristic."
129 (vc-svn-state file 'local))
130
131 (defun vc-svn-dir-state (dir &optional localp)
132 "Find the SVN state of all files in DIR."
133 (setq localp (or localp (vc-stay-local-p dir)))
134 (let ((default-directory dir))
135 ;; Don't specify DIR in this command, the default-directory is
136 ;; enough. Otherwise it might fail with remote repositories.
137 (with-temp-buffer
138 (vc-svn-command t 0 nil "status" (if localp "-v" "-u"))
139 (vc-svn-parse-status localp))))
140
141 (defun vc-svn-workfile-version (file)
142 "SVN-specific version of `vc-workfile-version'."
143 ;; There is no need to consult RCS headers under SVN, because we
144 ;; get the workfile version for free when we recognize that a file
145 ;; is registered in SVN.
146 (vc-svn-registered file)
147 (vc-file-getprop file 'vc-workfile-version))
148
149 (defun vc-svn-checkout-model (file)
150 "SVN-specific version of `vc-checkout-model'."
151 ;; It looks like Subversion has no equivalent of CVSREAD.
152 'implicit)
153
154 (defun vc-svn-dired-state-info (file)
155 "SVN-specific version of `vc-dired-state-info'."
156 (let ((svn-state (vc-state file)))
157 (cond ((eq svn-state 'edited)
158 (if (equal (vc-workfile-version file) "0")
159 "(added)" "(modified)"))
160 ((eq svn-state 'needs-patch) "(patch)")
161 ((eq svn-state 'needs-merge) "(merge)"))))
162
163
164 ;;;
165 ;;; State-changing functions
166 ;;;
167
168 (defun vc-svn-register (file &optional rev comment)
169 "Register FILE into the SVN version-control system.
170 COMMENT can be used to provide an initial description of FILE.
171
172 `vc-register-switches' and `vc-svn-register-switches' are passed to
173 the SVN command (in that order)."
174 (apply 'vc-svn-command nil 0 file
175 "add"
176 ;; (and comment (string-match "[^\t\n ]" comment)
177 ;; (concat "-m" comment))
178 (vc-switches 'SVN 'register)))
179
180 (defun vc-svn-responsible-p (file)
181 "Return non-nil if SVN thinks it is responsible for FILE."
182 (file-directory-p (expand-file-name ".svn"
183 (if (file-directory-p file)
184 file
185 (file-name-directory file)))))
186
187 (defalias 'vc-svn-could-register 'vc-svn-responsible-p
188 "Return non-nil if FILE could be registered in SVN.
189 This is only possible if SVN is responsible for FILE's directory.")
190
191 (defun vc-svn-checkin (file rev comment)
192 "SVN-specific version of `vc-backend-checkin'."
193 (let ((status (apply 'vc-svn-command nil 1 file
194 "ci" (list* "-m" comment (vc-switches 'SVN 'checkin)))))
195 (set-buffer "*vc*")
196 (goto-char (point-min))
197 (unless (equal status 0)
198 ;; Check checkin problem.
199 (cond
200 ((search-forward "Transaction is out of date" nil t)
201 (vc-file-setprop file 'vc-state 'needs-merge)
202 (error (substitute-command-keys
203 (concat "Up-to-date check failed: "
204 "type \\[vc-next-action] to merge in changes"))))
205 (t
206 (pop-to-buffer (current-buffer))
207 (goto-char (point-min))
208 (shrink-window-if-larger-than-buffer)
209 (error "Check-in failed"))))
210 ;; Update file properties
211 ;; (vc-file-setprop
212 ;; file 'vc-workfile-version
213 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
214 ))
215
216 (defun vc-svn-find-version (file rev buffer)
217 (apply 'vc-svn-command
218 buffer 0 file
219 "cat"
220 (and rev (not (string= rev ""))
221 (concat "-r" rev))
222 (vc-switches 'SVN 'checkout)))
223
224 (defun vc-svn-checkout (file &optional editable rev)
225 (message "Checking out %s..." file)
226 (with-current-buffer (or (get-file-buffer file) (current-buffer))
227 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
228 (vc-mode-line file)
229 (message "Checking out %s...done" file))
230
231 (defun vc-svn-update (file editable rev switches)
232 (if (and (file-exists-p file) (not rev))
233 ;; If no revision was specified, just make the file writable
234 ;; if necessary (using `svn-edit' if requested).
235 (and editable (not (eq (vc-svn-checkout-model file) 'implicit))
236 (if vc-svn-use-edit
237 (vc-svn-command nil 0 file "edit")
238 (set-file-modes file (logior (file-modes file) 128))
239 (if (equal file buffer-file-name) (toggle-read-only -1))))
240 ;; Check out a particular version (or recreate the file).
241 (vc-file-setprop file 'vc-workfile-version nil)
242 (apply 'vc-svn-command nil 0 file
243 "-w"
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 (if (or (not rev) (string= rev ""))
248 "-A"
249 (concat "-r" rev))
250 switches)))
251
252 (defun vc-svn-delete-file (file)
253 (vc-svn-command nil 0 file "remove"))
254
255 (defun vc-svn-rename-file (old new)
256 (vc-svn-command nil 0 new "move" (file-relative-name old)))
257
258 (defun vc-svn-revert (file &optional contents-done)
259 "Revert FILE to the version it was based on."
260 (unless contents-done
261 (vc-svn-command nil 0 file "revert"))
262 (unless (eq (vc-checkout-model file) 'implicit)
263 (if vc-svn-use-edit
264 (vc-svn-command nil 0 file "unedit")
265 ;; Make the file read-only by switching off all w-bits
266 (set-file-modes file (logand (file-modes file) 3950)))))
267
268 (defun vc-svn-merge (file first-version &optional second-version)
269 "Merge changes into current working copy of FILE.
270 The changes are between FIRST-VERSION and SECOND-VERSION."
271 (vc-svn-command nil 0 file
272 "merge"
273 "-r" (if second-version
274 (concat first-version ":" second-version)
275 first-version))
276 (vc-file-setprop file 'vc-state 'edited)
277 (with-current-buffer (get-buffer "*vc*")
278 (goto-char (point-min))
279 (if (looking-at "C ")
280 1 ; signal conflict
281 0))) ; signal success
282
283 (defun vc-svn-merge-news (file)
284 "Merge in any new changes made to FILE."
285 (message "Merging changes into %s..." file)
286 ;; (vc-file-setprop file 'vc-workfile-version nil)
287 (vc-file-setprop file 'vc-checkout-time 0)
288 (vc-svn-command nil 0 file "update")
289 ;; Analyze the merge result reported by SVN, and set
290 ;; file properties accordingly.
291 (with-current-buffer (get-buffer "*vc*")
292 (goto-char (point-min))
293 ;; get new workfile version
294 (if (re-search-forward
295 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
296 (vc-file-setprop file 'vc-workfile-version (match-string 2))
297 (vc-file-setprop file 'vc-workfile-version nil))
298 ;; get file status
299 (goto-char (point-min))
300 (prog1
301 (if (looking-at "At revision")
302 0 ;; there were no news; indicate success
303 (if (re-search-forward
304 (concat "^\\([CGDU] \\)?"
305 (regexp-quote (file-name-nondirectory file)))
306 nil t)
307 (cond
308 ;; Merge successful, we are in sync with repository now
309 ((string= (match-string 1) "U ")
310 (vc-file-setprop file 'vc-state 'up-to-date)
311 (vc-file-setprop file 'vc-checkout-time
312 (nth 5 (file-attributes file)))
313 0);; indicate success to the caller
314 ;; Merge successful, but our own changes are still in the file
315 ((string= (match-string 1) "G ")
316 (vc-file-setprop file 'vc-state 'edited)
317 0);; indicate success to the caller
318 ;; Conflicts detected!
319 (t
320 (vc-file-setprop file 'vc-state 'edited)
321 1);; signal the error to the caller
322 )
323 (pop-to-buffer "*vc*")
324 (error "Couldn't analyze svn update result")))
325 (message "Merging changes into %s...done" file))))
326
327
328 ;;;
329 ;;; History functions
330 ;;;
331
332 (defun vc-svn-print-log (file)
333 "Get change log associated with FILE."
334 (save-current-buffer
335 (vc-setup-buffer nil)
336 (let ((inhibit-read-only t))
337 (goto-char (point-min))
338 ;; Add a line to tell log-view-mode what file this is.
339 (insert "Working file: " (file-relative-name file) "\n"))
340 (vc-svn-command
341 t
342 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
343 file "log")))
344
345 (defun vc-svn-diff (file &optional oldvers newvers)
346 "Get a difference report using SVN between two versions of FILE."
347 (if (string= (vc-workfile-version file) "0")
348 ;; This file is added but not yet committed; there is no master file.
349 (if (or oldvers newvers)
350 (error "No revisions of %s exist" file)
351 ;; We regard this as "changed".
352 ;; Diff it against /dev/null.
353 ;; Note: this is NOT a "svn diff".
354 (apply 'vc-do-command "*vc-diff*"
355 1 "diff" file
356 (append (vc-switches nil 'diff) '("/dev/null")))
357 ;; Even if it's empty, it's locally modified.
358 1)
359 (let* ((switches (vc-switches 'SVN 'diff))
360 (async (and (vc-stay-local-p file)
361 (or oldvers newvers) ; Svn diffs those locally.
362 (fboundp 'start-process))))
363 (apply 'vc-svn-command "*vc-diff*"
364 (if async 'async 0)
365 file "diff"
366 (append
367 (when switches
368 (list "-x" (mapconcat 'identity switches " ")))
369 (when oldvers
370 (list "-r" (if newvers (concat oldvers ":" newvers)
371 oldvers)))))
372 (if async 1 ; async diff => pessimistic assumption
373 ;; For some reason `svn diff' does not return a useful
374 ;; status w.r.t whether the diff was empty or not.
375 (buffer-size (get-buffer "*vc-diff*"))))))
376
377 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
378 "Diff all files at and below DIR."
379 (with-current-buffer "*vc-diff*"
380 (setq default-directory dir)
381 (if (vc-stay-local-p dir)
382 ;; local diff: do it filewise, and only for files that are modified
383 (vc-file-tree-walk
384 dir
385 (lambda (f)
386 (vc-exec-after
387 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
388 ;; possible optimization: fetch the state of all files
389 ;; in the tree via vc-svn-dir-state-heuristic
390 (unless (vc-up-to-date-p ',f)
391 (message "Looking at %s" ',f)
392 (vc-diff-internal ',f ',rev1 ',rev2))))))
393 ;; svn diff: use a single call for the entire tree
394 (let ((coding-system-for-read (or coding-system-for-read 'undecided))
395 (switches (vc-switches 'SVN 'diff)))
396 (apply 'vc-svn-command "*vc-diff*" 1 nil "diff"
397 (append
398 (when rev1
399 (list "-r" (if rev2 (concat rev1 ":" rev2) rev1)))
400 (when switches
401 (list "-x" (mapconcat 'identity switches " ")))))))))
402
403 ;;;
404 ;;; Snapshot system
405 ;;;
406
407 (defun vc-svn-create-snapshot (dir name branchp)
408 "Assign to DIR's current version a given NAME.
409 If BRANCHP is non-nil, the name is created as a branch (and the current
410 workspace is immediately moved to that new branch)."
411 (vc-svn-command nil 0 dir "tag" "-c" (if branchp "-b") name)
412 (when branchp (vc-svn-command nil 0 dir "update" "-r" name)))
413
414 (defun vc-svn-retrieve-snapshot (dir name update)
415 "Retrieve a snapshot at and below DIR.
416 NAME is the name of the snapshot; if it is empty, do a `svn update'.
417 If UPDATE is non-nil, then update (resynch) any affected buffers."
418 (with-current-buffer (get-buffer-create "*vc*")
419 (let ((default-directory dir)
420 (sticky-tag))
421 (erase-buffer)
422 (if (or (not name) (string= name ""))
423 (vc-svn-command t 0 nil "update")
424 (vc-svn-command t 0 nil "update" "-r" name)
425 (setq sticky-tag name))
426 (when update
427 (goto-char (point-min))
428 (while (not (eobp))
429 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
430 (let* ((file (expand-file-name (match-string 2) dir))
431 (state (match-string 1))
432 (buffer (find-buffer-visiting file)))
433 (when buffer
434 (cond
435 ((or (string= state "U")
436 (string= state "P"))
437 (vc-file-setprop file 'vc-state 'up-to-date)
438 (vc-file-setprop file 'vc-workfile-version nil)
439 (vc-file-setprop file 'vc-checkout-time
440 (nth 5 (file-attributes file))))
441 ((or (string= state "M")
442 (string= state "C"))
443 (vc-file-setprop file 'vc-state 'edited)
444 (vc-file-setprop file 'vc-workfile-version nil)
445 (vc-file-setprop file 'vc-checkout-time 0)))
446 (vc-file-setprop file 'vc-svn-sticky-tag sticky-tag)
447 (vc-resynch-buffer file t t))))
448 (forward-line 1))))))
449
450
451 ;;;
452 ;;; Miscellaneous
453 ;;;
454
455 ;; Subversion makes backups for us, so don't bother.
456 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
457 ;; "Return non-nil if version backups should be made for FILE.")
458
459 (defun vc-svn-check-headers ()
460 "Check if the current file has any headers in it."
461 (save-excursion
462 (goto-char (point-min))
463 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
464 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
465
466
467 ;;;
468 ;;; Internal functions
469 ;;;
470
471 (defun vc-svn-command (buffer okstatus file &rest flags)
472 "A wrapper around `vc-do-command' for use in vc-svn.el.
473 The difference to vc-do-command is that this function always invokes `svn',
474 and that it passes `vc-svn-global-switches' to it before FLAGS."
475 (apply 'vc-do-command buffer okstatus "svn" file
476 (if (stringp vc-svn-global-switches)
477 (cons vc-svn-global-switches flags)
478 (append vc-svn-global-switches
479 flags))))
480
481 (defun vc-svn-repository-hostname (dirname)
482 (with-temp-buffer
483 (let ((coding-system-for-read
484 (or file-name-coding-system
485 default-file-name-coding-system)))
486 (vc-insert-file (expand-file-name ".svn/entries" dirname)))
487 (goto-char (point-min))
488 (when (re-search-forward
489 (concat "name=\"svn:this_dir\"[\n\t ]*"
490 "\\([-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
491 "url=\"\\([^\"]+\\)\"") nil t)
492 (match-string 2))))
493
494 (defun vc-svn-parse-status (localp)
495 "Parse output of \"svn status\" command in the current buffer.
496 Set file properties accordingly. Unless FULL is t, parse only
497 essential information."
498 (let (file status)
499 (goto-char (point-min))
500 (while (re-search-forward
501 "^[ ADMCI?!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
502 (setq file (expand-file-name
503 (buffer-substring (point) (line-end-position))))
504 (setq status (char-after (line-beginning-position)))
505 (unless (eq status ??)
506 (vc-file-setprop file 'vc-backend 'SVN)
507 ;; Use the last-modified revision, so that searching in vc-print-log
508 ;; output works.
509 (vc-file-setprop file 'vc-workfile-version (match-string 3))
510 (vc-file-setprop
511 file 'vc-state
512 (cond
513 ((eq status ?\ )
514 (if (eq (char-after (match-beginning 1)) ?*)
515 'needs-patch
516 (vc-file-setprop file 'vc-checkout-time
517 (nth 5 (file-attributes file)))
518 'up-to-date))
519 ((eq status ?A)
520 ;; If the file was actually copied, (match-string 2) is "-".
521 (vc-file-setprop file 'vc-workfile-version "0")
522 (vc-file-setprop file 'vc-checkout-time 0)
523 'edited)
524 ((memq status '(?M ?C))
525 (if (eq (char-after (match-beginning 1)) ?*)
526 'needs-merge
527 'edited))
528 (t 'edited)))))))
529
530 (defun vc-svn-dir-state-heuristic (dir)
531 "Find the SVN state of all files in DIR, using only local information."
532 (vc-svn-dir-state dir 'local))
533
534 (defun vc-svn-valid-symbolic-tag-name-p (tag)
535 "Return non-nil if TAG is a valid symbolic tag name."
536 ;; According to the SVN manual, a valid symbolic tag must start with
537 ;; an uppercase or lowercase letter and can contain uppercase and
538 ;; lowercase letters, digits, `-', and `_'.
539 (and (string-match "^[a-zA-Z]" tag)
540 (not (string-match "[^a-z0-9A-Z-_]" tag))))
541
542 (defun vc-svn-valid-version-number-p (tag)
543 "Return non-nil if TAG is a valid version number."
544 (and (string-match "^[0-9]" tag)
545 (not (string-match "[^0-9]" tag))))
546
547 (provide 'vc-svn)
548
549 ;;; vc-svn.el ends here