From cca3e39af0685d7a68878110ad2d668359213c67 Mon Sep 17 00:00:00 2001 From: Hunter Halloran Date: Wed, 31 Dec 2025 16:09:34 -0500 Subject: [PATCH] feat: add '--ssh' flag to update-ref tool to choose ssh url or default to https url --- sw/update-ref.nix | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/sw/update-ref.nix b/sw/update-ref.nix index 19f5009..8cd0770 100644 --- a/sw/update-ref.nix +++ b/sw/update-ref.nix @@ -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= | system=: 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")" -- 2.39.5