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

Browse file on remote #91

Open
vermiculus opened this issue Jan 26, 2019 · 17 comments
Open

Browse file on remote #91

vermiculus opened this issue Jan 26, 2019 · 17 comments
Labels
area: links enhancement New feature or request

Comments

@vermiculus
Copy link
Contributor

vermiculus commented Jan 26, 2019

Optionally with selected region highlighted.

vermiculus/magithub#400

@vermiculus
Copy link
Contributor Author

This will probably involve a new slot (or three) in forge-repository:

  • base URL of the file at a commit / on a branch
  • above + anchor line
  • above + start & end anchor lines

@tarsius
Copy link
Member

tarsius commented Jan 28, 2019

Browse file on remote

Yes, though I don't think that's urgent. I prefer viewing files in
Emacs :-)

While I would probably not "browse" files much, I would still like
to be able to quickly get the corresponding url so that I can paste
them somewhere. Luckily you have added support for that recently ;-)

This will probably involve a new slot (or three) in forge-repository

But those will just be class-allocated *-format slots right? So not
a big deal.

@tarsius tarsius added the enhancement New feature or request label Jan 28, 2019
@tarsius
Copy link
Member

tarsius commented Jan 28, 2019

Oh, and feel free to add issues such as this one, but please add the enhancement label right away.

Also if you do intend on something yourself, then assign it to yourself. Hm, you probably are not allowed to do that yet. Time to give you some permissions.

@vermiculus
Copy link
Contributor Author

Hm, you probably are not allowed to do that yet. Time to give you some permissions.

I would like that :-) I'm normally very diligent about labeling issues I create (at least in my repositories).

@Linuus
Copy link

Linuus commented Feb 11, 2020

Any status on this one? Would be a really great feature!

@achempion
Copy link
Contributor

There is a package with the same name which does the same thing https://github.com/rmuslimov/browse-at-remote.

@Linuus
Copy link

Linuus commented Mar 20, 2020

There is a package with the same name which does the same thing https://github.com/rmuslimov/browse-at-remote.

Yeah I know. There are multiple, I am using git-link myself: https://github.com/sshaw/git-link

It would just be nice to have it in forge/magit instead :)

@fredefox
Copy link

I created a snippet for this. I took inspiration for how forge-browse-commit was implemented. Here's what I came up with:

(defun forge-browse-buffer-file ()
  (interactive
   (browse-url
    (let
        ((rev (magit-get-current-branch))
         (repo (forge-get-repository 'stub))
         (file (file-relative-name buffer-file-name (projectile-project-root))))
      (forge--format repo "https://%h/%o/%n/blob/%r/%f"
                     `((?r . ,rev) (?f . ,file)))))))

It requires projectile for figuring out the project-relative path of currently visited file.

@srcrip
Copy link

srcrip commented Sep 21, 2020

@fredefox's implementation is cool but projectile-project-root is a little janky for me. It would be nice if magit just kept a variable referencing the relative path (from the base git directory) to the current buffer. I would think it wouldn't be hard, but my elisp-fu kinda sucks.

@tarsius
Copy link
Member

tarsius commented Sep 23, 2020

It would be nice if magit just kept a variable referencing the relative path (from the base git directory) to the current buffer.

(magit-file-relative-name buffer-file-name)

@raszi
Copy link

raszi commented Nov 26, 2020

This feature would be really useful if you would like to send over a reference to a file or a highlighted couple of lines to somebody over chat or email.

@fredefox
Copy link

fredefox commented Nov 26, 2020

This feature would be really useful if you would like to send over a reference to a file or a highlighted couple of lines to somebody over chat or email.

I suppose you could combine the snippet @tarsius shared with the snippet I wrote replacing browse-url with kill-new.

@bezirg
Copy link

bezirg commented Dec 1, 2020

This feature would be really useful if you would like to send over a reference to a file or a highlighted couple of lines to somebody over chat or email.

I suppose you could combine the snippet @tarsius shared with the snippet I wrote replacing browse-url with kill-new.

(defun forge-browse-buffer-file ()
  (interactive)
  (kill-new
    (let
        ((rev (magit-get-current-branch))
         (repo (forge-get-repository 'stub))
         (file (magit-file-relative-name buffer-file-name)))
      (forge--format repo "https://%h/%o/%n/blob/%r/%f"
                     `((?r . ,rev) (?f . ,file))))))

@raszi
Copy link

raszi commented Dec 2, 2020

Thanks @bezirg I could understand and combine the two solutions. Actually I am working on a PR to add this functionality with line handling as well.

The solution is working so far but I am struggling with this issue.

@raszi
Copy link

raszi commented Dec 8, 2020

I've created at PR #317

@umanwizard
Copy link

This version extends @fredefox 's to use the revision hash, rather than the branch name (so that the link is permanent), and adds highlighting from the region if applicable. (Only tested on GitHub so far)

(defun btv-forge-browse-buffer-file ()
  (interactive)
  (browse-url
   (let
       ((rev (magit-rev-abbrev "HEAD"))
        (repo (forge-get-repository 'stub))
        (file (magit-file-relative-name buffer-file-name))
        (highlight
         (if
             (use-region-p)
             (let ((l1 (line-number-at-pos (region-beginning)))
                   (l2 (line-number-at-pos (- (region-end) 1))))
               (format "#L%d-L%d" l1 l2))
           ""
           )))
     (forge--format repo "https://%h/%o/%n/blob/%r/%f%L"
                    `((?r . ,rev) (?f . ,file) (?L . ,highlight))))))

@fredefox
Copy link

fredefox commented Jan 7, 2022

That's a very nice addition. It's nice to see people share their creativity. I would probably replace magit-rev-abbrev with magit-rev-parse. Also I would suggest defining it as a helper forge--buffer-file so you can do e.g.:

(defun forge-browse-buffer-file ()
  (interactive)
  (browse-url (forge--buffer-file)))

(defun forge-buffer-file-as-kill ()
  (interactive)
  (kill-new (forge--buffer-file)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: links enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

9 participants