DEV Community

Jeremy Friesen
Jeremy Friesen

Posted on • Originally published at takeonrules.com on

Emacs Function to Open Magit Log PR at Point

A Quick Hack to Help My Code Spelunking

At Forem, we make extensive use of the Github pull request conversations on the forem codebase. We also use the Squash and Merge strategy for Github.
See What’s the Difference Between the 3 Github Merge Methods? for details on the strategy.

One side-effect of the Squash and Merge is that Github appends the merged pull request number to the commit message. So this evening, I whipped up an Emacs function that I can call from a Magit log to open the pull request in my default browser.

(defun jnf/magit-browse-pull-request ()
  "In `magit-log-mode', open the associated pull request at point."
  (interactive)
  (let* ((remote-url
          (car
           (git-link--exec
            "remote" "get-url"
            (format "%s"
                    (magit-get-current-remote)))))
         (beg (line-beginning-position))
         (end (line-end-position))
         (region (buffer-substring-no-properties beg end)))
    (save-match-data
      (and (string-match "(\\#\\([0-9]+\\))$" region)
           (browse-url-default-macosx-browser
            (concat
             (s-replace ".git" "" remote-url)
             "/pull/"
             (match-string 1 region)))))))

Enter fullscreen mode Exit fullscreen mode

This works, and I’m certain there are improvements to my code. The above function relies on the s, magit, and git-link package.

In magit-log-mode I bound s-6 to jnf/magit-browse-pull-request.

Top comments (0)