DEV Community

Prathamesh Sonpatki
Prathamesh Sonpatki

Posted on • Originally published at prathamesh.tech on

4

Creating pull requests from emacs

Creating pull requests from emacs

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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

<!--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))
Enter fullscreen mode Exit fullscreen mode

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!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay