1 ;;; vc-svn.el --- non-resident support for Subversion version-control
3 ;; Copyright (C) 1995,98,99,2000,2001,2002 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
8 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
31 ;; Sync'd with Subversion's vc-svn.el as of revision 5801.
35 ;; - VC-dired is (really) slow.
43 ;;; Customization options
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"
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
60 :type
'(choice (const :tag
"None" nil
)
61 (string :tag
"Argument String")
62 (repeat :tag
"Argument List"
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"
78 (defcustom vc-svn-header
(or (cdr (assoc 'SVN vc-header-alist
)) '("\$Id\$"))
79 "*Header keywords to be inserted by `vc-insert-headers'."
81 :type
'(repeat string
)
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)."
92 (defcustom vc-svn-stay-local t
93 "*Non-nil means use local operations when possible for remote repositories.
94 This avoids slow queries over the network and instead uses heuristics
95 and past information to determine the current status of a file.
97 The value can also be a regular expression or list of regular
98 expressions to match against the host name of a repository; then VC
99 only stays local for hosts that match it. Alternatively, the value
100 can be a list of regular expressions where the first element is the
101 symbol `except'; then VC always stays local except for hosts matched
102 by these regular expressions."
103 :type
'(choice (const :tag
"Always stay local" t
)
104 (const :tag
"Don't stay local" nil
)
105 (list :format
"\nExamine hostname and %v" :tag
"Examine hostname ..."
106 (set :format
"%v" :inline t
(const :format
"%t" :tag
"don't" except
))
107 (regexp :format
" stay local,\n%t: %v" :tag
"if it matches")
108 (repeat :format
"%v%i\n" :inline t
(regexp :tag
"or"))))
113 ;;; State-querying functions
116 ;;;###autoload (defun vc-svn-registered (f)
117 ;;;###autoload (when (file-readable-p (expand-file-name
118 ;;;###autoload ".svn/entries" (file-name-directory f)))
119 ;;;###autoload (load "vc-svn")
120 ;;;###autoload (vc-svn-registered f)))
122 (defun vc-svn-registered (file)
123 "Check if FILE is SVN registered."
124 (when (file-readable-p (expand-file-name ".svn/entries"
125 (file-name-directory file
)))
127 (cd (file-name-directory file
))
129 (vc-svn-command t
0 file
"status" "-v")
130 ;; We can't find an `svn' executable. We could also deregister SVN.
132 (vc-svn-parse-status t
)
133 (eq 'SVN
(vc-file-getprop file
'vc-backend
)))))
135 (defun vc-svn-state (file &optional localp
)
136 "SVN-specific version of `vc-state'."
137 (setq localp
(or localp
(vc-svn-stay-local-p file
)))
139 (cd (file-name-directory file
))
140 (vc-svn-command t
0 file
"status" (if localp
"-v" "-u"))
141 (vc-svn-parse-status localp
)
142 (vc-file-getprop file
'vc-state
)))
144 (defun vc-svn-state-heuristic (file)
145 "SVN-specific state heuristic."
146 (vc-svn-state file
'local
))
148 (defun vc-svn-dir-state (dir &optional localp
)
149 "Find the SVN state of all files in DIR."
150 (setq localp
(or localp
(vc-svn-stay-local-p dir
)))
151 (let ((default-directory dir
))
152 ;; Don't specify DIR in this command, the default-directory is
153 ;; enough. Otherwise it might fail with remote repositories.
155 (vc-svn-command t
0 nil
"status" (if localp
"-v" "-u"))
156 (vc-svn-parse-status localp
))))
158 (defun vc-svn-workfile-version (file)
159 "SVN-specific version of `vc-workfile-version'."
160 ;; There is no need to consult RCS headers under SVN, because we
161 ;; get the workfile version for free when we recognize that a file
162 ;; is registered in SVN.
163 (vc-svn-registered file
)
164 (vc-file-getprop file
'vc-workfile-version
))
166 (defun vc-svn-checkout-model (file)
167 "SVN-specific version of `vc-checkout-model'."
168 ;; It looks like Subversion has no equivalent of CVSREAD.
171 (defun vc-svn-mode-line-string (file)
172 "Return string for placement into the modeline for FILE.
173 Compared to the default implementation, this function does two things:
174 Handle the special case of a SVN file that is added but not yet
175 committed and support display of sticky tags."
176 (let* ((state (vc-state file
))
177 (rev (vc-workfile-version file
))
178 (sticky-tag (vc-file-getprop file
'vc-svn-sticky-tag
))
179 (sticky-tag-printable (and sticky-tag
180 (not (string= sticky-tag
""))
181 (concat "[" sticky-tag
"]"))))
182 (cond ((string= rev
"0")
183 ;; A file that is added but not yet committed.
185 ((or (eq state
'up-to-date
)
186 (eq state
'needs-patch
))
187 (concat "SVN-" rev sticky-tag-printable
))
189 (concat "SVN:" state
":" rev sticky-tag-printable
))
191 ;; Not just for the 'edited state, but also a fallback
192 ;; for all other states. Think about different symbols
193 ;; for 'needs-patch and 'needs-merge.
194 (concat "SVN:" rev sticky-tag-printable
)))))
196 (defun vc-svn-dired-state-info (file)
197 "SVN-specific version of `vc-dired-state-info'."
198 (let ((svn-state (vc-state file
)))
199 (cond ((eq svn-state
'edited
)
200 (if (equal (vc-workfile-version file
) "0")
201 "(added)" "(modified)"))
202 ((eq svn-state
'needs-patch
) "(patch)")
203 ((eq svn-state
'needs-merge
) "(merge)"))))
207 ;;; State-changing functions
210 (defun vc-svn-register (file &optional rev comment
)
211 "Register FILE into the SVN version-control system.
212 COMMENT can be used to provide an initial description of FILE.
214 `vc-register-switches' and `vc-svn-register-switches' are passed to
215 the SVN command (in that order)."
216 (apply 'vc-svn-command nil
0 file
218 ;; (and comment (string-match "[^\t\n ]" comment)
219 ;; (concat "-m" comment))
220 (vc-switches 'SVN
'register
)))
222 (defun vc-svn-responsible-p (file)
223 "Return non-nil if SVN thinks it is responsible for FILE."
224 (file-directory-p (expand-file-name ".svn"
225 (if (file-directory-p file
)
227 (file-name-directory file
)))))
229 (defalias 'vc-svn-could-register
'vc-svn-responsible-p
230 "Return non-nil if FILE could be registered in SVN.
231 This is only possible if SVN is responsible for FILE's directory.")
233 (defun vc-svn-checkin (file rev comment
)
234 "SVN-specific version of `vc-backend-checkin'."
235 (let ((status (apply 'vc-svn-command nil
1 file
236 "ci" (list* "-m" comment
(vc-switches 'SVN
'checkin
)))))
238 (goto-char (point-min))
239 (unless (equal status
0)
240 ;; Check checkin problem.
242 ((search-forward "Transaction is out of date" nil t
)
243 (vc-file-setprop file
'vc-state
'needs-merge
)
244 (error (substitute-command-keys
245 (concat "Up-to-date check failed: "
246 "type \\[vc-next-action] to merge in changes"))))
248 (pop-to-buffer (current-buffer))
249 (goto-char (point-min))
250 (shrink-window-if-larger-than-buffer)
251 (error "Check-in failed"))))
252 ;; Update file properties
254 ;; file 'vc-workfile-version
255 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
258 (defun vc-svn-find-version (file rev buffer
)
259 (apply 'vc-svn-command
262 (and rev
(not (string= rev
""))
264 (vc-switches 'SVN
'checkout
)))
266 (defun vc-svn-checkout (file &optional editable rev
)
267 (message "Checking out %s..." file
)
268 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
269 (vc-call update file editable rev
(vc-switches 'SVN
'checkout
)))
271 (message "Checking out %s...done" file
))
273 (defun vc-svn-update (file editable rev switches
)
274 (if (and (file-exists-p file
) (not rev
))
275 ;; If no revision was specified, just make the file writable
276 ;; if necessary (using `svn-edit' if requested).
277 (and editable
(not (eq (vc-svn-checkout-model file
) 'implicit
))
279 (vc-svn-command nil
0 file
"edit")
280 (set-file-modes file
(logior (file-modes file
) 128))
281 (if (equal file buffer-file-name
) (toggle-read-only -
1))))
282 ;; Check out a particular version (or recreate the file).
283 (vc-file-setprop file
'vc-workfile-version nil
)
284 (apply 'vc-svn-command nil
0 file
287 ;; default for verbose checkout: clear the sticky tag so
288 ;; that the actual update will get the head of the trunk
289 (if (or (not rev
) (string= rev
""))
294 (defun vc-svn-rename-file (old new
)
295 (vc-svn-command nil
0 new
"move" (file-relative-name old
)))
297 (defun vc-svn-revert (file &optional contents-done
)
298 "Revert FILE to the version it was based on."
299 (unless contents-done
300 (vc-svn-command nil
0 file
"revert"))
301 (unless (eq (vc-checkout-model file
) 'implicit
)
303 (vc-svn-command nil
0 file
"unedit")
304 ;; Make the file read-only by switching off all w-bits
305 (set-file-modes file
(logand (file-modes file
) 3950)))))
307 (defun vc-svn-merge (file first-version
&optional second-version
)
308 "Merge changes into current working copy of FILE.
309 The changes are between FIRST-VERSION and SECOND-VERSION."
310 (vc-svn-command nil
0 file
312 "-r" (if second-version
313 (concat first-version
":" second-version
)
315 (vc-file-setprop file
'vc-state
'edited
)
316 (with-current-buffer (get-buffer "*vc*")
317 (goto-char (point-min))
318 (if (looking-at "C ")
320 0))) ; signal success
322 (defun vc-svn-merge-news (file)
323 "Merge in any new changes made to FILE."
324 (message "Merging changes into %s..." file
)
325 ;; (vc-file-setprop file 'vc-workfile-version nil)
326 (vc-file-setprop file
'vc-checkout-time
0)
327 (vc-svn-command nil
0 file
"update")
328 ;; Analyze the merge result reported by SVN, and set
329 ;; file properties accordingly.
330 (with-current-buffer (get-buffer "*vc*")
331 (goto-char (point-min))
332 ;; get new workfile version
333 (if (re-search-forward
334 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t
)
335 (vc-file-setprop file
'vc-workfile-version
(match-string 2))
336 (vc-file-setprop file
'vc-workfile-version nil
))
338 (goto-char (point-min))
340 (if (looking-at "At revision")
341 0 ;; there were no news; indicate success
342 (if (re-search-forward
343 (concat "^\\([CGDU] \\)?"
344 (regexp-quote (file-name-nondirectory file
)))
347 ;; Merge successful, we are in sync with repository now
348 ((string= (match-string 1) "U ")
349 (vc-file-setprop file
'vc-state
'up-to-date
)
350 (vc-file-setprop file
'vc-checkout-time
351 (nth 5 (file-attributes file
)))
352 0);; indicate success to the caller
353 ;; Merge successful, but our own changes are still in the file
354 ((string= (match-string 1) "G ")
355 (vc-file-setprop file
'vc-state
'edited
)
356 0);; indicate success to the caller
357 ;; Conflicts detected!
359 (vc-file-setprop file
'vc-state
'edited
)
360 1);; signal the error to the caller
362 (pop-to-buffer "*vc*")
363 (error "Couldn't analyze svn update result")))
364 (message "Merging changes into %s...done" file
))))
368 ;;; History functions
371 (defun vc-svn-print-log (file)
372 "Get change log associated with FILE."
374 (vc-setup-buffer nil
)
375 (let ((inhibit-read-only t
))
376 (goto-char (point-min))
377 ;; Add a line to tell log-view-mode what file this is.
378 (insert "Working file: " (file-relative-name file
) "\n"))
381 (if (and (vc-svn-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
384 (defun vc-svn-diff (file &optional oldvers newvers
)
385 "Get a difference report using SVN between two versions of FILE."
386 (if (string= (vc-workfile-version file
) "0")
387 ;; This file is added but not yet committed; there is no master file.
388 (if (or oldvers newvers
)
389 (error "No revisions of %s exist" file
)
390 ;; We regard this as "changed".
391 ;; Diff it against /dev/null.
392 ;; Note: this is NOT a "svn diff".
393 (apply 'vc-do-command
"*vc-diff*"
395 (append (vc-switches nil
'diff
) '("/dev/null")))
396 ;; Even if it's empty, it's locally modified.
398 (let* ((switches (vc-switches 'SVN
'diff
))
399 (async (and (vc-svn-stay-local-p file
)
400 (or oldvers newvers
) ; Svn diffs those locally.
401 (fboundp 'start-process
))))
402 (apply 'vc-svn-command
"*vc-diff*"
407 (list "-x" (mapconcat 'identity switches
" ")))
409 (list "-r" (if newvers
(concat oldvers
":" newvers
)
411 (if async
1 ; async diff => pessimistic assumption
412 ;; For some reason `svn diff' does not return a useful
413 ;; status w.r.t whether the diff was empty or not.
414 (buffer-size (get-buffer "*vc-diff*"))))))
416 (defun vc-svn-diff-tree (dir &optional rev1 rev2
)
417 "Diff all files at and below DIR."
418 (with-current-buffer "*vc-diff*"
419 (setq default-directory dir
)
420 (if (vc-svn-stay-local-p dir
)
421 ;; local diff: do it filewise, and only for files that are modified
426 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f
)))
427 ;; possible optimization: fetch the state of all files
428 ;; in the tree via vc-svn-dir-state-heuristic
429 (unless (vc-up-to-date-p ',f
)
430 (message "Looking at %s" ',f
)
431 (vc-diff-internal ',f
',rev1
',rev2
))))))
432 ;; svn diff: use a single call for the entire tree
433 (let ((coding-system-for-read (or coding-system-for-read
'undecided
))
434 (switches (vc-switches 'SVN
'diff
)))
435 (apply 'vc-svn-command
"*vc-diff*" 1 nil
"diff"
438 (list "-r" (if rev2
(concat rev1
":" rev2
) rev1
)))
440 (list "-x" (mapconcat 'identity switches
" ")))))))))
446 (defun vc-svn-create-snapshot (dir name branchp
)
447 "Assign to DIR's current version a given NAME.
448 If BRANCHP is non-nil, the name is created as a branch (and the current
449 workspace is immediately moved to that new branch)."
450 (vc-svn-command nil
0 dir
"tag" "-c" (if branchp
"-b") name
)
451 (when branchp
(vc-svn-command nil
0 dir
"update" "-r" name
)))
453 (defun vc-svn-retrieve-snapshot (dir name update
)
454 "Retrieve a snapshot at and below DIR.
455 NAME is the name of the snapshot; if it is empty, do a `svn update'.
456 If UPDATE is non-nil, then update (resynch) any affected buffers."
457 (with-current-buffer (get-buffer-create "*vc*")
458 (let ((default-directory dir
)
461 (if (or (not name
) (string= name
""))
462 (vc-svn-command t
0 nil
"update")
463 (vc-svn-command t
0 nil
"update" "-r" name
)
464 (setq sticky-tag name
))
466 (goto-char (point-min))
468 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
469 (let* ((file (expand-file-name (match-string 2) dir
))
470 (state (match-string 1))
471 (buffer (find-buffer-visiting file
)))
474 ((or (string= state
"U")
476 (vc-file-setprop file
'vc-state
'up-to-date
)
477 (vc-file-setprop file
'vc-workfile-version nil
)
478 (vc-file-setprop file
'vc-checkout-time
479 (nth 5 (file-attributes file
))))
480 ((or (string= state
"M")
482 (vc-file-setprop file
'vc-state
'edited
)
483 (vc-file-setprop file
'vc-workfile-version nil
)
484 (vc-file-setprop file
'vc-checkout-time
0)))
485 (vc-file-setprop file
'vc-svn-sticky-tag sticky-tag
)
486 (vc-resynch-buffer file t t
))))
487 (forward-line 1))))))
494 ;; Subversion makes backups for us, so don't bother.
495 ;; (defalias 'vc-svn-make-version-backups-p 'vc-svn-stay-local-p
496 ;; "Return non-nil if version backups should be made for FILE.")
498 (defun vc-svn-check-headers ()
499 "Check if the current file has any headers in it."
501 (goto-char (point-min))
502 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
503 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t
)))
507 ;;; Internal functions
510 (defun vc-svn-command (buffer okstatus file
&rest flags
)
511 "A wrapper around `vc-do-command' for use in vc-svn.el.
512 The difference to vc-do-command is that this function always invokes `svn',
513 and that it passes `vc-svn-global-switches' to it before FLAGS."
514 (apply 'vc-do-command buffer okstatus
"svn" file
515 (if (stringp vc-svn-global-switches
)
516 (cons vc-svn-global-switches flags
)
517 (append vc-svn-global-switches
520 (defun vc-svn-stay-local-p (file)
521 "Return non-nil if VC should stay local when handling FILE.
522 See `vc-svn-stay-local'."
523 (when vc-svn-stay-local
524 (let* ((dirname (if (file-directory-p file
)
525 (directory-file-name file
)
526 (file-name-directory file
)))
528 (or (vc-file-getprop dirname
'vc-svn-stay-local-p
)
530 dirname
'vc-svn-stay-local-p
531 (let ((rootname (expand-file-name ".svn/entries" dirname
)))
533 ((not (file-readable-p rootname
)) 'no
)
534 ((stringp vc-svn-stay-local
)
536 (let ((coding-system-for-read
537 (or file-name-coding-system
538 default-file-name-coding-system
)))
539 (vc-insert-file rootname
))
540 (goto-char (point-min))
541 (when (re-search-forward
542 (concat "name=\"svn:this_dir\"[\n\t ]*"
543 "url=\"\\([^\"]+\\)\"") nil t
)
544 (let ((hostname (match-string 1)))
547 (let* ((stay-local t
)
550 ;; vc-svn-stay-local: rx
551 ((stringp vc-svn-stay-local
)
553 ;; vc-svn-stay-local: '( [except] rx ... )
554 ((consp vc-svn-stay-local
)
557 (if (not (eq (car vc-svn-stay-local
)
560 (setq stay-local nil
)
561 (cdr vc-svn-stay-local
))
565 (if (not (string-match rx hostname
))
566 (setq stay-local
(not stay-local
)))
570 ;; vc-svn-stay-local is neither nil nor list nor string.
572 (if (eq prop
'yes
) t nil
))))
574 (defun vc-svn-parse-status (localp)
575 "Parse output of \"svn status\" command in the current buffer.
576 Set file properties accordingly. Unless FULL is t, parse only
577 essential information."
579 (goto-char (point-min))
580 (while (re-search-forward
581 "^[ ADMCI?!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t
)
582 (setq file
(expand-file-name
583 (buffer-substring (point) (line-end-position))))
584 (setq status
(char-after (line-beginning-position)))
585 (unless (eq status ??
)
586 (vc-file-setprop file
'vc-backend
'SVN
)
587 ;; Use the last-modified revision, so that searching in vc-print-log
589 (vc-file-setprop file
'vc-workfile-version
(match-string 3))
594 (if (eq (char-after (match-beginning 1)) ?
*)
596 (vc-file-setprop file
'vc-checkout-time
597 (nth 5 (file-attributes file
)))
600 ;; If the file was actually copied, (match-string 2) is "-".
601 (vc-file-setprop file
'vc-workfile-version
"0")
602 (vc-file-setprop file
'vc-checkout-time
0)
604 ((memq status
'(?M ?C
))
605 (if (eq (char-after (match-beginning 1)) ?
*)
610 (defun vc-svn-dir-state-heuristic (dir)
611 "Find the SVN state of all files in DIR, using only local information."
612 (vc-svn-dir-state dir
'local
))
614 (defun vc-svn-valid-symbolic-tag-name-p (tag)
615 "Return non-nil if TAG is a valid symbolic tag name."
616 ;; According to the SVN manual, a valid symbolic tag must start with
617 ;; an uppercase or lowercase letter and can contain uppercase and
618 ;; lowercase letters, digits, `-', and `_'.
619 (and (string-match "^[a-zA-Z]" tag
)
620 (not (string-match "[^a-z0-9A-Z-_]" tag
))))
622 (defun vc-svn-valid-version-number-p (tag)
623 "Return non-nil if TAG is a valid version number."
624 (and (string-match "^[0-9]" tag
)
625 (not (string-match "[^0-9]" tag
))))
629 ;;; vc-svn.el ends here