From 9fc19b086689e9b1c0a031e5faf841b454468d64 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sat, 13 Jul 2024 08:40:39 +0200 Subject: [PATCH] maintainers/scripts/update.py: Fix worktree cleanup on failure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- maintainers/scripts/update.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/maintainers/scripts/update.py b/maintainers/scripts/update.py index 2aebecd10b6a..5803ef32f6e7 100644 --- a/maintainers/scripts/update.py +++ b/maintainers/scripts/update.py @@ -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: