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