* src/unexmacosx.c: Add comment about include order.
[bpt/emacs.git] / lisp / vc / vc-svn.el
CommitLineData
1fd3454a
SM
1;;; vc-svn.el --- non-resident support for Subversion version-control
2
5df4f04c 3;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
a80a6b03 4;; Free Software Foundation, Inc.
1fd3454a
SM
5
6;; Author: FSF (see vc.el for full credits)
7;; Maintainer: Stefan Monnier <monnier@gnu.org>
bd78fa1d 8;; Package: vc
1fd3454a
SM
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
1fd3454a 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
1fd3454a
SM
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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
1fd3454a
SM
24
25;;; Commentary:
26
f9914e54
ER
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.
1fd3454a 29
1fd3454a
SM
30;;; Code:
31
32(eval-when-compile
33 (require 'vc))
34
8228a275
MH
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
1fd3454a
SM
39;;;
40;;; Customization options
41;;;
42
f9d5dc48
GM
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
1fd3454a 49(defcustom vc-svn-global-switches nil
f9d5dc48 50 "Global switches to pass to any SVN command."
1fd3454a
SM
51 :type '(choice (const :tag "None" nil)
52 (string :tag "Argument String")
53 (repeat :tag "Argument List"
54 :value ("")
55 string))
bf247b6e 56 :version "22.1"
1fd3454a
SM
57 :group 'vc)
58
59(defcustom vc-svn-register-switches nil
c8d6b4bc 60 "Switches for registering a file into SVN.
1fd3454a 61A string or list of strings passed to the checkin program by
c8d6b4bc
GM
62\\[vc-register]. If nil, use the value of `vc-register-switches'.
63If t, use no switches."
64 :type '(choice (const :tag "Unspecified" nil)
65 (const :tag "None" t)
1fd3454a 66 (string :tag "Argument String")
c8d6b4bc 67 (repeat :tag "Argument List" :value ("") string))
bf247b6e 68 :version "22.1"
1fd3454a
SM
69 :group 'vc)
70
8462aca5
SM
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.
9751169a
GM
74If nil, use the value of `vc-diff-switches' (or `diff-switches'),
75together with \"-x --diff-cmd=diff\" (since svn diff does not
76support the default \"-c\" value of `diff-switches'). If you
77want to force an empty list of arguments, use t."
8462aca5
SM
78 :type '(choice (const :tag "Unspecified" nil)
79 (const :tag "None" t)
1fd3454a
SM
80 (string :tag "Argument String")
81 (repeat :tag "Argument List"
82 :value ("")
83 string))
bf247b6e 84 :version "22.1"
1fd3454a
SM
85 :group 'vc)
86
67141a37 87(defcustom vc-svn-header '("\$Id\$")
f9d5dc48 88 "Header keywords to be inserted by `vc-insert-headers'."
67141a37 89 :version "24.1" ; no longer consult the obsolete vc-header-alist
1fd3454a
SM
90 :type '(repeat string)
91 :group 'vc)
92
fc2fb30c
SM
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))
ace2fad9
CY
99 (getenv "SVN_ASP_DOT_NET_HACK"))
100 "_svn")
101 (t ".svn"))
102 "The name of the \".svn\" subdirectory or its equivalent.")
103
8cdd17b4
ER
104;;; Properties of the backend
105
70e2f6c7
ER
106(defun vc-svn-revision-granularity () 'repository)
107(defun vc-svn-checkout-model (files) 'implicit)
108
1fd3454a
SM
109;;;
110;;; State-querying functions
111;;;
112
ace2fad9
CY
113;;; vc-svn-admin-directory is generally not defined when the
114;;; autoloaded function is called.
115
1fd3454a 116;;;###autoload (defun vc-svn-registered (f)
ace2fad9 117;;;###autoload (let ((admin-dir (cond ((and (eq system-type 'windows-nt)
fc2fb30c
SM
118;;;###autoload (getenv "SVN_ASP_DOT_NET_HACK"))
119;;;###autoload "_svn")
120;;;###autoload (t ".svn"))))
ace2fad9 121;;;###autoload (when (file-readable-p (expand-file-name
fc2fb30c
SM
122;;;###autoload (concat admin-dir "/entries")
123;;;###autoload (file-name-directory f)))
1fd3454a 124;;;###autoload (load "vc-svn")
ace2fad9 125;;;###autoload (vc-svn-registered f))))
1fd3454a
SM
126
127(defun vc-svn-registered (file)
128 "Check if FILE is SVN registered."
ace2fad9
CY
129 (when (file-readable-p (expand-file-name (concat vc-svn-admin-directory
130 "/entries")
1fd3454a
SM
131 (file-name-directory file)))
132 (with-temp-buffer
133 (cd (file-name-directory file))
04473f67
MA
134 (let* (process-file-side-effects
135 (status
df4da7f4
SM
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)
b5e791bd
DN
146 (let ((parsed (vc-svn-parse-status file)))
147 (and parsed (not (memq parsed '(ignored unregistered))))))))))
1fd3454a
SM
148
149(defun vc-svn-state (file &optional localp)
150 "SVN-specific version of `vc-state'."
04473f67
MA
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))))
1fd3454a
SM
157
158(defun vc-svn-state-heuristic (file)
159 "SVN-specific state heuristic."
160 (vc-svn-state file 'local))
161
a80a6b03
GM
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)
c222c25f 165 (let ((state-map '((?A . added)
b2ee56c9 166 (?C . conflict)
b2ee56c9
SM
167 (?I . ignored)
168 (?M . edited)
e6c01f09 169 (?D . removed)
b2ee56c9
SM
170 (?R . removed)
171 (?? . unregistered)
172 ;; This is what vc-svn-parse-status does.
173 (?~ . edited)))
5dd4f3f7
GM
174 (re (if remote "^\\(.\\)\\(.\\).....? \\([ *]\\) +\\(?:[-0-9]+\\)? \\(.*\\)$"
175 ;; Subexp 3 is a dummy in this case, so the numbers match.
176 "^\\(.\\)\\(.\\)...\\(.\\) \\(.*\\)$"))
c222c25f
DN
177 result)
178 (goto-char (point-min))
a80a6b03 179 (while (re-search-forward re nil t)
c222c25f 180 (let ((state (cdr (assq (aref (match-string 1) 0) state-map)))
5dd4f3f7
GM
181 (propstat (cdr (assq (aref (match-string 2) 0) state-map)))
182 (filename (match-string 4)))
183 (if (memq propstat '(conflict edited))
184 (setq state propstat))
185 (and remote (string-equal (match-string 3) "*")
a80a6b03
GM
186 ;; FIXME are there other possible combinations?
187 (cond ((eq state 'edited) (setq state 'needs-merge))
188 ((not state) (setq state 'needs-update))))
524a655d 189 (when (and state (not (string= "." filename)))
1b3f2d4e 190 (setq result (cons (list filename state) result)))))
b2ee56c9 191 (funcall callback result)))
c222c25f 192
c1b51374 193(defun vc-svn-dir-status (dir callback)
c222c25f
DN
194 "Run 'svn status' for DIR and update BUFFER via CALLBACK.
195CALLBACK is called as (CALLBACK RESULT BUFFER), where
196RESULT is a list of conses (FILE . STATE) for directory DIR."
a80a6b03 197 ;; FIXME should this rather be all the files in dir?
1826af5e
DN
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.
77bf3f54 202 (let* ((local (and nil (vc-stay-local-p dir 'SVN)))
1826af5e 203 (remote (or t (not local) (eq local 'only-file))))
a80a6b03
GM
204 (vc-svn-command (current-buffer) 'async nil "status"
205 (if remote "-u"))
115c0061 206 (vc-exec-after
a80a6b03 207 `(vc-svn-after-dir-status (quote ,callback) ,remote))))
f8e89f19 208
847fb889
DN
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
13ad7457 214(defun vc-svn-dir-extra-headers (dir)
2ec0d864 215 "Generate extra status headers for a Subversion working copy."
04473f67
MA
216 (let (process-file-side-effects)
217 (vc-svn-command "*vc*" 0 nil "info"))
2ec0d864 218 (let ((repo
def61be2 219 (save-excursion
2ec0d864
ER
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
ac3f4c6f
ER
232(defun vc-svn-working-revision (file)
233 "SVN-specific version of `vc-working-revision'."
1fd3454a
SM
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)
ac3f4c6f 238 (vc-file-getprop file 'vc-working-revision))
1fd3454a 239
5cc7cb96
SM
240;; vc-svn-mode-line-string doesn't exist because the default implementation
241;; works just fine.
242
5b5afd50 243(defun vc-svn-previous-revision (file rev)
cbbd2cd3
TTN
244 (let ((newrev (1- (string-to-number rev))))
245 (when (< 0 newrev)
246 (number-to-string newrev))))
247
5b5afd50 248(defun vc-svn-next-revision (file rev)
cbbd2cd3 249 (let ((newrev (1+ (string-to-number rev))))
5b5afd50 250 ;; The "working revision" is an uneasy conceptual fit under Subversion;
cbbd2cd3
TTN
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.
ac3f4c6f 256 (unless (< (string-to-number (vc-file-getprop file 'vc-working-revision))
cbbd2cd3
TTN
257 newrev)
258 (number-to-string newrev))))
259
1fd3454a
SM
260
261;;;
262;;; State-changing functions
263;;;
264
8cdd17b4
ER
265(defun vc-svn-create-repo ()
266 "Create a new SVN repository."
2888a97e 267 (vc-do-command "*vc*" 0 "svnadmin" '("create" "SVN"))
f9d5dc48 268 (vc-do-command "*vc*" 0 vc-svn-program '(".")
8cdd17b4
ER
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.
273The COMMENT argument is ignored This does an add but not a commit.
c8d6b4bc
GM
274Passes either `vc-svn-register-switches' or `vc-register-switches'
275to the SVN command."
8cdd17b4 276 (apply 'vc-svn-command nil 0 files "add" (vc-switches 'SVN 'register)))
1fd3454a
SM
277
278(defun vc-svn-responsible-p (file)
279 "Return non-nil if SVN thinks it is responsible for FILE."
ace2fad9 280 (file-directory-p (expand-file-name vc-svn-admin-directory
1fd3454a
SM
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.
287This is only possible if SVN is responsible for FILE's directory.")
288
09158997 289(defun vc-svn-checkin (files rev comment &optional extra-args-ignored)
1fd3454a 290 "SVN-specific version of `vc-backend-checkin'."
4ced8551 291 (if rev (error "Committing to a specific revision is unsupported in SVN"))
5129f10c 292 (let ((status (apply
8cdd17b4 293 'vc-svn-command nil 1 files "ci"
5129f10c 294 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
1fd3454a
SM
295 (set-buffer "*vc*")
296 (goto-char (point-min))
297 (unless (equal status 0)
298 ;; Check checkin problem.
299 (cond
fd140743 300 ((search-forward "Transaction is out of date" nil t)
8cdd17b4
ER
301 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
302 files)
1fd3454a
SM
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
ac3f4c6f 313 ;; file 'vc-working-revision
1fd3454a
SM
314 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
315 ))
316
ac3f4c6f 317(defun vc-svn-find-revision (file rev buffer)
8cdd17b4 318 "SVN-specific retrieval of a specified version into a buffer."
04473f67
MA
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))))
1fd3454a
SM
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))
a749e19d 330 (vc-svn-update file editable rev (vc-switches 'SVN 'checkout)))
77bf3f54 331 (vc-mode-line file 'SVN)
1fd3454a
SM
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))
fc2fb30c
SM
336 ;; If no revision was specified, there's nothing to do.
337 nil
1fd3454a 338 ;; Check out a particular version (or recreate the file).
ac3f4c6f 339 (vc-file-setprop file 'vc-working-revision nil)
1fd3454a 340 (apply 'vc-svn-command nil 0 file
c85a168b 341 "--non-interactive" ; bug#4280
1fd3454a 342 "update"
5cc7cb96
SM
343 (cond
344 ((null rev) "-rBASE")
345 ((or (eq rev t) (equal rev "")) nil)
346 (t (concat "-r" rev)))
1fd3454a
SM
347 switches)))
348
3bdc13e4
SM
349(defun vc-svn-delete-file (file)
350 (vc-svn-command nil 0 file "remove"))
351
2766aaaf
SM
352(defun vc-svn-rename-file (old new)
353 (vc-svn-command nil 0 new "move" (file-relative-name old)))
354
1fd3454a
SM
355(defun vc-svn-revert (file &optional contents-done)
356 "Revert FILE to the version it was based on."
357 (unless contents-done
fc2fb30c 358 (vc-svn-command nil 0 file "revert")))
1fd3454a
SM
359
360(defun vc-svn-merge (file first-version &optional second-version)
361 "Merge changes into current working copy of FILE.
362The changes are between FIRST-VERSION and SECOND-VERSION."
363 (vc-svn-command nil 0 file
c217cb04 364 "merge"
02610d0e 365 "-r" (if second-version
c217cb04
SM
366 (concat first-version ":" second-version)
367 first-version))
1fd3454a
SM
368 (vc-file-setprop file 'vc-state 'edited)
369 (with-current-buffer (get-buffer "*vc*")
370 (goto-char (point-min))
c217cb04
SM
371 (if (looking-at "C ")
372 1 ; signal conflict
1fd3454a
SM
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)
ac3f4c6f 378 ;; (vc-file-setprop file 'vc-working-revision nil)
1fd3454a 379 (vc-file-setprop file 'vc-checkout-time 0)
8686a5ea 380 (vc-svn-command nil 0 file "--non-interactive" "update") ; see bug#7152
1fd3454a
SM
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))
5b5afd50 385 ;; get new working revision
1fd3454a 386 (if (re-search-forward
1468d754 387 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
ac3f4c6f
ER
388 (vc-file-setprop file 'vc-working-revision (match-string 2))
389 (vc-file-setprop file 'vc-working-revision nil))
1fd3454a 390 ;; get file status
1468d754 391 (goto-char (point-min))
1fd3454a 392 (prog1
1468d754 393 (if (looking-at "At revision")
1fd3454a
SM
394 0 ;; there were no news; indicate success
395 (if (re-search-forward
459b1fe4
SM
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 ]\\)? \\)"
1468d754 402 (regexp-quote (file-name-nondirectory file)))
1fd3454a
SM
403 nil t)
404 (cond
405 ;; Merge successful, we are in sync with repository now
459b1fe4 406 ((string= (match-string 2) "U")
1fd3454a
SM
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
459b1fe4 412 ((string= (match-string 2) "G")
1fd3454a
SM
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
42a0a135
ER
424(defun vc-svn-modify-change-comment (files rev comment)
425 "Modify the change comments for a specified REV.
426You must have ssh access to the repository host, and the directory Emacs
e09deae9 427uses locally for temp files must also be writable by you on that host.
fd064451
DN
428This is only supported if the repository access method is either file://
429or svn+ssh://."
430 (let (tempfile host remotefile directory fileurl-p)
42a0a135 431 (with-temp-buffer
f9d5dc48 432 (vc-do-command (current-buffer) 0 vc-svn-program nil "info")
fd064451
DN
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
2888a97e 449 "*vc*" 0 "svnadmin" nil
def61be2 450 "setlog" "--bypass-hooks" directory
fd064451
DN
451 "-r" rev (format "%s" tempfile))
452 (error "Log edit failed"))
453 (delete-file tempfile))
454
455 ;; Remote repository, using svn+ssh.
2888a97e 456 (unless (vc-do-command "*vc*" 0 "scp" nil "-q" tempfile remotefile)
fd064451
DN
457 (error "Copy of comment to %s failed" remotefile))
458 (unless (vc-do-command
2888a97e 459 "*vc*" 0 "ssh" nil "-q" host
fd064451
DN
460 (format "svnadmin setlog --bypass-hooks %s -r %s %s; rm %s"
461 directory rev tempfile tempfile))
462 (error "Log edit failed")))))
1fd3454a
SM
463
464;;;
465;;; History functions
466;;;
467
def61be2
JB
468(defvar log-view-per-file-logs)
469
6653c6b7
DN
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
662c5698 474(defun vc-svn-print-log (files buffer &optional shortlog start-revision limit)
8cdd17b4 475 "Get change log(s) associated with FILES."
1fd3454a 476 (save-current-buffer
b349012b 477 (vc-setup-buffer buffer)
1fd3454a
SM
478 (let ((inhibit-read-only t))
479 (goto-char (point-min))
13b56025
ER
480 (if files
481 (dolist (file files)
482 (insert "Working file: " file "\n")
6616006b
DN
483 (apply
484 'vc-svn-command
13b56025
ER
485 buffer
486 'async
77bf3f54 487 ;; (if (and (= (length files) 1) (vc-stay-local-p file 'SVN)) 'async 0)
13b56025
ER
488 (list file)
489 "log"
662c5698
DN
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"))
1db3226b 499 (when limit (list "--limit" (format "%s" limit))))))
13b56025 500 ;; Dump log for the entire directory.
662c5698
DN
501 (apply 'vc-svn-command buffer 0 nil "log"
502 (append
503 (list
504 (if start-revision (format "-r%s" start-revision) "-rHEAD:0"))
1db3226b 505 (when limit (list "--limit" (format "%s" limit)))))))))
8cdd17b4 506
8cdd17b4 507(defun vc-svn-diff (files &optional oldvers newvers buffer)
5b5afd50 508 "Get a difference report using SVN between two revisions of fileset FILES."
22b5692c
NR
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))
8cdd17b4 522 (let* ((switches
f9d1f3be
SM
523 (if vc-svn-diff-switches
524 (vc-switches 'SVN 'diff)
9751169a
GM
525 (list "--diff-cmd=diff" "-x"
526 (mapconcat 'identity (vc-switches nil 'diff) " "))))
2d4e93b9 527 (async (and (not vc-disable-async-diff)
77bf3f54 528 (vc-stay-local-p files 'SVN)
fe1919ab 529 (or oldvers newvers)))) ; Svn diffs those locally.
b349012b 530 (apply 'vc-svn-command buffer
2766aaaf 531 (if async 'async 0)
8cdd17b4 532 files "diff"
2766aaaf 533 (append
f9d1f3be 534 switches
2766aaaf
SM
535 (when oldvers
536 (list "-r" (if newvers (concat oldvers ":" newvers)
537 oldvers)))))
fd140743
SM
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.
8cdd17b4 541 (buffer-size (get-buffer buffer)))))
1fd3454a 542
1fd3454a 543;;;
370fded4 544;;; Tag system
1fd3454a
SM
545;;;
546
370fded4 547(defun vc-svn-create-tag (dir name branchp)
5b5afd50 548 "Assign to DIR's current revision a given NAME.
1fd3454a 549If BRANCHP is non-nil, the name is created as a branch (and the current
5cc7cb96
SM
550workspace is immediately moved to that new branch).
551NAME is assumed to be a URL."
552 (vc-svn-command nil 0 dir "copy" name)
370fded4 553 (when branchp (vc-svn-retrieve-tag dir name nil)))
1fd3454a 554
370fded4
ER
555(defun vc-svn-retrieve-tag (dir name update)
556 "Retrieve a tag at and below DIR.
557NAME is the name of the tag; if it is empty, do a `svn update'.
5cc7cb96
SM
558If UPDATE is non-nil, then update (resynch) any affected buffers.
559NAME is assumed to be a URL."
560 (vc-svn-command nil 0 dir "switch" name)
561 ;; FIXME: parse the output and obey `update'.
562 )
1fd3454a
SM
563
564;;;
565;;; Miscellaneous
566;;;
567
568;; Subversion makes backups for us, so don't bother.
77bf3f54
DN
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))
1fd3454a
SM
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
8cdd17b4 585(defun vc-svn-command (buffer okstatus file-or-list &rest flags)
1fd3454a
SM
586 "A wrapper around `vc-do-command' for use in vc-svn.el.
587The difference to vc-do-command is that this function always invokes `svn',
588and that it passes `vc-svn-global-switches' to it before FLAGS."
2888a97e 589 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-svn-program file-or-list
1fd3454a
SM
590 (if (stringp vc-svn-global-switches)
591 (cons vc-svn-global-switches flags)
592 (append vc-svn-global-switches
593 flags))))
594
7ffc77d3
SM
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)))
ace2fad9
CY
600 (vc-insert-file (expand-file-name (concat vc-svn-admin-directory
601 "/entries")
602 dirname)))
7ffc77d3
SM
603 (goto-char (point-min))
604 (when (re-search-forward
c45b3be3 605 ;; Old `svn' used name="svn:this_dir", newer use just name="".
17a5a301
SM
606 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
607 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
0b0dad41
SM
608 "url=\"\\(?1:[^\"]+\\)\""
609 ;; Yet newer ones don't use XML any more.
610 "\\|^\ndir\n[0-9]+\n\\(?1:.*\\)") nil t)
17a5a301
SM
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))))
1fd3454a 615
1c67a814
SM
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))
54648b5c
DN
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))))
1c67a814
SM
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
28e4e2b4 635 (smerge-start-session)
1c67a814
SM
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
bc8c1bb4 645(defun vc-svn-parse-status (&optional filename)
1fd3454a 646 "Parse output of \"svn status\" command in the current buffer.
bc8c1bb4
SM
647Set file properties accordingly. Unless FILENAME is non-nil, parse only
648information about FILENAME and return its status."
5dd4f3f7 649 (let (file status propstat)
1fd3454a
SM
650 (goto-char (point-min))
651 (while (re-search-forward
484c1b1f 652 ;; Ignore the files with status X.
245cacf1 653 "^\\(?:\\?\\|[ ACDGIMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\)\\) +" nil t)
c45b3be3
SM
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)))))
5dd4f3f7
GM
659 (setq status (char-after (line-beginning-position))
660 ;; Status of the item's properties ([ MC]).
661 propstat (char-after (1+ (line-beginning-position))))
484c1b1f 662 (if (eq status ??)
4f07b9f2 663 (vc-file-setprop file 'vc-state 'unregistered)
fd140743
SM
664 ;; Use the last-modified revision, so that searching in vc-print-log
665 ;; output works.
4f07b9f2 666 (vc-file-setprop file 'vc-working-revision (match-string 3))
1c67a814 667 ;; Remember Svn's own status.
4f07b9f2
ER
668 (vc-file-setprop file 'vc-svn-status status)
669 (vc-file-setprop
1fd3454a
SM
670 file 'vc-state
671 (cond
5dd4f3f7 672 ((and (eq status ?\ ) (eq propstat ?\ ))
1fd3454a 673 (if (eq (char-after (match-beginning 1)) ?*)
3702367b 674 'needs-update
4f07b9f2 675 (vc-file-setprop file 'vc-checkout-time
1fd3454a
SM
676 (nth 5 (file-attributes file)))
677 'up-to-date))
678 ((eq status ?A)
fd140743 679 ;; If the file was actually copied, (match-string 2) is "-".
4f07b9f2
ER
680 (vc-file-setprop file 'vc-working-revision "0")
681 (vc-file-setprop file 'vc-checkout-time 0)
484c1b1f 682 'added)
5dd4f3f7
GM
683 ;; Conflict in contents or properties.
684 ((or (eq status ?C) (eq propstat ?C))
7fbb4797 685 (vc-file-setprop file 'vc-state 'conflict))
5dd4f3f7
GM
686 ;; Modified contents or properties.
687 ((or (eq status ?M) (eq propstat ?M))
1fd3454a
SM
688 (if (eq (char-after (match-beginning 1)) ?*)
689 'needs-merge
690 'edited))
722f037f 691 ((eq status ?I)
4f07b9f2 692 (vc-file-setprop file 'vc-state 'ignored))
e6c01f09 693 ((memq status '(?D ?R))
4f07b9f2 694 (vc-file-setprop file 'vc-state 'removed))
bc8c1bb4 695 (t 'edited)))))
245cacf1 696 (when filename (vc-file-getprop filename 'vc-state))))
1fd3454a 697
1fd3454a
SM
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
5b5afd50
ER
706(defun vc-svn-valid-revision-number-p (tag)
707 "Return non-nil if TAG is a valid revision number."
1fd3454a
SM
708 (and (string-match "^[0-9]" tag)
709 (not (string-match "[^0-9]" tag))))
710
17a5a301
SM
711;; Support for `svn annotate'
712
713(defun vc-svn-annotate-command (file buf &optional rev)
837b0e99 714 (vc-svn-command buf 'async file "annotate" (if rev (concat "-r" rev))))
17a5a301
SM
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
e53ac718
DN
720(defvar vc-annotate-parent-rev)
721
17a5a301
SM
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
8228a275
MH
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
1fd3454a
SM
752(provide 'vc-svn)
753
754;;; vc-svn.el ends here