2022-11-19 Streamlining my workflow with Magit and BitBucket

We use BitBucket at our company, which is some source of frustration for us. One of the issues we have with BitBucket is that it seems to lack a decent CLI tool. This means that in order to e.g. create a pull request, you go to the website, click a few times and only then confirm that a PR is really what you want.

Well, after some time I learned that it’s not that bad. When you git push your changes, Git (on the command line) responds with the URL you need to go to create a pull request out of the branch you just pushed. (This is most probably achieved via post-receive or some other Git hook.) Nice.

Well, of course I don’t use Git from the command line – I use Magit. So naturally I wanted Emacs to extract that information for me and open the URL in the browser. It turns out that it was easier than I thought. Using the famous Emacs self-documenting capabilities, I found two commands: magit-section-backward, and, most importantly, magit-process-buffer. (Go check their docstrings to learn what they do, or perhaps confirm your guess.) So, I hacked up a pretty simple function to grab the data from the Magit process buffer and open the PR page in my browser.

(defun magit-open-pull-request-pr ()
  "Open the pull request URL if applicable."
  (interactive)
  (save-excursion
    (set-buffer (magit-process-buffer t))
    (goto-char (point-max))
    (magit-section-backward)
    (when
        (search-backward-regexp "remote: \\(To create a merge\\|Create pull\\) request" nil t)
      (forward-line 1)
      (re-search-forward "remote: +" (line-end-position) t)
      (browse-url-at-point))))

As a bonus, I modified the first regex in the code above so that the same command works for GitLab (which we also use for a few projects).

I feel like a broken record, but once again Emacs turns out to be an extremely user-friendly environment (provided that the user is a tinkerer kind, of course).

CategoryEnglish, CategoryBlog, CategoryEmacs