Further Hacking on Emacs for Github Pull Requests
source link: https://dev.to/devteam/further-hacking-on-emacs-for-github-pull-requests-f9a
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
You Ain't Emacs-ing if You Ain't Always Hacking Your Config
Earlier I wrote Emacs Function to Open Magit Log PR at Point. Over on Reddit, a user asked about not requiring git-link
dependency nor browse-url-default-macosx-browser
.
Since then, I’ve split apart the functions and added another use case. First and foremost, the magic “open the pull request associated with a commit” relies on an implementation feature of Github’s “Squash and merge” command. That command creates a commit with a summary (e.g., the first line of the commit message) that is the pull request’s title and the associated pull request.
Functions
With that as a caveat, there are five functions that I’ve written to help jump to pull requests on Github:
jnf/git-current-remote-url
jnf/open-pull-request-for
jnf/magit-browse-pull-request
jnf/open-pull-request-for-current-line
jnf/git-messenger-popup
jnf/git-current-remote-url
The following Elisp: dialect of Lisp used in GNU Emacs (Elisp 🔍) code defines the jnf/git-current-remote-url
function which gets the current remote url (for the given branch).
It’s usually “origin.”
(defun jnf/git-current-remote-url ()
"Get the current remote url."
(s-trim
(shell-command-to-string
(concat
"git remote get-url "
(format "%s" (magit-get-current-remote))))))
jnf/open-pull-request-for
The following elsip code defines jnf/open-pull-request-for
, which takes the named parameter :summary
. If that :summary
contains a pull request number, opens the pull request in an external browser.
(cl-defun jnf/open-pull-request-for (&key summary)
"Given the SUMMARY open the related pull request."
(let ((remote-url (jnf/git-current-remote-url)))
(save-match-data
(and (string-match "(\\#\\([0-9]+\\))$" summary)
(eww-browse-with-external-browser
(concat
;; I tend to favor HTTPS and the
;; repos end in ".git"
(s-replace ".git" "" remote-url)
"/pull/"
(match-string 1 summary)))))))
jnf/magit-browse-pull-request
The following Elisp code defines jnf/magit-browse-pull-request
, which will open the associate pull request when your point is on a Magit 🔍 log entry.
I’ve mapped that to s-6
(or Cmd+6)
(defun jnf/magit-browse-pull-request ()
"In `magit-log-mode' open the associated pull request
at point.
Assumes that the commit log title ends in the PR #, which
is the case when you use the Squash and Merge strategy.
This implementation is dependent on `magit' and `s'."
(interactive)
(let* ((beg (line-beginning-position))
(end (line-end-position))
(summary
(buffer-substring-no-properties
beg end)))
(jnf/open-pull-request-for :summary summary)))
jnf/open-pull-request-for-current-line
The following Elisp code defines jnf/open-pull-request-for-current-line
. When invoked, this function will open the pull request for the commit associated with the current line.
It does that by using git annotate
on the current line, and pulling the commit’s summary via ripgrep
.
(defun jnf/open-pull-request-for-current-line ()
"For the current line open the applicable pull request."
(interactive)
(let ((summary
(s-trim
(shell-command-to-string
(concat "git --no-pager annotate "
"-L "
(format "%s" (line-number-at-pos))
",+1 "
"--porcelain "
buffer-file-name
" | rg \"^summary\"")))))
(jnf/open-pull-request-for :summary summary)))
jnf/git-messenger-popup
The following Elisp code defines jnf/git-messenger-popup
. When invoked it launches the git-messenger popup.
(defun jnf/git-messenger-popup ()
"Open `git-messenger' or github PR.
With universal argument, open the github PR for
current line.
Without universal argument, open `git-messenger'."
(interactive)
(if (equal current-prefix-arg nil) ; no C-u
(git-messenger:popup-message)
(jnf/open-pull-request-for-current-line)))
I have mapped the function to s-6
(e.g., Cmd+6 on OS X 🔍).
If I first pass the universal argument, that is I first type C-u
then s-6
(or Ctrl+u then Cmd+6 in OS X) I will open that line’s pull request.
When in the git-messenger’s popup, I can type p to go to that line’s pull request.
Conclusion
I wrote these functions to better help me better understand Forem’s codebase. It was also a chance to continue practicing coding and learning.
If you’re interested, you can see more of my git configuration on Github
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK