Function: majutsu-conflict--parse-git-hunk

majutsu-conflict--parse-git-hunk is a natively compiled function defined in majutsu-conflict.el.

Signature

(majutsu-conflict--parse-git-hunk BEGIN END MARKER-LEN)

Documentation

Parse Git-style conflict hunk between BEGIN and END.

MARKER-LEN is the expected marker length. Returns (='git REMOVES ADDS) or nil if invalid.

Source Code

;; Defined in /nix/store/jagqw3nms546pad0gxi5f3wsckk6pji7-emacs-packages-deps/share/emacs/site-lisp/majutsu-conflict.el
;;; Git-Style Parser

(defun majutsu-conflict--parse-git-hunk (begin end marker-len)
  "Parse Git-style conflict hunk between BEGIN and END.
MARKER-LEN is the expected marker length.
Returns (\='git REMOVES ADDS) or nil if invalid."
  (save-excursion
    (goto-char begin)
    (forward-line 1)  ; skip <<<<<<< line
    (let ((state 'left)
          (left "")
          (base "")
          (right "")
          (left-label nil)
          (base-label nil)
          (right-label nil)
          (done nil))
      ;; Get left label from <<<<<<< line
      (save-excursion
        (goto-char begin)
        (let* ((line (buffer-substring-no-properties
                      (line-beginning-position)
                      (line-end-position)))
               (marker (majutsu-conflict--line-marker line marker-len)))
          (when (eq (car-safe marker) 'begin)
            (setq left-label (cdr marker)))))

      (catch 'invalid
        (while (and (not done) (< (point) end))
          (let* ((line (buffer-substring-no-properties
                        (line-beginning-position)
                        (line-end-position)))
                 (marker (majutsu-conflict--line-marker line marker-len))
                 (kind (car-safe marker))
                 (label (cdr-safe marker)))
            (cond
             ((eq kind 'end)
              (setq right-label label
                    done t))
             ((eq kind 'git-ancestor)
              (if (eq state 'left)
                  (setq state 'base
                        base-label label)
                (throw 'invalid nil)))
             ((eq kind 'git-separator)
              (if (eq state 'base)
                  (setq state 'right)
                (throw 'invalid nil)))
             (t
              (let ((content (concat line "\n")))
                (pcase state
                  ('left (setq left (concat left content)))
                  ('base (setq base (concat base content)))
                  ('right (setq right (concat right content)))
                  (_ (throw 'invalid nil)))))))
          (unless done
            (forward-line 1)))

        ;; Validate: must have reached right side before end marker
        (if (eq state 'right)
            (let ((removes (list (cons base-label base)))
                  (adds (list (cons left-label left)
                              (cons right-label right))))
              (list 'git removes adds (car removes)))
          nil)))))