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