Creating pull requests from emacs
Tags: emacs
Published: 2019-07-24 19:43:10

---
title: Creating pull requests from emacs
published: true
tags: emacs
canonical_url: https://prathamesh.tech/2019/06/21/creating-pull-requests-from-emacs/
---

 ![Creating pull requests from emacs](https://prathamesh.tech/content/images/2019/06/callum-shaw-555357-unsplash.jpg)

I use [magit](https://magit.vc/) to work with git and emacs. Magit makes it easy to create branches and push them to github. After creating a branch, the natural thing is to create a pull request. But one has to visit github and click on "New pull request" to create the pull request.

I want to create the pull request automatically from emacs and magit. After googling for this issue, I came across [this solution](https://endlessparentheses.com/create-github-prs-from-emacs-with-magit.html). Once I added this code and tried to create the pull request using keyboard shortcut `v`, emacs opened the page using `w3m` browser. I wanted to open the URL using chrome which is my default browser. We can tell emacs to use the default browser for opening URLs using following code.

<!--kg-card-begin: code-->
```
;; Set default browser as default OS X browser
(setq browse-url-browser-function 'browse-url-default-macosx-browser)
```
<!--kg-card-end: code-->

This makes sure that any URL that emacs tries to open is opened in the default browser of your Mac OS X system. For windows, there is similar configuration.

<!--kg-card-begin: code-->
```
(setq browse-url-browser-function 'browse-url-default-windows-browser)
```
<!--kg-card-end: code-->

Now I can easily open the pull requests from emacs. I just need to be logged into github in my browser. The original solution from author of emacs can also be extended to use gitlab if you are using gitlab for hosting your code.

<!--kg-card-begin: code-->
```
;; original

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

(eval-after-load 'magit
  '(define-key magit-mode-map "v"
     #'endless/visit-pull-request-url))
```
<!--kg-card-end: code--><!--kg-card-begin: code-->
```
;;; modified for gitlab
(defun endless/visit-pull-request-url-gitlab ()
  "Visit the current branch's PR on Gitlab."
  (interactive)
  (browse-url
   (format "https://gitlab.com/%s/pull/new/%s"
           (replace-regexp-in-string
            "\\`.+gitlab\\.com:\\(.+\\)\\.git\\'" "\\1"
            (magit-get "remote"
                       (magit-get-push-remote)
                       "url"))
           (magit-get-current-branch))))

(eval-after-load 'magit
  '(define-key magit-mode-map "h"
     #'endless/visit-pull-request-url-gitlab))
```
<!--kg-card-end: code-->

This code works with magit version `20190620.2234` and Git 2.20.1 and Emacs 26.1

Subscribe to my [newsletter](https://www.prathamesh.tech/mailing-list) to know more about such interesting insights about Emacs and magit.

Happy hacking!