Public repository: timo / mitch

Reader

A migration stitcher (therefore mitch) for PostgreSQL, SQLite, MariaDB, and others

Cleanup checkout.py

public

Cleanup checkout.py
Changesetc8fe3f228c5400bf08f483148ae20e235070c21d
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parents5c96730c9b63
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
22
 from pathlib import Path
33
 from typing import Annotated, Literal, NamedTuple, Protocol, Generator
44
 from dataclasses import dataclass
@@ -57,8 +57,9 @@
5757
     def is_dirty(self) -> bool: ...
5858
     def changes(self, branch: str) -> Generator[Add | Modified | Deleted | TypeChanged | Renamed | Copied, None, None]: ...
5959
     def checkout(self, branch: str) -> None: ...
60
-    def stash_changes(self, message: str) -> None: ...
60
+    def stash_changes(self, message: str | None = None) -> None: ...
6161
     def restore_changes(self) -> None: ...
62
+    def stashed(self) -> AbstractContextManager: ...
6263
 
6364
 
6465
 class GitManager(VersionControlManagerProtocol):
@@ -145,46 +146,37 @@
145146
     def checkout(self, branch: str) -> None:
146147
         self._run("checkout", branch)
147148
 
148
-    def stash_changes(self, message: str) -> None:
149
+    def stash_changes(self, message: str | None = None) -> None:
149150
         # Stash local changes after DB reverts so reverts use current on-disk migration files.
150151
         self._run(
151152
             "stash",
152153
             "push",
153154
             "--include-untracked",
154155
             "--message",
155
-            message,
156
+            message or "stash before mitch checkout",
156157
         )
157158
 
158159
     def restore_changes(self) -> None:
159160
         # Restore local changes after checkout and migration re-application.
160161
         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()
177166
         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()
188180
 
189181
 
190182
 @app.command(name="checkout")
@@ -290,7 +282,7 @@
290282
 
291283
 
292284
     # 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():
294286
         # Switch working tree to the target branch once pre-checkout DB reverts are complete.
295287
         git_manager.checkout(branch)
296288