"""Auto-clone and update OWS service repos before analysis. Ensures repos exist at ~/work/ows/{service_name} and are on latest master. """ import subprocess from pathlib import Path GITHUB_ORG_URL = 'git@github.com:theorchard' def ensure_repo(service_name: str, repo_base: Path) -> Path: """Ensure a service repo exists and is up-to-date. Returns the repo path.""" repo_path = repo_base / service_name if not repo_path.exists(): clone_url = f'{GITHUB_ORG_URL}/{service_name}.git' print(f' Cloning {clone_url}...') result = _git_run(['git', 'clone', clone_url, str(repo_path)]) if result.returncode != 0: print(f' ERROR cloning: {result.stderr.strip()}') return repo_path print(f' Cloned to {repo_path}') return repo_path _update_repo(repo_path) return repo_path def _update_repo(repo_path: Path): """Stash changes, checkout master, pull latest.""" def git(*args: str) -> subprocess.CompletedProcess: return _git_run(['git', '-C', str(repo_path)] + list(args)) # Stash if dirty status = git('status', '--porcelain') if status.stdout.strip(): print(f' Stashing local changes...') git('stash') # Checkout master git('checkout', 'master') # Determine remote: prefer "orchard" if it exists, else "origin" remotes = git('remote') remote_list = [r for r in remotes.stdout.strip().split('\n') if r] if 'orchard' in remote_list: remote = 'orchard' else: remote = 'origin' # Force SSH URL on the remote ssh_url = f'{GITHUB_ORG_URL}/{repo_path.name}.git' git('remote', 'set-url', remote, ssh_url) print(f' Pulling {remote}/master...') result = git('pull', remote, 'master') if result.returncode != 0: print(f' Warning: git pull failed: {result.stderr.strip()}') def _git_run(cmd: list[str]) -> subprocess.CompletedProcess: return subprocess.run(cmd, capture_output=True, text=True, timeout=120)