From cddd9b1a1e24f22c42c43b6101d43944574a2bcb Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 25 May 2025 20:48:07 +0200 Subject: [PATCH] pkgs/git-show-link: Improve linking to directory --- pkgs/git-show-link/git-show-link.py | 38 +++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/pkgs/git-show-link/git-show-link.py b/pkgs/git-show-link/git-show-link.py index 3ffaea3..ed4cad6 100755 --- a/pkgs/git-show-link/git-show-link.py +++ b/pkgs/git-show-link/git-show-link.py @@ -10,16 +10,20 @@ 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-branch-file": lambda g: f"https://github.com/{g.username}/{g.project}/blob/{g.branch}/{g.file}", + "format-branch-dir": lambda g: f"https://github.com/{g.username}/{g.project}/tree/{g.branch}/{g.dir}", "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}", + "format-commit-file": lambda g: f"https://github.com/{g.username}/{g.project}/blob/{g.commit}/{g.file}", + "format-commit-dir": lambda g: f"https://github.com/{g.username}/{g.project}/tree/{g.commit}/{g.dir}", }, "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-branch-file": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/{g.file}", + "format-branch-dir": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/{g.dir}", "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}", + "format-commit-file": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/commit/{g.commit}/{g.file}", + "format-commit-dir": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/commit/{g.commit}/{g.dir}", }, } @@ -31,7 +35,8 @@ class FormatArgs: project: str = None commit: str = None branch: str = None - path: str = None + file: str = None + dir: str = None def is_git_repo(): s = subprocess.run(["git", "rev-parse"], capture_output=True, text=True) @@ -124,14 +129,19 @@ def main(): path = Path(args.path).absolute() path = path.relative_to(git_dir_path) - path = str(path) + if path.is_dir(): + path = str(path) - if path == ".": - path = "" + if path == ".": + path = "" + else: + path += "/" - g.path = path + g.dir = path + else: + g.file = str(path) - if args.path is not None: + if g.file is not None: if args.display_branch: g.branch = r["branch"] print(remote_type["format-branch-file"](g)) @@ -139,6 +149,14 @@ def main(): commit = get_last_commit() g.commit = commit print(remote_type["format-commit-file"](g)) + elif g.dir is not None: + if args.display_branch: + g.branch = r["branch"] + print(remote_type["format-branch-dir"](g)) + else: + commit = get_last_commit() + g.commit = commit + print(remote_type["format-commit-dir"](g)) else: if args.display_branch: g.branch = r["branch"]