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