Skip to content

Instantly share code, notes, and snippets.

@ieure
Created March 23, 2011 19:01
Show Gist options
  • Save ieure/883725 to your computer and use it in GitHub Desktop.
Save ieure/883725 to your computer and use it in GitHub Desktop.
(eval-after-load "magit"
'(progn
(defun magit-browse-github (commit)
"Open a browser pointing to the tracked branch 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-branch (or (and branch (magit-remote-branch-for branch))
branch))
(remote-url (magit-get "remote" remote "url"))
(head (magit-git-string "log" "--max-count=1"
"--pretty=format:%h"))
(remote-tree (if (> commit 1) head remote-branch)))
(browse-url
(cond ((string-match "git@github\\.com" remote-url)
(replace-regexp-in-string
"^git@github.com:\\(.*?\\)\\(\\.git\\)?$"
(format "https://github.com/\\1/tree/%s" remote-tree)
remote-url))
((string-match "https?://github\\.com" remote-url)
(replace-regexp-in-string
"^\\(https?://github\\.com/.*?\\)\\(\\.git\\)?$"
(format "\\1/tree/%s" remote-tree) remote-url))
(t (error "Tracked branch is not on GitHub."))))))
(define-key magit-mode-map "\C-ch" 'magit-browse-github)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment