Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- 新增 `dyro line add-repo`:把一个已经登记的仓库加入现有开发线。已有子线保持不变;子线只能加入父线里已经有的仓库。
- 修复 `dyro line create` / `dyro line spawn` 中途失败回滚时留下本次新建分支的问题:之前用另一个基线重试会静默复用旧分支;现在回滚会删除本次新建的分支,原本就有的分支保持不动。

## 0.7.18 - 2026-11-14

对使用者的影响:修复 0.7.17 中 `dyro line merge --into --push` 不写远端锚点、推送前就报完成的问题;macOS 上工作区或主目录位于 `/var`、`/tmp` 下时不再误报不安全文件或清单过期。
Expand Down
37 changes: 37 additions & 0 deletions src/dyro/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@
)
from .workspace import (
OBSERVATION_DEADLINE_FINDING,
add_line_repository,
blocking_line_doctor_failures,
create_line,
doctor,
Expand Down Expand Up @@ -3124,6 +3125,23 @@ def cmd_line_spawn(args: argparse.Namespace) -> None:
)


def cmd_line_add_repo(args: argparse.Namespace) -> None:
config = _config(args)
_require_yes(args, "向开发线加入仓库")
line = add_line_repository(
config,
args.line,
args.repo,
base=args.base,
storage=args.storage,
dry_run=args.dry_run,
)
print(
f"{'DRY RUN: ' if args.dry_run else ''}已为 {line.kind} {line.id} 加入仓库 {args.repo},"
f"分支 {line.branch},基线 {line.base_for(args.repo)},存储 {line.storage_for(args.repo)}"
)


def cmd_line_merge(args: argparse.Namespace) -> None:
config = _config(args)
if getattr(args, "recover", None) is not None:
Expand Down Expand Up @@ -5401,6 +5419,25 @@ def build_parser() -> argparse.ArgumentParser:
)
line_spawn.add_argument("--yes", action="store_true")
line_spawn.set_defaults(func=cmd_line_spawn)
line_add_repo = line_sub.add_parser(
"add-repo", help="把一个已登记仓库加入现有开发线(不改子线)"
)
line_add_repo.add_argument("line", help="开发线 ID")
line_add_repo.add_argument("repo", help="已登记的仓库 ID")
line_add_repo.add_argument("--base", help="该仓库的基线;顶层线默认用线的 base")
line_add_repo.add_argument(
"--storage",
help="存储方式:linked-worktree 或 anchor-reference;默认 linked-worktree",
)
line_add_repo.add_argument(
"--dry-run",
dest="dry_run",
action="store_true",
default=argparse.SUPPRESS,
help="只打印计划,不创建工作树或改清单(兼容全局 --dry-run)",
)
line_add_repo.add_argument("--yes", action="store_true")
line_add_repo.set_defaults(func=cmd_line_add_repo)
line_merge = line_sub.add_parser(
"merge",
help="子线合入父线(--into)、跨线合入(--from)或 --recover 对账",
Expand Down
259 changes: 204 additions & 55 deletions src/dyro/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,17 @@ def _linked_worktree_command(
)


def _remove_line_worktree(config: Config, line: Line, repo_id: str, destination: Path) -> str | None:
"""Best-effort cleanup for one worktree or anchor-reference created during line setup."""
def _remove_line_worktree(
config: Config, line: Line, repo_id: str, destination: Path, *, new_branch: bool = False
) -> str | None:
"""Best-effort cleanup for one worktree or anchor-reference created during line setup.

``new_branch`` means this setup created ``line.branch`` with ``-b``; delete it too,
otherwise a retry with another base would silently reuse the leftover branch.
The delete is attempted even after a path-cleanup failure; Git refuses it while
the branch is still checked out, and both failures are reported.
"""
failure: str | None = None
try:
if line.storage_for(repo_id) == "anchor-reference" or destination.is_symlink():
if destination.is_symlink() or destination.exists():
Expand All @@ -452,18 +461,29 @@ def _remove_line_worktree(config: Config, line: Line, repo_id: str, destination:
shutil.rmtree(destination)
prune = git(anchor, "worktree", "prune", timeout=60)
if prune.code != 0:
return f"{repo_id}: worktree remove failed: {result.stdout.strip() or 'unknown error'}"
return None
failure = f"{repo_id}: worktree remove failed: {result.stdout.strip() or 'unknown error'}"
except OSError as exc:
return f"{repo_id}: {exc}"
return None
failure = f"{repo_id}: {exc}"
if new_branch:
deleted = git(repository_path(config, repo_id), "branch", "-D", line.branch, timeout=60)
if deleted.code != 0:
note = (
f"{repo_id}: 删除本次新建分支 {line.branch} 失败:"
f"{deleted.stdout.strip() or 'unknown error'}"
)
failure = f"{failure}; {note}" if failure else note
return failure


def _rollback_line_creations(config: Config, line: Line, created: list[str]) -> list[str]:
def _rollback_line_creations(
config: Config, line: Line, created: list[tuple[str, bool]]
) -> list[str]:
failures: list[str] = []
for repo_id in reversed(created):
for repo_id, new_branch in reversed(created):
destination = line_repository_path(config, line, repo_id)
failure = _remove_line_worktree(config, line, repo_id, destination)
failure = _remove_line_worktree(
config, line, repo_id, destination, new_branch=new_branch
)
if failure:
failures.append(failure)
target_root = line_root(config, line)
Expand All @@ -480,6 +500,20 @@ def _rollback_line_creations(config: Config, line: Line, created: list[str]) ->
return failures


def _remove_empty_parents(destination: Path, stop: Path) -> str | None:
"""Remove empty ancestors of destination, stopping before stop itself."""
current = destination.parent
try:
while current != stop and stop in current.parents:
if not current.is_dir() or any(current.iterdir()):
return None
current.rmdir()
current = current.parent
except OSError as exc:
return str(exc)
return None


def _build_line(
config: Config,
*,
Expand Down Expand Up @@ -527,6 +561,37 @@ def _build_line(
)


def _plan_repository_creation(
config: Config, line: Line, repo_id: str
) -> tuple[str, Path, tuple[str, ...]]:
"""Validate one repository and return its worktree destination and Git command."""
anchor = repository_path(config, repo_id)
destination = line_repository_path(config, line, repo_id)
if not _is_git_repo(anchor):
raise DyroError(f"仓库 anchor 不存在或不是 Git 仓库:{anchor}")
_ensure_clean(anchor)
repo_base = line.base_for(repo_id)
require_ok(
git_read(anchor, "rev-parse", "--verify", f"{repo_base}^{{commit}}"),
f"校验 {repo_id} 基线 {repo_base}",
)
if destination.exists() or destination.is_symlink():
raise DyroError(f"worktree 目标已存在:{destination}")
if line.storage_for(repo_id) == "anchor-reference":
anchor_branch = require_ok(
git_read(anchor, "branch", "--show-current"),
f"读取 {repo_id} anchor 分支",
).stdout.strip()
if anchor_branch != line.branch:
raise DyroError(
f"{repo_id} 的 anchor-reference 要求 anchor 正位于 {line.branch},"
f"当前为 {anchor_branch or 'DETACHED'}"
)
return (repo_id, destination, ())
command = _linked_worktree_command(anchor, repo_id, line, repo_base, destination)
return (repo_id, destination, command)


def _plan_line_creation(
config: Config, line: Line
) -> list[tuple[str, Path, tuple[str, ...]]]:
Expand All @@ -536,37 +601,9 @@ def _plan_line_creation(
raise DyroError(f"目标工作区已存在且非空:{target_root}")

# Validate every repository before mutating any worktree so partial creates are rare.
planned: list[tuple[str, Path, tuple[str, ...]]] = []
for repo_id in line.repositories:
anchor = repository_path(config, repo_id)
destination = line_repository_path(config, line, repo_id)
if not _is_git_repo(anchor):
raise DyroError(f"仓库 anchor 不存在或不是 Git 仓库:{anchor}")
_ensure_clean(anchor)
repo_base = line.base_for(repo_id)
require_ok(
git_read(anchor, "rev-parse", "--verify", f"{repo_base}^{{commit}}"),
f"校验 {repo_id} 基线 {repo_base}",
)
if destination.exists() or destination.is_symlink():
raise DyroError(f"worktree 目标已存在:{destination}")
if line.storage_for(repo_id) == "anchor-reference":
anchor_branch = require_ok(
git_read(anchor, "branch", "--show-current"),
f"读取 {repo_id} anchor 分支",
).stdout.strip()
if anchor_branch != line.branch:
raise DyroError(
f"{repo_id} 的 anchor-reference 要求 anchor 正位于 {line.branch},"
f"当前为 {anchor_branch or 'DETACHED'}"
)
planned.append((repo_id, destination, ()))
continue
command = _linked_worktree_command(
anchor, repo_id, line, repo_base, destination
)
planned.append((repo_id, destination, command))
return planned
return [
_plan_repository_creation(config, line, repo_id) for repo_id in line.repositories
]


def preflight_line(
Expand Down Expand Up @@ -632,27 +669,42 @@ def create_line(
return _create_line_worktrees(config, line, dry_run=dry_run)


def _create_repository_worktree(
config: Config,
line: Line,
repo_id: str,
destination: Path,
command: tuple[str, ...],
*,
dry_run: bool,
) -> bool:
"""Create one worktree. Return whether a path was actually created."""
if line.storage_for(repo_id) == "anchor-reference":
if dry_run:
return False
destination.parent.mkdir(parents=True, exist_ok=True)
destination.symlink_to(repository_path(config, repo_id), target_is_directory=True)
return True
if not dry_run:
destination.parent.mkdir(parents=True, exist_ok=True)
require_ok(
git(repository_path(config, repo_id), *command, dry_run=dry_run, timeout=300),
f"创建 {repo_id} worktree",
)
return not dry_run


def _create_line_worktrees(config: Config, line: Line, *, dry_run: bool) -> Line:
"""Create and roll back only while the caller holds L0 and every L2."""
planned = _plan_line_creation(config, line)

created: list[str] = []
created: list[tuple[str, bool]] = []
try:
for repo_id, destination, command in planned:
if line.storage_for(repo_id) == "anchor-reference":
if not dry_run:
destination.parent.mkdir(parents=True, exist_ok=True)
destination.symlink_to(repository_path(config, repo_id), target_is_directory=True)
created.append(repo_id)
continue
if not dry_run:
destination.parent.mkdir(parents=True, exist_ok=True)
require_ok(
git(repository_path(config, repo_id), *command, dry_run=dry_run, timeout=300),
f"创建 {repo_id} worktree",
)
if not dry_run:
created.append(repo_id)
if _create_repository_worktree(
config, line, repo_id, destination, command, dry_run=dry_run
):
created.append((repo_id, "-b" in command))
_write_line(config, line, dry_run=dry_run)
except Exception as exc:
if created:
Expand Down Expand Up @@ -774,6 +826,103 @@ def spawn_line(
return line


def add_line_repository(
config: Config,
line_id: str,
repo_id: str,
*,
base: str | None = None,
storage: str | None = None,
dry_run: bool = False,
) -> Line:
"""Add one already-registered repository to one existing line.

Child lines of this line are not modified. The worktree is created before
the manifest rewrite; a later failure removes only that new destination.
"""
with line_identity_lock(config):
with exclusive_lock(
merge_lock_path(config, line_id),
timeout_seconds=MERGE_LOCK_TIMEOUT_SECONDS,
):
line = get_line(config, line_id)
require_no_unfinished_op(config, line.id)
if repo_id not in config.repositories:
raise ValidationError(f"未配置的仓库:{repo_id}")
if repo_id in line.repositories:
raise DyroError(f"仓库 {repo_id} 已包含在开发线 {line.id} 中")
if line.parent:
parent = get_line(config, line.parent)
if repo_id not in parent.repositories:
raise ValidationError(
f"子线仓库必须是父线 {parent.id} 仓库的子集:{repo_id};"
f"请先把该仓库加入父线 {parent.id}"
)
if base is not None or storage is not None:
raise ValidationError(
f"子线 {line.id} 的仓库基线与存储方式必须从父线 {parent.id} 派生,"
"不能指定 --base 或 --storage"
)
resolved_base = _spawn_base_for(config, parent, repo_id)
resolved_storage = parent.storage_for(repo_id)
else:
resolved_base = (base or line.base).strip()
if not resolved_base:
raise ValidationError(f"{repo_id} 的基线不能为空")
resolved_storage = storage or "linked-worktree"
if resolved_storage not in STORAGE_MODES:
raise ValidationError(
f"{repo_id} 的存储方式必须是:{', '.join(sorted(STORAGE_MODES))}"
)
updated = replace(
line,
repositories=line.repositories + (repo_id,),
repository_bases=(
{**line.repository_bases, repo_id: resolved_base}
if resolved_base != line.base
else dict(line.repository_bases)
),
storage_modes=(
{**line.storage_modes, repo_id: resolved_storage}
if resolved_storage != "linked-worktree"
else dict(line.storage_modes)
),
)
with anchor_locks(config, [repo_id]):
_repo_id, destination, command = _plan_repository_creation(
config, updated, repo_id
)
created = False
try:
created = _create_repository_worktree(
config,
updated,
repo_id,
destination,
command,
dry_run=dry_run,
)
_write_line(config, updated, dry_run=dry_run)
except Exception as exc:
if created:
failures: list[str] = []
failure = _remove_line_worktree(
config, updated, repo_id, destination, new_branch="-b" in command
)
if failure:
failures.append(failure)
parent_failure = _remove_empty_parents(
destination, line_root(config, line)
)
if parent_failure:
failures.append(parent_failure)
if failures:
detail = "; ".join(failures)
raise DyroError(f"{exc}\n自动清理未完全成功:{detail}") from exc
raise
return updated


@dataclass(frozen=True)
class _LineMergePlan:
repository: str
Expand Down
Loading
Loading