1
0

pkgs/git-show-link: Improve linking to directory

This commit is contained in:
2025-05-25 20:48:07 +02:00
parent efad5a6cbb
commit cddd9b1a1e

View File

@@ -10,16 +10,20 @@ 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: f"https://github.com/{g.username}/{g.project}/tree/{g.branch}/", "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": 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": { "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: f"https://{g.host}/{g.username}/{g.project}/src/branch/{g.branch}/", "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": 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 project: str = None
commit: str = None commit: str = None
branch: str = None branch: str = None
path: str = None file: str = None
dir: str = None
def is_git_repo(): def is_git_repo():
s = subprocess.run(["git", "rev-parse"], capture_output=True, text=True) s = subprocess.run(["git", "rev-parse"], capture_output=True, text=True)
@@ -124,14 +129,19 @@ def main():
path = Path(args.path).absolute() path = Path(args.path).absolute()
path = path.relative_to(git_dir_path) path = path.relative_to(git_dir_path)
path = str(path) if path.is_dir():
path = str(path)
if path == ".": if path == ".":
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: if args.display_branch:
g.branch = r["branch"] g.branch = r["branch"]
print(remote_type["format-branch-file"](g)) print(remote_type["format-branch-file"](g))
@@ -139,6 +149,14 @@ def main():
commit = get_last_commit() commit = get_last_commit()
g.commit = commit g.commit = commit
print(remote_type["format-commit-file"](g)) 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: else:
if args.display_branch: if args.display_branch:
g.branch = r["branch"] g.branch = r["branch"]