ediff-vers.el 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. ;;; ediff-vers.el --- version control interface to Ediff
  2. ;; Copyright (C) 1995-1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
  4. ;; Package: ediff
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. ;; Compiler pacifier
  19. (defvar rcs-default-co-switches)
  20. (and noninteractive
  21. (eval-when-compile
  22. (condition-case nil
  23. ;; for compatibility with current stable version of xemacs
  24. (progn
  25. ;;(require 'pcvs nil 'noerror)
  26. ;;(require 'rcs nil 'noerror)
  27. (require 'pcvs)
  28. (require 'rcs))
  29. (error nil))
  30. (require 'vc)
  31. (require 'ediff-init)
  32. ))
  33. ;; end pacifier
  34. (defcustom ediff-keep-tmp-versions nil
  35. "If t, do not delete temporary previous versions for the files on which
  36. comparison or merge operations are being performed."
  37. :type 'boolean
  38. :group 'ediff-vers
  39. )
  40. (defalias 'ediff-vc-revision-other-window
  41. (if (fboundp 'vc-revision-other-window)
  42. 'vc-revision-other-window
  43. 'vc-version-other-window))
  44. (defalias 'ediff-vc-working-revision
  45. (if (fboundp 'vc-working-revision)
  46. 'vc-working-revision
  47. 'vc-workfile-version))
  48. ;; VC.el support
  49. (eval-when-compile
  50. (require 'vc-hooks)) ;; for vc-call macro
  51. (defun ediff-vc-latest-version (file)
  52. "Return the version level of the latest version of FILE in repository."
  53. (if (fboundp 'vc-latest-version)
  54. (vc-latest-version file)
  55. (or (vc-file-getprop file 'vc-latest-revision)
  56. (cond ((vc-backend file)
  57. (vc-call state file)
  58. (vc-file-getprop file 'vc-latest-revision))
  59. (t (error "File %s is not under version control" file))))
  60. ))
  61. (defun ediff-vc-internal (rev1 rev2 &optional startup-hooks)
  62. ;; Run Ediff on versions of the current buffer.
  63. ;; If REV1 is "", use the latest version of the current buffer's file.
  64. ;; If REV2 is "" then compare current buffer with REV1.
  65. ;; If the current buffer is named `F', the version is named `F.~REV~'.
  66. ;; If `F.~REV~' already exists, it is used instead of being re-created.
  67. (let (file1 file2 rev1buf rev2buf)
  68. (if (string= rev1 "")
  69. (setq rev1 (ediff-vc-latest-version (buffer-file-name))))
  70. (save-window-excursion
  71. (save-excursion
  72. (ediff-vc-revision-other-window rev1)
  73. (setq rev1buf (current-buffer)
  74. file1 (buffer-file-name)))
  75. (save-excursion
  76. (or (string= rev2 "") ; use current buffer
  77. (ediff-vc-revision-other-window rev2))
  78. (setq rev2buf (current-buffer)
  79. file2 (buffer-file-name)))
  80. (setq startup-hooks
  81. (cons `(lambda ()
  82. (ediff-delete-version-file ,file1)
  83. (or ,(string= rev2 "") (ediff-delete-version-file ,file2)))
  84. startup-hooks)))
  85. (ediff-buffers
  86. rev1buf rev2buf
  87. startup-hooks
  88. 'ediff-revision)))
  89. ;; RCS.el support
  90. (defun rcs-ediff-view-revision (&optional rev)
  91. ;; View previous RCS revision of current file.
  92. ;; With prefix argument, prompts for a revision name.
  93. (interactive (list (if current-prefix-arg
  94. (read-string "Revision: "))))
  95. (let* ((filename (buffer-file-name (current-buffer)))
  96. (switches (append '("-p")
  97. (if rev (list (concat "-r" rev)) nil)))
  98. (buff (concat (file-name-nondirectory filename) ".~" rev "~")))
  99. (message "Working ...")
  100. (setq filename (expand-file-name filename))
  101. (with-output-to-temp-buffer buff
  102. (ediff-with-current-buffer standard-output
  103. (fundamental-mode))
  104. (let ((output-buffer (ediff-rcs-get-output-buffer filename buff)))
  105. (delete-windows-on output-buffer)
  106. (with-current-buffer output-buffer
  107. (apply 'call-process "co" nil t nil
  108. ;; -q: quiet (no diagnostics)
  109. (append switches rcs-default-co-switches
  110. (list "-q" filename)))))
  111. (message "")
  112. buff)))
  113. (defun ediff-rcs-get-output-buffer (file name)
  114. ;; Get a buffer for RCS output for FILE, make it writable and clean it up.
  115. ;; Optional NAME is name to use instead of `*RCS-output*'.
  116. ;; This is a modified version from rcs.el v1.1. I use it here to make
  117. ;; Ediff immune to changes in rcs.el
  118. (let ((buf (get-buffer-create name)))
  119. (with-current-buffer buf
  120. (setq buffer-read-only nil
  121. default-directory (file-name-directory (expand-file-name file)))
  122. (erase-buffer))
  123. buf))
  124. (defun ediff-rcs-internal (rev1 rev2 &optional startup-hooks)
  125. ;; Run Ediff on versions of the current buffer.
  126. ;; If REV2 is "" then use current buffer.
  127. (let (rev2buf rev1buf)
  128. (save-window-excursion
  129. (setq rev2buf (if (string= rev2 "")
  130. (current-buffer)
  131. (rcs-ediff-view-revision rev2))
  132. rev1buf (rcs-ediff-view-revision rev1)))
  133. ;; rcs.el doesn't create temp version files, so we don't have to delete
  134. ;; anything in startup hooks to ediff-buffers
  135. (ediff-buffers rev1buf rev2buf startup-hooks 'ediff-revision)
  136. ))
  137. ;;; Merge with Version Control
  138. (defun ediff-vc-merge-internal (rev1 rev2 ancestor-rev
  139. &optional startup-hooks merge-buffer-file)
  140. ;; If ANCESTOR-REV non-nil, merge with ancestor
  141. (let (buf1 buf2 ancestor-buf)
  142. (save-window-excursion
  143. (save-excursion
  144. (ediff-vc-revision-other-window rev1)
  145. (setq buf1 (current-buffer)))
  146. (save-excursion
  147. (or (string= rev2 "")
  148. (ediff-vc-revision-other-window rev2))
  149. (setq buf2 (current-buffer)))
  150. (if ancestor-rev
  151. (save-excursion
  152. (if (string= ancestor-rev "")
  153. (setq ancestor-rev (ediff-vc-working-revision buffer-file-name)))
  154. (ediff-vc-revision-other-window ancestor-rev)
  155. (setq ancestor-buf (current-buffer))))
  156. (setq startup-hooks
  157. (cons
  158. `(lambda ()
  159. (ediff-delete-version-file ,(buffer-file-name buf1))
  160. (or ,(string= rev2 "")
  161. (ediff-delete-version-file ,(buffer-file-name buf2)))
  162. (or ,(string= ancestor-rev "")
  163. ,(not ancestor-rev)
  164. (ediff-delete-version-file ,(buffer-file-name ancestor-buf)))
  165. )
  166. startup-hooks)))
  167. (if ancestor-rev
  168. (ediff-merge-buffers-with-ancestor
  169. buf1 buf2 ancestor-buf
  170. startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
  171. (ediff-merge-buffers
  172. buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))
  173. ))
  174. (defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
  175. &optional
  176. startup-hooks merge-buffer-file)
  177. ;; If ANCESTOR-REV non-nil, merge with ancestor
  178. (let (buf1 buf2 ancestor-buf)
  179. (save-window-excursion
  180. (setq buf1 (rcs-ediff-view-revision rev1)
  181. buf2 (if (string= rev2 "")
  182. (current-buffer)
  183. (rcs-ediff-view-revision rev2))
  184. ancestor-buf (if ancestor-rev
  185. (if (string= ancestor-rev "")
  186. (current-buffer)
  187. (rcs-ediff-view-revision ancestor-rev)))))
  188. ;; rcs.el doesn't create temp version files, so we don't have to delete
  189. ;; anything in startup hooks to ediff-buffers
  190. (if ancestor-rev
  191. (ediff-merge-buffers-with-ancestor
  192. buf1 buf2 ancestor-buf
  193. startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
  194. (ediff-merge-buffers
  195. buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))))
  196. ;; delete version file on exit unless ediff-keep-tmp-versions is true
  197. (defun ediff-delete-version-file (file)
  198. (or ediff-keep-tmp-versions (delete-file file)))
  199. (provide 'ediff-vers)
  200. ;; Local Variables:
  201. ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
  202. ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
  203. ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
  204. ;; End:
  205. ;;; ediff-vers.el ends here