Skip to content
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

Closed
wants to merge 7 commits into from
28 changes: 28 additions & 0 deletions magithub.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Owner

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.

Copy link
Owner

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.

Copy link
Contributor Author

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.

(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"
Copy link
Owner

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

Copy link
Contributor Author

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. ;)

(magit-git-string "rev-parse" "--abbrev-ref" "HEAD"))))
Copy link
Owner

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.

Copy link
Contributor Author

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.

(file-relative-path (replace-regexp-in-string
Copy link
Owner

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.

Copy link
Contributor Author

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

(magit-toplevel)
""
(expand-file-name
(if-let ((filepath (buffer-file-name)))
;; Get file relative path and line number if `buffer-file-name' is non-nil.
(format "%s#L%s" filepath (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!")
Expand Down