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