2023-12-23 20:14:13 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
import sys
|
2023-12-23 20:14:14 -07:00
|
|
|
import os
|
2023-12-23 20:14:13 -07:00
|
|
|
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",
|
|
|
|
)
|
|
|
|
|
2023-12-23 20:14:15 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"--depth",
|
|
|
|
type=int,
|
|
|
|
required=False,
|
|
|
|
help="Max depth to clone",
|
|
|
|
)
|
|
|
|
|
2023-12-23 20:14:13 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"url",
|
|
|
|
type=str,
|
|
|
|
help="HTTP or ssh url to use",
|
|
|
|
)
|
|
|
|
|
2023-12-23 20:14:14 -07:00
|
|
|
parser.add_argument(
|
|
|
|
"out_name",
|
|
|
|
type=str,
|
|
|
|
nargs="?",
|
|
|
|
help="Name to use for the directory",
|
|
|
|
)
|
|
|
|
|
2023-12-23 20:14:13 -07:00
|
|
|
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)
|
|
|
|
|
2023-12-23 20:14:14 -07:00
|
|
|
if "git.sr.ht" not in args.url and not ssh_url.endswith(".git"):
|
2023-12-23 20:14:13 -07:00
|
|
|
ssh_url += ".git"
|
|
|
|
|
2023-12-23 20:14:15 -07:00
|
|
|
if args.depth is not None:
|
|
|
|
git_cmd = ["git", "clone", "--depth", str(args.depth), ssh_url]
|
|
|
|
else:
|
|
|
|
git_cmd = ["git", "clone", ssh_url]
|
2023-12-23 20:14:14 -07:00
|
|
|
|
|
|
|
if args.out_name is not None:
|
|
|
|
git_cmd.append(args.out_name)
|
|
|
|
|
2023-12-23 20:14:14 -07:00
|
|
|
initial_dirs = set(filter(lambda x: os.path.isdir(x), os.listdir()))
|
|
|
|
|
2023-12-23 20:14:14 -07:00
|
|
|
Popen(git_cmd).wait()
|
2023-12-23 20:14:14 -07:00
|
|
|
|
|
|
|
current_dirs = set(filter(lambda x: os.path.isdir(x), os.listdir()))
|
|
|
|
new_dirs = current_dirs - initial_dirs
|
|
|
|
|
|
|
|
if len(new_dirs) == 1:
|
|
|
|
print(f"====\nCloned in `{list(new_dirs)[0]}`")
|