Repository navigation
Resume merged branch cleanup before removing the worktree #1760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
36995ce
c87e64b
c2a3dac
8e40527
b592248
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| import tempfile | ||
| from collections.abc import Iterator | ||
| from contextlib import contextmanager, suppress | ||
| from itertools import pairwise | ||
| from pathlib import Path | ||
| from select import select | ||
|
|
||
|
|
@@ -309,23 +310,62 @@ def _require_no_ignored_user_files(wt: Path) -> None: | |
| raise WorktreeError(f"worktree contains ignored file {path!r}, refusing to remove: {wt}") | ||
|
|
||
|
|
||
| def cleanup(wt: Path, expected_head: str) -> None: | ||
| """Remove a clean merged worktree and its branch if it has no local user files.""" | ||
| def detached_from(wt: Path) -> str | None: | ||
| """Return the branch named by the latest checkout to this detached HEAD.""" | ||
| if _git(wt, "branch", "--show-current", preserve_whitespace=True): | ||
| return None | ||
| entries = _git(wt, "reflog", "--format=%H %gs", "HEAD", preserve_whitespace=True).splitlines() | ||
| sha = head(wt) | ||
| for current, previous in pairwise(entries): | ||
| if " checkout: moving from " in current: | ||
| match = re.fullmatch(rf"{sha} checkout: moving from (.+) to {sha}", current) | ||
| # The prior entry records the actual old SHA, even if checkout | ||
| # read its source branch before a concurrent HEAD update. | ||
| return match[1] if match and previous.startswith(sha + " ") else None | ||
| return None | ||
|
|
||
|
|
||
| def cleanup(wt: Path, expected_head: str, name: str | None = None) -> None: | ||
| """Delete a verified merged branch before removing its clean linked worktree.""" | ||
| wt = Path(wt).absolute() | ||
| if not wt.exists(): | ||
| raise WorktreeError(f"no such worktree: {wt}") | ||
| if head(wt) != expected_head: | ||
| raise WorktreeError("local HEAD differs from merged PR head") | ||
| name = branch(wt) | ||
| name = name if name is not None else branch(wt) | ||
| gd = Path(_git(wt, *_ABSOLUTE_COMMON_DIR, preserve_whitespace=True)) | ||
| ref = f"refs/heads/{name}" | ||
| with _lock_verified_ref(wt, ref, expected_head): | ||
| if branch(wt) != name: | ||
| raise WorktreeError("local branch changed during merge cleanup") | ||
| if is_dirty(wt): | ||
| raise WorktreeError(f"worktree is dirty, refusing to remove: {wt}") | ||
| if Path(_git(wt, "rev-parse", "--absolute-git-dir", preserve_whitespace=True)) == gd: | ||
| raise WorktreeError("refusing cleanup from the primary checkout") | ||
| current = _git(wt, "branch", "--show-current", preserve_whitespace=True) | ||
| if current: | ||
| _detach_clean_worktree(wt, expected_head, name) | ||
| with _lock_verified_ref(wt, "HEAD", expected_head): | ||
| if detached_from(wt) != name: | ||
| raise WorktreeError("local checkout changed during merge cleanup") | ||
| _require_clean_worktree(wt) | ||
| _delete_merged_branch(gd, name, expected_head) | ||
| _require_no_ignored_user_files(wt) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: Ignored-file scan runs twice under the HEAD lock
|
||
| _git(gd, f"--git-dir={gd}", "worktree", "remove", str(wt)) # never the primary checkout | ||
| _git(gd, f"--git-dir={gd}", "worktree", "remove", str(wt)) | ||
|
|
||
|
|
||
| def _require_clean_worktree(wt: Path) -> None: | ||
| if is_dirty(wt): | ||
| raise WorktreeError(f"worktree is dirty, refusing to remove: {wt}") | ||
| _require_no_ignored_user_files(wt) | ||
|
|
||
|
|
||
| def _detach_clean_worktree(wt: Path, expected_head: str, name: str) -> None: | ||
| with _lock_verified_ref(wt, f"refs/heads/{name}", expected_head): | ||
| if _git(wt, "branch", "--show-current", preserve_whitespace=True) != name: | ||
| raise WorktreeError("local branch changed during merge cleanup") | ||
| _require_clean_worktree(wt) | ||
| # Git needs its own HEAD lock for the switch. Cleanup verifies the checkout | ||
| # recorded by Git and takes the detached HEAD lock before deleting anything. | ||
| _git(wt, "-c", "core.logAllRefUpdates=true", "switch", "--quiet", "--detach", expected_head) | ||
|
|
||
|
|
||
| def _delete_merged_branch(gd: Path, name: str, expected_head: str) -> None: | ||
| ref = f"refs/heads/{name}" | ||
| worktrees = _git(gd, f"--git-dir={gd}", "worktree", "list", "--porcelain", "-z") | ||
| if f"branch {ref}" in worktrees.split("\0"): | ||
| raise WorktreeError(f"local branch {name!r} is checked out in another worktree") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Closed-PR history paging duplicates merged_pr_for_branch with weaker validation
_merged_pr_from_historyreimplements thepulls?state=closed&head=owner:branchpaging loop thatmerged_pr_for_branch(github.py:750) already has. The new copy is missing some of the original's checks. It does not reject pages with more thanPAGErows, it does not dedupe repeated PR numbers, and it skips the row validation in_closed_history_number. Also,_matching_merged_prraises a generic("gh","api")error that does not name the endpoint. Because_pr_detailand the exact head/branch match recheck every candidate, this does not create a safety hole. It does mean two history readers can drift apart. Smallest fix: run the closed-history walk through the existing validation (_closed_history_numberplus the page-size check), or add an optional exact-shafilter tomerged_pr_for_branchinstead of keeping a parallel loop.