Skip to content

Commit a8036fd

Browse files
authored
Merge pull request #71 from baba-dev/codex/update-repository-cloning-logic
chore(fetch): refresh dependencies in place
2 parents f4be2ab + a7b1adb commit a8036fd

3 files changed

Lines changed: 44 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ Optional:
126126

127127
git clone https://github.com/baba-dev/M5Tab5-UserDemo.git
128128
cd M5Tab5-UserDemo
129-
python fetch_repos.py # fetch M5 & LVGL components
129+
python fetch_repos.py # fetch or update M5 & LVGL components
130130

131131
2. **Set ESP32-P4 target**
132132

docs/DEV_SETUP.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
`release-v5.4` toolchain to ensure the project uses the NG I²C driver stack
77
exclusively.
88
- Run `python3 fetch_repos.py` before building so that managed components are
9-
pulled into the workspace.
9+
pulled into the workspace or refreshed to the pinned revisions.
1010

1111
## Build checklist
1212

fetch_repos.py

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,53 @@
1+
"""Fetch dependent repositories defined in repos.json."""
2+
3+
import json
14
import os
25
import subprocess
3-
import json
6+
from pathlib import Path
7+
8+
9+
def _run_git(args):
10+
subprocess.run(['git', *args], check=True)
11+
12+
13+
def _maybe_update_submodules(repo_path: Path) -> None:
14+
if (repo_path / '.gitmodules').is_file():
15+
_run_git(['-C', str(repo_path), 'submodule', 'update', '--init', '--recursive'])
416

517

618
def clone_or_update_repo(repo_url, path, branch):
7-
command = []
8-
command.append('git')
9-
command.append('clone')
10-
19+
repo_path = Path(path)
20+
21+
if repo_path.is_dir():
22+
_run_git(['-C', str(repo_path), 'fetch', '--prune'])
23+
24+
if branch:
25+
_run_git(['-C', str(repo_path), 'checkout', branch])
26+
27+
remote_ref = subprocess.run(
28+
['git', '-C', str(repo_path), 'show-ref', '--verify', f'refs/remotes/origin/{branch}'],
29+
stdout=subprocess.DEVNULL,
30+
stderr=subprocess.DEVNULL,
31+
check=False,
32+
)
33+
34+
if remote_ref.returncode == 0:
35+
_run_git(['-C', str(repo_path), 'reset', '--hard', f'origin/{branch}'])
36+
else:
37+
_run_git(['-C', str(repo_path), 'pull', '--ff-only'])
38+
39+
_maybe_update_submodules(repo_path)
40+
return
41+
42+
command = ['clone']
43+
1144
if branch:
12-
command.append('-b')
13-
command.append(branch)
45+
command.extend(['-b', branch])
1446

15-
command.append(repo_url)
16-
command.append(path)
47+
command.extend([repo_url, str(repo_path)])
1748

18-
subprocess.run(command)
49+
_run_git(command)
50+
_maybe_update_submodules(repo_path)
1951

2052
def main():
2153
script_dir = os.path.dirname(os.path.abspath(__file__))

0 commit comments

Comments
 (0)