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