Skip to content

Commit 7c82146

Browse files
Fetch origin is commit is not found in cached repository
Fixes #9
1 parent a1f3213 commit 7c82146

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ All notable changes to this project will be documented in this file.
1414

1515
- MuJoCo examples: try ``mj_description`` suffix rather than ``description``
1616

17+
### Fixed
18+
19+
- Update cached repository after a description was updated
20+
1721
## [0.6.0] - 2022/09/28
1822

1923
### Added

robot_descriptions/_cache.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"""
2121

2222
import os
23-
from typing import Union
23+
from typing import Optional, Union
2424

25-
from git import RemoteProgress, Repo
25+
from git import GitCommandError, RemoteProgress, Repo
2626
from tqdm import tqdm
2727

2828
from ._repositories import REPOSITORIES
@@ -63,28 +63,42 @@ def update(
6363
self.progress.refresh()
6464

6565

66-
def clone_to_directory(repo_url: str, target_dir: str) -> Repo:
66+
def clone_to_directory(
67+
repo_url: str, target_dir: str, commit: Optional[str] = None
68+
) -> Repo:
6769
"""
6870
Clone a git repository to a designated directory.
6971
7072
Args:
7173
repo_url: URL to the git repository to clone.
7274
target_dir: Directory to clone the repository to.
75+
commit: Optional commit to check out after cloning.
7376
7477
Returns:
7578
Cloned git repository.
7679
"""
7780
if os.path.exists(target_dir):
78-
return Repo(target_dir)
79-
80-
print(f"Cloning {repo_url}...")
81-
os.makedirs(target_dir)
82-
progress_bar = CloneProgressBar()
83-
return Repo.clone_from(
84-
repo_url,
85-
target_dir,
86-
progress=progress_bar.update,
87-
)
81+
clone = Repo(target_dir)
82+
else:
83+
print(f"Cloning {repo_url}...")
84+
os.makedirs(target_dir)
85+
progress_bar = CloneProgressBar()
86+
clone = Repo.clone_from(
87+
repo_url,
88+
target_dir,
89+
progress=progress_bar.update,
90+
)
91+
if commit is not None:
92+
try:
93+
clone.git.checkout(commit)
94+
except GitCommandError:
95+
print(
96+
f"Commit {commit} not found, "
97+
"let's fetch origin and try again..."
98+
)
99+
clone.git.fetch("origin")
100+
clone.git.checkout(commit)
101+
return clone
88102

89103

90104
def clone_to_cache(description_name: str) -> str:
@@ -116,6 +130,7 @@ def clone_to_cache(description_name: str) -> str:
116130
)
117131

118132
target_dir = os.path.join(cache_dir, repository.cache_path)
119-
clone = clone_to_directory(repository.url, target_dir)
120-
clone.git.checkout(repository.commit)
133+
clone = clone_to_directory(
134+
repository.url, target_dir, commit=repository.commit
135+
)
121136
return str(clone.working_dir)

0 commit comments

Comments
 (0)