maintainers/scripts/update.py: Fix worktree cleanup on failure

The context manager would previously just terminate early on exception.
As a result, the worktree and branch would not get pruned when update script failed.

Let’s wrap the cleanup code in `finally` block as suggested by Python docs:
https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager
This commit is contained in:
Jan Tojnar 2024-07-13 08:40:39 +02:00
parent 3c78e0ce04
commit 9fc19b0866

View File

@ -91,9 +91,11 @@ def make_worktree() -> Generator[Tuple[str, str], None, None]:
target_directory = f'{wt}/nixpkgs'
subprocess.run(['git', 'worktree', 'add', '-b', branch_name, target_directory])
yield (target_directory, branch_name)
subprocess.run(['git', 'worktree', 'remove', '--force', target_directory])
subprocess.run(['git', 'branch', '-D', branch_name])
try:
yield (target_directory, branch_name)
finally:
subprocess.run(['git', 'worktree', 'remove', '--force', target_directory])
subprocess.run(['git', 'branch', '-D', branch_name])
async def commit_changes(name: str, merge_lock: asyncio.Lock, worktree: str, branch: str, changes: List[Dict]) -> None:
for change in changes: