Cleanup checkout.py
public
Cleanup checkout.py
| Changeset | c8fe3f228c5400bf08f483148ae20e235070c21d |
|---|---|
| Author (claimed) | Timo Stolz <timo.stolz@gmail.com>, |
| Parents | 5c96730c9b63 |
| Children | |
| Pushlog | by timo via ssh |
modified src/mitch/cli/checkout.py +23 −31
| @@ -1,4 +1,4 @@ | ||
| 1 | -from contextlib import contextmanager | |
| 1 | +from contextlib import contextmanager, nullcontext, AbstractContextManager | |
| 2 | 2 | from pathlib import Path |
| 3 | 3 | from typing import Annotated, Literal, NamedTuple, Protocol, Generator |
| 4 | 4 | from dataclasses import dataclass |
| @@ -57,8 +57,9 @@ | ||
| 57 | 57 | def is_dirty(self) -> bool: ... |
| 58 | 58 | def changes(self, branch: str) -> Generator[Add | Modified | Deleted | TypeChanged | Renamed | Copied, None, None]: ... |
| 59 | 59 | def checkout(self, branch: str) -> None: ... |
| 60 | - def stash_changes(self, message: str) -> None: ... | |
| 60 | + def stash_changes(self, message: str | None = None) -> None: ... | |
| 61 | 61 | def restore_changes(self) -> None: ... |
| 62 | + def stashed(self) -> AbstractContextManager: ... | |
| 62 | 63 | |
| 63 | 64 | |
| 64 | 65 | class GitManager(VersionControlManagerProtocol): |
| @@ -145,46 +146,37 @@ | ||
| 145 | 146 | def checkout(self, branch: str) -> None: |
| 146 | 147 | self._run("checkout", branch) |
| 147 | 148 | |
| 148 | - def stash_changes(self, message: str) -> None: | |
| 149 | + def stash_changes(self, message: str | None = None) -> None: | |
| 149 | 150 | # Stash local changes after DB reverts so reverts use current on-disk migration files. |
| 150 | 151 | self._run( |
| 151 | 152 | "stash", |
| 152 | 153 | "push", |
| 153 | 154 | "--include-untracked", |
| 154 | 155 | "--message", |
| 155 | - message, | |
| 156 | + message or "stash before mitch checkout", | |
| 156 | 157 | ) |
| 157 | 158 | |
| 158 | 159 | def restore_changes(self) -> None: |
| 159 | 160 | # Restore local changes after checkout and migration re-application. |
| 160 | 161 | self._run("stash", "pop", "--index") |
| 161 | - | |
| 162 | - | |
| 163 | -@contextmanager | |
| 164 | -def _stashed_worktree( | |
| 165 | - git_manager: VersionControlManagerProtocol, | |
| 166 | - enabled: bool, | |
| 167 | - message: str, | |
| 168 | -): | |
| 169 | - if not enabled: | |
| 170 | - yield | |
| 171 | - return | |
| 172 | - | |
| 173 | - git_manager.stash_changes(message) | |
| 174 | - try: | |
| 175 | - yield | |
| 176 | - except Exception: | |
| 162 | + | |
| 163 | + @contextmanager | |
| 164 | + def stashed(self): | |
| 165 | + self.stash_changes() | |
| 177 | 166 | try: |
| 178 | - git_manager.restore_changes() | |
| 179 | - except Exception as restore_error: | |
| 180 | - typer.echo( | |
| 181 | - "Error: checkout failed and stashed changes could not be restored automatically.", | |
| 182 | - err=True, | |
| 183 | - ) | |
| 184 | - raise RuntimeError("Failed to restore stashed changes.") from restore_error | |
| 185 | - raise | |
| 186 | - else: | |
| 187 | - git_manager.restore_changes() | |
| 167 | + yield | |
| 168 | + except Exception: | |
| 169 | + try: | |
| 170 | + self.restore_changes() | |
| 171 | + except Exception as restore_error: | |
| 172 | + typer.echo( | |
| 173 | + "Error: checkout failed and stashed changes could not be restored automatically. (Search for 'stash before mitch checkout' in your `git stash list` to recover changes.)", | |
| 174 | + err=True, | |
| 175 | + ) | |
| 176 | + raise RuntimeError("Failed to restore stashed changes.") from restore_error | |
| 177 | + raise | |
| 178 | + else: | |
| 179 | + self.restore_changes() | |
| 188 | 180 | |
| 189 | 181 | |
| 190 | 182 | @app.command(name="checkout") |
| @@ -290,7 +282,7 @@ | ||
| 290 | 282 | |
| 291 | 283 | |
| 292 | 284 | # For dirty worktrees, stash only after DB reverts so reverts reflect current local files. |
| 293 | - with _stashed_worktree(git_manager, should_pop_stash, f"mitch checkout {branch}"): | |
| 285 | + with git_manager.stashed() if should_pop_stash else nullcontext(): | |
| 294 | 286 | # Switch working tree to the target branch once pre-checkout DB reverts are complete. |
| 295 | 287 | git_manager.checkout(branch) |
| 296 | 288 | |