From d91efd7ca51ad1d9d3e79ef25a5fa72548a08f64 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 08:12:50 +0100 Subject: [PATCH] Append task excludes without overwriting concurrent edits Architecture-Review: unchanged; start still installs the same repository-local Git ignore rules before adding a task checkout. Co-Authored-By: Codex --- src/loopzero/worktree.py | 37 +++++++++++++++++++------------------ tests/test_worktree.py | 22 +++++++++++++++++----- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/loopzero/worktree.py b/src/loopzero/worktree.py index 7bf653e3..0ad509e2 100644 --- a/src/loopzero/worktree.py +++ b/src/loopzero/worktree.py @@ -3,17 +3,17 @@ from __future__ import annotations import errno +import fcntl +import os import re -import stat import tempfile from pathlib import Path -from uuid import uuid4 from loopzero._proc import run GIT_ENV = ("PATH", "HOME", "LANG", "LC_ALL", "TERM", "GIT_SSH_COMMAND", "SSH_AUTH_SOCK") _ABSOLUTE_COMMON_DIR = ("rev-parse", "--path-format=absolute", "--git-common-dir") -EXCLUDES = (".worktrees/", ".loopzero/") +EXCLUDES = (b".worktrees/", b".loopzero/") _BASE_LINE = re.compile(r"^Base:\s*([0-9a-fA-F]{7,40})\s*$", re.MULTILINE) _TASK_TEMPLATE = """# {title} @@ -105,21 +105,22 @@ def _ensure_excludes(repo_root: Path) -> None: exclude = common / "info" / "exclude" exclude.parent.mkdir(parents=True, exist_ok=True) exclude = exclude.resolve() - existing = exclude.read_text() if exclude.exists() else "" - missing = [entry for entry in EXCLUDES if entry not in existing.splitlines()] - if missing: - sep = "" if not existing or existing.endswith("\n") else "\n" - mode = stat.S_IMODE(exclude.stat().st_mode) if exclude.exists() else None - temp = exclude.with_name(f".exclude-{uuid4().hex}") - stream = temp.open("x") - try: - with stream: - stream.write(existing + sep + "\n".join(missing) + "\n") - if mode is not None: - temp.chmod(mode) - temp.replace(exclude) - finally: - temp.unlink(missing_ok=True) + while True: + fd = os.open(exclude, os.O_RDWR | os.O_CREAT | os.O_APPEND | os.O_NOFOLLOW, 0o666) + with os.fdopen(fd, "rb") as stream: + fcntl.flock(fd, fcntl.LOCK_EX) + lines = set(stream.read().splitlines()) + missing = [entry for entry in EXCLUDES if entry not in lines] + if missing: + # An editor can append an unterminated line before this write. + rules = b"\n" + b"\n".join(missing) + b"\n" + if os.write(fd, rules) != len(rules): + raise WorktreeError(f"incomplete write to Git exclude: {exclude}") + try: + if os.path.samestat(os.fstat(fd), exclude.lstat()): + return + except FileNotFoundError: + pass def start(repo_root: Path, slug: str, base_branch: str) -> Path: diff --git a/tests/test_worktree.py b/tests/test_worktree.py index 230adc02..3002e0d8 100644 --- a/tests/test_worktree.py +++ b/tests/test_worktree.py @@ -131,15 +131,27 @@ def interrupt_after_add(cwd, *args, **kwargs): assert task.read_text().endswith("(filled by `loopzero review`)\n") -def test_start_excludes_worktrees_and_task_dir(repo: Path) -> None: +@pytest.mark.parametrize("replacement", [False, True], ids=["append", "replace"]) +def test_start_excludes_worktrees_and_task_dir(repo: Path, monkeypatch, replacement) -> None: exclude = repo / ".git" / "info" / "exclude" - exclude.parent.mkdir(parents=True, exist_ok=True) exclude.write_text("*.log") # no trailing newline on purpose old_inode = exclude.stat().st_ino + write = worktree.os.write + + def interleave(fd, rules): + if rules.endswith(b".loopzero/\n"): + edit = exclude.with_suffix(".edited") if replacement else exclude + with edit.open("w" if replacement else "a") as stream: + stream.write("*.log\nlocal.env" if replacement else "\nlocal.env") + if replacement: + edit.replace(exclude) + monkeypatch.setattr(worktree.os, "write", write) + return write(fd, rules) + + monkeypatch.setattr(worktree.os, "write", interleave) wt = worktree.start(repo, "t1", "main") - assert exclude.stat().st_ino != old_inode - lines = exclude.read_text().splitlines() - assert lines == ["*.log", ".worktrees/", ".loopzero/"] + assert (exclude.stat().st_ino != old_inode) == replacement + assert exclude.read_text().splitlines() == ["*.log", "local.env", ".worktrees/", ".loopzero/"] assert not worktree.is_dirty(wt), "fresh worktree with task.md is clean" assert git(repo, "status", "--porcelain") == "" installed = exclude.stat()