From ad4ba302c0ed1741fe0d7ccf0026682644a52743 Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Fri, 17 Mar 2023 13:57:57 -0600 Subject: [PATCH] New: git clone ssh script --- bin/clone_with_ssh.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100755 bin/clone_with_ssh.py diff --git a/bin/clone_with_ssh.py b/bin/clone_with_ssh.py new file mode 100755 index 0000000..2fb9179 --- /dev/null +++ b/bin/clone_with_ssh.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +import argparse +import re +import sys +from subprocess import Popen + +SSH_RE = re.compile(r"(ssh://)?git@([^/]+):([^/]+)/([^/]+)(/[^/]+)?.*") +HTTP_RE = re.compile(r"https://([^/]+)/([^/]+)/([^/]+).*") + +parser = argparse.ArgumentParser( + prog="Clone with SSH v1.0.0", + description="Git-clone an http link as ssh instead", +) + +parser.add_argument( + "url", + type=str, + help="HTTP or ssh url to use", +) + +args = parser.parse_args() + +if m := HTTP_RE.fullmatch(args.url): + ssh_url = f"ssh://git@{m[1]}:22/{m[2]}/{m[3]}" +elif m := SSH_RE.fullmatch(args.url): + ssh_url = f"ssh://git@{m[2]}:22/{m[3]}/{m[4]}" +else: + print(f"Failed to parse URL `{args.url}`", file=sys.stderr) + exit(1) + +if "git.sr.ht" not in args.url and not ssh_url.endswith('.git'): + ssh_url += ".git" + +Popen(["git", "clone", ssh_url]).wait()