(vc-svn-merge-news): Understand the new format with two
[emacs.git] / lisp / vc-svn.el
blob730806fdcd08c191e852b66f64b877f5fff9dd88
1 ;;; vc-svn.el --- non-resident support for Subversion version-control
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
8 ;; This file is part of GNU Emacs.
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
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
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.
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
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; This is preliminary support for Subversion (http://subversion.tigris.org/).
28 ;; It started as `sed s/cvs/svn/ vc.cvs.el' (from version 1.56)
29 ;; and hasn't been completely fixed since.
31 ;; Sync'd with Subversion's vc-svn.el as of revision 5801.
33 ;;; Bugs:
35 ;; - VC-dired is (really) slow.
37 ;;; Code:
39 (eval-when-compile
40 (require 'vc))
42 ;;;
43 ;;; Customization options
44 ;;;
46 (defcustom vc-svn-global-switches nil
47 "*Global switches to pass to any SVN command."
48 :type '(choice (const :tag "None" nil)
49 (string :tag "Argument String")
50 (repeat :tag "Argument List"
51 :value ("")
52 string))
53 :version "22.1"
54 :group 'vc)
56 (defcustom vc-svn-register-switches nil
57 "*Extra switches for registering a file into SVN.
58 A string or list of strings passed to the checkin program by
59 \\[vc-register]."
60 :type '(choice (const :tag "None" nil)
61 (string :tag "Argument String")
62 (repeat :tag "Argument List"
63 :value ("")
64 string))
65 :version "22.1"
66 :group 'vc)
68 (defcustom vc-svn-diff-switches
69 t ;`svn' doesn't support common args like -c or -b.
70 "String or list of strings specifying extra switches for svn diff under VC.
71 If nil, use the value of `vc-diff-switches'.
72 If you want to force an empty list of arguments, use t."
73 :type '(choice (const :tag "Unspecified" nil)
74 (const :tag "None" t)
75 (string :tag "Argument String")
76 (repeat :tag "Argument List"
77 :value ("")
78 string))
79 :version "22.1"
80 :group 'vc)
82 (defcustom vc-svn-header (or (cdr (assoc 'SVN vc-header-alist)) '("\$Id\$"))
83 "*Header keywords to be inserted by `vc-insert-headers'."
84 :version "22.1"
85 :type '(repeat string)
86 :group 'vc)
88 (defconst vc-svn-use-edit nil
89 ;; Subversion does not provide this feature (yet).
90 "*Non-nil means to use `svn edit' to \"check out\" a file.
91 This is only meaningful if you don't use the implicit checkout model
92 \(i.e. if you have $SVNREAD set)."
93 ;; :type 'boolean
94 ;; :version "22.1"
95 ;; :group 'vc
98 (defvar vc-svn-admin-directory
99 (cond ((and (eq system-type 'windows-nt)
100 (getenv "SVN_ASP_DOT_NET_HACK"))
101 "_svn")
102 (t ".svn"))
103 "The name of the \".svn\" subdirectory or its equivalent.")
106 ;;; State-querying functions
109 ;;; vc-svn-admin-directory is generally not defined when the
110 ;;; autoloaded function is called.
112 ;;;###autoload (defun vc-svn-registered (f)
113 ;;;###autoload (let ((admin-dir (cond ((and (eq system-type 'windows-nt)
114 ;;;###autoload (getenv "SVN_ASP_DOT_NET_HACK"))
115 ;;;###autoload "_svn")
116 ;;;###autoload (t ".svn"))))
117 ;;;###autoload (when (file-readable-p (expand-file-name
118 ;;;###autoload (concat admin-dir "/entries")
119 ;;;###autoload (file-name-directory f)))
120 ;;;###autoload (load "vc-svn")
121 ;;;###autoload (vc-svn-registered f))))
123 ;;;###autoload
124 (add-to-list 'completion-ignored-extensions ".svn/")
126 (defun vc-svn-registered (file)
127 "Check if FILE is SVN registered."
128 (when (file-readable-p (expand-file-name (concat vc-svn-admin-directory
129 "/entries")
130 (file-name-directory file)))
131 (with-temp-buffer
132 (cd (file-name-directory file))
133 (let ((status
134 (condition-case nil
135 ;; Ignore all errors.
136 (vc-svn-command t t file "status" "-v")
137 ;; Some problem happened. E.g. We can't find an `svn'
138 ;; executable. We used to only catch `file-error' but when
139 ;; the process is run on a remote host via Tramp, the error
140 ;; is only reported via the exit status which is turned into
141 ;; an `error' by vc-do-command.
142 (error nil))))
143 (when (eq 0 status)
144 (vc-svn-parse-status file))))))
146 (defun vc-svn-state (file &optional localp)
147 "SVN-specific version of `vc-state'."
148 (setq localp (or localp (vc-stay-local-p file)))
149 (with-temp-buffer
150 (cd (file-name-directory file))
151 (vc-svn-command t 0 file "status" (if localp "-v" "-u"))
152 (vc-svn-parse-status file)))
154 (defun vc-svn-state-heuristic (file)
155 "SVN-specific state heuristic."
156 (vc-svn-state file 'local))
158 (defun vc-svn-dir-state (dir &optional localp)
159 "Find the SVN state of all files in DIR."
160 (setq localp (or localp (vc-stay-local-p dir)))
161 (let ((default-directory dir))
162 ;; Don't specify DIR in this command, the default-directory is
163 ;; enough. Otherwise it might fail with remote repositories.
164 (with-temp-buffer
165 (vc-svn-command t 0 nil "status" (if localp "-v" "-u"))
166 (vc-svn-parse-status))))
168 (defun vc-svn-workfile-version (file)
169 "SVN-specific version of `vc-workfile-version'."
170 ;; There is no need to consult RCS headers under SVN, because we
171 ;; get the workfile version for free when we recognize that a file
172 ;; is registered in SVN.
173 (vc-svn-registered file)
174 (vc-file-getprop file 'vc-workfile-version))
176 (defun vc-svn-checkout-model (file)
177 "SVN-specific version of `vc-checkout-model'."
178 ;; It looks like Subversion has no equivalent of CVSREAD.
179 'implicit)
181 ;; vc-svn-mode-line-string doesn't exist because the default implementation
182 ;; works just fine.
184 (defun vc-svn-dired-state-info (file)
185 "SVN-specific version of `vc-dired-state-info'."
186 (let ((svn-state (vc-state file)))
187 (cond ((eq svn-state 'edited)
188 (if (equal (vc-workfile-version file) "0")
189 "(added)" "(modified)"))
190 ((eq svn-state 'needs-patch) "(patch)")
191 ((eq svn-state 'needs-merge) "(merge)"))))
193 (defun vc-svn-previous-version (file rev)
194 (let ((newrev (1- (string-to-number rev))))
195 (when (< 0 newrev)
196 (number-to-string newrev))))
198 (defun vc-svn-next-version (file rev)
199 (let ((newrev (1+ (string-to-number rev))))
200 ;; The "workfile version" is an uneasy conceptual fit under Subversion;
201 ;; we use it as the upper bound until a better idea comes along. If the
202 ;; workfile version W coincides with the tree's latest revision R, then
203 ;; this check prevents a "no such revision: R+1" error. Otherwise, it
204 ;; inhibits showing of W+1 through R, which could be considered anywhere
205 ;; from gracious to impolite.
206 (unless (< (string-to-number (vc-file-getprop file 'vc-workfile-version))
207 newrev)
208 (number-to-string newrev))))
212 ;;; State-changing functions
215 (defun vc-svn-register (file &optional rev comment)
216 "Register FILE into the SVN version-control system.
217 COMMENT can be used to provide an initial description of FILE.
219 `vc-register-switches' and `vc-svn-register-switches' are passed to
220 the SVN command (in that order)."
221 (apply 'vc-svn-command nil 0 file "add" (vc-switches 'SVN 'register)))
223 (defun vc-svn-responsible-p (file)
224 "Return non-nil if SVN thinks it is responsible for FILE."
225 (file-directory-p (expand-file-name vc-svn-admin-directory
226 (if (file-directory-p file)
227 file
228 (file-name-directory file)))))
230 (defalias 'vc-svn-could-register 'vc-svn-responsible-p
231 "Return non-nil if FILE could be registered in SVN.
232 This is only possible if SVN is responsible for FILE's directory.")
234 (defun vc-svn-checkin (file rev comment)
235 "SVN-specific version of `vc-backend-checkin'."
236 (let ((status (apply
237 'vc-svn-command nil 1 file "ci"
238 (nconc (list "-m" comment) (vc-switches 'SVN 'checkin)))))
239 (set-buffer "*vc*")
240 (goto-char (point-min))
241 (unless (equal status 0)
242 ;; Check checkin problem.
243 (cond
244 ((search-forward "Transaction is out of date" nil t)
245 (vc-file-setprop file 'vc-state 'needs-merge)
246 (error (substitute-command-keys
247 (concat "Up-to-date check failed: "
248 "type \\[vc-next-action] to merge in changes"))))
250 (pop-to-buffer (current-buffer))
251 (goto-char (point-min))
252 (shrink-window-if-larger-than-buffer)
253 (error "Check-in failed"))))
254 ;; Update file properties
255 ;; (vc-file-setprop
256 ;; file 'vc-workfile-version
257 ;; (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
260 (defun vc-svn-find-version (file rev buffer)
261 (apply 'vc-svn-command
262 buffer 0 file
263 "cat"
264 (and rev (not (string= rev ""))
265 (concat "-r" rev))
266 (vc-switches 'SVN 'checkout)))
268 (defun vc-svn-checkout (file &optional editable rev)
269 (message "Checking out %s..." file)
270 (with-current-buffer (or (get-file-buffer file) (current-buffer))
271 (vc-call update file editable rev (vc-switches 'SVN 'checkout)))
272 (vc-mode-line file)
273 (message "Checking out %s...done" file))
275 (defun vc-svn-update (file editable rev switches)
276 (if (and (file-exists-p file) (not rev))
277 ;; If no revision was specified, just make the file writable
278 ;; if necessary (using `svn-edit' if requested).
279 (and editable (not (eq (vc-svn-checkout-model file) 'implicit))
280 (if vc-svn-use-edit
281 (vc-svn-command nil 0 file "edit")
282 (set-file-modes file (logior (file-modes file) 128))
283 (if (equal file buffer-file-name) (toggle-read-only -1))))
284 ;; Check out a particular version (or recreate the file).
285 (vc-file-setprop file 'vc-workfile-version nil)
286 (apply 'vc-svn-command nil 0 file
287 "update"
288 ;; default for verbose checkout: clear the sticky tag so
289 ;; that the actual update will get the head of the trunk
290 (cond
291 ((null rev) "-rBASE")
292 ((or (eq rev t) (equal rev "")) nil)
293 (t (concat "-r" rev)))
294 switches)))
296 (defun vc-svn-delete-file (file)
297 (vc-svn-command nil 0 file "remove"))
299 (defun vc-svn-rename-file (old new)
300 (vc-svn-command nil 0 new "move" (file-relative-name old)))
302 (defun vc-svn-revert (file &optional contents-done)
303 "Revert FILE to the version it was based on."
304 (unless contents-done
305 (vc-svn-command nil 0 file "revert"))
306 (unless (eq (vc-checkout-model file) 'implicit)
307 (if vc-svn-use-edit
308 (vc-svn-command nil 0 file "unedit")
309 ;; Make the file read-only by switching off all w-bits
310 (set-file-modes file (logand (file-modes file) 3950)))))
312 (defun vc-svn-merge (file first-version &optional second-version)
313 "Merge changes into current working copy of FILE.
314 The changes are between FIRST-VERSION and SECOND-VERSION."
315 (vc-svn-command nil 0 file
316 "merge"
317 "-r" (if second-version
318 (concat first-version ":" second-version)
319 first-version))
320 (vc-file-setprop file 'vc-state 'edited)
321 (with-current-buffer (get-buffer "*vc*")
322 (goto-char (point-min))
323 (if (looking-at "C ")
324 1 ; signal conflict
325 0))) ; signal success
327 (defun vc-svn-merge-news (file)
328 "Merge in any new changes made to FILE."
329 (message "Merging changes into %s..." file)
330 ;; (vc-file-setprop file 'vc-workfile-version nil)
331 (vc-file-setprop file 'vc-checkout-time 0)
332 (vc-svn-command nil 0 file "update")
333 ;; Analyze the merge result reported by SVN, and set
334 ;; file properties accordingly.
335 (with-current-buffer (get-buffer "*vc*")
336 (goto-char (point-min))
337 ;; get new workfile version
338 (if (re-search-forward
339 "^\\(Updated to\\|At\\) revision \\([0-9]+\\)" nil t)
340 (vc-file-setprop file 'vc-workfile-version (match-string 2))
341 (vc-file-setprop file 'vc-workfile-version nil))
342 ;; get file status
343 (goto-char (point-min))
344 (prog1
345 (if (looking-at "At revision")
346 0 ;; there were no news; indicate success
347 (if (re-search-forward
348 ;; Newer SVN clients have 3 columns of chars (one for the
349 ;; file's contents, then second for its properties, and the
350 ;; third for lock-grabbing info), before the 2 spaces.
351 ;; We also used to match the filename in column 0 without any
352 ;; meta-info before it, but I believe this can never happen.
353 (concat "^\\(\\([ACGDU]\\)\\(.[B ]\\)? \\)"
354 (regexp-quote (file-name-nondirectory file)))
355 nil t)
356 (cond
357 ;; Merge successful, we are in sync with repository now
358 ((string= (match-string 2) "U")
359 (vc-file-setprop file 'vc-state 'up-to-date)
360 (vc-file-setprop file 'vc-checkout-time
361 (nth 5 (file-attributes file)))
362 0);; indicate success to the caller
363 ;; Merge successful, but our own changes are still in the file
364 ((string= (match-string 2) "G")
365 (vc-file-setprop file 'vc-state 'edited)
366 0);; indicate success to the caller
367 ;; Conflicts detected!
369 (vc-file-setprop file 'vc-state 'edited)
370 1);; signal the error to the caller
372 (pop-to-buffer "*vc*")
373 (error "Couldn't analyze svn update result")))
374 (message "Merging changes into %s...done" file))))
378 ;;; History functions
381 (defun vc-svn-print-log (file &optional buffer)
382 "Get change log associated with FILE."
383 (save-current-buffer
384 (vc-setup-buffer buffer)
385 (let ((inhibit-read-only t))
386 (goto-char (point-min))
387 ;; Add a line to tell log-view-mode what file this is.
388 (insert "Working file: " (file-relative-name file) "\n"))
389 (vc-svn-command
390 buffer
391 (if (and (vc-stay-local-p file) (fboundp 'start-process)) 'async 0)
392 file "log"
393 ;; By default Subversion only shows the log upto the working version,
394 ;; whereas we also want the log of the subsequent commits. At least
395 ;; that's what the vc-cvs.el code does.
396 "-rHEAD:0")))
398 (defun vc-svn-diff (file &optional oldvers newvers buffer)
399 "Get a difference report using SVN between two versions of FILE."
400 (unless buffer (setq buffer "*vc-diff*"))
401 (if (and oldvers (equal oldvers (vc-workfile-version file)))
402 ;; Use nil rather than the current revision because svn handles it
403 ;; better (i.e. locally).
404 (setq oldvers nil))
405 (if (string= (vc-workfile-version file) "0")
406 ;; This file is added but not yet committed; there is no master file.
407 (if (or oldvers newvers)
408 (error "No revisions of %s exist" file)
409 ;; We regard this as "changed".
410 ;; Diff it against /dev/null.
411 ;; Note: this is NOT a "svn diff".
412 (apply 'vc-do-command buffer
413 1 "diff" file
414 (append (vc-switches nil 'diff) '("/dev/null")))
415 ;; Even if it's empty, it's locally modified.
417 (let* ((switches
418 (if vc-svn-diff-switches
419 (vc-switches 'SVN 'diff)
420 (list "-x" (mapconcat 'identity (vc-switches nil 'diff) " "))))
421 (async (and (not vc-disable-async-diff)
422 (vc-stay-local-p file)
423 (or oldvers newvers) ; Svn diffs those locally.
424 (fboundp 'start-process))))
425 (apply 'vc-svn-command buffer
426 (if async 'async 0)
427 file "diff"
428 (append
429 switches
430 (when oldvers
431 (list "-r" (if newvers (concat oldvers ":" newvers)
432 oldvers)))))
433 (if async 1 ; async diff => pessimistic assumption
434 ;; For some reason `svn diff' does not return a useful
435 ;; status w.r.t whether the diff was empty or not.
436 (buffer-size (get-buffer buffer))))))
438 (defun vc-svn-diff-tree (dir &optional rev1 rev2)
439 "Diff all files at and below DIR."
440 (vc-svn-diff (file-name-as-directory dir) rev1 rev2))
443 ;;; Snapshot system
446 (defun vc-svn-create-snapshot (dir name branchp)
447 "Assign to DIR's current version a given NAME.
448 If BRANCHP is non-nil, the name is created as a branch (and the current
449 workspace is immediately moved to that new branch).
450 NAME is assumed to be a URL."
451 (vc-svn-command nil 0 dir "copy" name)
452 (when branchp (vc-svn-retrieve-snapshot dir name nil)))
454 (defun vc-svn-retrieve-snapshot (dir name update)
455 "Retrieve a snapshot at and below DIR.
456 NAME is the name of the snapshot; if it is empty, do a `svn update'.
457 If UPDATE is non-nil, then update (resynch) any affected buffers.
458 NAME is assumed to be a URL."
459 (vc-svn-command nil 0 dir "switch" name)
460 ;; FIXME: parse the output and obey `update'.
464 ;;; Miscellaneous
467 ;; Subversion makes backups for us, so don't bother.
468 ;; (defalias 'vc-svn-make-version-backups-p 'vc-stay-local-p
469 ;; "Return non-nil if version backups should be made for FILE.")
471 (defun vc-svn-check-headers ()
472 "Check if the current file has any headers in it."
473 (save-excursion
474 (goto-char (point-min))
475 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
476 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
480 ;;; Internal functions
483 (defun vc-svn-command (buffer okstatus file &rest flags)
484 "A wrapper around `vc-do-command' for use in vc-svn.el.
485 The difference to vc-do-command is that this function always invokes `svn',
486 and that it passes `vc-svn-global-switches' to it before FLAGS."
487 (apply 'vc-do-command buffer okstatus "svn" file
488 (if (stringp vc-svn-global-switches)
489 (cons vc-svn-global-switches flags)
490 (append vc-svn-global-switches
491 flags))))
493 (defun vc-svn-repository-hostname (dirname)
494 (with-temp-buffer
495 (let ((coding-system-for-read
496 (or file-name-coding-system
497 default-file-name-coding-system)))
498 (vc-insert-file (expand-file-name (concat vc-svn-admin-directory
499 "/entries")
500 dirname)))
501 (goto-char (point-min))
502 (when (re-search-forward
503 ;; Old `svn' used name="svn:this_dir", newer use just name="".
504 (concat "name=\"\\(?:svn:this_dir\\)?\"[\n\t ]*"
505 "\\(?:[-a-z]+=\"[^\"]*\"[\n\t ]*\\)*?"
506 "url=\"\\([^\"]+\\)\"") nil t)
507 ;; This is not a hostname but a URL. This may actually be considered
508 ;; as a feature since it allows vc-svn-stay-local to specify different
509 ;; behavior for different modules on the same server.
510 (match-string 1))))
512 (defun vc-svn-parse-status (&optional filename)
513 "Parse output of \"svn status\" command in the current buffer.
514 Set file properties accordingly. Unless FILENAME is non-nil, parse only
515 information about FILENAME and return its status."
516 (let (file status)
517 (goto-char (point-min))
518 (while (re-search-forward
519 ;; Ignore the files with status in [IX?].
520 "^[ ACDGMR!~][ MC][ L][ +][ S]..\\([ *]\\) +\\([-0-9]+\\) +\\([0-9?]+\\) +\\([^ ]+\\) +" nil t)
521 ;; If the username contains spaces, the output format is ambiguous,
522 ;; so don't trust the output's filename unless we have to.
523 (setq file (or filename
524 (expand-file-name
525 (buffer-substring (point) (line-end-position)))))
526 (setq status (char-after (line-beginning-position)))
527 (unless (eq status ??)
528 ;; `vc-BACKEND-registered' must not set vc-backend,
529 ;; which is instead set in vc-registered.
530 (unless filename (vc-file-setprop file 'vc-backend 'SVN))
531 ;; Use the last-modified revision, so that searching in vc-print-log
532 ;; output works.
533 (vc-file-setprop file 'vc-workfile-version (match-string 3))
534 (vc-file-setprop
535 file 'vc-state
536 (cond
537 ((eq status ?\ )
538 (if (eq (char-after (match-beginning 1)) ?*)
539 'needs-patch
540 (vc-file-setprop file 'vc-checkout-time
541 (nth 5 (file-attributes file)))
542 'up-to-date))
543 ((eq status ?A)
544 ;; If the file was actually copied, (match-string 2) is "-".
545 (vc-file-setprop file 'vc-workfile-version "0")
546 (vc-file-setprop file 'vc-checkout-time 0)
547 'edited)
548 ((memq status '(?M ?C))
549 (if (eq (char-after (match-beginning 1)) ?*)
550 'needs-merge
551 'edited))
552 (t 'edited)))))
553 (if filename (vc-file-getprop filename 'vc-state))))
555 (defun vc-svn-dir-state-heuristic (dir)
556 "Find the SVN state of all files in DIR, using only local information."
557 (vc-svn-dir-state dir 'local))
559 (defun vc-svn-valid-symbolic-tag-name-p (tag)
560 "Return non-nil if TAG is a valid symbolic tag name."
561 ;; According to the SVN manual, a valid symbolic tag must start with
562 ;; an uppercase or lowercase letter and can contain uppercase and
563 ;; lowercase letters, digits, `-', and `_'.
564 (and (string-match "^[a-zA-Z]" tag)
565 (not (string-match "[^a-z0-9A-Z-_]" tag))))
567 (defun vc-svn-valid-version-number-p (tag)
568 "Return non-nil if TAG is a valid version number."
569 (and (string-match "^[0-9]" tag)
570 (not (string-match "[^0-9]" tag))))
572 ;; Support for `svn annotate'
574 (defun vc-svn-annotate-command (file buf &optional rev)
575 (vc-svn-command buf 0 file "annotate" (if rev (concat "-r" rev))))
577 (defun vc-svn-annotate-time-of-rev (rev)
578 ;; Arbitrarily assume 10 commmits per day.
579 (/ (string-to-number rev) 10.0))
581 (defun vc-svn-annotate-current-time ()
582 (vc-svn-annotate-time-of-rev vc-annotate-parent-rev))
584 (defconst vc-svn-annotate-re "[ \t]*\\([0-9]+\\)[ \t]+[^\t ]+ ")
586 (defun vc-svn-annotate-time ()
587 (when (looking-at vc-svn-annotate-re)
588 (goto-char (match-end 0))
589 (vc-svn-annotate-time-of-rev (match-string 1))))
591 (defun vc-svn-annotate-extract-revision-at-line ()
592 (save-excursion
593 (beginning-of-line)
594 (if (looking-at vc-svn-annotate-re) (match-string 1))))
596 (provide 'vc-svn)
598 ;; arch-tag: 02f10c68-2b4d-453a-90fc-1eee6cfb268d
599 ;;; vc-svn.el ends here