Refactor VC merging to fix a layer violation.
[emacs.git] / lisp / vc / vc-svn.el
blob00a0388c599b9242f865a2c3880c6406da90fe87
1 ;;; vc-svn.el --- non-resident support for Subversion version-control -*- lexical-binding:t -*-
3 ;; Copyright (C) 2003-2014 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
7 ;; Package: vc
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
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.
29 ;;; Code:
31 (eval-when-compile
32 (require 'vc))
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)
38 ;;;
39 ;;; Customization options
40 ;;;
42 (defgroup vc-svn nil
43 "VC Subversion (svn) backend."
44 :version "24.1"
45 :group 'vc)
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)
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)
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)
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)
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)
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.")
115 ;;; Properties of the backend
117 (defun vc-svn-revision-granularity () 'repository)
118 (defun vc-svn-checkout-model (_files) 'implicit)
121 ;;; State-querying functions
124 ;;; vc-svn-admin-directory is generally not defined when the
125 ;;; autoloaded function is called.
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))))
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))))))))))
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))))
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)))
198 ;; -dir-status called from vc-dir, which loads vc, which loads vc-dispatcher.
199 (declare-function vc-exec-after "vc-dispatcher" (code))
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)))
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)))
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 "")))))
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))
240 ;; vc-svn-mode-line-string doesn't exist because the default implementation
241 ;; works just fine.
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))))
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))))
262 ;;; State-changing functions
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")))
273 (autoload 'vc-switches "vc")
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)))
282 (defun vc-svn-root (file)
283 (vc-find-root file vc-svn-admin-directory))
285 (defalias 'vc-svn-responsible-p 'vc-svn-root)
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.")
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"))))
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))
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))))
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))))
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))))
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)))
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))
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.
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)))
371 (defun vc-svn-delete-file (file)
372 (vc-svn-command nil 0 file "remove"))
374 (defun vc-svn-rename-file (old new)
375 (vc-svn-command nil 0 new "move" (file-relative-name old)))
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")))
382 (defun vc-svn-merge-file (file)
383 "Accept a file merge request, prompting for revisions."
384 (let* ((first-revision
385 (vc-read-revision
386 (concat "Merge " file
387 " from SVN revision "
388 "(default news on current branch): ")
389 (list file)
390 'SVN))
391 second-revision
392 status)
393 (cond
394 ((string= first-revision "")
395 (setq status (vc-svn-merge-news file)))
397 (setq second-revision
398 (vc-read-revision
399 "Second SVN revision: "
400 (list file) 'SVN nil
401 first-revision))
402 (setq status (vc-svn-merge file first-revision second-revision))))
403 status))
405 (defun vc-svn-merge (file first-version &optional second-version)
406 "Merge changes into current working copy of FILE.
407 The changes are between FIRST-VERSION and SECOND-VERSION."
408 (vc-svn-command nil 0 file
409 "merge"
410 "-r" (if second-version
411 (concat first-version ":" second-version)
412 first-version))
413 (vc-file-setprop file 'vc-state 'edited)
414 (with-current-buffer (get-buffer "*vc*")
415 (goto-char (point-min))
416 (if (looking-at "C ")
417 1 ; signal conflict
418 0))) ; signal success
420 (defun vc-svn-merge-news (file)
421 "Merge in any new changes made to FILE."
422 (message "Merging changes into %s..." file)
423 ;; (vc-file-setprop file 'vc-working-revision nil)
424 (vc-file-setprop file 'vc-checkout-time 0)
425 (vc-svn-command nil 0 file "update")
426 ;; Analyze the merge result reported by SVN, and set
427 ;; file properties accordingly.
428 (with-current-buffer (get-buffer "*vc*")
429 (goto-char (point-min))
430 ;; get new working revision
431 (if (re-search-forward
432 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
433 (vc-file-setprop file 'vc-working-revision (match-string 2))
434 (vc-file-setprop file 'vc-working-revision nil))
435 ;; get file status
436 (goto-char (point-min))
437 (prog1
438 (if (looking-at "At revision")
439 0 ;; there were no news; indicate success
440 (if (re-search-forward
441 ;; Newer SVN clients have 3 columns of chars (one for the
442 ;; file's contents, then second for its properties, and the
443 ;; third for lock-grabbing info), before the 2 spaces.
444 ;; We also used to match the filename in column 0 without any
445 ;; meta-info before it, but I believe this can never happen.
446 (concat "^\\(\\([ACGDU]\\)\\(.[B ]\\)? \\)"
447 (regexp-quote (file-relative-name file)))
448 nil t)
449 (cond
450 ;; Merge successful, we are in sync with repository now
451 ((string= (match-string 2) "U")
452 (vc-file-setprop file 'vc-state 'up-to-date)
453 (vc-file-setprop file 'vc-checkout-time
454 (nth 5 (file-attributes file)))
455 0);; indicate success to the caller
456 ;; Merge successful, but our own changes are still in the file
457 ((string= (match-string 2) "G")
458 (vc-file-setprop file 'vc-state 'edited)
459 0);; indicate success to the caller
460 ;; Conflicts detected!
462 (vc-file-setprop file 'vc-state 'edited)
463 1);; signal the error to the caller
465 (pop-to-buffer "*vc*")
466 (error "Couldn't analyze svn update result")))
467 (message "Merging changes into %s...done" file))))
469 (defun vc-svn-modify-change-comment (_files rev comment)
470 "Modify the change comments for a specified REV.
471 You must have ssh access to the repository host, and the directory Emacs
472 uses locally for temp files must also be writable by you on that host.
473 This is only supported if the repository access method is either file://
474 or svn+ssh://."
475 (let (tempfile host remotefile directory fileurl-p)
476 (with-temp-buffer
477 (vc-svn-command (current-buffer) 0 nil "info")
478 (goto-char (point-min))
479 (unless (re-search-forward "Repository Root: \\(file://\\(/.*\\)\\)\\|\\(svn\\+ssh://\\([^/]+\\)\\(/.*\\)\\)" nil t)
480 (error "Repository information is unavailable"))
481 (if (match-string 1)
482 (progn
483 (setq fileurl-p t)
484 (setq directory (match-string 2)))
485 (setq host (match-string 4))
486 (setq directory (match-string 5))
487 (setq remotefile (concat host ":" tempfile))))
488 (with-temp-file (setq tempfile (make-temp-file user-mail-address))
489 (insert comment))
490 (if fileurl-p
491 ;; Repository Root is a local file.
492 (progn
493 (unless (vc-do-command
494 "*vc*" 0 "svnadmin" nil
495 "setlog" "--bypass-hooks" directory
496 "-r" rev (format "%s" tempfile))
497 (error "Log edit failed"))
498 (delete-file tempfile))
500 ;; Remote repository, using svn+ssh.
501 (unless (vc-do-command "*vc*" 0 "scp" nil "-q" tempfile remotefile)
502 (error "Copy of comment to %s failed" remotefile))
503 (unless (vc-do-command
504 "*vc*" 0 "ssh" nil "-q" host
505 (format "svnadmin setlog --bypass-hooks %s -r %s %s; rm %s"
506 directory rev tempfile tempfile))
507 (error "Log edit failed")))))
510 ;;; History functions
513 (defvar log-view-per-file-logs)
515 (define-derived-mode vc-svn-log-view-mode log-view-mode "SVN-Log-View"
516 (require 'add-log)
517 (set (make-local-variable 'log-view-per-file-logs) nil))
519 (autoload 'vc-setup-buffer "vc-dispatcher")
521 (defun vc-svn-print-log (files buffer &optional _shortlog start-revision limit)
522 "Print commit log associated with FILES into specified BUFFER.
523 SHORTLOG is ignored.
524 If START-REVISION is non-nil, it is the newest revision to show.
525 If LIMIT is non-nil, show no more than this many entries."
526 (save-current-buffer
527 (vc-setup-buffer buffer)
528 (let ((inhibit-read-only t))
529 (goto-char (point-min))
530 (if files
531 (dolist (file files)
532 (insert "Working file: " file "\n")
533 (apply
534 'vc-svn-command
535 buffer
536 'async
537 (list file)
538 "log"
539 (append
540 (list
541 (if start-revision
542 (format "-r%s:1" start-revision)
543 ;; By default Subversion only shows the log up to the
544 ;; working revision, whereas we also want the log of the
545 ;; subsequent commits. At least that's what the
546 ;; vc-cvs.el code does.
547 "-rHEAD:0"))
548 (when limit (list "--limit" (format "%s" limit))))))
549 ;; Dump log for the entire directory.
550 (apply 'vc-svn-command buffer 0 nil "log"
551 (append
552 (list
553 (if start-revision (format "-r%s" start-revision) "-rHEAD:0"))
554 (when limit (list "--limit" (format "%s" limit)))))))))
556 (defun vc-svn-diff (files &optional oldvers newvers buffer)
557 "Get a difference report using SVN between two revisions of fileset FILES."
558 (and oldvers
559 (not newvers)
560 files
561 (catch 'no
562 (dolist (f files)
563 (or (equal oldvers (vc-working-revision f))
564 (throw 'no nil)))
566 ;; Use nil rather than the current revision because svn handles
567 ;; it better (i.e. locally). Note that if _any_ of the files
568 ;; has a different revision, we fetch the lot, which is
569 ;; obviously sub-optimal.
570 (setq oldvers nil))
571 (let* ((switches
572 (if vc-svn-diff-switches
573 (vc-switches 'SVN 'diff)
574 (list (concat "--diff-cmd=" diff-command) "-x"
575 (mapconcat 'identity (vc-switches nil 'diff) " "))))
576 (async (and (not vc-disable-async-diff)
577 (or oldvers newvers)))) ; Svn diffs those locally.
578 (apply 'vc-svn-command buffer
579 (if async 'async 0)
580 files "diff"
581 (append
582 switches
583 (when oldvers
584 (list "-r" (if newvers (concat oldvers ":" newvers)
585 oldvers)))))
586 (if async 1 ; async diff => pessimistic assumption
587 ;; For some reason `svn diff' does not return a useful
588 ;; status w.r.t whether the diff was empty or not.
589 (buffer-size (get-buffer buffer)))))
592 ;;; Tag system
595 (defun vc-svn-create-tag (dir name branchp)
596 "Assign to DIR's current revision a given NAME.
597 If BRANCHP is non-nil, the name is created as a branch (and the current
598 workspace is immediately moved to that new branch).
599 NAME is assumed to be a URL."
600 (vc-svn-command nil 0 dir "copy" name)
601 (when branchp (vc-svn-retrieve-tag dir name nil)))
603 (defun vc-svn-retrieve-tag (dir name _update)
604 "Retrieve a tag at and below DIR.
605 NAME is the name of the tag; if it is empty, do a `svn update'.
606 If UPDATE is non-nil, then update (resynch) any affected buffers.
607 NAME is assumed to be a URL."
608 (vc-svn-command nil 0 dir "switch" name)
609 ;; FIXME: parse the output and obey `update'.
613 ;;; Miscellaneous
616 ;; Subversion makes backups for us, so don't bother.
617 ;; (defun vc-svn-make-version-backups-p (file)
618 ;; "Return non-nil if version backups should be made for FILE."
619 ;; nil)
621 (defun vc-svn-check-headers ()
622 "Check if the current file has any headers in it."
623 (save-excursion
624 (goto-char (point-min))
625 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
626 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
630 ;;; Internal functions
633 (defun vc-svn-command (buffer okstatus file-or-list &rest flags)
634 "A wrapper around `vc-do-command' for use in vc-svn.el.
635 The difference to vc-do-command is that this function always invokes `svn',
636 and that it passes `vc-svn-global-switches' to it before FLAGS."
637 (apply 'vc-do-command (or buffer "*vc*") okstatus vc-svn-program file-or-list
638 (if (stringp vc-svn-global-switches)
639 (cons vc-svn-global-switches flags)
640 (append vc-svn-global-switches flags))))
642 (defun vc-svn-resolve-when-done ()
643 "Call \"svn resolved\" if the conflict markers have been removed."
644 (save-excursion
645 (goto-char (point-min))
646 (unless (re-search-forward "^<<<<<<< " nil t)
647 (vc-svn-command nil 0 buffer-file-name "resolved")
648 ;; Remove the hook so that it is not called multiple times.
649 (remove-hook 'after-save-hook 'vc-svn-resolve-when-done t))))
651 ;; Inspired by vc-arch-find-file-hook.
652 (defun vc-svn-find-file-hook ()
653 (when (eq ?C (vc-file-getprop buffer-file-name 'vc-svn-status))
654 ;; If the file is marked as "conflicted", then we should try and call
655 ;; "svn resolved" when applicable.
656 (if (save-excursion
657 (goto-char (point-min))
658 (re-search-forward "^<<<<<<< " nil t))
659 ;; There are conflict markers.
660 (progn
661 (smerge-start-session)
662 (add-hook 'after-save-hook 'vc-svn-resolve-when-done nil t))
663 ;; There are no conflict markers. This is problematic: maybe it means
664 ;; the conflict has been resolved and we should immediately call "svn
665 ;; resolved", or it means that the file's type does not allow Svn to
666 ;; use conflict markers in which case we don't really know what to do.
667 ;; So let's just punt for now.
668 nil)
669 (message "There are unresolved conflicts in this file")))
671 (defun vc-svn-parse-status (&optional filename)
672 "Parse output of \"svn status\" command in the current buffer.
673 Set file properties accordingly. If FILENAME is non-nil, return its status."
674 (let (multifile file status propstat)
675 (goto-char (point-min))
676 (while (re-search-forward
677 ;; Ignore the files with status X.
678 "^\\(?:\\?\\|[ ACDGIMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\)\\) +" nil t)
679 ;; If the username contains spaces, the output format is ambiguous,
680 ;; so don't trust the output's filename unless we have to.
681 (setq file (or (unless multifile filename)
682 (expand-file-name
683 (buffer-substring (point) (line-end-position))))
684 ;; If we are parsing the result of running status on a directory,
685 ;; there could be multiple files in the output.
686 ;; We assume that filename, if supplied, applies to the first
687 ;; listed file (ie, the directory). Bug#15322.
688 multifile t
689 status (char-after (line-beginning-position))
690 ;; Status of the item's properties ([ MC]).
691 propstat (char-after (1+ (line-beginning-position))))
692 (if (eq status ??)
693 (vc-file-setprop file 'vc-state 'unregistered)
694 ;; Use the last-modified revision, so that searching in vc-print-log
695 ;; output works.
696 (vc-file-setprop file 'vc-working-revision (match-string 3))
697 ;; Remember Svn's own status.
698 (vc-file-setprop file 'vc-svn-status status)
699 (vc-file-setprop
700 file 'vc-state
701 (cond
702 ((and (eq status ?\ ) (eq propstat ?\ ))
703 (if (eq (char-after (match-beginning 1)) ?*)
704 'needs-update
705 (vc-file-setprop file 'vc-checkout-time
706 (nth 5 (file-attributes file)))
707 'up-to-date))
708 ((eq status ?A)
709 ;; If the file was actually copied, (match-string 2) is "-".
710 (vc-file-setprop file 'vc-working-revision "0")
711 (vc-file-setprop file 'vc-checkout-time 0)
712 'added)
713 ;; Conflict in contents or properties.
714 ((or (eq status ?C) (eq propstat ?C))
715 (vc-file-setprop file 'vc-state 'conflict))
716 ;; Modified contents or properties.
717 ((or (eq status ?M) (eq propstat ?M))
718 (if (eq (char-after (match-beginning 1)) ?*)
719 'needs-merge
720 'edited))
721 ((eq status ?I)
722 (vc-file-setprop file 'vc-state 'ignored))
723 ((memq status '(?D ?R))
724 (vc-file-setprop file 'vc-state 'removed))
725 (t 'edited)))))
726 (when filename (vc-file-getprop filename 'vc-state))))
728 (defun vc-svn-valid-symbolic-tag-name-p (tag)
729 "Return non-nil if TAG is a valid symbolic tag name."
730 ;; According to the SVN manual, a valid symbolic tag must start with
731 ;; an uppercase or lowercase letter and can contain uppercase and
732 ;; lowercase letters, digits, `-', and `_'.
733 (and (string-match "^[a-zA-Z]" tag)
734 (not (string-match "[^a-z0-9A-Z-_]" tag))))
736 (defun vc-svn-valid-revision-number-p (tag)
737 "Return non-nil if TAG is a valid revision number."
738 (and (string-match "^[0-9]" tag)
739 (not (string-match "[^0-9]" tag))))
741 ;; Support for `svn annotate'
743 (defun vc-svn-annotate-command (file buf &optional rev)
744 (vc-svn-command buf 'async file "annotate" (if rev (concat "-r" rev))))
746 (defun vc-svn-annotate-time-of-rev (rev)
747 ;; Arbitrarily assume 10 commits per day.
748 (/ (string-to-number rev) 10.0))
750 (defvar vc-annotate-parent-rev)
752 (defun vc-svn-annotate-current-time ()
753 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
755 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
757 (defun vc-svn-annotate-time ()
758 (when (looking-at vc-svn-annotate-re)
759 (goto-char (match-end 0))
760 (vc-svn-annotate-time-of-rev (match-string 1))))
762 (defun vc-svn-annotate-extract-revision-at-line ()
763 (save-excursion
764 (beginning-of-line)
765 (if (looking-at vc-svn-annotate-re) (match-string 1))))
767 (defun vc-svn-revision-table (files)
768 (let ((vc-svn-revisions '()))
769 (with-current-buffer "*vc*"
770 (vc-svn-command nil 0 files "log" "-q")
771 (goto-char (point-min))
772 (forward-line)
773 (let ((start (point-min))
774 (loglines (buffer-substring-no-properties (point-min)
775 (point-max))))
776 (while (string-match "^r\\([0-9]+\\) " loglines)
777 (push (match-string 1 loglines) vc-svn-revisions)
778 (setq start (+ start (match-end 0)))
779 (setq loglines (buffer-substring-no-properties start (point-max)))))
780 vc-svn-revisions)))
782 (provide 'vc-svn)
784 ;;; vc-svn.el ends here