forked from m5stack/M5Tab5-UserDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch_repos.py
More file actions
67 lines (46 loc) · 1.79 KB
/
fetch_repos.py
File metadata and controls
67 lines (46 loc) · 1.79 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
"""Fetch dependent repositories defined in repos.json."""
import json
import os
import subprocess
from pathlib import Path
def _run_git(args):
subprocess.run(['git', *args], check=True)
def _maybe_update_submodules(repo_path: Path) -> None:
if (repo_path / '.gitmodules').is_file():
_run_git(['-C', str(repo_path), 'submodule', 'update', '--init', '--recursive'])
def clone_or_update_repo(repo_url, path, branch):
repo_path = Path(path)
if repo_path.is_dir():
_run_git(['-C', str(repo_path), 'fetch', '--prune'])
if branch:
_run_git(['-C', str(repo_path), 'checkout', branch])
remote_ref = subprocess.run(
['git', '-C', str(repo_path), 'show-ref', '--verify', f'refs/remotes/origin/{branch}'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
if remote_ref.returncode == 0:
_run_git(['-C', str(repo_path), 'reset', '--hard', f'origin/{branch}'])
else:
_run_git(['-C', str(repo_path), 'pull', '--ff-only'])
_maybe_update_submodules(repo_path)
return
command = ['clone']
if branch:
command.extend(['-b', branch])
command.extend([repo_url, str(repo_path)])
_run_git(command)
_maybe_update_submodules(repo_path)
def main():
script_dir = os.path.dirname(os.path.abspath(__file__))
config_path = os.path.join(script_dir, 'repos.json')
with open(config_path) as f:
repos = json.load(f)
for repo in repos:
repo_path = os.path.join(script_dir, repo['path'])
branch = repo.get('branch')
clone_or_update_repo(repo['url'], repo_path, branch)
if __name__ == "__main__":
print("start fetching dependent repos..")
main()