feat: add '--ssh' flag to update-ref tool to choose ssh url or default to https url #27

Merged
hdh20267 merged 1 commits from update-ref-https into main 2026-01-07 00:03:46 +00:00

View File

@@ -16,7 +16,7 @@
update-ref [-R PATH|--athenix-repo=PATH] [-b BRANCH|--athenix-branch=BRANCH]
[-m "msg"|--message "msg"]
[-p[=false] [remote[=URL]]|--push[=false] [remote[=URL]]]
[--make-local|-l] [--make-remote|-r]
[--make-local|-l] [--make-remote|-r] [--ssh]
user=<username> | system=<device-type>:<hostkey>
EOF
exit 2
@@ -60,16 +60,39 @@
extract_existing_fetch_url() {
# args: mode file username key
python3 - "$1" "$2" "$3" "$4" <<'PY'
python3 - "$1" "$2" "$3" "$4" "$5"<<'PY'
import sys, re, pathlib
mode, file, username, key = sys.argv[1:5]
mode, file, username, key, use_ssh = sys.argv[1:5]
t = pathlib.Path(file).read_text()
def url_from_block(block: str) -> str:
if not block:
return ""
m = re.search(r'url\s*=\s*"([^"]+)"\s*;', block)
return m.group(1) if m else ""
url = m.group(1) if m else ""
if use_ssh = "true":
return url
# Already https
if url.startswith("https://"):
return url
# ssh://git@host/org/repo.git
m = re.match(r"ssh://(?:.+?)@([^/]+)/(.+)", url)
if m:
host, path = m.groups()
return f"https://{host}/{path}"
# git@host:org/repo.git
m = re.match(r"(?:.+?)@([^:]+):(.+)", url)
if m:
host, path = m.groups()
return f"https://{host}/{path}"
# If you gave me something cursed
raise ValueError(f"Unrecognized SSH git URL format: {url}")
if mode == "user":
m = re.search(r'(?s)\n\s*' + re.escape(username) + r'\.external\s*=\s*builtins\.fetchGit\s*\{(.*?)\n\s*\};', t)
@@ -154,6 +177,7 @@
--make-local|-l) MODE_FORCE="local"; shift ;;
--make-remote|-r) MODE_FORCE="remote"; shift ;;
--ssh) USE_SSH="true"; shift ;;
-h|--help) usage ;;
*) die "Unknown argument: $1" ;;
esac
@@ -214,7 +238,7 @@
EXISTING_URL=""
ENTRY_EXISTS=0
if [ "$MODE" = "user" ]; then
EXISTING_URL="$(extract_existing_fetch_url user "$FILE" "$USERNAME" "")"
EXISTING_URL="$(extract_existing_fetch_url user "$FILE" "$USERNAME" "" "false")"
[ -n "$EXISTING_URL" ] && ENTRY_EXISTS=1 || true
else
FULL="$(derive_full_hostname "$DEVTYPE" "$HOSTKEY")"