From 884a42d1ca9e6a873d7d8520e0fc98c1fb59735f Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Thu, 12 Oct 2017 23:30:22 -0400 Subject: notmuch: Add command to show GitHub PRs in Magit Inspired by William Casarin: https://notmuchmail.org/pipermail/notmuch/2017/025542.html --- lisp/km-mail.el | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'lisp') diff --git a/lisp/km-mail.el b/lisp/km-mail.el index 1ff6907..19e1a2d 100644 --- a/lisp/km-mail.el +++ b/lisp/km-mail.el @@ -79,6 +79,59 @@ (notmuch-show id) (call-interactively #'notmuch-show)))) +(defun km/notmuch-github-pr-number () + "Return the PR number for this message." + (let (pr) + (with-current-notmuch-show-message + (goto-char (point-min)) + (if (re-search-forward "https://github\\.com/.*/pull/\\([0-9]+\\)" nil t) + (setq pr (match-string-no-properties 1)) + (user-error "Could not find PR number"))) + pr)) + +(defvar km/notmuch-github-repo-function nil + "Function that returns repo information from this message. + +If the function can determine the repository, it should return a +list, structured as (DIRECTORY REMOTE BASE). + + DIRECTORY absolute path to the top-level of the local repo + REMOTE name of the remote to fetch from + BASE used to limit the log (i.e., \"BASE..PR-REF\").") + +;;;###autoload +(defun km/notmuch-show-pr-in-magit (&optional force-fetch) + "Show the Magit log for this message's PR. + +With a prefix argument, fetch from the remote even if the ref +already exists locally. The repository information is extracted +with `km/notmuch-github-repo-function'. + +This function assumes that the remote is a GitHub repo and that +you've configured \"git fetch \" to fetch pull request +refs. This can be done by placing a line like + + fetch = +refs/pull/*/head:refs/pull//* + +in the remote's \".git/config\" entry." + (interactive "P") + (require 'magit) + (unless (functionp km/notmuch-github-repo-function) + (user-error "`km/notmuch-github-repo-function' is not specified")) + (let* ((info (or (funcall km/notmuch-github-repo-function) + (user-error "Could not determine repository"))) + (remote (or (nth 1 info) "origin")) + (base-ref (or (nth 2 info) + (concat remote "/master"))) + (local-ref (format "refs/pull/%s/%s" + remote + (km/notmuch-github-pr-number))) + (default-directory (nth 0 info))) + (when (or force-fetch + (not (magit-ref-exists-p local-ref))) + (magit-call-git "fetch" remote)) + (magit-log (list (concat base-ref ".." local-ref))))) + ;;; Mail sync -- cgit v1.2.3