-
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 4 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,34 @@ | |
(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* ((github-branch-path (let-alist (magithub-repo) | ||
(format "%s/%s/%s/" | ||
.html_url | ||
"blob" | ||
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. you can incorporate this literal value directly into the format string 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 don't understand how to improve this line with your suggestion. ;) |
||
(magit-get-current-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. This does not necessarily return the name of the remote branch. This is a hard problem, though; I can work on it today unless you feel up to the challenge. See also #358 (comment) 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 found magit-get-current-branch use
I research some resource with Google, do you mean we shoud use below code to fetch currenet local 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. Or we need command "git branch" then grep branch name that start with * ? |
||
(file-relative-path (replace-regexp-in-string | ||
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.
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 will use string-remove-prefix instead |
||
(magit-toplevel) | ||
"" | ||
(expand-file-name | ||
(if (buffer-file-name) | ||
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. I have changed in my local version |
||
;; Get file relative path and line number if `buffer-file-name' is non-nil. | ||
(format "%s#L%s" (buffer-file-name) (line-number-at-pos)) | ||
(if (derived-mode-p 'dired-mode) | ||
;; Get file relative path if in `dired-mode' | ||
(dired-file-name-at-point) | ||
;; Otherwise, get current directory as relative path | ||
default-directory)))))) | ||
(browse-url (concat github-branch-path 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.