pkgs/git-show-link: Pass format args as dataclass
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
from dataclasses import dataclass
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
@ -8,17 +9,26 @@ REMOTE_TYPES = [
|
|||||||
{
|
{
|
||||||
# github
|
# github
|
||||||
"match": re.compile(r'git@github.com:(?P<username>[\w\.-]+)/(?P<project>[\w\.-]+).git'),
|
"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-branch": lambda g: f"https://github.com/{g.username}/{g.project}/tree/{g.branch}/",
|
||||||
"format-commit": lambda g, c: f"https://github.com/{g['username']}/{g['project']}/commit/{c}/",
|
"format-commit": lambda g: f"https://github.com/{g.username}/{g.project}/commit/{g.commit}/",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
# gitea
|
# gitea
|
||||||
"match": re.compile(r'(?P<gituser>[\w\.-]+)@(?P<host>[\w\.-]+):(?P<username>[\w\.-]+)/(?P<project>[\w\.-]+).git'),
|
"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-branch": lambda g: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/",
|
||||||
"format-commit": lambda g, c: f"https://{g['host']}/{g['username']}/{g['project']}/commit/{c}/",
|
"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():
|
def get_remote_branch():
|
||||||
s = subprocess.run(["git", "status", "--porcelain", "-uno", "-b", "--no-ahead-behind"], capture_output=True, text=True)
|
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:
|
if m is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
g = m.groupdict()
|
g = FormatArgs(**m.groupdict())
|
||||||
|
|
||||||
if args.display_branch:
|
if args.display_branch:
|
||||||
print(remote_type["format-branch"](g, r["branch"]))
|
g.branch = r["branch"]
|
||||||
|
print(remote_type["format-branch"](g))
|
||||||
else:
|
else:
|
||||||
commit = get_last_commit()
|
commit = get_last_commit()
|
||||||
print(remote_type["format-commit"](g, commit))
|
g.commit = commit
|
||||||
|
print(remote_type["format-commit"](g))
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user