Skip to content

Commit

Permalink
Merge pull request #1957 from timbrel/fix-1854
Browse files Browse the repository at this point in the history
Add "github: copy file url to clipboard"
  • Loading branch information
kaste authored Nov 27, 2024
2 parents 13cbaea + 559a8aa commit 64b21db
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
13 changes: 9 additions & 4 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@
"command": "gs_graph_pickaxe"
},
{
"caption": "github: open file on remote",
"command": "gs_github_open_file_on_remote",
"caption": "github: open file in browser",
"command": "gs_github_open_file_in_browser",
"args": { "preselect": true }
},
{
"caption": "github: copy file url to clipboard",
"command": "gs_github_copy_file_url",
"args": { "preselect": true }
},
{
Expand Down Expand Up @@ -154,8 +159,8 @@
"command": "gs_github_create_pull_request"
},
{
"caption": "gitlab: open file on remote",
"command": "gs_gitlab_open_file_on_remote",
"caption": "gitlab: open file in browser",
"command": "gs_gitlab_open_file_in_browser",
"args": { "preselect": true }
},
{
Expand Down
2 changes: 1 addition & 1 deletion core/interfaces/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def run(self, edit):
# type: (sublime.Edit) -> None
file_paths = self.get_selected_subjects('staged', 'unstaged', 'merge-conflicts')
if file_paths:
self.view.run_command("gs_github_open_file_on_remote", {"fpath": file_paths})
self.view.run_command("gs_github_open_file_in_browser", {"fpath": file_paths})


class gs_status_diff_inline(StatusInterfaceCommand):
Expand Down
40 changes: 34 additions & 6 deletions github/commands/open_on_remote.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from __future__ import annotations

from webbrowser import open as open_in_browser

import sublime

from ..github import open_file_in_browser, open_issues, open_repo
from ..github import construct_github_file_url, open_issues, open_repo

from .. import git_mixins
from ...core.ui_mixins.quick_panel import show_remote_panel
from ...core.utils import flash
from ...core.utils import flash, hprint
from GitSavvy.core.base_commands import GsTextCommand
from GitSavvy.core.runtime import on_worker


__all__ = (
"gs_github_open_file_on_remote",
"gs_github_open_file_in_browser",
"gs_github_copy_file_url",
"gs_github_open_repo",
"gs_github_open_issues",
)
Expand All @@ -20,7 +25,7 @@
"Open the file {} before?")


class gs_github_open_file_on_remote(GsTextCommand, git_mixins.GithubRemotesMixin):
class GsGithubFileUrlMixin(GsTextCommand, git_mixins.GithubRemotesMixin):

"""
Open a new browser window to the web-version of the currently opened
Expand Down Expand Up @@ -105,14 +110,37 @@ def open_file_on_remote(self, remote):
start_line = self.adjust_line_according_to_diff(diff, start_line)
end_line = self.adjust_line_according_to_diff(diff, end_line)

for p in fpath:
open_file_in_browser(
urls = [
construct_github_file_url(
p,
remote_url,
commit_hash,
start_line=start_line,
end_line=end_line
)
for p in fpath
]
self.computed_urls(urls)

def computed_urls(self, urls: list[str]) -> None:
raise NotImplementedError


class gs_github_open_file_in_browser(GsGithubFileUrlMixin):
def computed_urls(self, urls: list[str]) -> None:
for url in urls:
open_in_browser(url)


class gs_github_copy_file_url(GsGithubFileUrlMixin):
def computed_urls(self, urls: list[str]) -> None:
if len(urls) > 1:
hprint("Multiple files selected, only copying the first URL")

for url in urls:
sublime.set_clipboard(url)
flash(self.view, f"Copied '{url}' to the clipboard")
break


class gs_github_open_repo(GsTextCommand, git_mixins.GithubRemotesMixin):
Expand Down
6 changes: 2 additions & 4 deletions github/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,20 @@ def parse_remote(remote_url: str) -> GitHubRepo:
return GitHubRepo(url, fqdn, owner, repo, token)


def open_file_in_browser(rel_path, remote_url, commit_hash, start_line=None, end_line=None):
def construct_github_file_url(rel_path, remote_url, commit_hash, start_line=None, end_line=None) -> str:
"""
Open the URL corresponding to the provided `rel_path` on `remote_url`.
"""
github_repo = parse_remote(remote_url)
line_numbers = "#L{}-L{}".format(start_line, end_line) if start_line is not None else ""

url = "{repo_url}/blob/{commit_hash}/{path}{lines}".format(
return "{repo_url}/blob/{commit_hash}/{path}{lines}".format(
repo_url=github_repo.url,
commit_hash=commit_hash,
path=rel_path,
lines=line_numbers
)

open_in_browser(url)


def open_repo(remote_url):
"""
Expand Down
4 changes: 2 additions & 2 deletions gitlab/commands/open_on_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


__all__ = (
"gs_gitlab_open_file_on_remote",
"gs_gitlab_open_file_in_browser",
"gs_gitlab_open_repo",
"gs_gitlab_open_issues",
)
Expand All @@ -18,7 +18,7 @@
"Open the file {} before?")


class gs_gitlab_open_file_on_remote(GsTextCommand, GitLabRemotesMixin):
class gs_gitlab_open_file_in_browser(GsTextCommand, GitLabRemotesMixin):

"""
Open a new browser window to the web-version of the currently opened
Expand Down

0 comments on commit 64b21db

Please sign in to comment.