Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

511 line
20 KiB

5 年之前
  1. ;;; magit-ediff.el --- Ediff extension for Magit -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2010-2019 The Magit Project Contributors
  3. ;;
  4. ;; You should have received a copy of the AUTHORS.md file which
  5. ;; lists all contributors. If not, see http://magit.vc/authors.
  6. ;; Author: Jonas Bernoulli <jonas@bernoul.li>
  7. ;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
  8. ;; Magit is free software; you can redistribute it and/or modify it
  9. ;; under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; Magit is distributed in the hope that it will be useful, but WITHOUT
  14. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. ;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  16. ;; License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with Magit. If not, see http://www.gnu.org/licenses.
  20. ;;; Commentary:
  21. ;; This library provides basic support for Ediff.
  22. ;;; Code:
  23. (require 'magit)
  24. (require 'ediff)
  25. (require 'smerge-mode)
  26. (defvar smerge-ediff-buf)
  27. (defvar smerge-ediff-windows)
  28. ;;; Options
  29. (defgroup magit-ediff nil
  30. "Ediff support for Magit."
  31. :link '(info-link "(magit)Ediffing")
  32. :group 'magit-extensions)
  33. (defcustom magit-ediff-quit-hook
  34. '(magit-ediff-cleanup-auxiliary-buffers
  35. magit-ediff-restore-previous-winconf)
  36. "Hooks to run after finishing Ediff, when that was invoked using Magit.
  37. The hooks are run in the Ediff control buffer. This is similar
  38. to `ediff-quit-hook' but takes the needs of Magit into account.
  39. The `ediff-quit-hook' is ignored by Ediff sessions which were
  40. invoked using Magit."
  41. :package-version '(magit . "2.2.0")
  42. :group 'magit-ediff
  43. :type 'hook
  44. :get 'magit-hook-custom-get
  45. :options '(magit-ediff-cleanup-auxiliary-buffers
  46. magit-ediff-restore-previous-winconf))
  47. (defcustom magit-ediff-dwim-show-on-hunks nil
  48. "Whether `magit-ediff-dwim' runs show variants on hunks.
  49. If non-nil, `magit-ediff-show-staged' or
  50. `magit-ediff-show-unstaged' are called based on what section the
  51. hunk is in. Otherwise, `magit-ediff-dwim' runs
  52. `magit-ediff-stage' when point is on an uncommitted hunk."
  53. :package-version '(magit . "2.2.0")
  54. :group 'magit-ediff
  55. :type 'boolean)
  56. (defcustom magit-ediff-show-stash-with-index t
  57. "Whether `magit-ediff-show-stash' shows the state of the index.
  58. If non-nil, use a third Ediff buffer to distinguish which changes
  59. in the stash were staged. In cases where the stash contains no
  60. staged changes, fall back to a two-buffer Ediff.
  61. More specifically, a stash is a merge commit, stash@{N}, with
  62. potentially three parents.
  63. * stash@{N}^1 represents the `HEAD' commit at the time the stash
  64. was created.
  65. * stash@{N}^2 records any changes that were staged when the stash
  66. was made.
  67. * stash@{N}^3, if it exists, contains files that were untracked
  68. when stashing.
  69. If this option is non-nil, `magit-ediff-show-stash' will run
  70. Ediff on a file using three buffers: one for stash@{N}, another
  71. for stash@{N}^1, and a third for stash@{N}^2.
  72. Otherwise, Ediff uses two buffers, comparing
  73. stash@{N}^1..stash@{N}. Along with any unstaged changes, changes
  74. in the index commit, stash@{N}^2, will be shown in this
  75. comparison unless they conflicted with changes in the working
  76. tree at the time of stashing."
  77. :package-version '(magit . "2.6.0")
  78. :group 'magit-ediff
  79. :type 'boolean)
  80. ;;; Commands
  81. (defvar magit-ediff-previous-winconf nil)
  82. ;;;###autoload (autoload 'magit-ediff "magit-ediff" nil)
  83. (define-transient-command magit-ediff ()
  84. "Show differences using the Ediff package."
  85. :info-manual "(ediff)"
  86. ["Ediff"
  87. [("E" "Dwim" magit-ediff-dwim)
  88. ("s" "Stage" magit-ediff-stage)
  89. ("m" "Resolve" magit-ediff-resolve)]
  90. [("u" "Show unstaged" magit-ediff-show-unstaged)
  91. ("i" "Show staged" magit-ediff-show-staged)
  92. ("w" "Show worktree" magit-ediff-show-working-tree)]
  93. [("c" "Show commit" magit-ediff-show-commit)
  94. ("r" "Show range" magit-ediff-compare)
  95. ("z" "Show stash" magit-ediff-show-stash)]])
  96. ;;;###autoload
  97. (defun magit-ediff-resolve (file)
  98. "Resolve outstanding conflicts in FILE using Ediff.
  99. FILE has to be relative to the top directory of the repository.
  100. In the rare event that you want to manually resolve all
  101. conflicts, including those already resolved by Git, use
  102. `ediff-merge-revisions-with-ancestor'."
  103. (interactive
  104. (let ((current (magit-current-file))
  105. (unmerged (magit-unmerged-files)))
  106. (unless unmerged
  107. (user-error "There are no unresolved conflicts"))
  108. (list (magit-completing-read "Resolve file" unmerged nil t nil nil
  109. (car (member current unmerged))))))
  110. (magit-with-toplevel
  111. (with-current-buffer (find-file-noselect file)
  112. (smerge-ediff)
  113. (setq-local
  114. ediff-quit-hook
  115. (lambda ()
  116. (let ((bufC ediff-buffer-C)
  117. (bufS smerge-ediff-buf))
  118. (with-current-buffer bufS
  119. (when (yes-or-no-p (format "Conflict resolution finished; save %s? "
  120. buffer-file-name))
  121. (erase-buffer)
  122. (insert-buffer-substring bufC)
  123. (save-buffer))))
  124. (when (buffer-live-p ediff-buffer-A) (kill-buffer ediff-buffer-A))
  125. (when (buffer-live-p ediff-buffer-B) (kill-buffer ediff-buffer-B))
  126. (when (buffer-live-p ediff-buffer-C) (kill-buffer ediff-buffer-C))
  127. (when (buffer-live-p ediff-ancestor-buffer)
  128. (kill-buffer ediff-ancestor-buffer))
  129. (let ((magit-ediff-previous-winconf smerge-ediff-windows))
  130. (run-hooks 'magit-ediff-quit-hook)))))))
  131. ;;;###autoload
  132. (defun magit-ediff-stage (file)
  133. "Stage and unstage changes to FILE using Ediff.
  134. FILE has to be relative to the top directory of the repository."
  135. (interactive
  136. (let ((files (magit-tracked-files)))
  137. (list (magit-completing-read "Selectively stage file" files nil t nil nil
  138. (car (member (magit-current-file) files))))))
  139. (magit-with-toplevel
  140. (let* ((conf (current-window-configuration))
  141. (bufA (magit-get-revision-buffer "HEAD" file))
  142. (bufB (magit-get-revision-buffer "{index}" file))
  143. (bufBrw (and bufB (with-current-buffer bufB (not buffer-read-only))))
  144. (bufC (get-file-buffer file))
  145. (fileBufC (or bufC (find-file-noselect file)))
  146. (coding-system-for-read
  147. (with-current-buffer fileBufC buffer-file-coding-system)))
  148. (ediff-buffers3
  149. (or bufA (magit-find-file-noselect "HEAD" file))
  150. (with-current-buffer (magit-find-file-index-noselect file t)
  151. (setq buffer-read-only nil)
  152. (current-buffer))
  153. fileBufC
  154. `((lambda ()
  155. (setq-local
  156. ediff-quit-hook
  157. (lambda ()
  158. (and (buffer-live-p ediff-buffer-B)
  159. (buffer-modified-p ediff-buffer-B)
  160. (with-current-buffer ediff-buffer-B
  161. (magit-update-index)))
  162. (and (buffer-live-p ediff-buffer-C)
  163. (buffer-modified-p ediff-buffer-C)
  164. (with-current-buffer ediff-buffer-C
  165. (when (y-or-n-p
  166. (format "Save file %s? " buffer-file-name))
  167. (save-buffer))))
  168. ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
  169. ,@(if bufB
  170. (unless bufBrw '((with-current-buffer ediff-buffer-B
  171. (setq buffer-read-only t))))
  172. '((ediff-kill-buffer-carefully ediff-buffer-B)))
  173. ,@(unless bufC '((ediff-kill-buffer-carefully ediff-buffer-C)))
  174. (let ((magit-ediff-previous-winconf ,conf))
  175. (run-hooks 'magit-ediff-quit-hook))))))
  176. 'ediff-buffers3))))
  177. ;;;###autoload
  178. (defun magit-ediff-compare (revA revB fileA fileB)
  179. "Compare REVA:FILEA with REVB:FILEB using Ediff.
  180. FILEA and FILEB have to be relative to the top directory of the
  181. repository. If REVA or REVB is nil, then this stands for the
  182. working tree state.
  183. If the region is active, use the revisions on the first and last
  184. line of the region. With a prefix argument, instead of diffing
  185. the revisions, choose a revision to view changes along, starting
  186. at the common ancestor of both revisions (i.e., use a \"...\"
  187. range)."
  188. (interactive
  189. (pcase-let ((`(,revA ,revB) (magit-ediff-compare--read-revisions
  190. nil current-prefix-arg)))
  191. (nconc (list revA revB)
  192. (magit-ediff-read-files revA revB))))
  193. (magit-with-toplevel
  194. (let ((conf (current-window-configuration))
  195. (bufA (if revA
  196. (magit-get-revision-buffer revA fileA)
  197. (get-file-buffer fileA)))
  198. (bufB (if revB
  199. (magit-get-revision-buffer revB fileB)
  200. (get-file-buffer fileB))))
  201. (ediff-buffers
  202. (or bufA (if revA
  203. (magit-find-file-noselect revA fileA)
  204. (find-file-noselect fileA)))
  205. (or bufB (if revB
  206. (magit-find-file-noselect revB fileB)
  207. (find-file-noselect fileB)))
  208. `((lambda ()
  209. (setq-local
  210. ediff-quit-hook
  211. (lambda ()
  212. ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
  213. ,@(unless bufB '((ediff-kill-buffer-carefully ediff-buffer-B)))
  214. (let ((magit-ediff-previous-winconf ,conf))
  215. (run-hooks 'magit-ediff-quit-hook))))))
  216. 'ediff-revision))))
  217. (defun magit-ediff-compare--read-revisions (&optional arg mbase)
  218. (let ((input (or arg (magit-diff-read-range-or-commit
  219. "Compare range or commit"
  220. nil mbase))))
  221. (--if-let (magit-split-range input)
  222. (-cons-to-list it)
  223. (list input nil))))
  224. (defun magit-ediff-read-files (revA revB &optional fileB)
  225. "Read file in REVB, return it and the corresponding file in REVA.
  226. When FILEB is non-nil, use this as REVB's file instead of
  227. prompting for it."
  228. (unless fileB
  229. (setq fileB (magit-read-file-choice
  230. (format "File to compare between %s and %s"
  231. revA (or revB "the working tree"))
  232. (magit-changed-files revA revB)
  233. (format "No changed files between %s and %s"
  234. revA (or revB "the working tree")))))
  235. (list (or (car (member fileB (magit-revision-files revA)))
  236. (cdr (assoc fileB (magit-renamed-files revB revA)))
  237. (magit-read-file-choice
  238. (format "File in %s to compare with %s in %s"
  239. revA fileB (or revB "the working tree"))
  240. (magit-changed-files revB revA)
  241. (format "No files have changed between %s and %s"
  242. revA revB)))
  243. fileB))
  244. ;;;###autoload
  245. (defun magit-ediff-dwim ()
  246. "Compare, stage, or resolve using Ediff.
  247. This command tries to guess what file, and what commit or range
  248. the user wants to compare, stage, or resolve using Ediff. It
  249. might only be able to guess either the file, or range or commit,
  250. in which case the user is asked about the other. It might not
  251. always guess right, in which case the appropriate `magit-ediff-*'
  252. command has to be used explicitly. If it cannot read the user's
  253. mind at all, then it asks the user for a command to run."
  254. (interactive)
  255. (magit-section-case
  256. (hunk (save-excursion
  257. (goto-char (oref (oref it parent) start))
  258. (magit-ediff-dwim)))
  259. (t
  260. (let ((range (magit-diff--dwim))
  261. (file (magit-current-file))
  262. command revA revB)
  263. (pcase range
  264. ((and (guard (not magit-ediff-dwim-show-on-hunks))
  265. (or `unstaged `staged))
  266. (setq command (if (magit-anything-unmerged-p)
  267. #'magit-ediff-resolve
  268. #'magit-ediff-stage)))
  269. (`unstaged (setq command #'magit-ediff-show-unstaged))
  270. (`staged (setq command #'magit-ediff-show-staged))
  271. (`(commit . ,value)
  272. (setq command #'magit-ediff-show-commit)
  273. (setq revB value))
  274. (`(stash . ,value)
  275. (setq command #'magit-ediff-show-stash)
  276. (setq revB value))
  277. ((pred stringp)
  278. (pcase-let ((`(,a ,b) (magit-ediff-compare--read-revisions range)))
  279. (setq command #'magit-ediff-compare)
  280. (setq revA a)
  281. (setq revB b)))
  282. (_
  283. (when (derived-mode-p 'magit-diff-mode)
  284. (pcase (magit-diff-type)
  285. (`committed (pcase-let ((`(,a ,b)
  286. (magit-ediff-compare--read-revisions
  287. magit-buffer-range)))
  288. (setq revA a)
  289. (setq revB b)))
  290. ((guard (not magit-ediff-dwim-show-on-hunks))
  291. (setq command #'magit-ediff-stage))
  292. (`unstaged (setq command #'magit-ediff-show-unstaged))
  293. (`staged (setq command #'magit-ediff-show-staged))
  294. (`undefined (setq command nil))
  295. (_ (setq command nil))))))
  296. (cond ((not command)
  297. (call-interactively
  298. (magit-read-char-case
  299. "Failed to read your mind; do you want to " t
  300. (?c "[c]ommit" 'magit-ediff-show-commit)
  301. (?r "[r]ange" 'magit-ediff-compare)
  302. (?s "[s]tage" 'magit-ediff-stage)
  303. (?v "resol[v]e" 'magit-ediff-resolve))))
  304. ((eq command 'magit-ediff-compare)
  305. (apply 'magit-ediff-compare revA revB
  306. (magit-ediff-read-files revA revB file)))
  307. ((eq command 'magit-ediff-show-commit)
  308. (magit-ediff-show-commit revB))
  309. ((eq command 'magit-ediff-show-stash)
  310. (magit-ediff-show-stash revB))
  311. (file
  312. (funcall command file))
  313. (t
  314. (call-interactively command)))))))
  315. ;;;###autoload
  316. (defun magit-ediff-show-staged (file)
  317. "Show staged changes using Ediff.
  318. This only allows looking at the changes; to stage, unstage,
  319. and discard changes using Ediff, use `magit-ediff-stage'.
  320. FILE must be relative to the top directory of the repository."
  321. (interactive
  322. (list (magit-read-file-choice "Show staged changes for file"
  323. (magit-staged-files)
  324. "No staged files")))
  325. (let ((conf (current-window-configuration))
  326. (bufA (magit-get-revision-buffer "HEAD" file))
  327. (bufB (get-buffer (concat file ".~{index}~"))))
  328. (ediff-buffers
  329. (or bufA (magit-find-file-noselect "HEAD" file))
  330. (or bufB (magit-find-file-index-noselect file t))
  331. `((lambda ()
  332. (setq-local
  333. ediff-quit-hook
  334. (lambda ()
  335. ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
  336. ,@(unless bufB '((ediff-kill-buffer-carefully ediff-buffer-B)))
  337. (let ((magit-ediff-previous-winconf ,conf))
  338. (run-hooks 'magit-ediff-quit-hook))))))
  339. 'ediff-buffers)))
  340. ;;;###autoload
  341. (defun magit-ediff-show-unstaged (file)
  342. "Show unstaged changes using Ediff.
  343. This only allows looking at the changes; to stage, unstage,
  344. and discard changes using Ediff, use `magit-ediff-stage'.
  345. FILE must be relative to the top directory of the repository."
  346. (interactive
  347. (list (magit-read-file-choice "Show unstaged changes for file"
  348. (magit-unstaged-files)
  349. "No unstaged files")))
  350. (magit-with-toplevel
  351. (let ((conf (current-window-configuration))
  352. (bufA (get-buffer (concat file ".~{index}~")))
  353. (bufB (get-file-buffer file)))
  354. (ediff-buffers
  355. (or bufA (magit-find-file-index-noselect file t))
  356. (or bufB (find-file-noselect file))
  357. `((lambda ()
  358. (setq-local
  359. ediff-quit-hook
  360. (lambda ()
  361. ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
  362. ,@(unless bufB '((ediff-kill-buffer-carefully ediff-buffer-B)))
  363. (let ((magit-ediff-previous-winconf ,conf))
  364. (run-hooks 'magit-ediff-quit-hook))))))
  365. 'ediff-buffers))))
  366. ;;;###autoload
  367. (defun magit-ediff-show-working-tree (file)
  368. "Show changes between `HEAD' and working tree using Ediff.
  369. FILE must be relative to the top directory of the repository."
  370. (interactive
  371. (list (magit-read-file-choice "Show changes in file"
  372. (magit-changed-files "HEAD")
  373. "No changed files")))
  374. (magit-with-toplevel
  375. (let ((conf (current-window-configuration))
  376. (bufA (magit-get-revision-buffer "HEAD" file))
  377. (bufB (get-file-buffer file)))
  378. (ediff-buffers
  379. (or bufA (magit-find-file-noselect "HEAD" file))
  380. (or bufB (find-file-noselect file))
  381. `((lambda ()
  382. (setq-local
  383. ediff-quit-hook
  384. (lambda ()
  385. ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
  386. ,@(unless bufB '((ediff-kill-buffer-carefully ediff-buffer-B)))
  387. (let ((magit-ediff-previous-winconf ,conf))
  388. (run-hooks 'magit-ediff-quit-hook))))))
  389. 'ediff-buffers))))
  390. ;;;###autoload
  391. (defun magit-ediff-show-commit (commit)
  392. "Show changes introduced by COMMIT using Ediff."
  393. (interactive (list (magit-read-branch-or-commit "Revision")))
  394. (let ((revA (concat commit "^"))
  395. (revB commit))
  396. (apply #'magit-ediff-compare
  397. revA revB
  398. (magit-ediff-read-files revA revB (magit-current-file)))))
  399. ;;;###autoload
  400. (defun magit-ediff-show-stash (stash)
  401. "Show changes introduced by STASH using Ediff.
  402. `magit-ediff-show-stash-with-index' controls whether a
  403. three-buffer Ediff is used in order to distinguish changes in the
  404. stash that were staged."
  405. (interactive (list (magit-read-stash "Stash")))
  406. (pcase-let* ((revA (concat stash "^1"))
  407. (revB (concat stash "^2"))
  408. (revC stash)
  409. (`(,fileA ,fileC) (magit-ediff-read-files revA revC))
  410. (fileB fileC))
  411. (if (and magit-ediff-show-stash-with-index
  412. (member fileA (magit-changed-files revB revA)))
  413. (let ((conf (current-window-configuration))
  414. (bufA (magit-get-revision-buffer revA fileA))
  415. (bufB (magit-get-revision-buffer revB fileB))
  416. (bufC (magit-get-revision-buffer revC fileC)))
  417. (ediff-buffers3
  418. (or bufA (magit-find-file-noselect revA fileA))
  419. (or bufB (magit-find-file-noselect revB fileB))
  420. (or bufC (magit-find-file-noselect revC fileC))
  421. `((lambda ()
  422. (setq-local
  423. ediff-quit-hook
  424. (lambda ()
  425. ,@(unless bufA
  426. '((ediff-kill-buffer-carefully ediff-buffer-A)))
  427. ,@(unless bufB
  428. '((ediff-kill-buffer-carefully ediff-buffer-B)))
  429. ,@(unless bufC
  430. '((ediff-kill-buffer-carefully ediff-buffer-C)))
  431. (let ((magit-ediff-previous-winconf ,conf))
  432. (run-hooks 'magit-ediff-quit-hook))))))
  433. 'ediff-buffers3))
  434. (magit-ediff-compare revA revC fileA fileC))))
  435. (defun magit-ediff-cleanup-auxiliary-buffers ()
  436. (let* ((ctl-buf ediff-control-buffer)
  437. (ctl-win (ediff-get-visible-buffer-window ctl-buf))
  438. (ctl-frm ediff-control-frame)
  439. (main-frame (cond ((window-live-p ediff-window-A)
  440. (window-frame ediff-window-A))
  441. ((window-live-p ediff-window-B)
  442. (window-frame ediff-window-B)))))
  443. (ediff-kill-buffer-carefully ediff-diff-buffer)
  444. (ediff-kill-buffer-carefully ediff-custom-diff-buffer)
  445. (ediff-kill-buffer-carefully ediff-fine-diff-buffer)
  446. (ediff-kill-buffer-carefully ediff-tmp-buffer)
  447. (ediff-kill-buffer-carefully ediff-error-buffer)
  448. (ediff-kill-buffer-carefully ediff-msg-buffer)
  449. (ediff-kill-buffer-carefully ediff-debug-buffer)
  450. (when (boundp 'ediff-patch-diagnostics)
  451. (ediff-kill-buffer-carefully ediff-patch-diagnostics))
  452. (cond ((and (ediff-window-display-p)
  453. (frame-live-p ctl-frm))
  454. (delete-frame ctl-frm))
  455. ((window-live-p ctl-win)
  456. (delete-window ctl-win)))
  457. (unless (ediff-multiframe-setup-p)
  458. (ediff-kill-bottom-toolbar))
  459. (ediff-kill-buffer-carefully ctl-buf)
  460. (when (frame-live-p main-frame)
  461. (select-frame main-frame))))
  462. (defun magit-ediff-restore-previous-winconf ()
  463. (set-window-configuration magit-ediff-previous-winconf))
  464. ;;; _
  465. (provide 'magit-ediff)
  466. ;;; magit-ediff.el ends here