forge icon indicating copy to clipboard operation
forge copied to clipboard

Browse file on remote

Open vermiculus opened this issue 5 years ago • 17 comments

Optionally with selected region highlighted.

vermiculus/magithub#400

vermiculus avatar Jan 26 '19 06:01 vermiculus

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

vermiculus avatar Jan 26 '19 06:01 vermiculus

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 avatar Jan 28 '19 15:01 tarsius

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.

tarsius avatar Jan 28 '19 15:01 tarsius

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

vermiculus avatar Jan 29 '19 01:01 vermiculus

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

Linuus avatar Feb 11 '20 18:02 Linuus

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

achempion avatar Mar 20 '20 12:03 achempion

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

Linuus avatar Mar 20 '20 16:03 Linuus

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.

fredefox avatar Aug 31 '20 14:08 fredefox

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

srcrip avatar Sep 21 '20 19:09 srcrip

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)

tarsius avatar Sep 23 '20 15:09 tarsius

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.

raszi avatar Nov 26 '20 19:11 raszi

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.

fredefox avatar Nov 26 '20 20:11 fredefox

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

bezirg avatar Dec 01 '20 15:12 bezirg

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 avatar Dec 02 '20 09:12 raszi

I've created at PR #317

raszi avatar Dec 08 '20 18:12 raszi

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

umanwizard avatar Jan 03 '22 01:01 umanwizard

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

fredefox avatar Jan 07 '22 11:01 fredefox