vc-svn.el 30 KB

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