From efad5a6cbb7e9419514d9c35983a917fd1575372 Mon Sep 17 00:00:00 2001 From: clerie Date: Sun, 25 May 2025 20:21:51 +0200 Subject: [PATCH] pkgs/git-show-link: Normalize paths --- pkgs/git-show-link/git-show-link.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pkgs/git-show-link/git-show-link.py b/pkgs/git-show-link/git-show-link.py index d542d33..3ffaea3 100755 --- a/pkgs/git-show-link/git-show-link.py +++ b/pkgs/git-show-link/git-show-link.py @@ -4,6 +4,7 @@ import argparse from dataclasses import dataclass import re import subprocess +from pathlib import Path REMOTE_TYPES = { "github": { @@ -37,9 +38,17 @@ def is_git_repo(): return s.returncode == 0 +def get_git_dir(): + s = subprocess.run(["git", "rev-parse", "--show-toplevel"], capture_output=True, text=True) + return Path(s.stdout.strip()) + def get_remote_branch(): s = subprocess.run(["git", "status", "--porcelain", "-uno", "-b", "--no-ahead-behind"], capture_output=True, text=True) + if s.stdout.startswith("## HEAD (no branch)"): + print("Detached head, can't link") + exit(1) + git_status_branch_info = s.stdout.splitlines()[0][3:].split()[0] branches = git_status_branch_info.split("...") @@ -86,6 +95,8 @@ def main(): exit(1) + git_dir_path = get_git_dir() + r = get_remote_branch() remote_url = get_remote_url(r["remote"]) @@ -108,7 +119,17 @@ def main(): remote_type_found = True g = FormatArgs(**m.groupdict()) - g.path = args.path + + if args.path is not None: + path = Path(args.path).absolute() + path = path.relative_to(git_dir_path) + + path = str(path) + + if path == ".": + path = "" + + g.path = path if args.path is not None: if args.display_branch: