Skip to content

Commit f4fb7a4

Browse files
authored
Deploytool: Fix wrong warnings and small improvements (#522)
2 parents 4c4e11f + d9073b1 commit f4fb7a4

File tree

3 files changed

+46
-27
lines changed

3 files changed

+46
-27
lines changed

scripts/deploy/deploy_robots.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ def _register_tasks(self) -> list[AbstractTask]:
174174
"""
175175
tasks = []
176176

177-
if not self._args.skip_local_repo_check:
178-
tasks.append(CheckReposTask())
177+
tasks.append(CheckReposTask(only_workspace_status=self._args.skip_local_repo_check))
179178

180179
if self._args.sync:
181180
tasks.append(

scripts/deploy/tasks/build.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def _build(self, connections: Group) -> GroupResult:
8282
"sync;"
8383
)
8484
# TODO make output colored
85-
# TODO: check if only single core?!?
8685

8786
print_debug(f"Calling {cmd}")
8887
try:

scripts/deploy/tasks/check_repos.py

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import threading
44
from hashlib import md5
55

6-
from deploy.misc import be_quiet, print_debug, print_success, print_warning
6+
from deploy.misc import be_quiet, print_debug, print_info, print_success, print_warning
77
from deploy.tasks.abstract_task import AbstractTask
88
from fabric import Group, GroupResult, Result
99
from git import Repo
@@ -120,20 +120,24 @@ def check_ahead_behind(self, timeout: float = 5) -> bool:
120120
return True
121121

122122
print_debug(f"Repo {self}: Checking if behind: Comparing local and remote repository.")
123-
ahead = False
124-
for _ in self.iter_commits(f"{self.specified_branch}..{remote.name}/{self.specified_branch}"):
125-
ahead = True
126-
if ahead:
127-
print_debug(f"Repo {self}: The local repository is ahead of the remote repository.")
128-
self.warnings.append("The local repository is ahead of the remote repository.")
123+
behind = len(list(self.iter_commits(f"{self.specified_branch}..{remote.name}/{self.specified_branch}")))
124+
if behind:
125+
print_debug(
126+
f"Repo {self}: Your branch is behind the remote branch by {behind} commit{'s' if behind > 1 else ''}."
127+
)
128+
self.warnings.append(
129+
f"Your branch is behind the remote branch by {behind} commit{'s' if behind > 1 else ''}."
130+
)
129131

130132
print_debug(f"Repo {self}: Checking if ahead: Comparing remote and local repository.")
131-
behind = False
132-
for _ in self.iter_commits(f"{remote.name}/{self.specified_branch}..{self.specified_branch}"):
133-
behind = True
134-
if behind:
135-
print_debug(f"Repo {self}: The local repository is behind of the remote repository.")
136-
self.warnings.append("The local repository is behind of the remote repository.")
133+
ahead = len(list(self.iter_commits(f"{remote.name}/{self.specified_branch}..{self.specified_branch}")))
134+
if ahead:
135+
print_debug(
136+
f"Repo {self}: Your branch is ahead of the remote branch by {ahead} commit{'s' if ahead > 1 else ''}."
137+
)
138+
self.warnings.append(
139+
f"Your branch is ahead of the remote branch by {ahead} commit{'s' if ahead > 1 else ''}."
140+
)
137141

138142
return ahead or behind
139143

@@ -142,14 +146,16 @@ def __str__(self) -> str:
142146

143147

144148
class CheckReposTask(AbstractTask):
145-
def __init__(self) -> None:
149+
def __init__(self, only_workspace_status: bool = False) -> None:
146150
"""
147151
Task to check the local repositories (bitbots_main and others as specified in workspace.repos file).
148152
Displays the current commit status (with a friendly name) to compare with other people in a hurry.
149153
Also displays warnings and requires confirmation before proceeding when:
150154
- A repository is dirty (uncommitted changes).
151155
- A repository is not on the specified branch.
152156
- A repository is ahead or behind of the remote repository.
157+
158+
:param only_workspace_status: If True, only collect the workspace commit hashes and skip the local workspace check.
153159
"""
154160
super().__init__()
155161
self._show_status = False
@@ -162,6 +168,8 @@ def __init__(self) -> None:
162168
self.main_repo_path, "bitbots_misc/bitbots_utils/config/", "workspace_status.json"
163169
)
164170

171+
self.only_workspace_status: bool = only_workspace_status
172+
165173
self.main_repo: OurRepo = OurRepo(self.main_repo_path, "main")
166174
self.repos: dict[str, OurRepo] = {"bitbots_main": self.main_repo, **self._get_workspace_repos()}
167175

@@ -213,8 +221,14 @@ def failure(
213221
group_result._successes = results
214222
return group_result
215223

216-
# Collect commit hashes for the friendly commit name
217-
self.commit_hashes: dict[str, str] = {name: repo.get_commit_hash() for name, repo in self.repos.items()}
224+
workspace_hash, workspace_friendly_name = self._collect_workspace_hashes()
225+
if self.only_workspace_status:
226+
print_info(
227+
f"Current workspace hash: [bold]{workspace_friendly_name}[default] ({workspace_hash[:8]})\nSkipped workspace checks."
228+
)
229+
results = success(connections)
230+
return results
231+
218232
# Check all repositories and collect warnings with multiple threads
219233
threads: list[threading.Thread] = []
220234
for name, repo in self.repos.items():
@@ -224,16 +238,12 @@ def failure(
224238
for thread in threads:
225239
thread.join()
226240

227-
workspace_hash: str = self._get_workspace_hash(self.commit_hashes)
228-
self.commit_hashes["__WORKSPACE__"] = workspace_hash
229-
workspace_friendly_name: str = self._get_friendly_name(workspace_hash)
230-
231241
# Display results
232242
results: GroupResult
233243
# Do we have any warnings? If not, all checks for all repos were successful
234244
if not any([*self.warnings.values()]): # No warnings = Success
235245
print_success(
236-
f"Current commit: [bold]{workspace_friendly_name}[default] ({workspace_hash[:8]})\nYour local workspace is clean and up-to-date."
246+
f"Current workspace hash: [bold]{workspace_friendly_name}[default] ({workspace_hash[:8]})\nYour local workspace is clean and up-to-date."
237247
)
238248
results = success(connections)
239249

@@ -254,8 +264,8 @@ def failure(
254264
warning_table.add_row(repo_name, f"{commit_name} ({commit_hash[:8]})", warnings)
255265
print_warning(
256266
RichGroup(
257-
f"Current commit: [bold]{workspace_friendly_name}[default] ({workspace_hash[:8]})",
258-
"Your local workspace is [bold]BAD!",
267+
f"Current workspace hash: [bold]{workspace_friendly_name}[default] ({workspace_hash[:8]})",
268+
"Your local workspace is [bold][red]BAD!",
259269
warning_table,
260270
"Please check the warnings and decide if you want to proceed!",
261271
)
@@ -268,9 +278,20 @@ def failure(
268278
else:
269279
print_debug("Aborting because of user choice.")
270280
results = failure(connections, warnings, do_exit=True)
281+
return results
271282

283+
def _collect_workspace_hashes(self) -> tuple[str, str]:
284+
"""
285+
Collect commit hashes and generate the friendly commit names. Writes the results to a file.
286+
287+
:return: The workspace hash and the friendly name of the workspace hash.
288+
"""
289+
self.commit_hashes: dict[str, str] = {name: repo.get_commit_hash() for name, repo in self.repos.items()}
290+
workspace_hash: str = self._get_workspace_hash(self.commit_hashes)
291+
self.commit_hashes["__WORKSPACE__"] = workspace_hash
292+
workspace_friendly_name: str = self._get_friendly_name(workspace_hash)
272293
self._write_commits()
273-
return results
294+
return workspace_hash, workspace_friendly_name
274295

275296
def _get_workspace_hash(self, commit_hashes: dict[str, str]) -> str:
276297
"""

0 commit comments

Comments
 (0)