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