Skip to content

Instantly share code, notes, and snippets.

@chmouel
Created February 11, 2016 11:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chmouel/33ed0f000bcf674e2643 to your computer and use it in GitHub Desktop.
Save chmouel/33ed0f000bcf674e2643 to your computer and use it in GitHub Desktop.
;; Adapted from https://gist.github.com/ieure/883725 for magit-log
(eval-after-load "magit"
'(progn
(defun magit-log-browse-github-commit (commit)
"Open a browser pointing to the current commit on GitHub.
With prefix argument, go to the commit HEAD is at."
(interactive "p")
(let* ((branch (magit-get-current-branch))
(remote (and branch (magit-get "branch" branch "remote")))
(remote-url (magit-get "remote" remote "url"))
(current-commit (magit-commit-at-point)))
(browse-url
(cond ((string-match "git@github\\.com" remote-url)
(replace-regexp-in-string
"^git@github.com:\\(.*?\\)\\(\\.git\\)?$"
(format "https://github.com/\\1/commit/%s" current-commit)
remote-url))
((string-match "https?://github\\.com" remote-url)
(replace-regexp-in-string
"^\\(https?://github\\.com/.*?\\)\\(\\.git\\)?$"
(format "\\1/commit/%s" current-commit) remote-url))
(t (error "Tracked branch is not on GitHub."))))))
(define-key magit-log-mode-map "\C-cb" 'magit-log-browse-github-commit)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment