]> code.delx.au - gnu-emacs/blob - lisp/vc-svn.el
(vc-svn-print-log, vc-svn-diff): Add optional BUFFER arg.
[gnu-emacs] / lisp / vc-svn.el
1 ;;; vc-svn.el --- non-resident support for Subversion version-control
2
3 ;; Copyright (C) 1995,98,99,2000,2001,02,2003 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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, 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 "21.4"
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 "21.4"
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 "21.4"
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 "21.4"
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 "21.4"
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 (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.
120 (file-error nil))
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'."
126 (setq localp (or localp (vc-stay-local-p file)))
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."
139 (setq localp (or localp (vc-stay-local-p dir)))
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
160 ;; vc-svn-mode-line-string doesn't exist because the default implementation
161 ;; works just fine.
162
163 (defun vc-svn-dired-state-info (file)
164 "SVN-specific version of `vc-dired-state-info'."
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)"))))
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.
179 COMMENT can be used to provide an initial description of FILE.
180
181 `vc-register-switches' and `vc-svn-register-switches' are passed to
182 the SVN command (in that order)."
183 (apply 'vc-svn-command nil 0 file "add" (vc-switches 'SVN 'register)))
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.
194 This 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'."
198 (let ((status (apply 'vc-svn-command nil 1 file
199 "ci" (list* "-m" comment (vc-switches 'SVN 'checkin)))))
200 (set-buffer "*vc*")
201 (goto-char (point-min))
202 (unless (equal status 0)
203 ;; Check checkin problem.
204 (cond
205 ((search-forward "Transaction is out of date" nil t)
206 (vc-file-setprop file 'vc-state 'needs-merge)
207 (error (substitute-command-keys
208 (concat "Up-to-date check failed: "
209 "type \\[vc-next-action] to merge in changes"))))
210 (t
211 (pop-to-buffer (current-buffer))
212 (goto-char (point-min))
213 (shrink-window-if-larger-than-buffer)
214 (error "Check-in failed"))))
215 ;; Update file properties
216 ;; (vc-file-setprop
217 ;; file 'vc-workfile-version
218 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
219 ))
220
221 (defun vc-svn-find-version (file rev buffer)
222 (apply 'vc-svn-command
223 buffer 0 file
224 "cat"
225 (and rev (not (string= rev ""))
226 (concat "-r" rev))
227 (vc-switches 'SVN 'checkout)))
228
229 (defun vc-svn-checkout (file &optional editable rev)
230 (message "Checking out %s..." file)
231 (with-current-buffer (or (get-file-buffer file) (current-buffer))
232 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
233 (vc-mode-line file)
234 (message "Checking out %s...done" file))
235
236 (defun vc-svn-update (file editable rev switches)
237 (if (and (file-exists-p file) (not rev))
238 ;; If no revision was specified, just make the file writable
239 ;; if necessary (using `svn-edit' if requested).
240 (and editable (not (eq (vc-svn-checkout-model file) 'implicit))
241 (if vc-svn-use-edit
242 (vc-svn-command nil 0 file "edit")
243 (set-file-modes file (logior (file-modes file) 128))
244 (if (equal file buffer-file-name) (toggle-read-only -1))))
245 ;; Check out a particular version (or recreate the file).
246 (vc-file-setprop file 'vc-workfile-version nil)
247 (apply 'vc-svn-command nil 0 file
248 "update"
249 ;; default for verbose checkout: clear the sticky tag so
250 ;; that the actual update will get the head of the trunk
251 (cond
252 ((null rev) "-rBASE")
253 ((or (eq rev t) (equal rev "")) nil)
254 (t (concat "-r" rev)))
255 switches)))
256
257 (defun vc-svn-delete-file (file)
258 (vc-svn-command nil 0 file "remove"))
259
260 (defun vc-svn-rename-file (old new)
261 (vc-svn-command nil 0 new "move" (file-relative-name old)))
262
263 (defun vc-svn-revert (file &optional contents-done)
264 "Revert FILE to the version it was based on."
265 (unless contents-done
266 (vc-svn-command nil 0 file "revert"))
267 (unless (eq (vc-checkout-model file) 'implicit)
268 (if vc-svn-use-edit
269 (vc-svn-command nil 0 file "unedit")
270 ;; Make the file read-only by switching off all w-bits
271 (set-file-modes file (logand (file-modes file) 3950)))))
272
273 (defun vc-svn-merge (file first-version &optional second-version)
274 "Merge changes into current working copy of FILE.
275 The changes are between FIRST-VERSION and SECOND-VERSION."
276 (vc-svn-command nil 0 file
277 "merge"
278 "-r" (if second-version
279 (concat first-version ":" second-version)
280 first-version))
281 (vc-file-setprop file 'vc-state 'edited)
282 (with-current-buffer (get-buffer "*vc*")
283 (goto-char (point-min))
284 (if (looking-at "C ")
285 1 ; signal conflict
286 0))) ; signal success
287
288 (defun vc-svn-merge-news (file)
289 "Merge in any new changes made to FILE."
290 (message "Merging changes into %s..." file)
291 ;; (vc-file-setprop file 'vc-workfile-version nil)
292 (vc-file-setprop file 'vc-checkout-time 0)
293 (vc-svn-command nil 0 file "update")
294 ;; Analyze the merge result reported by SVN, and set
295 ;; file properties accordingly.
296 (with-current-buffer (get-buffer "*vc*")
297 (goto-char (point-min))
298 ;; get new workfile version
299 (if (re-search-forward
300 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
301 (vc-file-setprop file 'vc-workfile-version (match-string 2))
302 (vc-file-setprop file 'vc-workfile-version nil))
303 ;; get file status
304 (goto-char (point-min))
305 (prog1
306 (if (looking-at "At revision")
307 0 ;; there were no news; indicate success
308 (if (re-search-forward
309 (concat "^\\([CGDU] \\)?"
310 (regexp-quote (file-name-nondirectory file)))
311 nil t)
312 (cond
313 ;; Merge successful, we are in sync with repository now
314 ((string= (match-string 1) "U ")
315 (vc-file-setprop file 'vc-state 'up-to-date)
316 (vc-file-setprop file 'vc-checkout-time
317 (nth 5 (file-attributes file)))
318 0);; indicate success to the caller
319 ;; Merge successful, but our own changes are still in the file
320 ((string= (match-string 1) "G ")
321 (vc-file-setprop file 'vc-state 'edited)
322 0);; indicate success to the caller
323 ;; Conflicts detected!
324 (t
325 (vc-file-setprop file 'vc-state 'edited)
326 1);; signal the error to the caller
327 )
328 (pop-to-buffer "*vc*")
329 (error "Couldn't analyze svn update result")))
330 (message "Merging changes into %s...done" file))))
331
332
333 ;;;
334 ;;; History functions
335 ;;;
336
337 (defun vc-svn-print-log (file &optional buffer)
338 "Get change log associated with FILE."
339 (save-current-buffer
340 (vc-setup-buffer buffer)
341 (let ((inhibit-read-only t))
342 (goto-char (point-min))
343 ;; Add a line to tell log-view-mode what file this is.
344 (insert "Working file: " (file-relative-name file) "\n"))
345 (vc-svn-command
346 buffer
347 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
348 file "log")))
349
350 (defun vc-svn-diff (file &optional oldvers newvers buffer)
351 "Get a difference report using SVN between two versions of FILE."
352 (unless buffer (setq buffer "*vc-diff*"))
353 (if (string= (vc-workfile-version file) "0")
354 ;; This file is added but not yet committed; there is no master file.
355 (if (or oldvers newvers)
356 (error "No revisions of %s exist" file)
357 ;; We regard this as "changed".
358 ;; Diff it against /dev/null.
359 ;; Note: this is NOT a "svn diff".
360 (apply 'vc-do-command buffer
361 1 "diff" file
362 (append (vc-switches nil 'diff) '("/dev/null")))
363 ;; Even if it's empty, it's locally modified.
364 1)
365 (let* ((switches (vc-switches 'SVN 'diff))
366 (async (and (vc-stay-local-p file)
367 (or oldvers newvers) ; Svn diffs those locally.
368 (fboundp 'start-process))))
369 (apply 'vc-svn-command buffer
370 (if async 'async 0)
371 file "diff"
372 (append
373 (when switches
374 (list "-x" (mapconcat 'identity switches " ")))
375 (when oldvers
376 (list "-r" (if newvers (concat oldvers ":" newvers)
377 oldvers)))))
378 (if async 1 ; async diff => pessimistic assumption
379 ;; For some reason `svn diff' does not return a useful
380 ;; status w.r.t whether the diff was empty or not.
381 (buffer-size (get-buffer buffer))))))
382
383 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
384 "Diff all files at and below DIR."
385 (vc-svn-diff (file-name-as-directory dir) rev1 rev2))
386
387 ;;;
388 ;;; Snapshot system
389 ;;;
390
391 (defun vc-svn-create-snapshot (dir name branchp)
392 "Assign to DIR's current version a given NAME.
393 If BRANCHP is non-nil, the name is created as a branch (and the current
394 workspace is immediately moved to that new branch).
395 NAME is assumed to be a URL."
396 (vc-svn-command nil 0 dir "copy" name)
397 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
398
399 (defun vc-svn-retrieve-snapshot (dir name update)
400 "Retrieve a snapshot at and below DIR.
401 NAME is the name of the snapshot; if it is empty, do a `svn update'.
402 If UPDATE is non-nil, then update (resynch) any affected buffers.
403 NAME is assumed to be a URL."
404 (vc-svn-command nil 0 dir "switch" name)
405 ;; FIXME: parse the output and obey `update'.
406 )
407
408 ;;;
409 ;;; Miscellaneous
410 ;;;
411
412 ;; Subversion makes backups for us, so don't bother.
413 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
414 ;; "Return non-nil if version backups should be made for FILE.")
415
416 (defun vc-svn-check-headers ()
417 "Check if the current file has any headers in it."
418 (save-excursion
419 (goto-char (point-min))
420 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
421 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
422
423
424 ;;;
425 ;;; Internal functions
426 ;;;
427
428 (defun vc-svn-command (buffer okstatus file &rest flags)
429 "A wrapper around `vc-do-command' for use in vc-svn.el.
430 The difference to vc-do-command is that this function always invokes `svn',
431 and that it passes `vc-svn-global-switches' to it before FLAGS."
432 (apply 'vc-do-command buffer okstatus "svn" file
433 (if (stringp vc-svn-global-switches)
434 (cons vc-svn-global-switches flags)
435 (append vc-svn-global-switches
436 flags))))
437
438 (defun vc-svn-repository-hostname (dirname)
439 (with-temp-buffer
440 (let ((coding-system-for-read
441 (or file-name-coding-system
442 default-file-name-coding-system)))
443 (vc-insert-file (expand-file-name ".svn/entries" dirname)))
444 (goto-char (point-min))
445 (when (re-search-forward
446 (concat "name=\"svn:this_dir\"[\n\t ]*"
447 "\\([-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
448 "url=\"\\([^\"]+\\)\"") nil t)
449 (match-string 2))))
450
451 (defun vc-svn-parse-status (localp)
452 "Parse output of \"svn status\" command in the current buffer.
453 Set file properties accordingly. Unless FULL is t, parse only
454 essential information."
455 (let (file status)
456 (goto-char (point-min))
457 (while (re-search-forward
458 "^[ ADMCI?!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
459 (setq file (expand-file-name
460 (buffer-substring (point) (line-end-position))))
461 (setq status (char-after (line-beginning-position)))
462 (unless (eq status ??)
463 (vc-file-setprop file 'vc-backend 'SVN)
464 ;; Use the last-modified revision, so that searching in vc-print-log
465 ;; output works.
466 (vc-file-setprop file 'vc-workfile-version (match-string 3))
467 (vc-file-setprop
468 file 'vc-state
469 (cond
470 ((eq status ?\ )
471 (if (eq (char-after (match-beginning 1)) ?*)
472 'needs-patch
473 (vc-file-setprop file 'vc-checkout-time
474 (nth 5 (file-attributes file)))
475 'up-to-date))
476 ((eq status ?A)
477 ;; If the file was actually copied, (match-string 2) is "-".
478 (vc-file-setprop file 'vc-workfile-version "0")
479 (vc-file-setprop file 'vc-checkout-time 0)
480 'edited)
481 ((memq status '(?M ?C))
482 (if (eq (char-after (match-beginning 1)) ?*)
483 'needs-merge
484 'edited))
485 (t 'edited)))))))
486
487 (defun vc-svn-dir-state-heuristic (dir)
488 "Find the SVN state of all files in DIR, using only local information."
489 (vc-svn-dir-state dir 'local))
490
491 (defun vc-svn-valid-symbolic-tag-name-p (tag)
492 "Return non-nil if TAG is a valid symbolic tag name."
493 ;; According to the SVN manual, a valid symbolic tag must start with
494 ;; an uppercase or lowercase letter and can contain uppercase and
495 ;; lowercase letters, digits, `-', and `_'.
496 (and (string-match "^[a-zA-Z]" tag)
497 (not (string-match "[^a-z0-9A-Z-_]" tag))))
498
499 (defun vc-svn-valid-version-number-p (tag)
500 "Return non-nil if TAG is a valid version number."
501 (and (string-match "^[0-9]" tag)
502 (not (string-match "[^0-9]" tag))))
503
504 (provide 'vc-svn)
505
506 ;;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
507 ;;; vc-svn.el ends here