summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorKyle Meyer <kyle@kyleam.com>2020-09-01 22:15:45 -0400
committerKyle Meyer <kyle@kyleam.com>2020-09-01 22:57:19 -0400
commit5ba3fc32c00cbce8ebb2dd6b3e34fe91a9a02216 (patch)
treece8ddd91c1d9310596278e2f3231d6556e8550aa /lisp
parent7274025eafccc65f4a8496f4e5290727cd9d2731 (diff)
downloademacs.d-5ba3fc32c00cbce8ebb2dd6b3e34fe91a9a02216.tar.gz
magit: Add two custom patch commands
Diffstat (limited to 'lisp')
-rw-r--r--lisp/km-magit.el78
1 files changed, 78 insertions, 0 deletions
diff --git a/lisp/km-magit.el b/lisp/km-magit.el
index f0819de..8f2179a 100644
--- a/lisp/km-magit.el
+++ b/lisp/km-magit.el
@@ -26,6 +26,7 @@
(require 'km-util)
(require 'magit)
(require 'projectile)
+(require 'seq)
;;;###autoload
(defun km/magit-status (&optional default-display)
@@ -832,6 +833,83 @@ category."
(magit-run-git-async "for-each-ref" (format "--contains=%s" rev)
"--sort=v:refname" "refs/tags")))
+(defun km/magit-patch-create-from-upstream (&optional args)
+ (interactive (list (transient-args 'magit-patch-create)))
+ (if-let ((upstream (magit-get-upstream-branch)))
+ (apply #'magit-run-git "format-patch" upstream args)
+ (user-error "No upstream branch")))
+
+(defun km/magit-tweak-patch-series-summary ()
+ "Restyle \"My Name (N):\" heading.
+Drop author for single author series by `user-full-name',
+appending [M/N] to each entry."
+ (interactive)
+ (save-excursion
+ (goto-char (point-min))
+ (search-forward "*** BLURB HERE ***")
+ (let ((author-regexp (rx line-start word (zero-or-more not-newline)
+ "(" (one-or-more digit) "):" line-end)))
+ ;; Don't proceed if there looks to be more than one author.
+ (unless (and (re-search-forward author-regexp nil t)
+ (re-search-forward author-regexp nil t))
+ (let ((buffer-read-only nil))
+ (save-excursion
+ (goto-char (point-min))
+ (when-let ((total (and (re-search-forward
+ (rx line-start (eval user-full-name) " ("
+ (group (one-or-more digit))
+ "):" line-end)
+ nil t)
+ (match-string 1)))
+ (prefix (format "[%%%dd/%s] " (length total) total))
+ (idx 1))
+ (delete-region (line-beginning-position) (1+ (line-end-position)))
+ (while (and (not (eobp))
+ (not (looking-at-p "^$")))
+ (goto-char (line-beginning-position))
+ (skip-chars-forward " ")
+ (insert (format prefix idx))
+ (cl-incf idx)
+ (forward-line)))))))))
+
+(defun km/magit-patch-create-series (&optional args)
+ (interactive (list (transient-args 'magit-patch-create)))
+ (when (magit-anything-modified-p t)
+ (user-error "There are uncommitted changes"))
+ (setq args (seq-remove
+ (lambda (a) (string-match-p "\\`--base=" a))
+ args))
+ (if-let ((current (magit-get-current-branch))
+ (upstream (magit-get-upstream-branch current)))
+ (let ((patch-branch (concat "patches/" current)))
+ (if (not (magit-local-branch-p patch-branch))
+ (magit-branch-and-checkout patch-branch current)
+ (magit-update-ref (concat "refs/heads/" patch-branch)
+ (concat "reset: moving back to " current)
+ current)
+ (magit-branch-checkout patch-branch))
+ (apply #'magit-run-git "format-patch"
+ upstream (concat "--base=" upstream) args)
+ (let* ((patches (or (seq-filter
+ (lambda (f) (string-match-p "\\.patch\\'" f))
+ (magit-untracked-files))
+ (error "There should be patches")))
+ (cover (seq-find (lambda (f) (string-match-p "\\`0+-" f))
+ patches)))
+ (magit-call-git "add" patches)
+ (magit-run-git "commit" "-mpatches")
+ (when cover
+ (with-temp-buffer
+ (insert-file-contents cover)
+ (km/magit-tweak-patch-series-summary)
+ (write-region nil nil cover))
+ (magit-call-git "commit" "-mtweak cover" "--" cover))
+ (when cover
+ (find-file-other-window cover)
+ (re-search-forward (rx "Subject: [PATCH" (zero-or-more not-newline) "] "))
+ (setq buffer-read-only nil))))
+ (user-error "No upstream branch")))
+
;;; Copy functions