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