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