-
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
Conversation
Open git file in user browser magithub-browse just open git repo magithub-browse-file is location file link in browse.
@vermiculus I write a new function magithub-browse-file , please review code. Thanks for magithub! ;) |
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.
Thanks! I fixed some issues with the test infrastructure that were preventing your PR from being tested. Can you rebase onto |
magithub.el
Outdated
(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 comment
The reason will be displayed to describe this comment to others. Learn more.
Use (let-alist (magithub-repo) …)
instead of this let*
form. Then, you can refer to (alist-get 'html_url repo)
as simply .html_url
. This is the pattern used throughout the rest of magithub.
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.
Done, please review newest version.
magithub.el
Outdated
(user-error "Not a GitHub repository")) | ||
(let* ((repo (magithub-repo)) | ||
(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 comment
The 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., github.com/user/repo/blob/B/file.txt
).
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.
Done, please review newest version.
magithub.el
Outdated
(let* ((repo (magithub-repo)) | ||
(html-url (alist-get 'html_url repo)) | ||
(branch (alist-get 'default_branch repo)) | ||
(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 comment
The 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 /
– you can simplify this into a call to (format …)
(or mapconcat
if you prefer).
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.
Done, please review newest version.
magithub.el
Outdated
(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 comment
The reason will be displayed to describe this comment to others. Learn more.
Use derived-mode-p
.
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.
Done, please review newest version.
This PR will implement requested feature #280. |
Hi, vermiculus This version improve code with: ``` 1. Use magit-get-current-branch fetch current branch instead "master" branch. 2. Use format instead file-name-as-directory since browser always use / as path separator. 3. Use derived-mode-p instead (equal major-mode ...) 4. Use html_url need use alist-get, so not use let-alist. Thanks for tips. ;) 5. Use expand-file-name wrap default-directory, otherwise path that start with ~ will break code. ```
@vermiculus |
magithub.el
Outdated
(interactive) | ||
(unless (magithub-github-repository-p) | ||
(user-error "Not a GitHub repository")) | ||
(let* ((github-branch-path (format "%s/%s/%s/" |
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.
Use let-alist.
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.
Something like this?
(github-branch-path (let-alist (magithub-repo)
(format "%s/%s/%s/"
.html_url
"blob"
(magit-get-current-branch))))
@vermiculus |
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.
it's looking better!
magithub.el
Outdated
(format "%s/%s/%s/" | ||
.html_url | ||
"blob" | ||
(magit-get-current-branch)))) |
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
I found magit-get-current-branch use
(magit-git-string "symbolic-ref" "--short" "HEAD")
I research some resource with Google, do you mean we shoud use below code to fetch currenet local branch?
(magit-git-string "rev-parse" "--abbrev-ref" "HEAD")
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 we need command "git branch" then grep branch name that start with * ?
magithub.el
Outdated
(magit-toplevel) | ||
"" | ||
(expand-file-name | ||
(if (buffer-file-name) |
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.
use if-let
here to avoid calling (buffer-file-name)
twice.
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.
I have changed in my local version
…) get current local branch
@vermiculus
Leave message to me if you have any suggestion, I have to sleep, it's 24:00 in China. ;) |
@vermiculus Newest version is fine? Or any other improve suggestion? |
I have not had the time to review. This could take up to a week or more. Keep in mind I have a full time job and a home life :-) |
I also have full time job. Unfortunately review one line code need a week or more. No intention to offend, just feel that there is no contribution to enthusiasm, a little sad. Anyway, hope you can review and merge my patch when you have time. Have a nice day. ;) |
magithub.el
Outdated
.html_url | ||
"blob" | ||
(magit-git-string "rev-parse" "--abbrev-ref" "HEAD")))) | ||
(file-relative-path (replace-regexp-in-string |
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.
string-remove-prefix
may make more sense if that's what you're trying to do. If (magit-top-level)
contains any regexp-sensitive characters (like [
), this form could crash.
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.
I will use string-remove-prefix instead
magithub.el
Outdated
(format "%s/%s/%s/" | ||
.html_url | ||
"blob" | ||
(magit-git-string "rev-parse" "--abbrev-ref" "HEAD")))) |
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.
This does not resolve the remote-tracking branch problem discussed earlier.
If you get rid of --abbrev-ref
, though, that will return the full hash. That is definitely constant between local and remote, so it could be used instead. It's not perfect, but at least it's safe.
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.
Wow, it's cool, good to know that.
1. Use string-remove-prefix instead replace-regexp-in-string 2. Use (magit-git-string "rev-parse" "HEAD") get branch hash path.
@vermiculus Thanks |
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.
A few things to make the function more usable.
magithub.el
Outdated
(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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand how to improve this line with your suggestion. ;)
magithub.el
Outdated
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) |
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):
(interactive
(list
;; provide a default if we're in a git repository
(when-let ((toplevel (magit-toplevel)))
;; remove the directory-part -- just get the path
;; relative to topdir
(string-remove-prefix
toplevel
(let (filepath)
(cond
;; the current buffer is attached to a file; use that
((setq filepath (buffer-file-name))
(format "%s#L%s" filepath (line-number-at-pos)))
;; we're in dired;use the file at point
((derived-mode-p 'dired-mode)
(dired-file-name-at-point))
;; dunno; use the current directory
(t default-directory)))))))
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.
1. Support argument file and line now, if current git repo is A, we can browse file even it in git repo B 2. Browse git repo's homepage if relative path is empty. 3. Move format code out from let binding.
I upload new version that support argument file and line. Please let me know if you have any suggestions. Thanks! -- Andy |
Looks good. I'm curious though: why do you think it's ugly? It's idiomatic. I've opened bbatsov/emacs-lisp-style-guide#51 for discussion and guidance. |
Haha, It's personal style. I prefer use &optional style making the first impression of program feels very simple, It will make other people feels complicated if we put everything in interactive expression. BTW, it's time to merge ? haha. |
I don't agree with your reasoning, but I'll agree that it's good enough to merge. I may change my mind about the Please squash your commits into one. |
Hmm, can't you just click "Merged PR" button in github ? ;) |
I still prefer to merge only one squashed commit to keep the history clean and understandable. |
@vermiculus Please review #377 I close this PR now, thanks! |
In the future, force-push to the pull request branch instead. You do not need to create a new pull request. |
Open git file in user browser
magithub-browse just open git repo
magithub-browse-file is location file link in browser.