diff --git a/README.md b/README.md index 69b1c73..29d633f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ View the file you're editing in Emacs on GitHub. ### Installation: -Available as a package in [Marmalade](http://marmalade-repo.org/) and [MELPA](http://melpa.milkbox.net/). +Available as a package in [Marmalade](https://marmalade-repo.org/) and [MELPA](http://melpa.org/). `M-x package-install github-browse-file` @@ -16,7 +16,10 @@ Call `github-browse-file` (for the git blob) or `github-browse-file-blame` (for the git blame) to view current file on GitHub. With a prefix argument (`C-u`), you can force them to use the "master" branch. -For more information see [my blog post](http://ozansener.com/blog/view-the-file-youre-editing-in-emacs-on-github/). +`github-browse-commit` can be used to link to the current commit. ### Contributors * [Charles Comstock](https://github.com/dgtized) +* [Justin Talbott](https://github.com/waymondo) +* [William Roe](https://github.com/wjlroe) +* [Yukihiro Hara](https://github.com/yukihr) diff --git a/github-browse-file.el b/github-browse-file.el index d082725..5240d77 100644 --- a/github-browse-file.el +++ b/github-browse-file.el @@ -1,10 +1,10 @@ ;;; github-browse-file.el --- View the file you're editing on GitHub -;; Copyright (C) 2013 Ozan Sener +;; Copyright (C) 2013 Ozan Sener & Contributors ;; Author: Ozan Sener ;; Homepage: https://github.com/osener/github-browse-file -;; Version: 0.3.0 +;; Version: 0.5.0 ;; Keywords: convenience vc git github ;; Package-Requires: ((cl-lib "0.5")) @@ -63,6 +63,10 @@ This should only ever be `let'-bound, not set outright.") "Whether to use \"master\" regardless of current branch This should only ever be `let'-bound, not set outright.") +(defvar github-browse-file--magit-commit-link-modes + '(magit-commit-mode magit-revision-mode magit-log-mode) + "Non-file magit modes that should link to commits.") + (defun github-browse-file--relative-url () "Return \"username/repo\" for current repository. @@ -105,6 +109,8 @@ If github-browse-file--force-master is non-nil, return \"master\". Otherwise, return the name of the current branch." (cond (github-browse-file--force-master "master") + ((member major-mode github-browse-file--magit-commit-link-modes) + (magit-commit-at-point)) ((github-browse-file--ahead-p) (github-browse-file--remote-branch)) (t (let ((rev (vc-git--run-command-string nil "rev-parse" "HEAD"))) (and rev (replace-regexp-in-string "\n" "" rev)))))) @@ -115,15 +121,13 @@ the kill ring." (let ((url (concat "https://github.com/" (github-browse-file--relative-url) "/" (cond ((eq major-mode 'magit-status-mode) "tree") + ((member major-mode github-browse-file--magit-commit-link-modes) "commit") (github-browse-file--view-blame "blame") (t "blob")) "/" - (github-browse-file--current-rev) "/" - (github-browse-file--repo-relative-path) - (when anchor (concat "#" anchor))))) - (kill-new url) - (if github-browse-file-visit-url - (browse-url url) - (message "GitHub: %s" url)))) + (github-browse-file--current-rev) "/" + (github-browse-file--repo-relative-path) + (when anchor (concat "#" anchor))))) + (github-browse--save-and-view url))) (defun github-browse-file--anchor-lines () "Calculate anchor from lines in active region or current line @@ -137,10 +141,29 @@ default to current line." (when (eq (char-before (region-end)) ?\n) (cl-decf end)) (if (>= start end) (format "L%d" start) - (format "L%d-%d" start end)))) + (format "L%d-L%d" start end)))) (github-browse-file-show-line-at-point (format "L%d" (line-number-at-pos (point)))))) +(defun github-browse-file--guess-commit () + "Guess the current git commit. +If you are in any magit mode, use `magit-commit-at-point'. +Otherwise, if the region is active, use that. +Otherwse, use `github-browse-file--current-rev'." + (cond + ((and (derived-mode-p 'magit-mode) (magit-commit-at-point)) + (magit-commit-at-point)) + ((region-active-p) + (buffer-substring (region-beginning) (region-end))) + (t (github-browse-file--current-rev)))) + +(defun github-browse--save-and-view (url) + "Save url to kill ring and browse or show the url" + (kill-new url) + (if github-browse-file-visit-url + (browse-url url) + (message "GitHub: %s" url))) + ;;;###autoload (defun github-browse-file (&optional force-master) "Show the GitHub webpage for the current file. The URL for the webpage is @@ -166,5 +189,16 @@ region." (let ((github-browse-file--view-blame t)) (github-browse-file force-master))) +;;;###autoload +(defun github-browse-commit () + "Show the GitHub page for the current commit." + (interactive) + (let* ((commit (github-browse-file--guess-commit)) + (url (concat "https://github.com/" + (github-browse-file--relative-url) + "/commit/" + commit))) + (github-browse--save-and-view url))) + (provide 'github-browse-file) ;;; github-browse-file.el ends here