|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# ******************************************************************************* |
| 3 | +# Copyright (c) 2025 Contributors to the Eclipse Foundation |
| 4 | +# |
| 5 | +# See the NOTICE file(s) distributed with this work for additional |
| 6 | +# information regarding copyright ownership. |
| 7 | +# |
| 8 | +# This program and the accompanying materials are made available under the |
| 9 | +# terms of the Apache License Version 2.0 which is available at |
| 10 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# SPDX-License-Identifier: Apache-2.0 |
| 13 | +# ******************************************************************************* |
| 14 | +""" |
| 15 | +Checkout all pinned repositories based on repos.json configuration. |
| 16 | +""" |
| 17 | + |
| 18 | +import json |
| 19 | +import sys |
| 20 | +import subprocess |
| 21 | +import re |
| 22 | +import os |
| 23 | +from pathlib import Path |
| 24 | + |
| 25 | + |
| 26 | +def load_repos_config(config_file="./repos.json"): |
| 27 | + """ |
| 28 | + Load repository configuration from repos.json. |
| 29 | +
|
| 30 | + Args: |
| 31 | + config_file: Path to repos.json file |
| 32 | +
|
| 33 | + Returns: |
| 34 | + List of repository configurations |
| 35 | + """ |
| 36 | + config_path = Path(config_file) |
| 37 | + |
| 38 | + if not config_path.exists(): |
| 39 | + print(f"Error: file not found '{config_file}'", file=sys.stderr) |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + try: |
| 43 | + with open(config_path, "r") as f: |
| 44 | + repos = json.load(f) |
| 45 | + return repos |
| 46 | + except (json.JSONDecodeError, IOError) as e: |
| 47 | + print(f"Error: Failed to load repos.json: {e}", file=sys.stderr) |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + |
| 51 | +def is_commit_hash(ref): |
| 52 | + """ |
| 53 | + Check if reference looks like a commit hash (40 hex characters for SHA-1). |
| 54 | +
|
| 55 | + Args: |
| 56 | + ref: Git reference (branch, tag, or hash) |
| 57 | +
|
| 58 | + Returns: |
| 59 | + True if ref matches commit hash pattern |
| 60 | + """ |
| 61 | + return bool(re.match(r"^[0-9a-fA-F]{40}$", ref)) |
| 62 | + |
| 63 | + |
| 64 | +def checkout_repo(name, url, ref, path): |
| 65 | + """ |
| 66 | + Checkout a single repository. |
| 67 | +
|
| 68 | + Args: |
| 69 | + name: Repository name |
| 70 | + url: Repository URL |
| 71 | + ref: Git reference (branch, tag, or commit hash) |
| 72 | + path: Local path to checkout into |
| 73 | +
|
| 74 | + Returns: |
| 75 | + True if successful, False otherwise |
| 76 | + """ |
| 77 | + path_obj = Path(path) |
| 78 | + |
| 79 | + try: |
| 80 | + # Create parent directory if needed |
| 81 | + path_obj.parent.mkdir(parents=True, exist_ok=True) |
| 82 | + |
| 83 | + if is_commit_hash(ref): |
| 84 | + print(f"Checking out {name} ({ref}) to {path}") |
| 85 | + print(f" Detected commit hash. Cloning and then checking out.") |
| 86 | + |
| 87 | + # Clone the repository |
| 88 | + subprocess.run(["git", "clone", url, path], check=True, capture_output=True) |
| 89 | + |
| 90 | + # Checkout specific commit |
| 91 | + subprocess.run(["git", "-C", path, "checkout", ref], check=True, capture_output=True) |
| 92 | + else: |
| 93 | + print(f"Checking out {name} ({ref}) to {path}") |
| 94 | + print(f" Detected branch/tag. Cloning with --branch.") |
| 95 | + |
| 96 | + # Clone with shallow copy and specific branch/tag |
| 97 | + # Add 'v' prefix if not already present (common convention) |
| 98 | + branch_ref = ref if ref.startswith("v") else f"v{ref}" |
| 99 | + subprocess.run( |
| 100 | + ["git", "clone", "--depth", "1", "--branch", branch_ref, url, path], check=True, capture_output=True |
| 101 | + ) |
| 102 | + |
| 103 | + return True |
| 104 | + |
| 105 | + except subprocess.CalledProcessError as e: |
| 106 | + print(f"Error: Failed to checkout {name}: {e}", file=sys.stderr) |
| 107 | + return False |
| 108 | + |
| 109 | + |
| 110 | +def main(): |
| 111 | + """Main entry point.""" |
| 112 | + # Load repository configurations |
| 113 | + repos = load_repos_config("./repos.json") |
| 114 | + repo_count = len(repos) |
| 115 | + |
| 116 | + # Track successfully checked out repositories |
| 117 | + repo_paths = [] |
| 118 | + |
| 119 | + # Checkout each repository |
| 120 | + for i, repo in enumerate(repos): |
| 121 | + name = repo.get("name", f"repo-{i}") |
| 122 | + url = repo.get("url", "") |
| 123 | + ref = repo.get("version", "") |
| 124 | + path = repo.get("path", "") |
| 125 | + |
| 126 | + if not all([url, ref, path]): |
| 127 | + print(f"Warning: Skipping {name} - missing required fields", file=sys.stderr) |
| 128 | + continue |
| 129 | + |
| 130 | + if checkout_repo(name, url, ref, path): |
| 131 | + repo_paths.append(path) |
| 132 | + |
| 133 | + # Output all paths (comma-separated for GitHub Actions compatibility) |
| 134 | + repo_paths_output = ",".join(repo_paths) |
| 135 | + |
| 136 | + # Write to GITHUB_OUTPUT if available |
| 137 | + github_output = os.environ.get("GITHUB_OUTPUT") |
| 138 | + if github_output: |
| 139 | + try: |
| 140 | + with open(github_output, "a") as f: |
| 141 | + f.write(f"repo_paths={repo_paths_output}\n") |
| 142 | + except IOError as e: |
| 143 | + print(f"Warning: Failed to write GITHUB_OUTPUT: {e}", file=sys.stderr) |
| 144 | + |
| 145 | + # Also print for debugging |
| 146 | + print(f"\nSuccessfully checked out {len(repo_paths)} of {repo_count} repositories") |
| 147 | + print(f"repo_paths={repo_paths_output}") |
| 148 | + |
| 149 | + return 0 if len(repo_paths) == repo_count else 1 |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + sys.exit(main()) |
0 commit comments