Skip to content

Commit 5aaaa61

Browse files
committed
squash: show repository status on fail
1 parent b5cbeb1 commit 5aaaa61

1 file changed

Lines changed: 34 additions & 11 deletions

File tree

src/tmt_web/utils/git_handler.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,14 @@ def _create_hash(text: str):
2929
return hashed_text.hexdigest()
3030

3131

32-
def get_repo_hash(url: str) -> str:
33-
url = url.rstrip("/").removesuffix(".git")
34-
return _create_hash(url)
35-
36-
3732
def get_unique_clone_path(url: str) -> Path:
3833
"""
3934
Generate a unique path for cloning a repository.
4035
4136
:param url: Repository URL
4237
:return: Unique path for cloning
4338
"""
44-
return ROOT_DIR / settings.CLONE_DIR_PATH / get_repo_hash(url)
39+
return ROOT_DIR / settings.CLONE_DIR_PATH / _create_hash(url.rstrip("/").removesuffix(".git"))
4540

4641

4742
def clear_tmp_dir(logger: Logger) -> None:
@@ -118,15 +113,22 @@ def get_git_repository(url: str, logger: Logger, ref: str | None = None) -> Path
118113
if _is_branch(common, destination, ref):
119114
_reset_branch(common, destination, ref, logger)
120115

121-
try:
122-
common.run(Command("git", "checkout", ref), cwd=destination)
123-
except RunError as err:
124-
logger.fail(f"Failed to checkout ref '{ref}': {err.stderr}")
125-
raise AttributeError(f"Failed to checkout ref '{ref}'") from err
116+
_checkout(common, destination, ref, logger)
126117

127118
return destination
128119

129120

121+
def _checkout(common: Common, repo_path: Path, ref: str, logger: Logger):
122+
repo_status = _get_repository_status(common, repo_path)
123+
try:
124+
common.run(Command("git", "checkout", ref), cwd=repo_path)
125+
except RunError as err:
126+
logger.fail(f"Failed to checkout ref '{ref}': {err.stderr}")
127+
if repo_status:
128+
logger.fail(f"Previous repository status:\n{repo_status}")
129+
raise AttributeError(f"Failed to checkout ref '{ref}'") from err
130+
131+
130132
def _get_default_branch(common: Common, repo_path: Path, logger: Logger) -> str:
131133
"""Determine the default branch of a Git repository using a remote HEAD."""
132134
try:
@@ -152,6 +154,7 @@ def _get_default_branch(common: Common, repo_path: Path, logger: Logger) -> str:
152154

153155
def _fetch_remote(common: Common, repo_path: Path, logger: Logger) -> None:
154156
"""Fetch updates from the remote repository."""
157+
repo_status = _get_repository_status(common, repo_path)
155158
try:
156159
# Fetch all branches and tags, prune deleted ones
157160
common.run(
@@ -168,17 +171,22 @@ def _fetch_remote(common: Common, repo_path: Path, logger: Logger) -> None:
168171
)
169172
except RunError as err:
170173
logger.fail(f"Failed to fetch remote for repository '{repo_path}': {err.stderr}")
174+
if repo_status:
175+
logger.fail(f"Previous repository status:\n{repo_status}")
171176
raise GeneralError(f"Failed to fetch remote for repository '{repo_path}'") from err
172177

173178

174179
def _reset_branch(common: Common, repo_path: Path, branch: str, logger: Logger) -> None:
175180
"""Ensure the specified branch is up to date with its remote counterpart."""
181+
repo_status = _get_repository_status(common, repo_path)
176182
try:
177183
common.run(Command("git", "reset", "--hard", f"origin/{branch}"), cwd=repo_path)
178184
except RunError as err:
179185
logger.fail(
180186
f"Failed to update branch '{branch}' for repository '{repo_path}': {err.stderr}"
181187
)
188+
if repo_status:
189+
logger.fail(f"Previous repository status:\n{repo_status}")
182190
raise GeneralError(
183191
f"Failed to update branch '{branch}' for repository '{repo_path}'"
184192
) from err
@@ -195,3 +203,18 @@ def _is_branch(common: Common, repo_path: Path, ref: str) -> bool:
195203
return True
196204
except RunError:
197205
return False
206+
207+
208+
def _get_repository_status(common: Common, repo_path: Path) -> str | None:
209+
"""Get the current status of a Git repository."""
210+
try:
211+
log_result = common.run(Command("git", "log", "-1"), cwd=repo_path)
212+
status_result = common.run(Command("git", "status"), cwd=repo_path)
213+
214+
outputs = [output.strip() for output in (log_result.stdout, status_result.stdout) if output]
215+
if outputs:
216+
return "\n".join(outputs)
217+
218+
return None
219+
except RunError:
220+
return None

0 commit comments

Comments
 (0)