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
23 changes: 23 additions & 0 deletions magithub.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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* ((repo (magithub-repo))
Copy link
Owner

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.

Copy link
Contributor Author

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.

(html-url (alist-get 'html_url repo))
(branch (alist-get 'default_branch repo))
Copy link
Owner

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

Copy link
Contributor Author

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.

(github-branch-path (apply 'concat (mapcar 'file-name-as-directory (list html-url "blob" branch))))
Copy link
Owner

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

Copy link
Contributor Author

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.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use derived-mode-p.

Copy link
Contributor Author

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.

;; 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!")
Expand Down