]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-svn.el
Merge from emacs-24; up to 2012-12-17T11:17:34Z!rgm@gnu.org
[gnu-emacs] / lisp / vc / vc-svn.el
1 ;;; vc-svn.el --- non-resident support for Subversion version-control
2
3 ;; Copyright (C) 2003-2013 Free Software Foundation, Inc.
4
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
7 ;; Package: vc
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Sync'd with Subversion's vc-svn.el as of revision 5801. but this version
27 ;; has been extensively modified since to handle filesets.
28
29 ;;; Code:
30
31 (eval-when-compile
32 (require 'vc))
33
34 ;; Clear up the cache to force vc-call to check again and discover
35 ;; new functions when we reload this file.
36 (put 'SVN 'vc-functions nil)
37
38 ;;;
39 ;;; Customization options
40 ;;;
41
42 (defgroup vc-svn nil
43 "VC Subversion (svn) backend."
44 :version "24.1"
45 :group 'vc)
46
47 ;; FIXME there is also svnadmin.
48 (defcustom vc-svn-program "svn"
49 "Name of the SVN executable."
50 :type 'string
51 :group 'vc-svn)
52
53 ;; Might be nice if svn defaulted to non-interactive if stdin not tty.
54 ;; http://svn.haxx.se/dev/archive-2008-05/0762.shtml
55 ;; http://svn.haxx.se/dev/archive-2009-04/0094.shtml
56 ;; Maybe newer ones do?
57 (defcustom vc-svn-global-switches (unless (eq system-type 'darwin) ; bug#13513
58 '("--non-interactive"))
59 "Global switches to pass to any SVN command.
60 The option \"--non-interactive\" is often needed to prevent SVN
61 hanging while prompting for authorization."
62 :type '(choice (const :tag "None" nil)
63 (string :tag "Argument String")
64 (repeat :tag "Argument List"
65 :value ("")
66 string))
67 :version "24.4"
68 :group 'vc-svn)
69
70 (defcustom vc-svn-register-switches nil
71 "Switches for registering a file into SVN.
72 A string or list of strings passed to the checkin program by
73 \\[vc-register]. If nil, use the value of `vc-register-switches'.
74 If t, use no switches."
75 :type '(choice (const :tag "Unspecified" nil)
76 (const :tag "None" t)
77 (string :tag "Argument String")
78 (repeat :tag "Argument List" :value ("") string))
79 :version "22.1"
80 :group 'vc-svn)
81
82 (defcustom vc-svn-diff-switches
83 t ;`svn' doesn't support common args like -c or -b.
84 "String or list of strings specifying extra switches for svn diff under VC.
85 If nil, use the value of `vc-diff-switches' (or `diff-switches'),
86 together with \"-x --diff-cmd=\"`diff-command' (since 'svn diff'
87 does not support the default \"-c\" value of `diff-switches').
88 If you want to force an empty list of arguments, use t."
89 :type '(choice (const :tag "Unspecified" nil)
90 (const :tag "None" t)
91 (string :tag "Argument String")
92 (repeat :tag "Argument List"
93 :value ("")
94 string))
95 :version "22.1"
96 :group 'vc-svn)
97
98 (defcustom vc-svn-header '("\$Id\$")
99 "Header keywords to be inserted by `vc-insert-headers'."
100 :version "24.1" ; no longer consult the obsolete vc-header-alist
101 :type '(repeat string)
102 :group 'vc-svn)
103
104 ;; We want to autoload it for use by the autoloaded version of
105 ;; vc-svn-registered, but we want the value to be compiled at startup, not
106 ;; at dump time.
107 ;; ;;;###autoload
108 (defconst vc-svn-admin-directory
109 (cond ((and (memq system-type '(cygwin windows-nt ms-dos))
110 (getenv "SVN_ASP_DOT_NET_HACK"))
111 "_svn")
112 (t ".svn"))
113 "The name of the \".svn\" subdirectory or its equivalent.")
114
115 ;;; Properties of the backend
116
117 (defun vc-svn-revision-granularity () 'repository)
118 (defun vc-svn-checkout-model (files) 'implicit)
119
120 ;;;
121 ;;; State-querying functions
122 ;;;
123
124 ;;; vc-svn-admin-directory is generally not defined when the
125 ;;; autoloaded function is called.
126
127 ;;;###autoload (defun vc-svn-registered (f)
128 ;;;###autoload (let ((admin-dir (cond ((and (eq system-type 'windows-nt)
129 ;;;###autoload (getenv "SVN_ASP_DOT_NET_HACK"))
130 ;;;###autoload "_svn")
131 ;;;###autoload (t ".svn"))))
132 ;;;###autoload (when (vc-find-root f admin-dir)
133 ;;;###autoload (load "vc-svn" nil t)
134 ;;;###autoload (vc-svn-registered f))))
135
136 (defun vc-svn-registered (file)
137 "Check if FILE is SVN registered."
138 (when (vc-svn-root file)
139 (with-temp-buffer
140 (cd (file-name-directory file))
141 (let* (process-file-side-effects
142 (status
143 (condition-case nil
144 ;; Ignore all errors.
145 (vc-svn-command t t file "status" "-v")
146 ;; Some problem happened. E.g. We can't find an `svn'
147 ;; executable. We used to only catch `file-error' but when
148 ;; the process is run on a remote host via Tramp, the error
149 ;; is only reported via the exit status which is turned into
150 ;; an `error' by vc-do-command.
151 (error nil))))
152 (when (eq 0 status)
153 (let ((parsed (vc-svn-parse-status file)))
154 (and parsed (not (memq parsed '(ignored unregistered))))))))))
155
156 (defun vc-svn-state (file &optional localp)
157 "SVN-specific version of `vc-state'."
158 (let (process-file-side-effects)
159 (setq localp (or localp (vc-stay-local-p file 'SVN)))
160 (with-temp-buffer
161 (cd (file-name-directory file))
162 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
163 (vc-svn-parse-status file))))
164
165 ;; NB this does not handle svn properties, which can be changed
166 ;; without changing the file timestamp.
167 ;; Note that unlike vc-cvs-state-heuristic, this is not called from
168 ;; vc-svn-state. AFAICS, it is only called from vc-state-refresh via
169 ;; vc-after-save (bug#7850). Therefore the fact that it ignores
170 ;; properties is irrelevant. If you want to make vc-svn-state call
171 ;; this, it should be extended to handle svn properties.
172 (defun vc-svn-state-heuristic (file)
173 "SVN-specific state heuristic."
174 ;; If the file has not changed since checkout, consider it `up-to-date'.
175 ;; Otherwise consider it `edited'. Copied from vc-cvs-state-heuristic.
176 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
177 (lastmod (nth 5 (file-attributes file))))
178 (cond
179 ((equal checkout-time lastmod) 'up-to-date)
180 ((string= (vc-working-revision file) "0") 'added)
181 ((null checkout-time) 'unregistered)
182 (t 'edited))))
183
184 ;; FIXME it would be better not to have the "remote" argument,
185 ;; but to distinguish the two output formats based on content.
186 (defun vc-svn-after-dir-status (callback &optional remote)
187 (let ((state-map '((?A . added)
188 (?C . conflict)
189 (?I . ignored)
190 (?M . edited)
191 (?D . removed)
192 (?R . removed)
193 (?? . unregistered)
194 ;; This is what vc-svn-parse-status does.
195 (?~ . edited)))
196 (re (if remote "^\\(.\\)\\(.\\).....? \\([ *]\\) +\\(?:[-0-9]+\\)? \\(.*\\)$"
197 ;; Subexp 3 is a dummy in this case, so the numbers match.
198 "^\\(.\\)\\(.\\)...\\(.\\) \\(.*\\)$"))
199 result)
200 (goto-char (point-min))
201 (while (re-search-forward re nil t)
202 (let ((state (cdr (assq (aref (match-string 1) 0) state-map)))
203 (propstat (cdr (assq (aref (match-string 2) 0) state-map)))
204 (filename (if (memq system-type '(windows-nt ms-dos))
205 (replace-regexp-in-string "\\\\" "/" (match-string 4))
206 (match-string 4))))
207 (and (memq propstat '(conflict edited))
208 (not (eq state 'conflict)) ; conflict always wins
209 (setq state propstat))
210 (and remote (string-equal (match-string 3) "*")
211 ;; FIXME are there other possible combinations?
212 (cond ((eq state 'edited) (setq state 'needs-merge))
213 ((not state) (setq state 'needs-update))))
214 (when (and state (not (string= "." filename)))
215 (setq result (cons (list filename state) result)))))
216 (funcall callback result)))
217
218 (defun vc-svn-dir-status (dir callback)
219 "Run 'svn status' for DIR and update BUFFER via CALLBACK.
220 CALLBACK is called as (CALLBACK RESULT BUFFER), where
221 RESULT is a list of conses (FILE . STATE) for directory DIR."
222 ;; FIXME should this rather be all the files in dir?
223 ;; FIXME: the vc-stay-local-p logic below is disabled, it ends up
224 ;; calling synchronously (vc-svn-registered DIR) => calling svn status -v DIR
225 ;; which is VERY SLOW for big trees and it makes emacs
226 ;; completely unresponsive during that time.
227 (let* ((local (and nil (vc-stay-local-p dir 'SVN)))
228 (remote (or t (not local) (eq local 'only-file))))
229 (vc-svn-command (current-buffer) 'async nil "status"
230 (if remote "-u"))
231 (vc-exec-after
232 `(vc-svn-after-dir-status (quote ,callback) ,remote))))
233
234 (defun vc-svn-dir-status-files (dir files default-state callback)
235 (apply 'vc-svn-command (current-buffer) 'async nil "status" files)
236 (vc-exec-after
237 `(vc-svn-after-dir-status (quote ,callback))))
238
239 (defun vc-svn-dir-extra-headers (dir)
240 "Generate extra status headers for a Subversion working copy."
241 (let (process-file-side-effects)
242 (vc-svn-command "*vc*" 0 nil "info"))
243 (let ((repo
244 (save-excursion
245 (and (progn
246 (set-buffer "*vc*")
247 (goto-char (point-min))
248 (re-search-forward "Repository Root: *\\(.*\\)" nil t))
249 (match-string 1)))))
250 (concat
251 (cond (repo
252 (concat
253 (propertize "Repository : " 'face 'font-lock-type-face)
254 (propertize repo 'face 'font-lock-variable-name-face)))
255 (t "")))))
256
257 (defun vc-svn-working-revision (file)
258 "SVN-specific version of `vc-working-revision'."
259 ;; There is no need to consult RCS headers under SVN, because we
260 ;; get the workfile version for free when we recognize that a file
261 ;; is registered in SVN.
262 (vc-svn-registered file)
263 (vc-file-getprop file 'vc-working-revision))
264
265 ;; vc-svn-mode-line-string doesn't exist because the default implementation
266 ;; works just fine.
267
268 (defun vc-svn-previous-revision (file rev)
269 (let ((newrev (1- (string-to-number rev))))
270 (when (< 0 newrev)
271 (number-to-string newrev))))
272
273 (defun vc-svn-next-revision (file rev)
274 (let ((newrev (1+ (string-to-number rev))))
275 ;; The "working revision" is an uneasy conceptual fit under Subversion;
276 ;; we use it as the upper bound until a better idea comes along. If the
277 ;; workfile version W coincides with the tree's latest revision R, then
278 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
279 ;; inhibits showing of W+1 through R, which could be considered anywhere
280 ;; from gracious to impolite.
281 (unless (< (string-to-number (vc-file-getprop file 'vc-working-revision))
282 newrev)
283 (number-to-string newrev))))
284
285
286 ;;;
287 ;;; State-changing functions
288 ;;;
289
290 (defun vc-svn-create-repo ()
291 "Create a new SVN repository."
292 (vc-do-command "*vc*" 0 "svnadmin" '("create" "SVN"))
293 (vc-svn-command "*vc*" 0 "." "checkout"
294 (concat "file://" default-directory "SVN")))
295
296 (defun vc-svn-register (files &optional rev comment)
297 "Register FILES into the SVN version-control system.
298 The COMMENT argument is ignored This does an add but not a commit.
299 Passes either `vc-svn-register-switches' or `vc-register-switches'
300 to the SVN command."
301 (apply 'vc-svn-command nil 0 files "add" (vc-switches 'SVN 'register)))
302
303 (defun vc-svn-root (file)
304 (vc-find-root file vc-svn-admin-directory))
305
306 (defalias 'vc-svn-responsible-p 'vc-svn-root)
307
308 (defalias 'vc-svn-could-register 'vc-svn-root
309 "Return non-nil if FILE could be registered in SVN.
310 This is only possible if SVN is responsible for FILE's directory.")
311
312 (defun vc-svn-checkin (files rev comment &optional extra-args-ignored)
313 "SVN-specific version of `vc-backend-checkin'."
314 (if rev (error "Committing to a specific revision is unsupported in SVN"))
315 (let ((status (apply
316 'vc-svn-command nil 1 files "ci"
317 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
318 (set-buffer "*vc*")
319 (goto-char (point-min))
320 (unless (equal status 0)
321 ;; Check checkin problem.
322 (cond
323 ((search-forward "Transaction is out of date" nil t)
324 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
325 files)
326 (error (substitute-command-keys
327 (concat "Up-to-date check failed: "
328 "type \\[vc-next-action] to merge in changes"))))
329 (t
330 (pop-to-buffer (current-buffer))
331 (goto-char (point-min))
332 (shrink-window-if-larger-than-buffer)
333 (error "Check-in failed"))))
334 ;; Update file properties
335 ;; (vc-file-setprop
336 ;; file 'vc-working-revision
337 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
338 ))
339
340 (defun vc-svn-find-revision (file rev buffer)
341 "SVN-specific retrieval of a specified version into a buffer."
342 (let (process-file-side-effects)
343 (apply 'vc-svn-command
344 buffer 0 file
345 "cat"
346 (and rev (not (string= rev ""))
347 (concat "-r" rev))
348 (vc-switches 'SVN 'checkout))))
349
350 (defun vc-svn-checkout (file &optional editable rev)
351 (message "Checking out %s..." file)
352 (with-current-buffer (or (get-file-buffer file) (current-buffer))
353 (vc-svn-update file editable rev (vc-switches 'SVN 'checkout)))
354 (vc-mode-line file 'SVN)
355 (message "Checking out %s...done" file))
356
357 (defun vc-svn-update (file editable rev switches)
358 (if (and (file-exists-p file) (not rev))
359 ;; If no revision was specified, there's nothing to do.
360 nil
361 ;; Check out a particular version (or recreate the file).
362 (vc-file-setprop file 'vc-working-revision nil)
363 (apply 'vc-svn-command nil 0 file
364 "update"
365 (cond
366 ((null rev) "-rBASE")
367 ((or (eq rev t) (equal rev "")) nil)
368 (t (concat "-r" rev)))
369 switches)))
370
371 (defun vc-svn-delete-file (file)
372 (vc-svn-command nil 0 file "remove"))
373
374 (defun vc-svn-rename-file (old new)
375 (vc-svn-command nil 0 new "move" (file-relative-name old)))
376
377 (defun vc-svn-revert (file &optional contents-done)
378 "Revert FILE to the version it was based on."
379 (unless contents-done
380 (vc-svn-command nil 0 file "revert")))
381
382 (defun vc-svn-merge (file first-version &optional second-version)
383 "Merge changes into current working copy of FILE.
384 The changes are between FIRST-VERSION and SECOND-VERSION."
385 (vc-svn-command nil 0 file
386 "merge"
387 "-r" (if second-version
388 (concat first-version ":" second-version)
389 first-version))
390 (vc-file-setprop file 'vc-state 'edited)
391 (with-current-buffer (get-buffer "*vc*")
392 (goto-char (point-min))
393 (if (looking-at "C ")
394 1 ; signal conflict
395 0))) ; signal success
396
397 (defun vc-svn-merge-news (file)
398 "Merge in any new changes made to FILE."
399 (message "Merging changes into %s..." file)
400 ;; (vc-file-setprop file 'vc-working-revision nil)
401 (vc-file-setprop file 'vc-checkout-time 0)
402 (vc-svn-command nil 0 file "update")
403 ;; Analyze the merge result reported by SVN, and set
404 ;; file properties accordingly.
405 (with-current-buffer (get-buffer "*vc*")
406 (goto-char (point-min))
407 ;; get new working revision
408 (if (re-search-forward
409 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
410 (vc-file-setprop file 'vc-working-revision (match-string 2))
411 (vc-file-setprop file 'vc-working-revision nil))
412 ;; get file status
413 (goto-char (point-min))
414 (prog1
415 (if (looking-at "At revision")
416 0 ;; there were no news; indicate success
417 (if (re-search-forward
418 ;; Newer SVN clients have 3 columns of chars (one for the
419 ;; file's contents, then second for its properties, and the
420 ;; third for lock-grabbing info), before the 2 spaces.
421 ;; We also used to match the filename in column 0 without any
422 ;; meta-info before it, but I believe this can never happen.
423 (concat "^\\(\\([ACGDU]\\)\\(.[B ]\\)? \\)"
424 (regexp-quote (file-relative-name file)))
425 nil t)
426 (cond
427 ;; Merge successful, we are in sync with repository now
428 ((string= (match-string 2) "U")
429 (vc-file-setprop file 'vc-state 'up-to-date)
430 (vc-file-setprop file 'vc-checkout-time
431 (nth 5 (file-attributes file)))
432 0);; indicate success to the caller
433 ;; Merge successful, but our own changes are still in the file
434 ((string= (match-string 2) "G")
435 (vc-file-setprop file 'vc-state 'edited)
436 0);; indicate success to the caller
437 ;; Conflicts detected!
438 (t
439 (vc-file-setprop file 'vc-state 'edited)
440 1);; signal the error to the caller
441 )
442 (pop-to-buffer "*vc*")
443 (error "Couldn't analyze svn update result")))
444 (message "Merging changes into %s...done" file))))
445
446 (defun vc-svn-modify-change-comment (files rev comment)
447 "Modify the change comments for a specified REV.
448 You must have ssh access to the repository host, and the directory Emacs
449 uses locally for temp files must also be writable by you on that host.
450 This is only supported if the repository access method is either file://
451 or svn+ssh://."
452 (let (tempfile host remotefile directory fileurl-p)
453 (with-temp-buffer
454 (vc-svn-command (current-buffer) 0 nil "info")
455 (goto-char (point-min))
456 (unless (re-search-forward "Repository Root: \\(file://\\(/.*\\)\\)\\|\\(svn\\+ssh://\\([^/]+\\)\\(/.*\\)\\)" nil t)
457 (error "Repository information is unavailable"))
458 (if (match-string 1)
459 (progn
460 (setq fileurl-p t)
461 (setq directory (match-string 2)))
462 (setq host (match-string 4))
463 (setq directory (match-string 5))
464 (setq remotefile (concat host ":" tempfile))))
465 (with-temp-file (setq tempfile (make-temp-file user-mail-address))
466 (insert comment))
467 (if fileurl-p
468 ;; Repository Root is a local file.
469 (progn
470 (unless (vc-do-command
471 "*vc*" 0 "svnadmin" nil
472 "setlog" "--bypass-hooks" directory
473 "-r" rev (format "%s" tempfile))
474 (error "Log edit failed"))
475 (delete-file tempfile))
476
477 ;; Remote repository, using svn+ssh.
478 (unless (vc-do-command "*vc*" 0 "scp" nil "-q" tempfile remotefile)
479 (error "Copy of comment to %s failed" remotefile))
480 (unless (vc-do-command
481 "*vc*" 0 "ssh" nil "-q" host
482 (format "svnadmin setlog --bypass-hooks %s -r %s %s; rm %s"
483 directory rev tempfile tempfile))
484 (error "Log edit failed")))))
485
486 ;;;
487 ;;; History functions
488 ;;;
489
490 (defvar log-view-per-file-logs)
491
492 (define-derived-mode vc-svn-log-view-mode log-view-mode "SVN-Log-View"
493 (require 'add-log)
494 (set (make-local-variable 'log-view-per-file-logs) nil))
495
496 (defun vc-svn-print-log (files buffer &optional shortlog start-revision limit)
497 "Get change log(s) associated with FILES."
498 (save-current-buffer
499 (vc-setup-buffer buffer)
500 (let ((inhibit-read-only t))
501 (goto-char (point-min))
502 (if files
503 (dolist (file files)
504 (insert "Working file: " file "\n")
505 (apply
506 'vc-svn-command
507 buffer
508 'async
509 ;; (if (and (= (length files) 1) (vc-stay-local-p file 'SVN)) 'async 0)
510 (list file)
511 "log"
512 (append
513 (list
514 (if start-revision
515 (format "-r%s" start-revision)
516 ;; By default Subversion only shows the log up to the
517 ;; working revision, whereas we also want the log of the
518 ;; subsequent commits. At least that's what the
519 ;; vc-cvs.el code does.
520 "-rHEAD:0"))
521 (when limit (list "--limit" (format "%s" limit))))))
522 ;; Dump log for the entire directory.
523 (apply 'vc-svn-command buffer 0 nil "log"
524 (append
525 (list
526 (if start-revision (format "-r%s" start-revision) "-rHEAD:0"))
527 (when limit (list "--limit" (format "%s" limit)))))))))
528
529 (defun vc-svn-diff (files &optional oldvers newvers buffer)
530 "Get a difference report using SVN between two revisions of fileset FILES."
531 (and oldvers
532 (not newvers)
533 files
534 (catch 'no
535 (dolist (f files)
536 (or (equal oldvers (vc-working-revision f))
537 (throw 'no nil)))
538 t)
539 ;; Use nil rather than the current revision because svn handles
540 ;; it better (i.e. locally). Note that if _any_ of the files
541 ;; has a different revision, we fetch the lot, which is
542 ;; obviously sub-optimal.
543 (setq oldvers nil))
544 (let* ((switches
545 (if vc-svn-diff-switches
546 (vc-switches 'SVN 'diff)
547 (list (concat "--diff-cmd=" diff-command) "-x"
548 (mapconcat 'identity (vc-switches nil 'diff) " "))))
549 (async (and (not vc-disable-async-diff)
550 (vc-stay-local-p files 'SVN)
551 (or oldvers newvers)))) ; Svn diffs those locally.
552 (apply 'vc-svn-command buffer
553 (if async 'async 0)
554 files "diff"
555 (append
556 switches
557 (when oldvers
558 (list "-r" (if newvers (concat oldvers ":" newvers)
559 oldvers)))))
560 (if async 1 ; async diff => pessimistic assumption
561 ;; For some reason `svn diff' does not return a useful
562 ;; status w.r.t whether the diff was empty or not.
563 (buffer-size (get-buffer buffer)))))
564
565 ;;;
566 ;;; Tag system
567 ;;;
568
569 (defun vc-svn-create-tag (dir name branchp)
570 "Assign to DIR's current revision a given NAME.
571 If BRANCHP is non-nil, the name is created as a branch (and the current
572 workspace is immediately moved to that new branch).
573 NAME is assumed to be a URL."
574 (vc-svn-command nil 0 dir "copy" name)
575 (when branchp (vc-svn-retrieve-tag dir name nil)))
576
577 (defun vc-svn-retrieve-tag (dir name update)
578 "Retrieve a tag at and below DIR.
579 NAME is the name of the tag; if it is empty, do a `svn update'.
580 If UPDATE is non-nil, then update (resynch) any affected buffers.
581 NAME is assumed to be a URL."
582 (vc-svn-command nil 0 dir "switch" name)
583 ;; FIXME: parse the output and obey `update'.
584 )
585
586 ;;;
587 ;;; Miscellaneous
588 ;;;
589
590 ;; Subversion makes backups for us, so don't bother.
591 ;; (defun vc-svn-make-version-backups-p (file)
592 ;; "Return non-nil if version backups should be made for FILE."
593 ;; (vc-stay-local-p file 'SVN))
594
595 (defun vc-svn-check-headers ()
596 "Check if the current file has any headers in it."
597 (save-excursion
598 (goto-char (point-min))
599 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
600 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
601
602
603 ;;;
604 ;;; Internal functions
605 ;;;
606
607 (defun vc-svn-command (buffer okstatus file-or-list &rest flags)
608 "A wrapper around `vc-do-command' for use in vc-svn.el.
609 The difference to vc-do-command is that this function always invokes `svn',
610 and that it passes `vc-svn-global-switches' to it before FLAGS."
611 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-svn-program file-or-list
612 (if (stringp vc-svn-global-switches)
613 (cons vc-svn-global-switches flags)
614 (append vc-svn-global-switches flags))))
615
616 (defun vc-svn-repository-hostname (dirname)
617 (with-temp-buffer
618 (let (process-file-side-effects)
619 (vc-svn-command t t dirname "info" "--xml"))
620 (goto-char (point-min))
621 (when (re-search-forward "<url>\\(.*\\)</url>" nil t)
622 ;; This is not a hostname but a URL. This may actually be considered
623 ;; as a feature since it allows vc-svn-stay-local to specify different
624 ;; behavior for different modules on the same server.
625 (match-string 1))))
626
627 (defun vc-svn-resolve-when-done ()
628 "Call \"svn resolved\" if the conflict markers have been removed."
629 (save-excursion
630 (goto-char (point-min))
631 (unless (re-search-forward "^<<<<<<< " nil t)
632 (vc-svn-command nil 0 buffer-file-name "resolved")
633 ;; Remove the hook so that it is not called multiple times.
634 (remove-hook 'after-save-hook 'vc-svn-resolve-when-done t))))
635
636 ;; Inspired by vc-arch-find-file-hook.
637 (defun vc-svn-find-file-hook ()
638 (when (eq ?C (vc-file-getprop buffer-file-name 'vc-svn-status))
639 ;; If the file is marked as "conflicted", then we should try and call
640 ;; "svn resolved" when applicable.
641 (if (save-excursion
642 (goto-char (point-min))
643 (re-search-forward "^<<<<<<< " nil t))
644 ;; There are conflict markers.
645 (progn
646 (smerge-start-session)
647 (add-hook 'after-save-hook 'vc-svn-resolve-when-done nil t))
648 ;; There are no conflict markers. This is problematic: maybe it means
649 ;; the conflict has been resolved and we should immediately call "svn
650 ;; resolved", or it means that the file's type does not allow Svn to
651 ;; use conflict markers in which case we don't really know what to do.
652 ;; So let's just punt for now.
653 nil)
654 (message "There are unresolved conflicts in this file")))
655
656 (defun vc-svn-parse-status (&optional filename)
657 "Parse output of \"svn status\" command in the current buffer.
658 Set file properties accordingly. Unless FILENAME is non-nil, parse only
659 information about FILENAME and return its status."
660 (let (file status propstat)
661 (goto-char (point-min))
662 (while (re-search-forward
663 ;; Ignore the files with status X.
664 "^\\(?:\\?\\|[ ACDGIMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\)\\) +" nil t)
665 ;; If the username contains spaces, the output format is ambiguous,
666 ;; so don't trust the output's filename unless we have to.
667 (setq file (or filename
668 (expand-file-name
669 (buffer-substring (point) (line-end-position)))))
670 (setq status (char-after (line-beginning-position))
671 ;; Status of the item's properties ([ MC]).
672 propstat (char-after (1+ (line-beginning-position))))
673 (if (eq status ??)
674 (vc-file-setprop file 'vc-state 'unregistered)
675 ;; Use the last-modified revision, so that searching in vc-print-log
676 ;; output works.
677 (vc-file-setprop file 'vc-working-revision (match-string 3))
678 ;; Remember Svn's own status.
679 (vc-file-setprop file 'vc-svn-status status)
680 (vc-file-setprop
681 file 'vc-state
682 (cond
683 ((and (eq status ?\ ) (eq propstat ?\ ))
684 (if (eq (char-after (match-beginning 1)) ?*)
685 'needs-update
686 (vc-file-setprop file 'vc-checkout-time
687 (nth 5 (file-attributes file)))
688 'up-to-date))
689 ((eq status ?A)
690 ;; If the file was actually copied, (match-string 2) is "-".
691 (vc-file-setprop file 'vc-working-revision "0")
692 (vc-file-setprop file 'vc-checkout-time 0)
693 'added)
694 ;; Conflict in contents or properties.
695 ((or (eq status ?C) (eq propstat ?C))
696 (vc-file-setprop file 'vc-state 'conflict))
697 ;; Modified contents or properties.
698 ((or (eq status ?M) (eq propstat ?M))
699 (if (eq (char-after (match-beginning 1)) ?*)
700 'needs-merge
701 'edited))
702 ((eq status ?I)
703 (vc-file-setprop file 'vc-state 'ignored))
704 ((memq status '(?D ?R))
705 (vc-file-setprop file 'vc-state 'removed))
706 (t 'edited)))))
707 (when filename (vc-file-getprop filename 'vc-state))))
708
709 (defun vc-svn-valid-symbolic-tag-name-p (tag)
710 "Return non-nil if TAG is a valid symbolic tag name."
711 ;; According to the SVN manual, a valid symbolic tag must start with
712 ;; an uppercase or lowercase letter and can contain uppercase and
713 ;; lowercase letters, digits, `-', and `_'.
714 (and (string-match "^[a-zA-Z]" tag)
715 (not (string-match "[^a-z0-9A-Z-_]" tag))))
716
717 (defun vc-svn-valid-revision-number-p (tag)
718 "Return non-nil if TAG is a valid revision number."
719 (and (string-match "^[0-9]" tag)
720 (not (string-match "[^0-9]" tag))))
721
722 ;; Support for `svn annotate'
723
724 (defun vc-svn-annotate-command (file buf &optional rev)
725 (vc-svn-command buf 'async file "annotate" (if rev (concat "-r" rev))))
726
727 (defun vc-svn-annotate-time-of-rev (rev)
728 ;; Arbitrarily assume 10 commits per day.
729 (/ (string-to-number rev) 10.0))
730
731 (defvar vc-annotate-parent-rev)
732
733 (defun vc-svn-annotate-current-time ()
734 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
735
736 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
737
738 (defun vc-svn-annotate-time ()
739 (when (looking-at vc-svn-annotate-re)
740 (goto-char (match-end 0))
741 (vc-svn-annotate-time-of-rev (match-string 1))))
742
743 (defun vc-svn-annotate-extract-revision-at-line ()
744 (save-excursion
745 (beginning-of-line)
746 (if (looking-at vc-svn-annotate-re) (match-string 1))))
747
748 (defun vc-svn-revision-table (files)
749 (let ((vc-svn-revisions '()))
750 (with-current-buffer "*vc*"
751 (vc-svn-command nil 0 files "log" "-q")
752 (goto-char (point-min))
753 (forward-line)
754 (let ((start (point-min))
755 (loglines (buffer-substring-no-properties (point-min)
756 (point-max))))
757 (while (string-match "^r\\([0-9]+\\) " loglines)
758 (push (match-string 1 loglines) vc-svn-revisions)
759 (setq start (+ start (match-end 0)))
760 (setq loglines (buffer-substring-no-properties start (point-max)))))
761 vc-svn-revisions)))
762
763 (provide 'vc-svn)
764
765 ;;; vc-svn.el ends here