pkgs/git-show-link: Normalize paths
This commit is contained in:
@@ -4,6 +4,7 @@ import argparse
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
REMOTE_TYPES = {
|
REMOTE_TYPES = {
|
||||||
"github": {
|
"github": {
|
||||||
@@ -37,9 +38,17 @@ def is_git_repo():
|
|||||||
|
|
||||||
return s.returncode == 0
|
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():
|
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)
|
||||||
|
|
||||||
|
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]
|
git_status_branch_info = s.stdout.splitlines()[0][3:].split()[0]
|
||||||
|
|
||||||
branches = git_status_branch_info.split("...")
|
branches = git_status_branch_info.split("...")
|
||||||
@@ -86,6 +95,8 @@ def main():
|
|||||||
|
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
git_dir_path = get_git_dir()
|
||||||
|
|
||||||
r = get_remote_branch()
|
r = get_remote_branch()
|
||||||
|
|
||||||
remote_url = get_remote_url(r["remote"])
|
remote_url = get_remote_url(r["remote"])
|
||||||
@@ -108,7 +119,17 @@ def main():
|
|||||||
remote_type_found = True
|
remote_type_found = True
|
||||||
|
|
||||||
g = FormatArgs(**m.groupdict())
|
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.path is not None:
|
||||||
if args.display_branch:
|
if args.display_branch:
|
||||||
|
Reference in New Issue
Block a user