|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Build test git repository from seed file. |
| 4 | +
|
| 5 | +This script creates a git repository with commits defined in test_repo_seed.json. |
| 6 | +The repository is used for testing commit and activity extraction. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import os |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +def run_git_command(repo_path: str, command: list[str]) -> str: |
| 17 | + """Run a git command in the repository.""" |
| 18 | + result = subprocess.run(command, cwd=repo_path, capture_output=True, text=True, check=True) |
| 19 | + return result.stdout.strip() |
| 20 | + |
| 21 | + |
| 22 | +def initialize_repo(repo_path: str) -> None: |
| 23 | + """Initialize a new git repository.""" |
| 24 | + if os.path.exists(repo_path): |
| 25 | + print(f"Repository already exists at {repo_path}") |
| 26 | + return |
| 27 | + |
| 28 | + os.makedirs(repo_path, exist_ok=True) |
| 29 | + run_git_command(repo_path, ["git", "init"]) |
| 30 | + run_git_command(repo_path, ["git", "config", "user.name", "Test User"]) |
| 31 | + run_git_command(repo_path, ["git", "config", "user.email", "test@example.com"]) |
| 32 | + print(f"✅ Initialized git repository at {repo_path}") |
| 33 | + |
| 34 | + |
| 35 | +def create_commit(repo_path: str, commit_data: dict) -> str: |
| 36 | + """Create a single commit from commit data.""" |
| 37 | + author_name = commit_data["author"]["name"] |
| 38 | + author_email = commit_data["author"]["email"] |
| 39 | + message = commit_data["message"] |
| 40 | + |
| 41 | + # Get committer info (defaults to author if not specified) |
| 42 | + committer = commit_data.get("committer", commit_data["author"]) |
| 43 | + committer_name = committer["name"] |
| 44 | + committer_email = committer["email"] |
| 45 | + |
| 46 | + # Create/modify files |
| 47 | + for file_data in commit_data["files"]: |
| 48 | + file_path = os.path.join(repo_path, file_data["path"]) |
| 49 | + os.makedirs(os.path.dirname(file_path), exist_ok=True) |
| 50 | + |
| 51 | + with open(file_path, "w") as f: |
| 52 | + f.write(file_data["content"]) |
| 53 | + |
| 54 | + # Stage the file |
| 55 | + run_git_command(repo_path, ["git", "add", file_data["path"]]) |
| 56 | + |
| 57 | + # Set author and committer using minimal environment |
| 58 | + env = { |
| 59 | + "GIT_AUTHOR_NAME": author_name, |
| 60 | + "GIT_AUTHOR_EMAIL": author_email, |
| 61 | + "GIT_COMMITTER_NAME": committer_name, |
| 62 | + "GIT_COMMITTER_EMAIL": committer_email, |
| 63 | + "PATH": os.environ.get("PATH", "/usr/bin:/bin"), # Minimal PATH for git command |
| 64 | + } |
| 65 | + |
| 66 | + subprocess.run( |
| 67 | + ["git", "commit", "-m", message], |
| 68 | + cwd=repo_path, |
| 69 | + capture_output=True, |
| 70 | + text=True, |
| 71 | + check=True, |
| 72 | + env=env, |
| 73 | + ) |
| 74 | + |
| 75 | + # Get the commit hash |
| 76 | + commit_hash = run_git_command(repo_path, ["git", "rev-parse", "HEAD"]) |
| 77 | + |
| 78 | + return commit_hash |
| 79 | + |
| 80 | + |
| 81 | +def build_repository(seed_file: str, repo_path: str) -> dict: |
| 82 | + """Build git repository from seed file.""" |
| 83 | + print(f"📖 Reading seed file: {seed_file}") |
| 84 | + |
| 85 | + with open(seed_file, "r") as f: |
| 86 | + seed_data = json.load(f) |
| 87 | + |
| 88 | + print(f"🏗️ Building repository at: {repo_path}") |
| 89 | + |
| 90 | + # Initialize repository |
| 91 | + initialize_repo(repo_path) |
| 92 | + |
| 93 | + # Create commits |
| 94 | + commit_hashes = [] |
| 95 | + for i, commit_data in enumerate(seed_data["commits"], 1): |
| 96 | + commit_hash = create_commit(repo_path, commit_data) |
| 97 | + commit_hashes.append(commit_hash) |
| 98 | + print(f"✅ Created commit {i}/{len(seed_data['commits'])}: {commit_hash[:8]}") |
| 99 | + |
| 100 | + # Get repository statistics |
| 101 | + total_commits = run_git_command(repo_path, ["git", "rev-list", "--count", "HEAD"]) |
| 102 | + |
| 103 | + print("\n🎉 Repository built successfully!") |
| 104 | + print(f" Total commits: {total_commits}") |
| 105 | + print(f" Location: {repo_path}") |
| 106 | + |
| 107 | + return { |
| 108 | + "repo_path": repo_path, |
| 109 | + "commit_hashes": commit_hashes, |
| 110 | + "total_commits": int(total_commits), |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | +def main(): |
| 115 | + """Main entry point.""" |
| 116 | + # Get script directory |
| 117 | + script_dir = Path(__file__).parent |
| 118 | + |
| 119 | + # Default paths |
| 120 | + seed_file = script_dir / "test_repo_seed.json" |
| 121 | + repos_dir = script_dir.parent / "repos" |
| 122 | + repos_dir.mkdir(exist_ok=True) |
| 123 | + repo_path = repos_dir / "test-repo" |
| 124 | + |
| 125 | + # Allow overriding from command line |
| 126 | + if len(sys.argv) > 1: |
| 127 | + seed_file = Path(sys.argv[1]) |
| 128 | + if len(sys.argv) > 2: |
| 129 | + repo_path = Path(sys.argv[2]) |
| 130 | + |
| 131 | + # Build repository |
| 132 | + result = build_repository(str(seed_file), str(repo_path)) |
| 133 | + |
| 134 | + # Print result |
| 135 | + print("\n📊 Build Summary:") |
| 136 | + print(json.dumps(result, indent=2)) |
| 137 | + |
| 138 | + |
| 139 | +if __name__ == "__main__": |
| 140 | + main() |
0 commit comments