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