Function: magit-diff-dwim

magit-diff-dwim is an autoloaded, interactive and byte-compiled function defined in magit-diff.el.

Signature

(magit-diff-dwim &optional ARGS FILES)

Documentation

Show changes for the thing at point.

For example, if point is on a commit, show the changes introduced by that commit. Likewise if point is on the section titled "Unstaged changes", then show those changes in a separate buffer. Generally speaking, compare the thing at point with the most logical, trivial and (in *any* situation) at least potentially useful other thing it could be compared to.

When the region selects commits, then compare the two commits at either end. There are different ways two commits can be compared. In the buffer showing the diff, you can control how the comparison, is done, using "D r" and "D f".

This function does not always show the changes that you might want to view in any given situation. You can think of the changes being shown as the smallest common denominator. There is no AI involved. If this command never does what you want, then ignore it, and instead use the commands that allow you to explicitly specify what you need.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/magit-20260411.1452/magit-diff.el
;;;; Setup Commands

;;;###autoload
(defun magit-diff-dwim (&optional args files)
  "Show changes for the thing at point.

For example, if point is on a commit, show the changes introduced by
that commit.  Likewise if point is on the section titled \"Unstaged
changes\", then show those changes in a separate buffer.  Generally
speaking, compare the thing at point with the most logical, trivial
and (in *any* situation) at least potentially useful other thing it
could be compared to.

When the region selects commits, then compare the two commits at
either end.  There are different ways two commits can be compared.
In the buffer showing the diff, you can control how the comparison,
is done, using \"D r\" and \"D f\".

This function does not always show the changes that you might want
to view in any given situation.  You can think of the changes being
shown as the smallest common denominator.  There is no AI involved.
If this command never does what you want, then ignore it, and instead
use the commands that allow you to explicitly specify what you need."
  (interactive (magit-diff-arguments))
  (let ((default-directory default-directory)
        (section (magit-current-section)))
    (cond
      ((magit-section-match 'module section)
       (setq default-directory
             (expand-file-name
              (file-name-as-directory (oref section value))))
       (magit-diff-range (oref section range)))
      (t
       (when (magit-section-match 'module-commit section)
         (setq args nil)
         (setq files nil)
         (setq default-directory
               (expand-file-name
                (file-name-as-directory (magit-section-parent-value section)))))
       (pcase (magit-diff--dwim)
         ('unmerged (magit-diff-unmerged args files))
         ('unstaged (magit-diff-unstaged args files))
         ('staged
          (let ((file (magit-file-at-point)))
            (if (and file (equal (cddr (car (magit-file-status file))) '(?D ?U)))
                ;; File was deleted by us and modified by them.  Show the latter.
                (magit-diff-unmerged args (list file))
              (magit-diff-staged nil args files))))
         (`(stash . ,value) (magit-stash-show value args))
         (`(commit . ,value)
          (magit-diff-range (format "%s^..%s" value value) args files))
         ((and range (pred stringp))
          (magit-diff-range range args files))
         (_ (call-interactively #'magit-diff-range)))))))