(defun git-draft/diff () "Diff the git index and parse the result." (reverse (let (result) (with-temp-buffer (let ((pager (getenv "GIT_PAGER"))) (setenv "GIT_PAGER" "") (unwind-protect (shell-command "git diff -U --cached ." (current-buffer)) (when pager (setenv "GIT_PAGER" pager)))) (goto-char (point-min)) (while (re-search-forward (rx (or (and line-start ; diff start (group-n 1 "diff --git ") "a/" (group-n 2 (1+ (any "a-zA-Z0-9._-"))) " " "b/" (group-n 3 (1+ (any "a-zA-Z0-9._-"))) line-end) (and line-start ; hunk start (group-n 1 "@@ -") (group-n 2 (1+ (any "0-9"))) "," (group-n 3 (1+ (any "0-9"))) " +" (group-n 4 (1+ (any "0-9"))) (* (and "," (group-n 5 (1+ (any "0-9"))))) " @@" (* not-newline) line-end))) nil t) (if (equal (match-string 1) "@@ -") (push (list :hunk :from-start (match-string 2) :from-count (match-string 3) :to-start (match-string 4)) result) ;; Else it's a diff start (push (list :file :from (match-string 2) :to (match-string 3)) result))) result))))
Sat Jul 26 22:38:05 2014