Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Automatically configure Magit to access Github PRs

GitHub Integration post series

Did you know you can fetch Github pull requests with git by adding a remote.origin.fetch configuration? That insightful tip is a courtesy of Oleh at (or emacs, a very active blog that has a habit of unbalancing parentheses wherever it goes). I like the tip so much I wanted to add something to it. Instead of manually adding that line to you .git/config file, why not have Magit do that for you?

The following snippet will ensure that you always have this fetch configured on Github repositories as soon as you issue magit-status.

(defun endless/add-PR-fetch ()
  "If refs/pull is not defined on a GH repo, define it."
  (let ((fetch-address
         "+refs/pull/*/head:refs/pull/origin/*")
        (magit-remotes
         (magit-get-all "remote" "origin" "fetch")))
    (unless (or (not magit-remotes)
                (member fetch-address magit-remotes))
      (when (string-match
             "github" (magit-get "remote" "origin" "url"))
        (magit-git-string
         "config" "--add" "remote.origin.fetch"
         fetch-address)))))

(add-hook 'magit-mode-hook #'endless/add-PR-fetch)

This means you can always access pull requests from Magit by hitting b b, the usual branch command. The reference names start with /refs/pull/origin/, but completion does most of the job here.

This is more lightweight approach than my previous post on the subject, which used gh-pulls-mode. You should try both and see which one you prefer.

Tags: magit, init.el, emacs,

comments powered by Disqus