vc-svn.el 28 KB

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