From d334a1a73ca6cb2f817399100f4f8c2c307d2f8a Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 25 May 2025 19:59:59 +0200 Subject: [PATCH] pkgs/git-show-link: Link to files directly --- pkgs/git-show-link/git-show-link.py | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/pkgs/git-show-link/git-show-link.py b/pkgs/git-show-link/git-show-link.py index e7e78e6..d542d33 100755 --- a/pkgs/git-show-link/git-show-link.py +++ b/pkgs/git-show-link/git-show-link.py @@ -9,12 +9,16 @@ REMOTE_TYPES = { "github": { "match": re.compile(r'git@github.com:(?P[\w\.-]+)/(?P[\w\.-]+).git'), "format-branch": lambda g: f"https://github.com/{g.username}/{g.project}/tree/{g.branch}/", + "format-branch-file": lambda g: f"https://github.com/{g.username}/{g.project}/blob/{g.branch}/{g.path}", "format-commit": lambda g: f"https://github.com/{g.username}/{g.project}/commit/{g.commit}/", + "format-commit-file": lambda g: f"https://github.com/{g.username}/{g.project}/blob/{g.commit}/{g.path}", }, "gitea": { "match": re.compile(r'(?P[\w\.-]+)@(?P[\w\.-]+):(?P[\w\.-]+)/(?P[\w\.-]+).git'), "format-branch": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/", + "format-branch-file": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/{g.path}", "format-commit": lambda g: f"https://{g.host}/{g.username}/{g.project}/commit/{g.commit}/", + "format-commit-file": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/commit/{g.commit}/{g.path}", }, } @@ -26,6 +30,7 @@ class FormatArgs: project: str = None commit: str = None branch: str = None + path: str = None def is_git_repo(): s = subprocess.run(["git", "rev-parse"], capture_output=True, text=True) @@ -70,6 +75,7 @@ def main(): prog='git-show-link', ) + parser.add_argument("path", nargs="?", default=None, help="Path to link to specific file or directory") parser.add_argument("--branch", dest="display_branch", action='store_true', help="Display link to branch, instead to commit") parser.add_argument("--remote-type", dest="remote_type", choices=REMOTE_TYPES.keys(), help="Specify remote type") @@ -102,14 +108,25 @@ def main(): remote_type_found = True g = FormatArgs(**m.groupdict()) + g.path = args.path - if args.display_branch: - g.branch = r["branch"] - print(remote_type["format-branch"](g)) + if args.path is not None: + if args.display_branch: + g.branch = r["branch"] + print(remote_type["format-branch-file"](g)) + else: + commit = get_last_commit() + g.commit = commit + print(remote_type["format-commit-file"](g)) else: - commit = get_last_commit() - g.commit = commit - print(remote_type["format-commit"](g)) + if args.display_branch: + g.branch = r["branch"] + print(remote_type["format-branch"](g)) + else: + commit = get_last_commit() + g.commit = commit + print(remote_type["format-commit"](g)) + break if not remote_type_found: