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