Function: diff-hl-stage-current-hunk

diff-hl-stage-current-hunk is an interactive and natively compiled function defined in diff-hl.el.

Signature

(diff-hl-stage-current-hunk)

Documentation

Stage the hunk at or near point.

Only supported with Git.

Key Bindings

This command is not in any keymaps.

Source Code

;; Defined in /nix/store/cfz56rl3lp066226i0bmmgiicls3gcq7-emacs-packages-deps/share/emacs/site-lisp/elpa/diff-hl-20261001.135/diff-hl.el
(defun diff-hl-stage-current-hunk ()
  "Stage the hunk at or near point.

Only supported with Git."
  (interactive)
  (diff-hl--ensure-staging-supported)
  (diff-hl-find-current-hunk)
  (let* ((line (line-number-at-pos))
         (file (diff-hl--buffer-file-name))
         (dest-buffer (get-buffer-create " *diff-hl-stage*"))
         (orig-buffer (current-buffer))
         ;; FIXME: If the file name has double quotes, these need to be quoted.
         (file-base (file-name-nondirectory file))
         success)
    (with-current-buffer dest-buffer
      (let ((inhibit-read-only t))
        (erase-buffer)))
    (let (diff-hl-reference-revision
          diff-hl-update-async)
      (diff-hl-diff-buffer-with-reference file dest-buffer nil 3))
    (with-current-buffer dest-buffer
      (diff-hl-diff-skip-to line)
      (let ((inhibit-read-only t))
        (diff-hl-split-away-changes 3)
        (save-excursion
          (diff-end-of-hunk)
          (delete-region (point) (point-max)))
        (diff-beginning-of-hunk)
        (delete-region (point-min) (point))
        ;; diff-no-select creates a very ugly header; Git rejects it
        (insert (format "diff a/%s b/%s\n" file-base file-base))
        (insert (format "--- a/%s\n" file-base))
        (insert (format "+++ b/%s\n" file-base)))
      (setq success (diff-hl-stage-diff orig-buffer)))
    (when success
      (if diff-hl-show-staged-changes
          (message (concat "Hunk staged; customize `diff-hl-show-staged-changes'"
                           " to highlight only unstaged changes"))
        (message "Hunk staged"))
      (unless diff-hl-show-staged-changes
        (diff-hl-update)))))