#!/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"} __version__ = "0.1.0" def repository_path(url): """Return the repository path for a recognized absolute URL, or None.""" if (not isinstance(url, str) or not url or any(character.isspace() or ord(character) < 32 for character in url) or "\\" in url): return None if "://" in url: try: parsed = urlsplit(url) host = parsed.hostname except ValueError: return None if parsed.scheme not in {"http", "https", "ssh", "git"}: return None 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_url_entries(path, pattern): if not path.exists(): return [] output = git("config", "--file", str(path), "--null", "--get-regexp", pattern, allow_missing=True) return [tuple(record.split("\n", 1)) for record in output.split("\0") if record] def read_urls(path): urls = {} for key, value in read_url_entries(path, r"^submodule\..*\.url$"): 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)) # Initialized submodules keep their own remotes here, including nested ones. # Linked worktrees may also have their own modules directory. git_dir = Path(git("rev-parse", "--absolute-git-dir", cwd=root).strip()) module_configs = set() for directory in {common.resolve(), git_dir}: module_configs.update((directory / "modules").rglob("config")) for file in sorted(module_configs): if not file.is_file(): continue entries = read_url_entries( file, r"^(remote\..*\.(url|pushurl)|submodule\..*\.url)$" ) for key, old in dict.fromkeys(entries): path = repository_path(old) if path is not None and old != SSH_BASE + path: changes.append((file, key, old, SSH_BASE + path)) for file, key, old, new in changes: print("{}: {}\n {} -> {}".format(file, key, old or "(missing)", new)) if not dry_run: # Match only this value so other URLs on a multi-URL remote survive. pattern = [] if old is None else ["^" + re.escape(old) + "$"] git("config", "--file", str(file), "--replace-all", key, new, *pattern) 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("--version", action="version", version=__version__) 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())