From 98b4cde2e45c8b8f76e4b0638aab4f3e12680559 Mon Sep 17 00:00:00 2001 From: clerie <git@clerie.de> Date: Mon, 7 Apr 2025 17:16:00 +0200 Subject: [PATCH] pkgs/git-show-link: Pass format args as dataclass --- pkgs/git-show-link/git-show-link.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/pkgs/git-show-link/git-show-link.py b/pkgs/git-show-link/git-show-link.py index 0364cd3..3ed4981 100755 --- a/pkgs/git-show-link/git-show-link.py +++ b/pkgs/git-show-link/git-show-link.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import argparse +from dataclasses import dataclass import re import subprocess @@ -8,17 +9,26 @@ REMOTE_TYPES = [ { # github "match": re.compile(r'git@github.com:(?P<username>[\w\.-]+)/(?P<project>[\w\.-]+).git'), - "format-branch": lambda g, b: f"https://github.com/{g['username']}/{g['project']}/tree/{b}/", - "format-commit": lambda g, c: f"https://github.com/{g['username']}/{g['project']}/commit/{c}/", + "format-branch": lambda g: f"https://github.com/{g.username}/{g.project}/tree/{g.branch}/", + "format-commit": lambda g: f"https://github.com/{g.username}/{g.project}/commit/{g.commit}/", }, { # gitea "match": re.compile(r'(?P<gituser>[\w\.-]+)@(?P<host>[\w\.-]+):(?P<username>[\w\.-]+)/(?P<project>[\w\.-]+).git'), - "format-branch": lambda g, b: f"https://{g['host']}/{g['username']}/{g['project']}/src/branch/{b}/", - "format-commit": lambda g, c: f"https://{g['host']}/{g['username']}/{g['project']}/commit/{c}/", + "format-branch": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/", + "format-commit": lambda g: f"https://{g.host}/{g.username}/{g.project}/commit/{g.commit}/", }, ] +@dataclass +class FormatArgs: + gituser: str = None + host: str = None + username: str = None + project: str = None + commit: str = None + branch: str = None + def get_remote_branch(): s = subprocess.run(["git", "status", "--porcelain", "-uno", "-b", "--no-ahead-behind"], capture_output=True, text=True) @@ -71,13 +81,15 @@ def main(): if m is None: continue - g = m.groupdict() + g = FormatArgs(**m.groupdict()) if args.display_branch: - print(remote_type["format-branch"](g, r["branch"])) + g.branch = r["branch"] + print(remote_type["format-branch"](g)) else: commit = get_last_commit() - print(remote_type["format-commit"](g, commit)) + g.commit = commit + print(remote_type["format-commit"](g)) break