* lisp/net/tramp.el (tramp-default-method): scp is the default method.
[bpt/emacs.git] / lisp / vc-svn.el
1 ;;; vc-svn.el --- non-resident support for Subversion version-control
2
3 ;; Copyright (C) 2003, 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, 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 "22.1"
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 "22.1"
66 :group 'vc)
67
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)
74 (const :tag "None" t)
75 (string :tag "Argument String")
76 (repeat :tag "Argument List"
77 :value ("")
78 string))
79 :version "22.1"
80 :group 'vc)
81
82 (defcustom vc-svn-header (or (cdr (assoc 'SVN vc-header-alist)) '("\$Id\$"))
83 "*Header keywords to be inserted by `vc-insert-headers'."
84 :version "22.1"
85 :type '(repeat string)
86 :group 'vc)
87
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)."
93 ;; :type 'boolean
94 ;; :version "22.1"
95 ;; :group 'vc
96 )
97
98 ;;;
99 ;;; State-querying functions
100 ;;;
101
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)))
107
108 ;;;###autoload
109 (add-to-list 'completion-ignored-extensions ".svn/")
110
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)))
115 (with-temp-buffer
116 (cd (file-name-directory file))
117 (let ((status
118 (condition-case nil
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.
126 (error nil))))
127 (when (eq 0 status)
128 (vc-svn-parse-status file))))))
129
130 (defun vc-svn-state (file &optional localp)
131 "SVN-specific version of `vc-state'."
132 (setq localp (or localp (vc-stay-local-p file)))
133 (with-temp-buffer
134 (cd (file-name-directory file))
135 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
136 (vc-svn-parse-status file)))
137
138 (defun vc-svn-state-heuristic (file)
139 "SVN-specific state heuristic."
140 (vc-svn-state file 'local))
141
142 (defun vc-svn-dir-state (dir &optional localp)
143 "Find the SVN state of all files in DIR."
144 (setq localp (or localp (vc-stay-local-p dir)))
145 (let ((default-directory dir))
146 ;; Don't specify DIR in this command, the default-directory is
147 ;; enough. Otherwise it might fail with remote repositories.
148 (with-temp-buffer
149 (vc-svn-command t 0 nil "status" (if localp "-v" "-u"))
150 (vc-svn-parse-status))))
151
152 (defun vc-svn-workfile-version (file)
153 "SVN-specific version of `vc-workfile-version'."
154 ;; There is no need to consult RCS headers under SVN, because we
155 ;; get the workfile version for free when we recognize that a file
156 ;; is registered in SVN.
157 (vc-svn-registered file)
158 (vc-file-getprop file 'vc-workfile-version))
159
160 (defun vc-svn-checkout-model (file)
161 "SVN-specific version of `vc-checkout-model'."
162 ;; It looks like Subversion has no equivalent of CVSREAD.
163 'implicit)
164
165 ;; vc-svn-mode-line-string doesn't exist because the default implementation
166 ;; works just fine.
167
168 (defun vc-svn-dired-state-info (file)
169 "SVN-specific version of `vc-dired-state-info'."
170 (let ((svn-state (vc-state file)))
171 (cond ((eq svn-state 'edited)
172 (if (equal (vc-workfile-version file) "0")
173 "(added)" "(modified)"))
174 ((eq svn-state 'needs-patch) "(patch)")
175 ((eq svn-state 'needs-merge) "(merge)"))))
176
177 (defun vc-svn-previous-version (file rev)
178 (let ((newrev (1- (string-to-number rev))))
179 (when (< 0 newrev)
180 (number-to-string newrev))))
181
182 (defun vc-svn-next-version (file rev)
183 (let ((newrev (1+ (string-to-number rev))))
184 ;; The "workfile version" is an uneasy conceptual fit under Subversion;
185 ;; we use it as the upper bound until a better idea comes along. If the
186 ;; workfile version W coincides with the tree's latest revision R, then
187 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
188 ;; inhibits showing of W+1 through R, which could be considered anywhere
189 ;; from gracious to impolite.
190 (unless (< (string-to-number (vc-file-getprop file 'vc-workfile-version))
191 newrev)
192 (number-to-string newrev))))
193
194
195 ;;;
196 ;;; State-changing functions
197 ;;;
198
199 (defun vc-svn-register (file &optional rev comment)
200 "Register FILE into the SVN version-control system.
201 COMMENT can be used to provide an initial description of FILE.
202
203 `vc-register-switches' and `vc-svn-register-switches' are passed to
204 the SVN command (in that order)."
205 (apply 'vc-svn-command nil 0 file "add" (vc-switches 'SVN 'register)))
206
207 (defun vc-svn-responsible-p (file)
208 "Return non-nil if SVN thinks it is responsible for FILE."
209 (file-directory-p (expand-file-name ".svn"
210 (if (file-directory-p file)
211 file
212 (file-name-directory file)))))
213
214 (defalias 'vc-svn-could-register 'vc-svn-responsible-p
215 "Return non-nil if FILE could be registered in SVN.
216 This is only possible if SVN is responsible for FILE's directory.")
217
218 (defun vc-svn-checkin (file rev comment)
219 "SVN-specific version of `vc-backend-checkin'."
220 (let ((status (apply
221 'vc-svn-command nil 1 file "ci"
222 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
223 (set-buffer "*vc*")
224 (goto-char (point-min))
225 (unless (equal status 0)
226 ;; Check checkin problem.
227 (cond
228 ((search-forward "Transaction is out of date" nil t)
229 (vc-file-setprop file 'vc-state 'needs-merge)
230 (error (substitute-command-keys
231 (concat "Up-to-date check failed: "
232 "type \\[vc-next-action] to merge in changes"))))
233 (t
234 (pop-to-buffer (current-buffer))
235 (goto-char (point-min))
236 (shrink-window-if-larger-than-buffer)
237 (error "Check-in failed"))))
238 ;; Update file properties
239 ;; (vc-file-setprop
240 ;; file 'vc-workfile-version
241 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
242 ))
243
244 (defun vc-svn-find-version (file rev buffer)
245 (apply 'vc-svn-command
246 buffer 0 file
247 "cat"
248 (and rev (not (string= rev ""))
249 (concat "-r" rev))
250 (vc-switches 'SVN 'checkout)))
251
252 (defun vc-svn-checkout (file &optional editable rev)
253 (message "Checking out %s..." file)
254 (with-current-buffer (or (get-file-buffer file) (current-buffer))
255 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
256 (vc-mode-line file)
257 (message "Checking out %s...done" file))
258
259 (defun vc-svn-update (file editable rev switches)
260 (if (and (file-exists-p file) (not rev))
261 ;; If no revision was specified, just make the file writable
262 ;; if necessary (using `svn-edit' if requested).
263 (and editable (not (eq (vc-svn-checkout-model file) 'implicit))
264 (if vc-svn-use-edit
265 (vc-svn-command nil 0 file "edit")
266 (set-file-modes file (logior (file-modes file) 128))
267 (if (equal file buffer-file-name) (toggle-read-only -1))))
268 ;; Check out a particular version (or recreate the file).
269 (vc-file-setprop file 'vc-workfile-version nil)
270 (apply 'vc-svn-command nil 0 file
271 "update"
272 ;; default for verbose checkout: clear the sticky tag so
273 ;; that the actual update will get the head of the trunk
274 (cond
275 ((null rev) "-rBASE")
276 ((or (eq rev t) (equal rev "")) nil)
277 (t (concat "-r" rev)))
278 switches)))
279
280 (defun vc-svn-delete-file (file)
281 (vc-svn-command nil 0 file "remove"))
282
283 (defun vc-svn-rename-file (old new)
284 (vc-svn-command nil 0 new "move" (file-relative-name old)))
285
286 (defun vc-svn-revert (file &optional contents-done)
287 "Revert FILE to the version it was based on."
288 (unless contents-done
289 (vc-svn-command nil 0 file "revert"))
290 (unless (eq (vc-checkout-model file) 'implicit)
291 (if vc-svn-use-edit
292 (vc-svn-command nil 0 file "unedit")
293 ;; Make the file read-only by switching off all w-bits
294 (set-file-modes file (logand (file-modes file) 3950)))))
295
296 (defun vc-svn-merge (file first-version &optional second-version)
297 "Merge changes into current working copy of FILE.
298 The changes are between FIRST-VERSION and SECOND-VERSION."
299 (vc-svn-command nil 0 file
300 "merge"
301 "-r" (if second-version
302 (concat first-version ":" second-version)
303 first-version))
304 (vc-file-setprop file 'vc-state 'edited)
305 (with-current-buffer (get-buffer "*vc*")
306 (goto-char (point-min))
307 (if (looking-at "C ")
308 1 ; signal conflict
309 0))) ; signal success
310
311 (defun vc-svn-merge-news (file)
312 "Merge in any new changes made to FILE."
313 (message "Merging changes into %s..." file)
314 ;; (vc-file-setprop file 'vc-workfile-version nil)
315 (vc-file-setprop file 'vc-checkout-time 0)
316 (vc-svn-command nil 0 file "update")
317 ;; Analyze the merge result reported by SVN, and set
318 ;; file properties accordingly.
319 (with-current-buffer (get-buffer "*vc*")
320 (goto-char (point-min))
321 ;; get new workfile version
322 (if (re-search-forward
323 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
324 (vc-file-setprop file 'vc-workfile-version (match-string 2))
325 (vc-file-setprop file 'vc-workfile-version nil))
326 ;; get file status
327 (goto-char (point-min))
328 (prog1
329 (if (looking-at "At revision")
330 0 ;; there were no news; indicate success
331 (if (re-search-forward
332 (concat "^\\([CGDU] \\)?"
333 (regexp-quote (file-name-nondirectory file)))
334 nil t)
335 (cond
336 ;; Merge successful, we are in sync with repository now
337 ((string= (match-string 1) "U ")
338 (vc-file-setprop file 'vc-state 'up-to-date)
339 (vc-file-setprop file 'vc-checkout-time
340 (nth 5 (file-attributes file)))
341 0);; indicate success to the caller
342 ;; Merge successful, but our own changes are still in the file
343 ((string= (match-string 1) "G ")
344 (vc-file-setprop file 'vc-state 'edited)
345 0);; indicate success to the caller
346 ;; Conflicts detected!
347 (t
348 (vc-file-setprop file 'vc-state 'edited)
349 1);; signal the error to the caller
350 )
351 (pop-to-buffer "*vc*")
352 (error "Couldn't analyze svn update result")))
353 (message "Merging changes into %s...done" file))))
354
355
356 ;;;
357 ;;; History functions
358 ;;;
359
360 (defun vc-svn-print-log (file &optional buffer)
361 "Get change log associated with FILE."
362 (save-current-buffer
363 (vc-setup-buffer buffer)
364 (let ((inhibit-read-only t))
365 (goto-char (point-min))
366 ;; Add a line to tell log-view-mode what file this is.
367 (insert "Working file: " (file-relative-name file) "\n"))
368 (vc-svn-command
369 buffer
370 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
371 file "log"
372 ;; By default Subversion only shows the log upto the working version,
373 ;; whereas we also want the log of the subsequent commits. At least
374 ;; that's what the vc-cvs.el code does.
375 "-rHEAD:0")))
376
377 (defun vc-svn-diff (file &optional oldvers newvers buffer)
378 "Get a difference report using SVN between two versions of FILE."
379 (unless buffer (setq buffer "*vc-diff*"))
380 (if (and oldvers (equal oldvers (vc-workfile-version file)))
381 ;; Use nil rather than the current revision because svn handles it
382 ;; better (i.e. locally).
383 (setq oldvers nil))
384 (if (string= (vc-workfile-version file) "0")
385 ;; This file is added but not yet committed; there is no master file.
386 (if (or oldvers newvers)
387 (error "No revisions of %s exist" file)
388 ;; We regard this as "changed".
389 ;; Diff it against /dev/null.
390 ;; Note: this is NOT a "svn diff".
391 (apply 'vc-do-command buffer
392 1 "diff" file
393 (append (vc-switches nil 'diff) '("/dev/null")))
394 ;; Even if it's empty, it's locally modified.
395 1)
396 (let* ((switches
397 (if vc-svn-diff-switches
398 (vc-switches 'SVN 'diff)
399 (list "-x" (mapconcat 'identity (vc-switches nil 'diff) " "))))
400 (async (and (not vc-disable-async-diff)
401 (vc-stay-local-p file)
402 (or oldvers newvers) ; Svn diffs those locally.
403 (fboundp 'start-process))))
404 (apply 'vc-svn-command buffer
405 (if async 'async 0)
406 file "diff"
407 (append
408 switches
409 (when oldvers
410 (list "-r" (if newvers (concat oldvers ":" newvers)
411 oldvers)))))
412 (if async 1 ; async diff => pessimistic assumption
413 ;; For some reason `svn diff' does not return a useful
414 ;; status w.r.t whether the diff was empty or not.
415 (buffer-size (get-buffer buffer))))))
416
417 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
418 "Diff all files at and below DIR."
419 (vc-svn-diff (file-name-as-directory dir) rev1 rev2))
420
421 ;;;
422 ;;; Snapshot system
423 ;;;
424
425 (defun vc-svn-create-snapshot (dir name branchp)
426 "Assign to DIR's current version a given NAME.
427 If BRANCHP is non-nil, the name is created as a branch (and the current
428 workspace is immediately moved to that new branch).
429 NAME is assumed to be a URL."
430 (vc-svn-command nil 0 dir "copy" name)
431 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
432
433 (defun vc-svn-retrieve-snapshot (dir name update)
434 "Retrieve a snapshot at and below DIR.
435 NAME is the name of the snapshot; if it is empty, do a `svn update'.
436 If UPDATE is non-nil, then update (resynch) any affected buffers.
437 NAME is assumed to be a URL."
438 (vc-svn-command nil 0 dir "switch" name)
439 ;; FIXME: parse the output and obey `update'.
440 )
441
442 ;;;
443 ;;; Miscellaneous
444 ;;;
445
446 ;; Subversion makes backups for us, so don't bother.
447 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
448 ;; "Return non-nil if version backups should be made for FILE.")
449
450 (defun vc-svn-check-headers ()
451 "Check if the current file has any headers in it."
452 (save-excursion
453 (goto-char (point-min))
454 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
455 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
456
457
458 ;;;
459 ;;; Internal functions
460 ;;;
461
462 (defun vc-svn-command (buffer okstatus file &rest flags)
463 "A wrapper around `vc-do-command' for use in vc-svn.el.
464 The difference to vc-do-command is that this function always invokes `svn',
465 and that it passes `vc-svn-global-switches' to it before FLAGS."
466 (apply 'vc-do-command buffer okstatus "svn" file
467 (if (stringp vc-svn-global-switches)
468 (cons vc-svn-global-switches flags)
469 (append vc-svn-global-switches
470 flags))))
471
472 (defun vc-svn-repository-hostname (dirname)
473 (with-temp-buffer
474 (let ((coding-system-for-read
475 (or file-name-coding-system
476 default-file-name-coding-system)))
477 (vc-insert-file (expand-file-name ".svn/entries" dirname)))
478 (goto-char (point-min))
479 (when (re-search-forward
480 ;; Old `svn' used name="svn:dir", newer use just name="".
481 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
482 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
483 "url=\"\\([^\"]+\\)\"") nil t)
484 ;; This is not a hostname but a URL. This may actually be considered
485 ;; as a feature since it allows vc-svn-stay-local to specify different
486 ;; behavior for different modules on the same server.
487 (match-string 1))))
488
489 (defun vc-svn-parse-status (&optional filename)
490 "Parse output of \"svn status\" command in the current buffer.
491 Set file properties accordingly. Unless FILENAME is non-nil, parse only
492 information about FILENAME and return its status."
493 (let (file status)
494 (goto-char (point-min))
495 (while (re-search-forward
496 "^[ ADMCI?!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
497 (setq file (expand-file-name
498 (buffer-substring (point) (line-end-position))))
499 (setq status (char-after (line-beginning-position)))
500 (unless (eq status ??)
501 ;; `vc-BACKEND-registered' must not set vc-backend,
502 ;; which is instead set in vc-registered.
503 (unless filename (vc-file-setprop file 'vc-backend 'SVN))
504 ;; Use the last-modified revision, so that searching in vc-print-log
505 ;; output works.
506 (vc-file-setprop file 'vc-workfile-version (match-string 3))
507 (vc-file-setprop
508 file 'vc-state
509 (cond
510 ((eq status ?\ )
511 (if (eq (char-after (match-beginning 1)) ?*)
512 'needs-patch
513 (vc-file-setprop file 'vc-checkout-time
514 (nth 5 (file-attributes file)))
515 'up-to-date))
516 ((eq status ?A)
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)
520 'edited)
521 ((memq status '(?M ?C))
522 (if (eq (char-after (match-beginning 1)) ?*)
523 'needs-merge
524 'edited))
525 (t 'edited)))))
526 (if filename (vc-file-getprop filename 'vc-state))))
527
528 (defun vc-svn-dir-state-heuristic (dir)
529 "Find the SVN state of all files in DIR, using only local information."
530 (vc-svn-dir-state dir 'local))
531
532 (defun vc-svn-valid-symbolic-tag-name-p (tag)
533 "Return non-nil if TAG is a valid symbolic tag name."
534 ;; According to the SVN manual, a valid symbolic tag must start with
535 ;; an uppercase or lowercase letter and can contain uppercase and
536 ;; lowercase letters, digits, `-', and `_'.
537 (and (string-match "^[a-zA-Z]" tag)
538 (not (string-match "[^a-z0-9A-Z-_]" tag))))
539
540 (defun vc-svn-valid-version-number-p (tag)
541 "Return non-nil if TAG is a valid version number."
542 (and (string-match "^[0-9]" tag)
543 (not (string-match "[^0-9]" tag))))
544
545 ;; Support for `svn annotate'
546
547 (defun vc-svn-annotate-command (file buf &optional rev)
548 (vc-svn-command buf 0 file "annotate" (if rev (concat "-r" rev))))
549
550 (defun vc-svn-annotate-time-of-rev (rev)
551 ;; Arbitrarily assume 10 commmits per day.
552 (/ (string-to-number rev) 10.0))
553
554 (defun vc-svn-annotate-current-time ()
555 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
556
557 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
558
559 (defun vc-svn-annotate-time ()
560 (when (looking-at vc-svn-annotate-re)
561 (goto-char (match-end 0))
562 (vc-svn-annotate-time-of-rev (match-string 1))))
563
564 (defun vc-svn-annotate-extract-revision-at-line ()
565 (save-excursion
566 (beginning-of-line)
567 (if (looking-at vc-svn-annotate-re) (match-string 1))))
568
569 (provide 'vc-svn)
570
571 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
572 ;;; vc-svn.el ends here