toggle-read-only fixes for bugs#7292, 10006.
[bpt/emacs.git] / lisp / vc / vc-svn.el
CommitLineData
1fd3454a
SM
1;;; vc-svn.el --- non-resident support for Subversion version-control
2
95df8112 3;; Copyright (C) 2003-2011 Free Software Foundation, Inc.
1fd3454a
SM
4
5;; Author: FSF (see vc.el for full credits)
6;; Maintainer: Stefan Monnier <monnier@gnu.org>
bd78fa1d 7;; Package: vc
1fd3454a
SM
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
1fd3454a 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
1fd3454a
SM
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
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1fd3454a
SM
23
24;;; Commentary:
25
f9914e54
ER
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.
1fd3454a 28
1fd3454a
SM
29;;; Code:
30
31(eval-when-compile
32 (require 'vc))
33
8228a275
MH
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
1fd3454a
SM
38;;;
39;;; Customization options
40;;;
41
f9d5dc48
GM
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
1fd3454a 48(defcustom vc-svn-global-switches nil
f9d5dc48 49 "Global switches to pass to any SVN command."
1fd3454a
SM
50 :type '(choice (const :tag "None" nil)
51 (string :tag "Argument String")
52 (repeat :tag "Argument List"
53 :value ("")
54 string))
bf247b6e 55 :version "22.1"
1fd3454a
SM
56 :group 'vc)
57
58(defcustom vc-svn-register-switches nil
c8d6b4bc 59 "Switches for registering a file into SVN.
1fd3454a 60A string or list of strings passed to the checkin program by
c8d6b4bc
GM
61\\[vc-register]. If nil, use the value of `vc-register-switches'.
62If t, use no switches."
63 :type '(choice (const :tag "Unspecified" nil)
64 (const :tag "None" t)
1fd3454a 65 (string :tag "Argument String")
c8d6b4bc 66 (repeat :tag "Argument List" :value ("") string))
bf247b6e 67 :version "22.1"
1fd3454a
SM
68 :group 'vc)
69
8462aca5
SM
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.
9751169a 73If nil, use the value of `vc-diff-switches' (or `diff-switches'),
ed68f651
SS
74together with \"-x --diff-cmd=\"`diff-command' (since 'svn diff'
75does not support the default \"-c\" value of `diff-switches').
76If you want to force an empty list of arguments, use t."
8462aca5
SM
77 :type '(choice (const :tag "Unspecified" nil)
78 (const :tag "None" t)
1fd3454a
SM
79 (string :tag "Argument String")
80 (repeat :tag "Argument List"
81 :value ("")
82 string))
bf247b6e 83 :version "22.1"
1fd3454a
SM
84 :group 'vc)
85
67141a37 86(defcustom vc-svn-header '("\$Id\$")
f9d5dc48 87 "Header keywords to be inserted by `vc-insert-headers'."
67141a37 88 :version "24.1" ; no longer consult the obsolete vc-header-alist
1fd3454a
SM
89 :type '(repeat string)
90 :group 'vc)
91
fc2fb30c
SM
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))
ace2fad9
CY
98 (getenv "SVN_ASP_DOT_NET_HACK"))
99 "_svn")
100 (t ".svn"))
101 "The name of the \".svn\" subdirectory or its equivalent.")
102
8cdd17b4
ER
103;;; Properties of the backend
104
70e2f6c7
ER
105(defun vc-svn-revision-granularity () 'repository)
106(defun vc-svn-checkout-model (files) 'implicit)
107
1fd3454a
SM
108;;;
109;;; State-querying functions
110;;;
111
ace2fad9
CY
112;;; vc-svn-admin-directory is generally not defined when the
113;;; autoloaded function is called.
114
1fd3454a 115;;;###autoload (defun vc-svn-registered (f)
ace2fad9 116;;;###autoload (let ((admin-dir (cond ((and (eq system-type 'windows-nt)
fc2fb30c
SM
117;;;###autoload (getenv "SVN_ASP_DOT_NET_HACK"))
118;;;###autoload "_svn")
119;;;###autoload (t ".svn"))))
b5a53795 120;;;###autoload (when (vc-find-root f admin-dir)
1fd3454a 121;;;###autoload (load "vc-svn")
ace2fad9 122;;;###autoload (vc-svn-registered f))))
1fd3454a
SM
123
124(defun vc-svn-registered (file)
125 "Check if FILE is SVN registered."
b5a53795 126 (when (vc-svn-root file)
1fd3454a
SM
127 (with-temp-buffer
128 (cd (file-name-directory file))
04473f67
MA
129 (let* (process-file-side-effects
130 (status
df4da7f4
SM
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)
b5e791bd
DN
141 (let ((parsed (vc-svn-parse-status file)))
142 (and parsed (not (memq parsed '(ignored unregistered))))))))))
1fd3454a
SM
143
144(defun vc-svn-state (file &optional localp)
145 "SVN-specific version of `vc-state'."
04473f67
MA
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))))
1fd3454a
SM
152
153(defun vc-svn-state-heuristic (file)
154 "SVN-specific state heuristic."
155 (vc-svn-state file 'local))
156
a80a6b03
GM
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)
c222c25f 160 (let ((state-map '((?A . added)
b2ee56c9 161 (?C . conflict)
b2ee56c9
SM
162 (?I . ignored)
163 (?M . edited)
e6c01f09 164 (?D . removed)
b2ee56c9
SM
165 (?R . removed)
166 (?? . unregistered)
167 ;; This is what vc-svn-parse-status does.
168 (?~ . edited)))
5dd4f3f7
GM
169 (re (if remote "^\\(.\\)\\(.\\).....? \\([ *]\\) +\\(?:[-0-9]+\\)? \\(.*\\)$"
170 ;; Subexp 3 is a dummy in this case, so the numbers match.
171 "^\\(.\\)\\(.\\)...\\(.\\) \\(.*\\)$"))
c222c25f
DN
172 result)
173 (goto-char (point-min))
a80a6b03 174 (while (re-search-forward re nil t)
c222c25f 175 (let ((state (cdr (assq (aref (match-string 1) 0) state-map)))
5dd4f3f7 176 (propstat (cdr (assq (aref (match-string 2) 0) state-map)))
614b85f8
VJ
177 (filename (if (memq system-type '(windows-nt ms-dos))
178 (replace-regexp-in-string "\\\\" "/" (match-string 4))
179 (match-string 4))))
dbfb414e
GM
180 (and (memq propstat '(conflict edited))
181 (not (eq state 'conflict)) ; conflict always wins
182 (setq state propstat))
5dd4f3f7 183 (and remote (string-equal (match-string 3) "*")
a80a6b03
GM
184 ;; FIXME are there other possible combinations?
185 (cond ((eq state 'edited) (setq state 'needs-merge))
186 ((not state) (setq state 'needs-update))))
524a655d 187 (when (and state (not (string= "." filename)))
1b3f2d4e 188 (setq result (cons (list filename state) result)))))
b2ee56c9 189 (funcall callback result)))
c222c25f 190
c1b51374 191(defun vc-svn-dir-status (dir callback)
c222c25f
DN
192 "Run 'svn status' for DIR and update BUFFER via CALLBACK.
193CALLBACK is called as (CALLBACK RESULT BUFFER), where
194RESULT is a list of conses (FILE . STATE) for directory DIR."
a80a6b03 195 ;; FIXME should this rather be all the files in dir?
1826af5e
DN
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.
77bf3f54 200 (let* ((local (and nil (vc-stay-local-p dir 'SVN)))
1826af5e 201 (remote (or t (not local) (eq local 'only-file))))
a80a6b03
GM
202 (vc-svn-command (current-buffer) 'async nil "status"
203 (if remote "-u"))
115c0061 204 (vc-exec-after
a80a6b03 205 `(vc-svn-after-dir-status (quote ,callback) ,remote))))
f8e89f19 206
847fb889
DN
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
13ad7457 212(defun vc-svn-dir-extra-headers (dir)
2ec0d864 213 "Generate extra status headers for a Subversion working copy."
04473f67
MA
214 (let (process-file-side-effects)
215 (vc-svn-command "*vc*" 0 nil "info"))
2ec0d864 216 (let ((repo
def61be2 217 (save-excursion
2ec0d864
ER
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
ac3f4c6f
ER
230(defun vc-svn-working-revision (file)
231 "SVN-specific version of `vc-working-revision'."
1fd3454a
SM
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)
ac3f4c6f 236 (vc-file-getprop file 'vc-working-revision))
1fd3454a 237
5cc7cb96
SM
238;; vc-svn-mode-line-string doesn't exist because the default implementation
239;; works just fine.
240
5b5afd50 241(defun vc-svn-previous-revision (file rev)
cbbd2cd3
TTN
242 (let ((newrev (1- (string-to-number rev))))
243 (when (< 0 newrev)
244 (number-to-string newrev))))
245
5b5afd50 246(defun vc-svn-next-revision (file rev)
cbbd2cd3 247 (let ((newrev (1+ (string-to-number rev))))
5b5afd50 248 ;; The "working revision" is an uneasy conceptual fit under Subversion;
cbbd2cd3
TTN
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.
ac3f4c6f 254 (unless (< (string-to-number (vc-file-getprop file 'vc-working-revision))
cbbd2cd3
TTN
255 newrev)
256 (number-to-string newrev))))
257
1fd3454a
SM
258
259;;;
260;;; State-changing functions
261;;;
262
8cdd17b4
ER
263(defun vc-svn-create-repo ()
264 "Create a new SVN repository."
2888a97e 265 (vc-do-command "*vc*" 0 "svnadmin" '("create" "SVN"))
f9d5dc48 266 (vc-do-command "*vc*" 0 vc-svn-program '(".")
8cdd17b4
ER
267 "checkout" (concat "file://" default-directory "SVN")))
268
269(defun vc-svn-register (files &optional rev comment)
270 "Register FILES into the SVN version-control system.
271The COMMENT argument is ignored This does an add but not a commit.
c8d6b4bc
GM
272Passes either `vc-svn-register-switches' or `vc-register-switches'
273to the SVN command."
8cdd17b4 274 (apply 'vc-svn-command nil 0 files "add" (vc-switches 'SVN 'register)))
1fd3454a 275
b5a53795
KP
276(defun vc-svn-root (file)
277 (vc-find-root file vc-svn-admin-directory))
1fd3454a 278
b5a53795
KP
279(defalias 'vc-svn-responsible-p 'vc-svn-root)
280
281(defalias 'vc-svn-could-register 'vc-svn-root
1fd3454a
SM
282 "Return non-nil if FILE could be registered in SVN.
283This is only possible if SVN is responsible for FILE's directory.")
284
09158997 285(defun vc-svn-checkin (files rev comment &optional extra-args-ignored)
1fd3454a 286 "SVN-specific version of `vc-backend-checkin'."
4ced8551 287 (if rev (error "Committing to a specific revision is unsupported in SVN"))
5129f10c 288 (let ((status (apply
8cdd17b4 289 'vc-svn-command nil 1 files "ci"
5129f10c 290 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
1fd3454a
SM
291 (set-buffer "*vc*")
292 (goto-char (point-min))
293 (unless (equal status 0)
294 ;; Check checkin problem.
295 (cond
fd140743 296 ((search-forward "Transaction is out of date" nil t)
8cdd17b4
ER
297 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
298 files)
1fd3454a
SM
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
ac3f4c6f 309 ;; file 'vc-working-revision
1fd3454a
SM
310 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
311 ))
312
ac3f4c6f 313(defun vc-svn-find-revision (file rev buffer)
8cdd17b4 314 "SVN-specific retrieval of a specified version into a buffer."
04473f67
MA
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))))
1fd3454a
SM
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))
a749e19d 326 (vc-svn-update file editable rev (vc-switches 'SVN 'checkout)))
77bf3f54 327 (vc-mode-line file 'SVN)
1fd3454a
SM
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))
fc2fb30c
SM
332 ;; If no revision was specified, there's nothing to do.
333 nil
1fd3454a 334 ;; Check out a particular version (or recreate the file).
ac3f4c6f 335 (vc-file-setprop file 'vc-working-revision nil)
1fd3454a 336 (apply 'vc-svn-command nil 0 file
c85a168b 337 "--non-interactive" ; bug#4280
1fd3454a 338 "update"
5cc7cb96
SM
339 (cond
340 ((null rev) "-rBASE")
341 ((or (eq rev t) (equal rev "")) nil)
342 (t (concat "-r" rev)))
1fd3454a
SM
343 switches)))
344
3bdc13e4
SM
345(defun vc-svn-delete-file (file)
346 (vc-svn-command nil 0 file "remove"))
347
2766aaaf
SM
348(defun vc-svn-rename-file (old new)
349 (vc-svn-command nil 0 new "move" (file-relative-name old)))
350
1fd3454a
SM
351(defun vc-svn-revert (file &optional contents-done)
352 "Revert FILE to the version it was based on."
353 (unless contents-done
fc2fb30c 354 (vc-svn-command nil 0 file "revert")))
1fd3454a
SM
355
356(defun vc-svn-merge (file first-version &optional second-version)
357 "Merge changes into current working copy of FILE.
358The changes are between FIRST-VERSION and SECOND-VERSION."
359 (vc-svn-command nil 0 file
c217cb04 360 "merge"
02610d0e 361 "-r" (if second-version
c217cb04
SM
362 (concat first-version ":" second-version)
363 first-version))
1fd3454a
SM
364 (vc-file-setprop file 'vc-state 'edited)
365 (with-current-buffer (get-buffer "*vc*")
366 (goto-char (point-min))
c217cb04
SM
367 (if (looking-at "C ")
368 1 ; signal conflict
1fd3454a
SM
369 0))) ; signal success
370
371(defun vc-svn-merge-news (file)
372 "Merge in any new changes made to FILE."
373 (message "Merging changes into %s..." file)
ac3f4c6f 374 ;; (vc-file-setprop file 'vc-working-revision nil)
1fd3454a 375 (vc-file-setprop file 'vc-checkout-time 0)
8686a5ea 376 (vc-svn-command nil 0 file "--non-interactive" "update") ; see bug#7152
1fd3454a
SM
377 ;; Analyze the merge result reported by SVN, and set
378 ;; file properties accordingly.
379 (with-current-buffer (get-buffer "*vc*")
380 (goto-char (point-min))
5b5afd50 381 ;; get new working revision
1fd3454a 382 (if (re-search-forward
1468d754 383 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
ac3f4c6f
ER
384 (vc-file-setprop file 'vc-working-revision (match-string 2))
385 (vc-file-setprop file 'vc-working-revision nil))
1fd3454a 386 ;; get file status
1468d754 387 (goto-char (point-min))
1fd3454a 388 (prog1
1468d754 389 (if (looking-at "At revision")
1fd3454a
SM
390 0 ;; there were no news; indicate success
391 (if (re-search-forward
459b1fe4
SM
392 ;; Newer SVN clients have 3 columns of chars (one for the
393 ;; file's contents, then second for its properties, and the
394 ;; third for lock-grabbing info), before the 2 spaces.
395 ;; We also used to match the filename in column 0 without any
396 ;; meta-info before it, but I believe this can never happen.
397 (concat "^\\(\\([ACGDU]\\)\\(.[B ]\\)? \\)"
1468d754 398 (regexp-quote (file-name-nondirectory file)))
1fd3454a
SM
399 nil t)
400 (cond
401 ;; Merge successful, we are in sync with repository now
459b1fe4 402 ((string= (match-string 2) "U")
1fd3454a
SM
403 (vc-file-setprop file 'vc-state 'up-to-date)
404 (vc-file-setprop file 'vc-checkout-time
405 (nth 5 (file-attributes file)))
406 0);; indicate success to the caller
407 ;; Merge successful, but our own changes are still in the file
459b1fe4 408 ((string= (match-string 2) "G")
1fd3454a
SM
409 (vc-file-setprop file 'vc-state 'edited)
410 0);; indicate success to the caller
411 ;; Conflicts detected!
412 (t
413 (vc-file-setprop file 'vc-state 'edited)
414 1);; signal the error to the caller
415 )
416 (pop-to-buffer "*vc*")
417 (error "Couldn't analyze svn update result")))
418 (message "Merging changes into %s...done" file))))
419
42a0a135
ER
420(defun vc-svn-modify-change-comment (files rev comment)
421 "Modify the change comments for a specified REV.
422You must have ssh access to the repository host, and the directory Emacs
e09deae9 423uses locally for temp files must also be writable by you on that host.
fd064451
DN
424This is only supported if the repository access method is either file://
425or svn+ssh://."
426 (let (tempfile host remotefile directory fileurl-p)
42a0a135 427 (with-temp-buffer
f9d5dc48 428 (vc-do-command (current-buffer) 0 vc-svn-program nil "info")
fd064451
DN
429 (goto-char (point-min))
430 (unless (re-search-forward "Repository Root: \\(file://\\(/.*\\)\\)\\|\\(svn\\+ssh://\\([^/]+\\)\\(/.*\\)\\)" nil t)
431 (error "Repository information is unavailable"))
432 (if (match-string 1)
433 (progn
434 (setq fileurl-p t)
435 (setq directory (match-string 2)))
436 (setq host (match-string 4))
437 (setq directory (match-string 5))
438 (setq remotefile (concat host ":" tempfile))))
439 (with-temp-file (setq tempfile (make-temp-file user-mail-address))
440 (insert comment))
441 (if fileurl-p
442 ;; Repository Root is a local file.
443 (progn
444 (unless (vc-do-command
2888a97e 445 "*vc*" 0 "svnadmin" nil
def61be2 446 "setlog" "--bypass-hooks" directory
fd064451
DN
447 "-r" rev (format "%s" tempfile))
448 (error "Log edit failed"))
449 (delete-file tempfile))
450
451 ;; Remote repository, using svn+ssh.
2888a97e 452 (unless (vc-do-command "*vc*" 0 "scp" nil "-q" tempfile remotefile)
fd064451
DN
453 (error "Copy of comment to %s failed" remotefile))
454 (unless (vc-do-command
2888a97e 455 "*vc*" 0 "ssh" nil "-q" host
fd064451
DN
456 (format "svnadmin setlog --bypass-hooks %s -r %s %s; rm %s"
457 directory rev tempfile tempfile))
458 (error "Log edit failed")))))
1fd3454a
SM
459
460;;;
461;;; History functions
462;;;
463
def61be2
JB
464(defvar log-view-per-file-logs)
465
6653c6b7
DN
466(define-derived-mode vc-svn-log-view-mode log-view-mode "SVN-Log-View"
467 (require 'add-log)
468 (set (make-local-variable 'log-view-per-file-logs) nil))
469
662c5698 470(defun vc-svn-print-log (files buffer &optional shortlog start-revision limit)
8cdd17b4 471 "Get change log(s) associated with FILES."
1fd3454a 472 (save-current-buffer
b349012b 473 (vc-setup-buffer buffer)
1fd3454a
SM
474 (let ((inhibit-read-only t))
475 (goto-char (point-min))
13b56025
ER
476 (if files
477 (dolist (file files)
478 (insert "Working file: " file "\n")
6616006b
DN
479 (apply
480 'vc-svn-command
13b56025
ER
481 buffer
482 'async
77bf3f54 483 ;; (if (and (= (length files) 1) (vc-stay-local-p file 'SVN)) 'async 0)
13b56025
ER
484 (list file)
485 "log"
662c5698
DN
486 (append
487 (list
488 (if start-revision
489 (format "-r%s" start-revision)
490 ;; By default Subversion only shows the log up to the
491 ;; working revision, whereas we also want the log of the
492 ;; subsequent commits. At least that's what the
493 ;; vc-cvs.el code does.
494 "-rHEAD:0"))
1db3226b 495 (when limit (list "--limit" (format "%s" limit))))))
13b56025 496 ;; Dump log for the entire directory.
662c5698
DN
497 (apply 'vc-svn-command buffer 0 nil "log"
498 (append
499 (list
500 (if start-revision (format "-r%s" start-revision) "-rHEAD:0"))
1db3226b 501 (when limit (list "--limit" (format "%s" limit)))))))))
8cdd17b4 502
8cdd17b4 503(defun vc-svn-diff (files &optional oldvers newvers buffer)
5b5afd50 504 "Get a difference report using SVN between two revisions of fileset FILES."
22b5692c
NR
505 (and oldvers
506 (not newvers)
507 files
508 (catch 'no
509 (dolist (f files)
510 (or (equal oldvers (vc-working-revision f))
511 (throw 'no nil)))
512 t)
513 ;; Use nil rather than the current revision because svn handles
514 ;; it better (i.e. locally). Note that if _any_ of the files
515 ;; has a different revision, we fetch the lot, which is
516 ;; obviously sub-optimal.
517 (setq oldvers nil))
8cdd17b4 518 (let* ((switches
f9d1f3be
SM
519 (if vc-svn-diff-switches
520 (vc-switches 'SVN 'diff)
ed68f651 521 (list (concat "--diff-cmd=" diff-command) "-x"
9751169a 522 (mapconcat 'identity (vc-switches nil 'diff) " "))))
2d4e93b9 523 (async (and (not vc-disable-async-diff)
77bf3f54 524 (vc-stay-local-p files 'SVN)
fe1919ab 525 (or oldvers newvers)))) ; Svn diffs those locally.
b349012b 526 (apply 'vc-svn-command buffer
2766aaaf 527 (if async 'async 0)
8cdd17b4 528 files "diff"
2766aaaf 529 (append
f9d1f3be 530 switches
2766aaaf
SM
531 (when oldvers
532 (list "-r" (if newvers (concat oldvers ":" newvers)
533 oldvers)))))
fd140743
SM
534 (if async 1 ; async diff => pessimistic assumption
535 ;; For some reason `svn diff' does not return a useful
536 ;; status w.r.t whether the diff was empty or not.
8cdd17b4 537 (buffer-size (get-buffer buffer)))))
1fd3454a 538
1fd3454a 539;;;
370fded4 540;;; Tag system
1fd3454a
SM
541;;;
542
370fded4 543(defun vc-svn-create-tag (dir name branchp)
5b5afd50 544 "Assign to DIR's current revision a given NAME.
1fd3454a 545If BRANCHP is non-nil, the name is created as a branch (and the current
5cc7cb96
SM
546workspace is immediately moved to that new branch).
547NAME is assumed to be a URL."
548 (vc-svn-command nil 0 dir "copy" name)
370fded4 549 (when branchp (vc-svn-retrieve-tag dir name nil)))
1fd3454a 550
370fded4
ER
551(defun vc-svn-retrieve-tag (dir name update)
552 "Retrieve a tag at and below DIR.
553NAME is the name of the tag; if it is empty, do a `svn update'.
5cc7cb96
SM
554If UPDATE is non-nil, then update (resynch) any affected buffers.
555NAME is assumed to be a URL."
556 (vc-svn-command nil 0 dir "switch" name)
557 ;; FIXME: parse the output and obey `update'.
558 )
1fd3454a
SM
559
560;;;
561;;; Miscellaneous
562;;;
563
564;; Subversion makes backups for us, so don't bother.
77bf3f54
DN
565;; (defun vc-svn-make-version-backups-p (file)
566;; "Return non-nil if version backups should be made for FILE."
567;; (vc-stay-local-p file 'SVN))
1fd3454a
SM
568
569(defun vc-svn-check-headers ()
570 "Check if the current file has any headers in it."
571 (save-excursion
572 (goto-char (point-min))
573 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
574\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
575
576
577;;;
578;;; Internal functions
579;;;
580
8cdd17b4 581(defun vc-svn-command (buffer okstatus file-or-list &rest flags)
1fd3454a
SM
582 "A wrapper around `vc-do-command' for use in vc-svn.el.
583The difference to vc-do-command is that this function always invokes `svn',
584and that it passes `vc-svn-global-switches' to it before FLAGS."
2888a97e 585 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-svn-program file-or-list
1fd3454a
SM
586 (if (stringp vc-svn-global-switches)
587 (cons vc-svn-global-switches flags)
588 (append vc-svn-global-switches
589 flags))))
590
7ffc77d3
SM
591(defun vc-svn-repository-hostname (dirname)
592 (with-temp-buffer
b5a53795
KP
593 (let (process-file-side-effects)
594 (vc-svn-command t t dirname "info" "--xml"))
7ffc77d3 595 (goto-char (point-min))
b5a53795 596 (when (re-search-forward "<url>\\(.*\\)</url>" nil t)
17a5a301
SM
597 ;; This is not a hostname but a URL. This may actually be considered
598 ;; as a feature since it allows vc-svn-stay-local to specify different
599 ;; behavior for different modules on the same server.
600 (match-string 1))))
1fd3454a 601
1c67a814
SM
602(defun vc-svn-resolve-when-done ()
603 "Call \"svn resolved\" if the conflict markers have been removed."
604 (save-excursion
605 (goto-char (point-min))
54648b5c
DN
606 (unless (re-search-forward "^<<<<<<< " nil t)
607 (vc-svn-command nil 0 buffer-file-name "resolved")
608 ;; Remove the hook so that it is not called multiple times.
609 (remove-hook 'after-save-hook 'vc-svn-resolve-when-done t))))
1c67a814
SM
610
611;; Inspired by vc-arch-find-file-hook.
612(defun vc-svn-find-file-hook ()
613 (when (eq ?C (vc-file-getprop buffer-file-name 'vc-svn-status))
614 ;; If the file is marked as "conflicted", then we should try and call
615 ;; "svn resolved" when applicable.
616 (if (save-excursion
617 (goto-char (point-min))
618 (re-search-forward "^<<<<<<< " nil t))
619 ;; There are conflict markers.
620 (progn
28e4e2b4 621 (smerge-start-session)
1c67a814
SM
622 (add-hook 'after-save-hook 'vc-svn-resolve-when-done nil t))
623 ;; There are no conflict markers. This is problematic: maybe it means
624 ;; the conflict has been resolved and we should immediately call "svn
625 ;; resolved", or it means that the file's type does not allow Svn to
626 ;; use conflict markers in which case we don't really know what to do.
627 ;; So let's just punt for now.
628 nil)
629 (message "There are unresolved conflicts in this file")))
630
bc8c1bb4 631(defun vc-svn-parse-status (&optional filename)
1fd3454a 632 "Parse output of \"svn status\" command in the current buffer.
bc8c1bb4
SM
633Set file properties accordingly. Unless FILENAME is non-nil, parse only
634information about FILENAME and return its status."
5dd4f3f7 635 (let (file status propstat)
1fd3454a
SM
636 (goto-char (point-min))
637 (while (re-search-forward
484c1b1f 638 ;; Ignore the files with status X.
245cacf1 639 "^\\(?:\\?\\|[ ACDGIMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\)\\) +" nil t)
c45b3be3
SM
640 ;; If the username contains spaces, the output format is ambiguous,
641 ;; so don't trust the output's filename unless we have to.
642 (setq file (or filename
643 (expand-file-name
644 (buffer-substring (point) (line-end-position)))))
5dd4f3f7
GM
645 (setq status (char-after (line-beginning-position))
646 ;; Status of the item's properties ([ MC]).
647 propstat (char-after (1+ (line-beginning-position))))
484c1b1f 648 (if (eq status ??)
4f07b9f2 649 (vc-file-setprop file 'vc-state 'unregistered)
fd140743
SM
650 ;; Use the last-modified revision, so that searching in vc-print-log
651 ;; output works.
4f07b9f2 652 (vc-file-setprop file 'vc-working-revision (match-string 3))
1c67a814 653 ;; Remember Svn's own status.
4f07b9f2
ER
654 (vc-file-setprop file 'vc-svn-status status)
655 (vc-file-setprop
1fd3454a
SM
656 file 'vc-state
657 (cond
5dd4f3f7 658 ((and (eq status ?\ ) (eq propstat ?\ ))
1fd3454a 659 (if (eq (char-after (match-beginning 1)) ?*)
3702367b 660 'needs-update
4f07b9f2 661 (vc-file-setprop file 'vc-checkout-time
1fd3454a
SM
662 (nth 5 (file-attributes file)))
663 'up-to-date))
664 ((eq status ?A)
fd140743 665 ;; If the file was actually copied, (match-string 2) is "-".
4f07b9f2
ER
666 (vc-file-setprop file 'vc-working-revision "0")
667 (vc-file-setprop file 'vc-checkout-time 0)
484c1b1f 668 'added)
5dd4f3f7
GM
669 ;; Conflict in contents or properties.
670 ((or (eq status ?C) (eq propstat ?C))
7fbb4797 671 (vc-file-setprop file 'vc-state 'conflict))
5dd4f3f7
GM
672 ;; Modified contents or properties.
673 ((or (eq status ?M) (eq propstat ?M))
1fd3454a
SM
674 (if (eq (char-after (match-beginning 1)) ?*)
675 'needs-merge
676 'edited))
722f037f 677 ((eq status ?I)
4f07b9f2 678 (vc-file-setprop file 'vc-state 'ignored))
e6c01f09 679 ((memq status '(?D ?R))
4f07b9f2 680 (vc-file-setprop file 'vc-state 'removed))
bc8c1bb4 681 (t 'edited)))))
245cacf1 682 (when filename (vc-file-getprop filename 'vc-state))))
1fd3454a 683
1fd3454a
SM
684(defun vc-svn-valid-symbolic-tag-name-p (tag)
685 "Return non-nil if TAG is a valid symbolic tag name."
686 ;; According to the SVN manual, a valid symbolic tag must start with
687 ;; an uppercase or lowercase letter and can contain uppercase and
688 ;; lowercase letters, digits, `-', and `_'.
689 (and (string-match "^[a-zA-Z]" tag)
690 (not (string-match "[^a-z0-9A-Z-_]" tag))))
691
5b5afd50
ER
692(defun vc-svn-valid-revision-number-p (tag)
693 "Return non-nil if TAG is a valid revision number."
1fd3454a
SM
694 (and (string-match "^[0-9]" tag)
695 (not (string-match "[^0-9]" tag))))
696
17a5a301
SM
697;; Support for `svn annotate'
698
699(defun vc-svn-annotate-command (file buf &optional rev)
837b0e99 700 (vc-svn-command buf 'async file "annotate" (if rev (concat "-r" rev))))
17a5a301
SM
701
702(defun vc-svn-annotate-time-of-rev (rev)
703 ;; Arbitrarily assume 10 commmits per day.
704 (/ (string-to-number rev) 10.0))
705
e53ac718
DN
706(defvar vc-annotate-parent-rev)
707
17a5a301
SM
708(defun vc-svn-annotate-current-time ()
709 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
710
711(defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
712
713(defun vc-svn-annotate-time ()
714 (when (looking-at vc-svn-annotate-re)
715 (goto-char (match-end 0))
716 (vc-svn-annotate-time-of-rev (match-string 1))))
717
718(defun vc-svn-annotate-extract-revision-at-line ()
719 (save-excursion
720 (beginning-of-line)
721 (if (looking-at vc-svn-annotate-re) (match-string 1))))
722
8228a275
MH
723(defun vc-svn-revision-table (files)
724 (let ((vc-svn-revisions '()))
725 (with-current-buffer "*vc*"
726 (vc-svn-command nil 0 files "log" "-q")
727 (goto-char (point-min))
728 (forward-line)
729 (let ((start (point-min))
730 (loglines (buffer-substring-no-properties (point-min)
731 (point-max))))
732 (while (string-match "^r\\([0-9]+\\) " loglines)
733 (push (match-string 1 loglines) vc-svn-revisions)
734 (setq start (+ start (match-end 0)))
735 (setq loglines (buffer-substring-no-properties start (point-max)))))
736 vc-svn-revisions)))
737
1fd3454a
SM
738(provide 'vc-svn)
739
740;;; vc-svn.el ends here