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