Function: majutsu-interactive--build-patch

majutsu-interactive--build-patch is a natively compiled function defined in majutsu-interactive.el.

Signature

(majutsu-interactive--build-patch &optional INVERT INCLUDE-ALL-FILES)

Documentation

Build complete patch from current selections.

When INVERT is non-nil, select non-matching change lines. When INCLUDE-ALL-FILES is non-nil, include hunks from all files in invert mode. Returns patch string or nil if no selections.

Source Code

;; Defined in /nix/store/jagqw3nms546pad0gxi5f3wsckk6pji7-emacs-packages-deps/share/emacs/site-lisp/majutsu-interactive.el
(defun majutsu-interactive--build-patch (&optional invert include-all-files)
  "Build complete patch from current selections.
When INVERT is non-nil, select non-matching change lines.
When INCLUDE-ALL-FILES is non-nil, include hunks from all files in invert mode.
Returns patch string or nil if no selections."
  (let* ((selected (majutsu-interactive--collect-selected-hunks))
         (by-file (make-hash-table :test 'eq)))
    ;; Group by file section
    (dolist (item selected)
      (let ((file (car item))
            (hunk (cadr item))
            (spec (caddr item)))
        (push (list hunk spec) (gethash file by-file))))
    ;; Build patches per file
    (let* ((patches nil)
           (all-hunks-by-file (and invert (majutsu-interactive--collect-hunks-by-file))))
      (cond
       ((not invert)
        (maphash
         (lambda (file hunks)
           (let ((hunks (nreverse hunks)))
             (when-let* ((patch (majutsu-interactive--build-file-patch
                                 file hunks invert)))
               (push patch patches))))
         by-file))
       (include-all-files
        (maphash
         (lambda (file all-hunks)
           (let* ((selected (gethash file by-file))
                  (spec-by-hunk (make-hash-table :test 'eq))
                  (expanded nil))
             (dolist (hs selected)
               (puthash (car hs) (cadr hs) spec-by-hunk))
             ;; Keep all unselected hunks; invert only selected hunks.
             (dolist (hunk all-hunks)
               (let ((spec (gethash hunk spec-by-hunk)))
                 (if spec
                     (push (list hunk spec t) expanded)
                   (push (list hunk :all nil) expanded))))
             (setq expanded (nreverse expanded))
             (when-let* ((patch (majutsu-interactive--build-file-patch
                                 file expanded invert)))
               (push patch patches))))
         all-hunks-by-file))
       (t
        (maphash
         (lambda (file hunks)
           (let* ((spec-by-hunk (make-hash-table :test 'eq))
                  (all-hunks (or (and all-hunks-by-file
                                      (gethash file all-hunks-by-file))
                                 (mapcar #'car hunks)))
                  (expanded nil))
             (dolist (hs hunks)
               (puthash (car hs) (cadr hs) spec-by-hunk))
             ;; Expand to all hunks in selected files.
             (dolist (hunk all-hunks)
               (let ((spec (gethash hunk spec-by-hunk)))
                 (if spec
                     (push (list hunk spec t) expanded)
                   (push (list hunk :all nil) expanded))))
             (setq expanded (nreverse expanded))
             (when-let* ((patch (majutsu-interactive--build-file-patch
                                 file expanded invert)))
               (push patch patches))))
         by-file)))
      (when patches
        (majutsu-interactive--fixup-patch
         (mapconcat #'identity (nreverse patches) ""))))))