Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Create Github PRs from Emacs with Magit (again)

GitHub Integration post series

I don’t usually dedicate an entire post to something I’ve already done in a previous one, but this nugget is so useful it deserves the attention. Remember how you can create Github PRs straight from Magit? Magit 2.1.0 is barely two weeks away, and it brings so many (awesome) changes that our little snippet is going to break.

Luckily for you, I’ve been using Magit’s next version for a couple of months now, so I have a fix all shiny and ready for you. This function is also slightly more robust, and fixes some corner-case issues in the other. So if you found that the previous function didn’t cover your use-case, you should definitely try the new one.

(defun endless/visit-pull-request-url ()
  "Visit the current branch's PR on Github."
  (interactive)
  (browse-url
   (format "https://github.com/%s/pull/new/%s"
           (replace-regexp-in-string
            "\\`.+github\\.com:\\(.+\\)\\.git\\'" "\\1"
            (magit-get "remote"
                       (magit-get-remote)
                       "url"))
           (cdr (or (magit-get-remote-branch)
                    (user-error "No remote branch"))))))
(eval-after-load 'magit
  '(define-key magit-mode-map "v"
     #'endless/visit-pull-request-url))

Note that I’ve changed the keybind to v. In Magit 2.1, that key is bound in buffer overlays, not on the mode-map, which means we can just bind it in the map and we’ll get a semi “do-what-I-mean” functionality.

Update 24 Jan 2016

The latest Magit from Melpa deals with branches in a slightly different way, so the code above won’t quite work. If you run into that problem, the update below should work fine.

(defun endless/visit-pull-request-url ()
  "Visit the current branch's PR on Github."
  (interactive)
  (browse-url
   (format "https://github.com/%s/pull/new/%s"
           (replace-regexp-in-string
            "\\`.+github\\.com:\\(.+\\)\\.git\\'" "\\1"
            (magit-get "remote"
                       (magit-get-push-remote)
                       "url"))
           (magit-get-current-branch))))

Tags: magit, init.el, emacs,

comments powered by Disqus