I use magit 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. 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.

;; Set default browser as default OS X browser (setq browse-url-browser-function 'browse-url-default-macosx-browser)

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.

(setq browse-url-browser-function 'browse-url-default-windows-browser)

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.

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

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

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

Subscribe to my newsletter to know more about such interesting insights about Emacs and magit.

Happy hacking!