magit-ediff.el 20.6 KB
Newer Older
1
;;; magit-ediff.el --- Ediff extension for Magit  -*- lexical-binding: t -*-
2

Jonas Bernoulli's avatar
Jonas Bernoulli committed
3
;; Copyright (C) 2010-2018  The Magit Project Contributors
4
;;
5 6
;; You should have received a copy of the AUTHORS.md file which
;; lists all contributors.  If not, see http://magit.vc/authors.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>

;; Magit is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;;
;; Magit is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
;; License for more details.
;;
;; You should have received a copy of the GNU General Public License
22
;; along with Magit.  If not, see http://www.gnu.org/licenses.
23 24 25 26 27 28 29 30

;;; Commentary:

;; This library provides basic support for Ediff.

;;; Code:

(require 'magit)
31 32

(require 'ediff)
33 34
(require 'smerge-mode)

35 36 37
(defvar smerge-ediff-buf)
(defvar smerge-ediff-windows)

38 39
;;; Options

40 41
(defgroup magit-ediff nil
  "Ediff support for Magit."
42
  :link '(info-link "(magit)Ediffing")
43 44 45 46 47 48 49 50 51 52
  :group 'magit-extensions)

(defcustom magit-ediff-quit-hook
  '(magit-ediff-cleanup-auxiliary-buffers
    magit-ediff-restore-previous-winconf)
  "Hooks to run after finishing Ediff, when that was invoked using Magit.
The hooks are run in the Ediff control buffer.  This is similar
to `ediff-quit-hook' but takes the needs of Magit into account.
The `ediff-quit-hook' is ignored by Ediff sessions which were
invoked using Magit."
53
  :package-version '(magit . "2.2.0")
54 55
  :group 'magit-ediff
  :type 'hook
56
  :get 'magit-hook-custom-get
57 58 59
  :options '(magit-ediff-cleanup-auxiliary-buffers
             magit-ediff-restore-previous-winconf))

60 61 62 63 64
(defcustom magit-ediff-dwim-show-on-hunks nil
  "Whether `magit-ediff-dwim' runs show variants on hunks.
If non-nil, `magit-ediff-show-staged' or
`magit-ediff-show-unstaged' are called based on what section the
hunk is in.  Otherwise, `magit-ediff-dwim' runs
65
`magit-ediff-stage' when point is on an uncommitted hunk."
66 67 68 69
  :package-version '(magit . "2.2.0")
  :group 'magit-ediff
  :type 'boolean)

70 71 72 73 74 75 76
(defcustom magit-ediff-show-stash-with-index t
  "Whether `magit-ediff-show-stash' shows the state of the index.

If non-nil, use a third Ediff buffer to distinguish which changes
in the stash were staged.  In cases where the stash contains no
staged changes, fall back to a two-buffer Ediff.

77
More specifically, a stash is a merge commit, stash@{N}, with
78 79
potentially three parents.

80
* stash@{N}^1 represents the `HEAD' commit at the time the stash
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  was created.

* stash@{N}^2 records any changes that were staged when the stash
  was made.

* stash@{N}^3, if it exists, contains files that were untracked
  when stashing.

If this option is non-nil, `magit-ediff-show-stash' will run
Ediff on a file using three buffers: one for stash@{N}, another
for stash@{N}^1, and a third for stash@{N}^2.

Otherwise, Ediff uses two buffers, comparing
stash@{N}^1..stash@{N}.  Along with any unstaged changes, changes
in the index commit, stash@{N}^2, will be shown in this
comparison unless they conflicted with changes in the working
tree at the time of stashing."
  :package-version '(magit . "2.6.0")
  :group 'magit-ediff
  :type 'boolean)

102 103
;;; Commands

104 105
(defvar magit-ediff-previous-winconf nil)

Jonas Bernoulli's avatar
Jonas Bernoulli committed
106
;;;###autoload (autoload 'magit-ediff-popup "magit-ediff" nil t)
107 108
(magit-define-popup magit-ediff-popup
  "Popup console for ediff commands."
109 110 111 112 113 114 115
  :actions '((?E "Dwim"          magit-ediff-dwim)
             (?u "Show unstaged" magit-ediff-show-unstaged)
             (?s "Stage"         magit-ediff-stage)
             (?i "Show staged"   magit-ediff-show-staged)
             (?m "Resolve"       magit-ediff-resolve)
             (?w "Show worktree" magit-ediff-show-working-tree)
             (?r "Diff range"    magit-ediff-compare)
116 117
             (?c "Show commit"   magit-ediff-show-commit) nil
             (?z "Show stash"    magit-ediff-show-stash))
118
  :max-action-columns 2)
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
;;;###autoload
(defun magit-ediff-resolve (file)
  "Resolve outstanding conflicts in FILE using Ediff.
FILE has to be relative to the top directory of the repository.

In the rare event that you want to manually resolve all
conflicts, including those already resolved by Git, use
`ediff-merge-revisions-with-ancestor'."
  (interactive
   (let ((current  (magit-current-file))
         (unmerged (magit-unmerged-files)))
     (unless unmerged
       (user-error "There are no unresolved conflicts"))
     (list (magit-completing-read "Resolve file" unmerged nil t nil nil
                                  (car (member current unmerged))))))
135 136 137 138 139 140 141 142 143
  (magit-with-toplevel
    (with-current-buffer (find-file-noselect file)
      (smerge-ediff)
      (setq-local
       ediff-quit-hook
       (lambda ()
         (let ((bufC ediff-buffer-C)
               (bufS smerge-ediff-buf))
           (with-current-buffer bufS
144
             (when (yes-or-no-p (format "Conflict resolution finished; save %s? "
145
                                        buffer-file-name))
146 147
               (erase-buffer)
               (insert-buffer-substring bufC)
148 149 150 151 152 153 154 155
               (save-buffer))))
         (when (buffer-live-p ediff-buffer-A) (kill-buffer ediff-buffer-A))
         (when (buffer-live-p ediff-buffer-B) (kill-buffer ediff-buffer-B))
         (when (buffer-live-p ediff-buffer-C) (kill-buffer ediff-buffer-C))
         (when (buffer-live-p ediff-ancestor-buffer)
           (kill-buffer ediff-ancestor-buffer))
         (let ((magit-ediff-previous-winconf smerge-ediff-windows))
           (run-hooks 'magit-ediff-quit-hook)))))))
156 157 158 159 160 161

;;;###autoload
(defun magit-ediff-stage (file)
  "Stage and unstage changes to FILE using Ediff.
FILE has to be relative to the top directory of the repository."
  (interactive
162 163
   (list (magit-completing-read "Selectively stage file"
                                (magit-tracked-files) nil nil nil nil
164
                                (magit-current-file))))
165 166 167 168 169
  (magit-with-toplevel
    (let* ((conf (current-window-configuration))
           (bufA (magit-get-revision-buffer "HEAD" file))
           (bufB (get-buffer (concat file ".~{index}~")))
           (bufBrw (and bufB (with-current-buffer bufB (not buffer-read-only))))
170 171 172 173
           (bufC (get-file-buffer file))
           (fileBufC (or bufC (find-file-noselect file)))
           (coding-system-for-read
            (with-current-buffer fileBufC buffer-file-coding-system)))
174 175 176 177 178
      (ediff-buffers3
       (or bufA (magit-find-file-noselect "HEAD" file))
       (with-current-buffer (magit-find-file-index-noselect file t)
         (setq buffer-read-only nil)
         (current-buffer))
179
       fileBufC
180 181 182 183 184 185 186 187 188 189 190 191
       `((lambda ()
           (setq-local
            ediff-quit-hook
            (lambda ()
              (and (buffer-live-p ediff-buffer-B)
                   (buffer-modified-p ediff-buffer-B)
                   (with-current-buffer ediff-buffer-B
                     (magit-update-index)))
              (and (buffer-live-p ediff-buffer-C)
                   (buffer-modified-p ediff-buffer-C)
                   (with-current-buffer ediff-buffer-C
                     (when (y-or-n-p
192
                            (format "Save file %s? " buffer-file-name))
193 194 195 196 197 198 199 200 201 202
                       (save-buffer))))
              ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
              ,@(if bufB
                    (unless bufBrw '((with-current-buffer ediff-buffer-B
                                       (setq buffer-read-only t))))
                  '((ediff-kill-buffer-carefully ediff-buffer-B)))
              ,@(unless bufC '((ediff-kill-buffer-carefully ediff-buffer-C)))
              (let ((magit-ediff-previous-winconf ,conf))
                (run-hooks 'magit-ediff-quit-hook))))))
       'ediff-buffers3))))
203 204 205 206

;;;###autoload
(defun magit-ediff-compare (revA revB fileA fileB)
  "Compare REVA:FILEA with REVB:FILEB using Ediff.
207

208
FILEA and FILEB have to be relative to the top directory of the
209
repository.  If REVA or REVB is nil, then this stands for the
210 211 212 213 214 215 216
working tree state.

If the region is active, use the revisions on the first and last
line of the region.  With a prefix argument, instead of diffing
the revisions, choose a revision to view changes along, starting
at the common ancestor of both revisions (i.e., use a \"...\"
range)."
217 218 219 220 221
  (interactive
   (pcase-let ((`(,revA ,revB) (magit-ediff-compare--read-revisions
                                nil current-prefix-arg)))
     (nconc (list revA revB)
            (magit-ediff-read-files revA revB))))
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  (magit-with-toplevel
    (let ((conf (current-window-configuration))
          (bufA (if revA
                    (magit-get-revision-buffer revA fileA)
                  (get-file-buffer fileA)))
          (bufB (if revB
                    (magit-get-revision-buffer revB fileB)
                  (get-file-buffer fileB))))
      (ediff-buffers
       (or bufA (if revA
                    (magit-find-file-noselect revA fileA)
                  (find-file-noselect fileA)))
       (or bufB (if revB
                    (magit-find-file-noselect revB fileB)
                  (find-file-noselect fileB)))
       `((lambda ()
           (setq-local
            ediff-quit-hook
            (lambda ()
              ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
              ,@(unless bufB '((ediff-kill-buffer-carefully ediff-buffer-B)))
              (let ((magit-ediff-previous-winconf ,conf))
                (run-hooks 'magit-ediff-quit-hook))))))
       'ediff-revision))))
246

247
(defun magit-ediff-compare--read-revisions (&optional arg mbase)
248 249 250 251 252 253
  (let ((input (or arg (magit-diff-read-range-or-commit
                        "Compare range or commit"
                        nil mbase))))
    (--if-let (magit-split-range input)
        (-cons-to-list it)
      (list input nil))))
254

255 256 257 258
(defun magit-ediff-read-files (revA revB &optional fileB)
  "Read file in REVB, return it and the corresponding file in REVA.
When FILEB is non-nil, use this as REVB's file instead of
prompting for it."
259
  (unless fileB
260 261 262 263 264 265
    (setq fileB (magit-read-file-choice
                 (format "File to compare between %s and %s"
                         revA (or revB "the working tree"))
                 (magit-changed-files revA revB)
                 (format "No changed files between %s and %s"
                         revA (or revB "the working tree")))))
266
  (list (or (car (member fileB (magit-revision-files revA)))
267 268 269 270 271
            (cdr (assoc fileB (magit-renamed-files revB revA)))
            (magit-read-file-choice
             (format "File in %s to compare with %s in %s"
                     revA fileB (or revB "the working tree"))
             (magit-changed-files revB revA)
272 273
             (format "No files have changed between %s and %s"
                     revA revB)))
274 275 276 277 278 279 280 281 282 283
        fileB))

;;;###autoload
(defun magit-ediff-dwim ()
  "Compare, stage, or resolve using Ediff.
This command tries to guess what file, and what commit or range
the user wants to compare, stage, or resolve using Ediff.  It
might only be able to guess either the file, or range or commit,
in which case the user is asked about the other.  It might not
always guess right, in which case the appropriate `magit-ediff-*'
284
command has to be used explicitly.  If it cannot read the user's
285 286 287 288
mind at all, then it asks the user for a command to run."
  (interactive)
  (magit-section-case
    (hunk (save-excursion
289
            (goto-char (oref (oref it parent) start))
290 291
            (magit-ediff-dwim)))
    (t
292
     (let ((range (magit-diff--dwim))
293
           (file (magit-current-file))
294 295
           command revA revB)
       (pcase range
296
         ((and (guard (not magit-ediff-dwim-show-on-hunks))
297
               (or `unstaged `staged))
298 299 300
          (setq command (if (magit-anything-unmerged-p)
                            #'magit-ediff-resolve
                          #'magit-ediff-stage)))
301 302
         (`unstaged (setq command #'magit-ediff-show-unstaged))
         (`staged (setq command #'magit-ediff-show-staged))
303
         (`(commit . ,value)
304 305
          (setq command #'magit-ediff-show-commit)
          (setq revB value))
306
         (`(stash . ,value)
307 308
          (setq command #'magit-ediff-show-stash)
          (setq revB value))
309
         ((pred stringp)
310
          (pcase-let ((`(,a ,b) (magit-ediff-compare--read-revisions range)))
311 312 313
            (setq command #'magit-ediff-compare)
            (setq revA a)
            (setq revB b)))
314 315 316
         (_
          (when (derived-mode-p 'magit-diff-mode)
            (pcase (magit-diff-type)
317 318 319
              (`committed (pcase-let ((`(,a ,b)
                                       (magit-ediff-compare--read-revisions
                                        (car magit-refresh-args))))
320 321
                            (setq revA a)
                            (setq revB b)))
322 323
              ((guard (not magit-ediff-dwim-show-on-hunks))
               (setq command #'magit-ediff-stage))
324 325 326
              (`unstaged  (setq command #'magit-ediff-show-unstaged))
              (`staged    (setq command #'magit-ediff-show-staged))
              (`undefined (setq command nil))
327
              (_          (setq command nil))))))
328 329 330 331
       (cond ((not command)
              (call-interactively
               (magit-read-char-case
                   "Failed to read your mind; do you want to " t
332 333 334 335
                 (?c "[c]ommit"  'magit-ediff-show-commit)
                 (?r "[r]ange"   'magit-ediff-compare)
                 (?s "[s]tage"   'magit-ediff-stage)
                 (?v "resol[v]e" 'magit-ediff-resolve))))
336 337
             ((eq command 'magit-ediff-compare)
              (apply 'magit-ediff-compare revA revB
338
                     (magit-ediff-read-files revA revB file)))
339 340
             ((eq command 'magit-ediff-show-commit)
              (magit-ediff-show-commit revB))
341 342
             ((eq command 'magit-ediff-show-stash)
              (magit-ediff-show-stash revB))
343 344 345 346 347
             (file
              (funcall command file))
             (t
              (call-interactively command)))))))

348 349 350
;;;###autoload
(defun magit-ediff-show-staged (file)
  "Show staged changes using Ediff.
351

352
This only allows looking at the changes; to stage, unstage,
353 354 355
and discard changes using Ediff, use `magit-ediff-stage'.

FILE must be relative to the top directory of the repository."
356
  (interactive
357 358 359
   (list (magit-read-file-choice "Show staged changes for file"
                                 (magit-staged-files)
                                 "No staged files")))
360 361 362 363 364 365 366
  (let ((conf (current-window-configuration))
        (bufA (magit-get-revision-buffer "HEAD" file))
        (bufB (get-buffer (concat file ".~{index}~"))))
    (ediff-buffers
     (or bufA (magit-find-file-noselect "HEAD" file))
     (or bufB (magit-find-file-index-noselect file t))
     `((lambda ()
367 368
         (setq-local
          ediff-quit-hook
369 370 371
          (lambda ()
            ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
            ,@(unless bufB '((ediff-kill-buffer-carefully ediff-buffer-B)))
372 373
            (let ((magit-ediff-previous-winconf ,conf))
              (run-hooks 'magit-ediff-quit-hook))))))
374 375 376 377 378
     'ediff-buffers)))

;;;###autoload
(defun magit-ediff-show-unstaged (file)
  "Show unstaged changes using Ediff.
379

380
This only allows looking at the changes; to stage, unstage,
381 382 383
and discard changes using Ediff, use `magit-ediff-stage'.

FILE must be relative to the top directory of the repository."
384
  (interactive
385
   (list (magit-read-file-choice "Show unstaged changes for file"
386
                                 (magit-unstaged-files)
387
                                 "No unstaged files")))
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  (magit-with-toplevel
    (let ((conf (current-window-configuration))
          (bufA (get-buffer (concat file ".~{index}~")))
          (bufB (get-file-buffer file)))
      (ediff-buffers
       (or bufA (magit-find-file-index-noselect file t))
       (or bufB (find-file-noselect file))
       `((lambda ()
           (setq-local
            ediff-quit-hook
            (lambda ()
              ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
              ,@(unless bufB '((ediff-kill-buffer-carefully ediff-buffer-B)))
              (let ((magit-ediff-previous-winconf ,conf))
                (run-hooks 'magit-ediff-quit-hook))))))
       'ediff-buffers))))
404 405 406

;;;###autoload
(defun magit-ediff-show-working-tree (file)
407
  "Show changes between `HEAD' and working tree using Ediff.
408 409 410 411 412
FILE must be relative to the top directory of the repository."
  (interactive
   (list (magit-read-file-choice "Show changes in file"
                                 (magit-changed-files "HEAD")
                                 "No changed files")))
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  (magit-with-toplevel
    (let ((conf (current-window-configuration))
          (bufA (magit-get-revision-buffer "HEAD" file))
          (bufB (get-file-buffer file)))
      (ediff-buffers
       (or bufA (magit-find-file-noselect "HEAD" file))
       (or bufB (find-file-noselect file))
       `((lambda ()
           (setq-local
            ediff-quit-hook
            (lambda ()
              ,@(unless bufA '((ediff-kill-buffer-carefully ediff-buffer-A)))
              ,@(unless bufB '((ediff-kill-buffer-carefully ediff-buffer-B)))
              (let ((magit-ediff-previous-winconf ,conf))
                (run-hooks 'magit-ediff-quit-hook))))))
       'ediff-buffers))))
429

430 431 432 433 434 435 436 437
;;;###autoload
(defun magit-ediff-show-commit (commit)
  "Show changes introduced by COMMIT using Ediff."
  (interactive (list (magit-read-branch-or-commit "Revision")))
  (let ((revA (concat commit "^"))
        (revB commit))
    (apply #'magit-ediff-compare
           revA revB
438
           (magit-ediff-read-files revA revB (magit-current-file)))))
439

440 441
;;;###autoload
(defun magit-ediff-show-stash (stash)
442 443 444 445
  "Show changes introduced by STASH using Ediff.
`magit-ediff-show-stash-with-index' controls whether a
three-buffer Ediff is used in order to distinguish changes in the
stash that were staged."
446
  (interactive (list (magit-read-stash "Stash")))
447 448 449 450 451
  (pcase-let* ((revA (concat stash "^1"))
               (revB (concat stash "^2"))
               (revC stash)
               (`(,fileA ,fileC) (magit-ediff-read-files revA revC))
               (fileB fileC))
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    (if (and magit-ediff-show-stash-with-index
             (member fileA (magit-changed-files revB revA)))
        (let ((conf (current-window-configuration))
              (bufA (magit-get-revision-buffer revA fileA))
              (bufB (magit-get-revision-buffer revB fileB))
              (bufC (magit-get-revision-buffer revC fileC)))
          (ediff-buffers3
           (or bufA (magit-find-file-noselect revA fileA))
           (or bufB (magit-find-file-noselect revB fileB))
           (or bufC (magit-find-file-noselect revC fileC))
           `((lambda ()
               (setq-local
                ediff-quit-hook
                (lambda ()
                  ,@(unless bufA
                      '((ediff-kill-buffer-carefully ediff-buffer-A)))
                  ,@(unless bufB
                      '((ediff-kill-buffer-carefully ediff-buffer-B)))
                  ,@(unless bufC
                      '((ediff-kill-buffer-carefully ediff-buffer-C)))
                  (let ((magit-ediff-previous-winconf ,conf))
                    (run-hooks 'magit-ediff-quit-hook))))))
           'ediff-buffers3))
      (magit-ediff-compare revA revC fileA fileC))))
476

477 478
(defun magit-ediff-cleanup-auxiliary-buffers ()
  (let* ((ctl-buf ediff-control-buffer)
Kyle Meyer's avatar
Kyle Meyer committed
479 480 481 482 483 484
         (ctl-win (ediff-get-visible-buffer-window ctl-buf))
         (ctl-frm ediff-control-frame)
         (main-frame (cond ((window-live-p ediff-window-A)
                            (window-frame ediff-window-A))
                           ((window-live-p ediff-window-B)
                            (window-frame ediff-window-B)))))
485 486 487 488 489 490 491 492 493 494 495
    (ediff-kill-buffer-carefully ediff-diff-buffer)
    (ediff-kill-buffer-carefully ediff-custom-diff-buffer)
    (ediff-kill-buffer-carefully ediff-fine-diff-buffer)
    (ediff-kill-buffer-carefully ediff-tmp-buffer)
    (ediff-kill-buffer-carefully ediff-error-buffer)
    (ediff-kill-buffer-carefully ediff-msg-buffer)
    (ediff-kill-buffer-carefully ediff-debug-buffer)
    (when (boundp 'ediff-patch-diagnostics)
      (ediff-kill-buffer-carefully ediff-patch-diagnostics))
    (cond ((and (ediff-window-display-p)
                (frame-live-p ctl-frm))
Kyle Meyer's avatar
Kyle Meyer committed
496 497 498
           (delete-frame ctl-frm))
          ((window-live-p ctl-win)
           (delete-window ctl-win)))
499 500 501 502 503 504
    (unless (ediff-multiframe-setup-p)
      (ediff-kill-bottom-toolbar))
    (ediff-kill-buffer-carefully ctl-buf)
    (when (frame-live-p main-frame)
      (select-frame main-frame))))

505 506 507
(defun magit-ediff-restore-previous-winconf ()
  (set-window-configuration magit-ediff-previous-winconf))

508
;;; _
509 510
(provide 'magit-ediff)
;;; magit-ediff.el ends here