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