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