1
0

pkgs/git-show-link: Pass format args as dataclass

This commit is contained in:
clerie 2025-04-07 17:16:00 +02:00
parent f9359f4d50
commit 98b4cde2e4

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