15

It's very common that, after pushing a given branch upstream, I'll want to visit it on github and create a pull request. I'm starting to get annoyed at having to manually visit the repo, find the branch, and create the PR.

Q: How can I write a command that visits the PR url in a browser?

The PR url is given by https://github.com/ORIGIN-REPO/compare/BRANCH-NAME. Since both the origin and the branch-name known by Magit (and by plain git), it should be easy to write a command that builds that URL and calls browse-url on it.

Unfortunately, I know nothing about the internals workings of Magit, so I don't know how to get that information.

9

Here's what I came up with:

(defun pull-request-url ()
  "Build the URL or the pull requestion on GitHub corresponding
to the current branch. Uses Magit."
  (interactive)
  (format "%s/compare/%s"
           (replace-regexp-in-string
            (rx (and string-start (1+ any) "github.com:" (group (1+ any)) ".git" string-end))
            "https://github.com/\\1"
            (magit-get "remote" (magit-get-current-remote) "url"))
          (magit-get-current-branch)))

magit.el is pretty easy to read, by the way.

3

Malabarba has posted his approach on his blog.

(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))))

By the way, I intend to add similar functionality (and a lot more ;-) to Magit itself, eventually.

  • @Malabarba the export of that snippet went wrong on your blog. – tarsius Jun 9 '16 at 10:09
  • And do let me know when you add something like that into magit. :-) – Malabarba Jun 9 '16 at 14:07
3

Have you considered magithub?

It doesn't let you view the pull request of the current branch, but it gives you a full list of pull requests which you can visit by pressing RET.

enter image description here

Note this requires hub 2.2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.