Make dired-do-compress understand files with spaces in them
[emacs.git] / lisp / vc / vc-svn.el
blobc5b817d2529a122f69a7535eced4b703e75d48ba
1 ;;; vc-svn.el --- non-resident support for Subversion version-control -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003-2016 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)
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 (?? . unregistered)
188 ;; This is what vc-svn-parse-status does.
189 (?~ . edited)))
190 (re (if remote "^\\(.\\)\\(.\\).....? \\([ *]\\) +\\(?:[-0-9]+\\)? \\(.*\\)$"
191 ;; Subexp 3 is a dummy in this case, so the numbers match.
192 "^\\(.\\)\\(.\\)...\\(.\\).? \\(.*\\)$"))
193 result)
194 (goto-char (point-min))
195 (while (re-search-forward re nil t)
196 (let ((state (cdr (assq (aref (match-string 1) 0) state-map)))
197 (propstat (cdr (assq (aref (match-string 2) 0) state-map)))
198 (filename (if (memq system-type '(windows-nt ms-dos))
199 (replace-regexp-in-string "\\\\" "/" (match-string 4))
200 (match-string 4))))
201 (and (memq propstat '(conflict edited))
202 (not (eq state 'conflict)) ; conflict always wins
203 (setq state propstat))
204 (and remote (string-equal (match-string 3) "*")
205 ;; FIXME are there other possible combinations?
206 (cond ((eq state 'edited) (setq state 'needs-merge))
207 ((not state) (setq state 'needs-update))))
208 (when (and state (not (string= "." filename)))
209 (setq result (cons (list filename state) result)))))
210 (funcall callback result)))
212 ;; dir-status-files called from vc-dir, which loads vc,
213 ;; which loads vc-dispatcher.
214 (declare-function vc-exec-after "vc-dispatcher" (code))
216 (autoload 'vc-expand-dirs "vc")
218 (defun vc-svn-dir-status-files (_dir files 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 shouldn't this rather default to all the files in dir?
223 (apply #'vc-svn-command (current-buffer) 'async nil "status" "-u" files)
224 (vc-run-delayed (vc-svn-after-dir-status callback t)))
226 (defun vc-svn-dir-extra-headers (_dir)
227 "Generate extra status headers for a Subversion working copy."
228 (let (process-file-side-effects)
229 (vc-svn-command "*vc*" 0 nil "info"))
230 (let ((repo
231 (save-excursion
232 (and (progn
233 (set-buffer "*vc*")
234 (goto-char (point-min))
235 (re-search-forward "Repository Root: *\\(.*\\)" nil t))
236 (match-string 1)))))
237 (concat
238 (cond (repo
239 (concat
240 (propertize "Repository : " 'face 'font-lock-type-face)
241 (propertize repo 'face 'font-lock-variable-name-face)))
242 (t "")))))
244 (defun vc-svn-working-revision (file)
245 "SVN-specific version of `vc-working-revision'."
246 ;; There is no need to consult RCS headers under SVN, because we
247 ;; get the workfile version for free when we recognize that a file
248 ;; is registered in SVN.
249 (vc-svn-registered file)
250 (vc-file-getprop file 'vc-working-revision))
252 ;; vc-svn-mode-line-string doesn't exist because the default implementation
253 ;; works just fine.
255 (defun vc-svn-previous-revision (_file rev)
256 (let ((newrev (1- (string-to-number rev))))
257 (when (< 0 newrev)
258 (number-to-string newrev))))
260 (defun vc-svn-next-revision (file rev)
261 (let ((newrev (1+ (string-to-number rev))))
262 ;; The "working revision" is an uneasy conceptual fit under Subversion;
263 ;; we use it as the upper bound until a better idea comes along. If the
264 ;; workfile version W coincides with the tree's latest revision R, then
265 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
266 ;; inhibits showing of W+1 through R, which could be considered anywhere
267 ;; from gracious to impolite.
268 (unless (< (string-to-number (vc-file-getprop file 'vc-working-revision))
269 newrev)
270 (number-to-string newrev))))
274 ;;; State-changing functions
277 (defun vc-svn-create-repo ()
278 "Create a new SVN repository."
279 (vc-do-command "*vc*" 0 "svnadmin" '("create" "SVN"))
280 ;; Expand default-directory because svn gets confused by eg
281 ;; file://~/path/to/file. (Bug#15446).
282 (vc-svn-command "*vc*" 0 "." "checkout"
283 (let ((defdir (expand-file-name default-directory))
284 (svn-prog (executable-find "svn")))
285 (when (and (fboundp 'w32-application-type)
286 (eq (w32-application-type svn-prog) 'msys))
287 (setq defdir
288 (replace-regexp-in-string "^\\(.\\):/" "/\\1/"
289 defdir)))
290 (concat (if (and (stringp defdir)
291 (eq (aref defdir 0) ?/))
292 "file://"
293 ;; MS-Windows files d:/foo/bar need to
294 ;; begin with 3 leading slashes.
295 "file:///")
296 defdir
297 "SVN"))))
299 (autoload 'vc-switches "vc")
301 (defun vc-svn-register (files &optional _comment)
302 "Register FILES into the SVN version-control system.
303 The COMMENT argument is ignored This does an add but not a commit.
304 Passes either `vc-svn-register-switches' or `vc-register-switches'
305 to the SVN command."
306 (apply 'vc-svn-command nil 0 files "add" (vc-switches 'SVN 'register)))
308 (defun vc-svn-root (file)
309 (vc-find-root file vc-svn-admin-directory))
311 (defalias 'vc-svn-responsible-p 'vc-svn-root)
313 (declare-function log-edit-extract-headers "log-edit" (headers string))
315 (defun vc-svn-checkin (files comment &optional _extra-args-ignored)
316 "SVN-specific version of `vc-backend-checkin'."
317 (let ((status (apply
318 'vc-svn-command nil 1 files "ci"
319 (nconc (cons "-m" (log-edit-extract-headers nil comment))
320 (vc-switches 'SVN 'checkin)))))
321 (set-buffer "*vc*")
322 (goto-char (point-min))
323 (unless (equal status 0)
324 ;; Check checkin problem.
325 (cond
326 ((search-forward "Transaction is out of date" nil t)
327 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
328 files)
329 (error (substitute-command-keys
330 (concat "Up-to-date check failed: "
331 "type \\[vc-next-action] to merge in changes"))))
333 (pop-to-buffer (current-buffer))
334 (goto-char (point-min))
335 (shrink-window-if-larger-than-buffer)
336 (error "Check-in failed"))))
337 ;; Update file properties
338 ;; (vc-file-setprop
339 ;; file 'vc-working-revision
340 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
343 (defun vc-svn-find-revision (file rev buffer)
344 "SVN-specific retrieval of a specified version into a buffer."
345 (let (process-file-side-effects)
346 (apply 'vc-svn-command
347 buffer 0 file
348 "cat"
349 (and rev (not (string= rev ""))
350 (concat "-r" rev))
351 (vc-switches 'SVN 'checkout))))
353 (defun vc-svn-ignore (file &optional directory remove)
354 "Ignore FILE under Subversion.
355 FILE is a file wildcard, relative to the root directory of DIRECTORY."
356 (let* ((ignores (vc-svn-ignore-completion-table directory))
357 (file (file-relative-name file directory))
358 (ignores (if remove
359 (delete file ignores)
360 (push file ignores))))
361 (vc-svn-command nil 0 nil nil "propset" "svn:ignore"
362 (mapconcat #'identity ignores "\n")
363 (expand-file-name directory))))
365 (defun vc-svn-ignore-completion-table (directory)
366 "Return the list of ignored files in DIRECTORY."
367 (with-temp-buffer
368 (vc-svn-command t t nil "propget" "svn:ignore" (expand-file-name directory))
369 (split-string (buffer-string))))
371 (defun vc-svn-find-admin-dir (file)
372 "Return the administrative directory of FILE."
373 (expand-file-name vc-svn-admin-directory (vc-svn-root file)))
375 (defun vc-svn-checkout (file &optional rev)
376 (message "Checking out %s..." file)
377 (with-current-buffer (or (get-file-buffer file) (current-buffer))
378 (vc-svn-update file rev (vc-switches 'SVN 'checkout)))
379 (vc-mode-line file 'SVN)
380 (message "Checking out %s...done" file))
382 (defun vc-svn-update (file rev switches)
383 (if (and (file-exists-p file) (not rev))
384 ;; If no revision was specified, there's nothing to do.
386 ;; Check out a particular version (or recreate the file).
387 (vc-file-setprop file 'vc-working-revision nil)
388 (apply 'vc-svn-command nil 0 file
389 "update"
390 (cond
391 ((null rev) "-rBASE")
392 ((or (eq rev t) (equal rev "")) nil)
393 (t (concat "-r" rev)))
394 switches)))
396 (defun vc-svn-delete-file (file)
397 (vc-svn-command nil 0 file "remove"))
399 (defun vc-svn-rename-file (old new)
400 (vc-svn-command nil 0 new "move" (file-relative-name old)))
402 (defun vc-svn-revert (file &optional contents-done)
403 "Revert FILE to the version it was based on."
404 (unless contents-done
405 (vc-svn-command nil 0 file "revert")))
407 (autoload 'vc-read-revision "vc")
409 (defun vc-svn-merge-file (file)
410 "Accept a file merge request, prompting for revisions."
411 (let* ((first-revision
412 (vc-read-revision
413 (concat "Merge " file
414 " from SVN revision "
415 "(default news on current branch): ")
416 (list file)
417 'SVN))
418 second-revision
419 status)
420 (cond
421 ((string= first-revision "")
422 (setq status (vc-svn-merge-news file)))
424 (setq second-revision
425 (vc-read-revision
426 "Second SVN revision: "
427 (list file) 'SVN nil
428 first-revision))
429 (setq status (vc-svn-merge file first-revision second-revision))))
430 status))
432 (defun vc-svn-merge (file first-version &optional second-version)
433 "Merge changes into current working copy of FILE.
434 The changes are between FIRST-VERSION and SECOND-VERSION."
435 (vc-svn-command nil 0 file
436 "merge"
437 "-r" (if second-version
438 (concat first-version ":" second-version)
439 first-version))
440 (vc-file-setprop file 'vc-state 'edited)
441 (with-current-buffer (get-buffer "*vc*")
442 (goto-char (point-min))
443 (if (looking-at "C ")
444 1 ; signal conflict
445 0))) ; signal success
447 (defun vc-svn-merge-news (file)
448 "Merge in any new changes made to FILE."
449 (message "Merging changes into %s..." file)
450 ;; (vc-file-setprop file 'vc-working-revision nil)
451 (vc-file-setprop file 'vc-checkout-time 0)
452 (vc-svn-command nil 0 file "update")
453 ;; Analyze the merge result reported by SVN, and set
454 ;; file properties accordingly.
455 (with-current-buffer (get-buffer "*vc*")
456 (goto-char (point-min))
457 ;; get new working revision
458 (if (re-search-forward
459 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
460 (vc-file-setprop file 'vc-working-revision (match-string 2))
461 (vc-file-setprop file 'vc-working-revision nil))
462 ;; get file status
463 (goto-char (point-min))
464 (prog1
465 (if (looking-at "At revision")
466 0 ;; there were no news; indicate success
467 (if (re-search-forward
468 ;; Newer SVN clients have 3 columns of chars (one for the
469 ;; file's contents, then second for its properties, and the
470 ;; third for lock-grabbing info), before the 2 spaces.
471 ;; We also used to match the filename in column 0 without any
472 ;; meta-info before it, but I believe this can never happen.
473 (concat "^\\(\\([ACGDU]\\)\\(.[B ]\\)? \\)"
474 (regexp-quote (file-relative-name file)))
475 nil t)
476 (cond
477 ;; Merge successful, we are in sync with repository now
478 ((string= (match-string 2) "U")
479 (vc-file-setprop file 'vc-state 'up-to-date)
480 (vc-file-setprop file 'vc-checkout-time
481 (nth 5 (file-attributes file)))
482 0);; indicate success to the caller
483 ;; Merge successful, but our own changes are still in the file
484 ((string= (match-string 2) "G")
485 (vc-file-setprop file 'vc-state 'edited)
486 0);; indicate success to the caller
487 ;; Conflicts detected!
489 (vc-file-setprop file 'vc-state 'edited)
490 1);; signal the error to the caller
492 (pop-to-buffer "*vc*")
493 (error "Couldn't analyze svn update result")))
494 (message "Merging changes into %s...done" file))))
496 (defun vc-svn-modify-change-comment (_files rev comment)
497 "Modify the change comments for a specified REV.
498 You must have ssh access to the repository host, and the directory Emacs
499 uses locally for temp files must also be writable by you on that host.
500 This is only supported if the repository access method is either file://
501 or svn+ssh://."
502 (let (tempfile host remotefile directory fileurl-p)
503 (with-temp-buffer
504 (vc-svn-command (current-buffer) 0 nil "info")
505 (goto-char (point-min))
506 (unless (re-search-forward "Repository Root: \\(file://\\(/.*\\)\\)\\|\\(svn\\+ssh://\\([^/]+\\)\\(/.*\\)\\)" nil t)
507 (error "Repository information is unavailable"))
508 (if (match-string 1)
509 (progn
510 (setq fileurl-p t)
511 (setq directory (match-string 2)))
512 (setq host (match-string 4))
513 (setq directory (match-string 5))
514 (setq remotefile (concat host ":" tempfile))))
515 (with-temp-file (setq tempfile (make-temp-file user-mail-address))
516 (insert comment))
517 (if fileurl-p
518 ;; Repository Root is a local file.
519 (progn
520 (unless (vc-do-command
521 "*vc*" 0 "svnadmin" nil
522 "setlog" "--bypass-hooks" directory
523 "-r" rev (format "%s" tempfile))
524 (error "Log edit failed"))
525 (delete-file tempfile))
527 ;; Remote repository, using svn+ssh.
528 (unless (vc-do-command "*vc*" 0 "scp" nil "-q" tempfile remotefile)
529 (error "Copy of comment to %s failed" remotefile))
530 (unless (vc-do-command
531 "*vc*" 0 "ssh" nil "-q" host
532 (format "svnadmin setlog --bypass-hooks %s -r %s %s; rm %s"
533 directory rev tempfile tempfile))
534 (error "Log edit failed")))))
537 ;;; History functions
540 (defvar log-view-per-file-logs)
542 (define-derived-mode vc-svn-log-view-mode log-view-mode "SVN-Log-View"
543 (require 'add-log)
544 (set (make-local-variable 'log-view-per-file-logs) nil))
546 (autoload 'vc-setup-buffer "vc-dispatcher")
548 (defun vc-svn-print-log (files buffer &optional _shortlog start-revision limit)
549 "Print commit log associated with FILES into specified BUFFER.
550 SHORTLOG is ignored.
551 If START-REVISION is non-nil, it is the newest revision to show.
552 If LIMIT is non-nil, show no more than this many entries."
553 (save-current-buffer
554 (vc-setup-buffer buffer)
555 (let ((inhibit-read-only t))
556 (goto-char (point-min))
557 (if files
558 (dolist (file files)
559 (insert "Working file: " file "\n")
560 (apply
561 'vc-svn-command
562 buffer
563 'async
564 (list file)
565 "log"
566 (append
567 (list
568 (if start-revision
569 (format "-r%s:1" start-revision)
570 ;; By default Subversion only shows the log up to the
571 ;; working revision, whereas we also want the log of the
572 ;; subsequent commits. At least that's what the
573 ;; vc-cvs.el code does.
574 "-rHEAD:0"))
575 (when limit (list "--limit" (format "%s" limit))))))
576 ;; Dump log for the entire directory.
577 (apply 'vc-svn-command buffer 0 nil "log"
578 (append
579 (list
580 (if start-revision (format "-r%s" start-revision) "-rHEAD:0"))
581 (when limit (list "--limit" (format "%s" limit)))))))))
583 (defun vc-svn-diff (files &optional oldvers newvers buffer async)
584 "Get a difference report using SVN between two revisions of fileset FILES."
585 (and oldvers
586 (not newvers)
587 files
588 (catch 'no
589 (dolist (f files)
590 (or (equal oldvers (vc-working-revision f))
591 (throw 'no nil)))
593 ;; Use nil rather than the current revision because svn handles
594 ;; it better (i.e. locally). Note that if _any_ of the files
595 ;; has a different revision, we fetch the lot, which is
596 ;; obviously sub-optimal.
597 (setq oldvers nil))
598 (setq async (and async (or oldvers newvers))) ; Svn diffs those locally.
599 (let* ((switches
600 (if vc-svn-diff-switches
601 (vc-switches 'SVN 'diff)
602 (list (concat "--diff-cmd=" diff-command) "-x"
603 (mapconcat 'identity (vc-switches nil 'diff) " ")))))
604 (apply 'vc-svn-command buffer
605 (if async 'async 0)
606 files "diff"
607 (append
608 switches
609 (when oldvers
610 (list "-r" (if newvers (concat oldvers ":" newvers)
611 oldvers)))))
612 (if async 1 ; async diff => pessimistic assumption
613 ;; For some reason `svn diff' does not return a useful
614 ;; status w.r.t whether the diff was empty or not.
615 (buffer-size (get-buffer buffer)))))
618 ;;; Tag system
621 (defun vc-svn-create-tag (dir name branchp)
622 "Assign to DIR's current revision a given NAME.
623 If BRANCHP is non-nil, the name is created as a branch (and the current
624 workspace is immediately moved to that new branch).
625 NAME is assumed to be a URL."
626 (vc-svn-command nil 0 dir "copy" name)
627 (when branchp (vc-svn-retrieve-tag dir name nil)))
629 (defun vc-svn-retrieve-tag (dir name _update)
630 "Retrieve a tag at and below DIR.
631 NAME is the name of the tag; if it is empty, do a `svn update'.
632 If UPDATE is non-nil, then update (resynch) any affected buffers.
633 NAME is assumed to be a URL."
634 (vc-svn-command nil 0 dir "switch" name)
635 ;; FIXME: parse the output and obey `update'.
639 ;;; Miscellaneous
642 ;; Subversion makes backups for us, so don't bother.
643 ;; (defun vc-svn-make-version-backups-p (file)
644 ;; "Return non-nil if version backups should be made for FILE."
645 ;; nil)
647 (defun vc-svn-check-headers ()
648 "Check if the current file has any headers in it."
649 (save-excursion
650 (goto-char (point-min))
651 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
652 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
656 ;;; Internal functions
659 (defun vc-svn-command (buffer okstatus file-or-list &rest flags)
660 "A wrapper around `vc-do-command' for use in vc-svn.el.
661 The difference to vc-do-command is that this function always invokes `svn',
662 and that it passes `vc-svn-global-switches' to it before FLAGS."
663 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-svn-program file-or-list
664 (if (stringp vc-svn-global-switches)
665 (cons vc-svn-global-switches flags)
666 (append vc-svn-global-switches flags))))
668 (defun vc-svn-resolve-when-done ()
669 "Call \"svn resolved\" if the conflict markers have been removed."
670 (save-excursion
671 (goto-char (point-min))
672 (unless (re-search-forward "^<<<<<<< " nil t)
673 (vc-svn-command nil 0 buffer-file-name "resolved")
674 ;; Remove the hook so that it is not called multiple times.
675 (remove-hook 'after-save-hook 'vc-svn-resolve-when-done t))))
677 ;; Inspired by vc-arch-find-file-hook.
678 (defun vc-svn-find-file-hook ()
679 (when (eq ?C (vc-file-getprop buffer-file-name 'vc-svn-status))
680 ;; If the file is marked as "conflicted", then we should try and call
681 ;; "svn resolved" when applicable.
682 (if (save-excursion
683 (goto-char (point-min))
684 (re-search-forward "^<<<<<<< " nil t))
685 ;; There are conflict markers.
686 (progn
687 (smerge-start-session)
688 (add-hook 'after-save-hook 'vc-svn-resolve-when-done nil t))
689 ;; There are no conflict markers. This is problematic: maybe it means
690 ;; the conflict has been resolved and we should immediately call "svn
691 ;; resolved", or it means that the file's type does not allow Svn to
692 ;; use conflict markers in which case we don't really know what to do.
693 ;; So let's just punt for now.
694 nil)
695 (vc-message-unresolved-conflicts buffer-file-name)))
697 (defun vc-svn-parse-status (&optional filename)
698 "Parse output of \"svn status\" command in the current buffer.
699 Set file properties accordingly. If FILENAME is non-nil, return its status."
700 (let (multifile file status propstat)
701 (goto-char (point-min))
702 (while (re-search-forward
703 ;; Ignore the files with status X.
704 "^\\(?:\\?\\|[ ACDGIMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-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