1
0

pkgs/git-show-link: Link to files directly

This commit is contained in:
2025-05-25 19:59:59 +02:00
parent 4fa4c8d669
commit d334a1a73c

View File

@@ -9,12 +9,16 @@ REMOTE_TYPES = {
"github": {
"match": re.compile(r'git@github.com:(?P<username>[\w\.-]+)/(?P<project>[\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<gituser>[\w\.-]+)@(?P<host>[\w\.-]+):(?P<username>[\w\.-]+)/(?P<project>[\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: