Function: magit-diff-type

magit-diff-type is a byte-compiled function defined in magit-diff.el.

Signature

(magit-diff-type &optional SECTION)

Documentation

Return the diff type of SECTION.

The returned type is one of the symbols staged, unstaged, committed, or undefined. This type serves a similar purpose as the general type common to all sections (which is stored in the type slot of the corresponding magit-section(var)/magit-section(fun) struct) but takes additional information into account. When the SECTION isn't related to diffs and the buffer containing it also isn't a diff-only buffer, then return nil.

Currently the type can also be one of tracked and untracked but these values are not handled explicitly everywhere they should be and a possible fix could be to just return nil here.

The section has to be a diff or hunk section, or a section whose children are of type diff. If optional SECTION is nil, return the diff type for the current section. In buffers whose major mode is magit-diff-mode SECTION is ignored and the type is determined using other means. In magit-revision-mode buffers the type is always committed.

Do not confuse this with magit-diff-scope (which see).

Source Code

;; Defined in ~/.emacs.d/elpa/magit-20260411.1452/magit-diff.el
;;; Diff Information

(defun magit-diff-type (&optional section)
  "Return the diff type of SECTION.

The returned type is one of the symbols `staged', `unstaged',
`committed', or `undefined'.  This type serves a similar purpose
as the general type common to all sections (which is stored in
the `type' slot of the corresponding `magit-section' struct) but
takes additional information into account.  When the SECTION
isn't related to diffs and the buffer containing it also isn't
a diff-only buffer, then return nil.

Currently the type can also be one of `tracked' and `untracked'
but these values are not handled explicitly everywhere they
should be and a possible fix could be to just return nil here.

The section has to be a `diff' or `hunk' section, or a section
whose children are of type `diff'.  If optional SECTION is nil,
return the diff type for the current section.  In buffers whose
major mode is `magit-diff-mode' SECTION is ignored and the type
is determined using other means.  In `magit-revision-mode'
buffers the type is always `committed'.

Do not confuse this with `magit-diff-scope' (which see)."
  (when-let ((section (or section (magit-current-section))))
    (cond ((derived-mode-p 'magit-revision-mode 'magit-stash-mode) 'committed)
          ((derived-mode-p 'magit-diff-mode)
           (cond
             (magit-buffer-diff-type)
             ((equal magit-buffer-diff-typearg "--no-index")
              'undefined)
             ((not magit-buffer-diff-range)
              'undefined)
             ((string-search "." magit-buffer-diff-range)
              'committed)
             ((magit-rev-head-p magit-buffer-diff-range)
              (if (equal magit-buffer-diff-typearg "--cached")
                  'staged
                'unstaged))
             ('committed)))
          ((derived-mode-p 'magit-status-mode)
           (pcase (oref section type)
             ((and type (or 'staged 'unstaged 'tracked 'untracked))
              type)
             ((or 'file 'module)
              (let* ((parent (oref section parent))
                     (type   (oref parent type)))
                (if (memq type '(file module))
                    (magit-diff-type parent)
                  type)))
             ('hunk (thread-first section
                      (oref parent)
                      (oref parent)
                      (oref type)))))
          ((derived-mode-p 'magit-log-mode)
           (if (or (and (magit-section-match 'commit section)
                        (oref section children))
                   (magit-section-match [* file commit] section))
               'committed
             'undefined))
          ('undefined))))