Skip to content

Commit b1dddc2

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

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

src/tmt_web/utils/git_handler.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,22 @@ def get_git_repository(url: str, logger: Logger, ref: str | None = None) -> Path
118118
if _is_branch(common, destination, ref):
119119
_reset_branch(common, destination, ref, logger)
120120

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
121+
_checkout(common, destination, ref, logger)
126122

127123
return destination
128124

129125

126+
def _checkout(common: Common, repo_path: Path, ref: str, logger: Logger):
127+
repo_status = _get_repository_status(common, repo_path)
128+
try:
129+
common.run(Command("git", "checkout", ref), cwd=repo_path)
130+
except RunError as err:
131+
logger.fail(f"Failed to checkout ref '{ref}': {err.stderr}")
132+
if repo_status:
133+
logger.fail(f"Previous repository status:\n{repo_status}")
134+
raise AttributeError(f"Failed to checkout ref '{ref}'") from err
135+
136+
130137
def _get_default_branch(common: Common, repo_path: Path, logger: Logger) -> str:
131138
"""Determine the default branch of a Git repository using a remote HEAD."""
132139
try:
@@ -152,6 +159,7 @@ def _get_default_branch(common: Common, repo_path: Path, logger: Logger) -> str:
152159

153160
def _fetch_remote(common: Common, repo_path: Path, logger: Logger) -> None:
154161
"""Fetch updates from the remote repository."""
162+
repo_status = _get_repository_status(common, repo_path)
155163
try:
156164
# Fetch all branches and tags, prune deleted ones
157165
common.run(
@@ -168,17 +176,22 @@ def _fetch_remote(common: Common, repo_path: Path, logger: Logger) -> None:
168176
)
169177
except RunError as err:
170178
logger.fail(f"Failed to fetch remote for repository '{repo_path}': {err.stderr}")
179+
if repo_status:
180+
logger.fail(f"Previous repository status:\n{repo_status}")
171181
raise GeneralError(f"Failed to fetch remote for repository '{repo_path}'") from err
172182

173183

174184
def _reset_branch(common: Common, repo_path: Path, branch: str, logger: Logger) -> None:
175185
"""Ensure the specified branch is up to date with its remote counterpart."""
186+
repo_status = _get_repository_status(common, repo_path)
176187
try:
177188
common.run(Command("git", "reset", "--hard", f"origin/{branch}"), cwd=repo_path)
178189
except RunError as err:
179190
logger.fail(
180191
f"Failed to update branch '{branch}' for repository '{repo_path}': {err.stderr}"
181192
)
193+
if repo_status:
194+
logger.fail(f"Previous repository status:\n{repo_status}")
182195
raise GeneralError(
183196
f"Failed to update branch '{branch}' for repository '{repo_path}'"
184197
) from err
@@ -195,3 +208,18 @@ def _is_branch(common: Common, repo_path: Path, ref: str) -> bool:
195208
return True
196209
except RunError:
197210
return False
211+
212+
213+
def _get_repository_status(common: Common, repo_path: Path) -> str | None:
214+
"""Get the current status of a Git repository."""
215+
try:
216+
log_result = common.run(Command("git", "log", "-1"), cwd=repo_path)
217+
status_result = common.run(Command("git", "status"), cwd=repo_path)
218+
219+
outputs = [output.strip() for output in (log_result.stdout, status_result.stdout) if output]
220+
if outputs:
221+
return "\n".join(outputs)
222+
223+
return None
224+
except RunError:
225+
return None

0 commit comments

Comments
 (0)