#!/usr/bin/env python3 """Normalize Dandaedre submodule URLs for CI and local SSH access.""" import argparse from pathlib import Path import re import subprocess import sys from urllib.parse import urlsplit HTTPS_BASE = "https://proxy.dandaedre.com/gitea/" SSH_BASE = "git@git.dandaedre.com:" HOSTS = {"dandaedre.com", "git.dandaedre.com", "proxy.dandaedre.com"} def repository_path(url): """Return the repository path for a recognized absolute URL, or None.""" if "://" in url: parsed = urlsplit(url) if parsed.scheme not in {"http", "https", "ssh", "git"}: return None host = parsed.hostname if host not in HOSTS or parsed.query or parsed.fragment: return None path = parsed.path.lstrip("/") else: match = re.fullmatch(r"(?:[^@/:]+@)?([^/:]+):(.+)", url) if not match or match[1].lower() not in HOSTS: return None host, path = match[1].lower(), match[2].lstrip("/") if host == "proxy.dandaedre.com" and path.startswith("gitea/"): path = path[len("gitea/"):] if len(path.split("/")) < 2 or any(p in {"", ".", ".."} for p in path.split("/")): return None return path def git(*args, cwd=None, allow_missing=False): result = subprocess.run( ["git", *args], cwd=cwd, text=True, capture_output=True, check=False ) if result.returncode and not (allow_missing and result.returncode == 1): raise RuntimeError(result.stderr.strip() or "Git command failed") return result.stdout def read_urls(path): if not path.exists(): return {} output = git("config", "--file", str(path), "--null", "--get-regexp", r"^submodule\..*\.url$", allow_missing=True) urls = {} for record in output.split("\0"): if record: key, value = record.split("\n", 1) if key in urls: raise RuntimeError("Duplicate submodule URL key in {}: {}".format(path, key)) urls[key] = value return urls def fix(repository, dry_run=False): root = Path(git("rev-parse", "--show-toplevel", cwd=repository).strip()) common = Path(git("rev-parse", "--git-common-dir", cwd=root).strip()) if not common.is_absolute(): common = root / common modules_file = root / ".gitmodules" config_file = common.resolve() / "config" modules = read_urls(modules_file) local = read_urls(config_file) changes = [] for key, old in modules.items(): path = repository_path(old) if path is None: print("Skipping unrecognized URL in .gitmodules: {}".format(key)) continue new = HTTPS_BASE + path if old != new: changes.append((modules_file, key, old, new)) if key not in local: changes.append((config_file, key, None, SSH_BASE + path)) for key, old in local.items(): path = repository_path(old) if path is None: print("Skipping unrecognized local URL: {}".format(key)) continue new = SSH_BASE + path if old != new: changes.append((config_file, key, old, new)) for file, key, old, new in changes: print("{}: {}\n {} -> {}".format(file, key, old or "(missing)", new)) if not dry_run: git("config", "--file", str(file), "--replace-all", key, new) print("{} {} URL(s).".format("Would update" if dry_run else "Updated", len(changes))) return len(changes) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("repository", nargs="?", default=".", help="repository directory (default: current directory)") parser.add_argument("--dry-run", action="store_true", help="show planned changes without writing files") args = parser.parse_args() try: fix(args.repository, args.dry_run) except (OSError, RuntimeError, ValueError) as error: print("Error: {}".format(error), file=sys.stderr) return 1 return 0 if __name__ == "__main__": sys.exit(main())