-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new function magithub-browse-file #373
Changes from 2 commits
dba4ebe
aac9e82
d8998d9
dac7894
8737949
946c331
5a95627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,6 +89,29 @@ | |
(user-error "Not a GitHub repository")) | ||
(magithub-repo-visit (magithub-repo))) | ||
|
||
(defun magithub-browse-file () | ||
"Open the git file in your browser. | ||
|
||
Jump to file with line number if you in buffer of git file. | ||
Jump to file if you in dired mode. | ||
Jump to parent directory if you not in buffer with non-dired mode." | ||
(interactive) | ||
(unless (magithub-github-repository-p) | ||
(user-error "Not a GitHub repository")) | ||
(let* ((repo (magithub-repo)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, please review newest version. |
||
(html-url (alist-get 'html_url repo)) | ||
(branch (alist-get 'default_branch repo)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we're on a local branch that is not the default branch? If I'm on some branch B, presumably we should browse that file as it exists on branch B (i.e., There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, please review newest version. |
||
(github-branch-path (apply 'concat (mapcar 'file-name-as-directory (list html-url "blob" branch)))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can safely assume the directory separator that browsers will respect will be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, please review newest version. |
||
(file-relative-path (if (buffer-file-name) | ||
;; Get file relative path and line number if `buffer-file-name' is non-nil. | ||
(concat (buffer-file-name) "#L" (prin1-to-string (line-number-at-pos))) | ||
(if (equal major-mode 'dired-mode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, please review newest version. |
||
;; Get file relative path if in `dired-mode' | ||
(expand-file-name (dired-file-name-at-point)) | ||
;; Otherwise, get current directory as relative path | ||
default-directory)))) | ||
(browse-url (concat github-branch-path (replace-regexp-in-string (magit-toplevel) "" file-relative-path))))) | ||
|
||
(defvar magithub-after-create-messages | ||
'("Don't be shy!" | ||
"Don't let your dreams be dreams!") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
(interactive)
form can give us a value for a formal parameter when used interactively. That way,magithub-browse-file
could take a file directly (e.g.,(magithub-browse-file "some/path/relative/to/topdir.txt")
).Your interactive form could look like this (untested):
Note this moves a lot of the logic out of the main body of the function -- logic that's for interactive use only, anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or better yet, split out the arguments into FILE and LINE.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's ugly that put everything in interactive, I will write new version to handle file and line argument.