-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_repo
More file actions
executable file
·155 lines (123 loc) · 4.93 KB
/
github_repo
File metadata and controls
executable file
·155 lines (123 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env -S uv run --script
# vi: ft=python
# /// script
# requires-python = ">=3.7"
# dependencies = [
# "click",
# ]
# ///
import os
import re
import subprocess
import sys
from pathlib import Path
import click
def get_git_base_dir():
"""Get the base git directory path."""
return Path.home() / "wsp" / "repos" / "git"
def parse_git_url(url):
"""Parse a Git URL to extract domain, owner, and repo."""
patterns = [
r"^https://([^/]+)/([^/]+)/([^/]+?)(?:\.git)?/?$",
r"^git@([^:]+):([^/]+)/([^/]+?)(?:\.git)?/?$",
r"^([^/]+)/([^/]+)$",
]
for pattern in patterns:
match = re.match(pattern, url)
if match:
groups = match.groups()
if len(groups) == 3:
domain, owner, repo = groups
else:
owner, repo = groups
domain = "github.com"
return domain, owner, repo
raise ValueError(f"Could not parse Git URL format: {url}")
def run_git_command(cmd, cwd=None, check=True):
"""Run a git command and return the result."""
try:
result = subprocess.run(
cmd,
cwd=cwd,
check=check,
text=True
)
return result
except subprocess.CalledProcessError as e:
if check:
click.echo(f"Git command failed: {' '.join(cmd)}", err=True)
raise
return e
@click.command()
@click.argument('repo_url', type=click.STRING)
@click.option('--branch', '-b', help='Branch or tag to checkout after cloning')
@click.option('--shell', is_flag=True, help='Start an interactive shell in the target directory')
@click.option('--verbose', '-v', is_flag=True, help='Verbose output')
def main(repo_url, branch, shell, verbose):
"""
Clone GitHub repositories to ~/wsp/repos/git/org/project_name.
REPO_URL: GitHub repository URL (https://github.com/org/repo, git@github.com:org/repo, or org/repo)
"""
try:
if verbose:
click.echo(f"Processing repository: {repo_url}")
try:
domain, owner, repo = parse_git_url(repo_url)
except ValueError as e:
click.echo(f"Error: {e}", err=True)
sys.exit(1)
if verbose:
click.echo(f"Domain: {domain}")
click.echo(f"Owner: {owner}")
click.echo(f"Repository: {repo}")
git_base_dir = get_git_base_dir()
target_dir = git_base_dir / owner / repo
click.echo(f"Target directory: {target_dir}")
target_dir.parent.mkdir(parents=True, exist_ok=True)
full_url = f"https://{domain}/{owner}/{repo}.git"
if target_dir.exists():
click.echo("Repository already exists")
os.chdir(target_dir)
result = run_git_command(['git', 'remote', 'get-url', 'origin'], check=False)
if result.returncode != 0:
click.echo(f"Adding remote origin: {full_url}")
run_git_command(['git', 'remote', 'add', 'origin', full_url])
click.echo("Fetching latest changes...")
run_git_command(['git', 'fetch', 'origin'])
else:
click.echo(f"Cloning repository {full_url}")
run_git_command(['git', 'clone', full_url, str(target_dir)])
os.chdir(target_dir)
if branch:
click.echo(f"Checking out branch/tag: {branch}")
result = run_git_command(['git', 'rev-parse', '--verify', branch], check=False)
if result.returncode != 0:
click.echo("Branch/tag not found locally, fetching...")
run_git_command(['git', 'fetch', '--tags', 'origin'])
result = run_git_command(['git', 'rev-parse', '--verify', branch], check=False)
if result.returncode != 0:
click.echo(f"Error: Branch/tag {branch} not found in repository", err=True)
sys.exit(1)
run_git_command(['git', 'checkout', branch])
click.echo(f"Successfully checked out {branch} in {target_dir}")
else:
click.echo(f"Successfully cloned to {target_dir}")
click.echo(f"Current directory: {os.getcwd()}")
click.echo(f"TARGET_DIR={target_dir}")
if shell:
click.echo("Starting interactive shell in target directory...")
shell_cmd = os.environ.get('SHELL', '/bin/bash')
os.execv(shell_cmd, [shell_cmd])
else:
click.echo(f"\nTo change to the directory, run:")
click.echo(f"cd {target_dir}")
except KeyboardInterrupt:
click.echo("\nOperation cancelled by user", err=True)
sys.exit(1)
except Exception as e:
if verbose:
raise
click.echo(f"Error: {e}", err=True)
sys.exit(1)
if __name__ == '__main__':
main()