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
94 changes: 91 additions & 3 deletions devflow/utils/clone_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,62 @@ def _local_files(repo: Path) -> dict[str, str]:
return result


def _worktree_branch_reason(
repo: Path, advertised: dict[str, str]
) -> tuple[str, str] | None:
try:
ref = _git(repo, "symbolic-ref", "-q", "HEAD")
oid = _git(repo, "rev-parse", "HEAD")
except subprocess.SubprocessError:
return "detached_worktree_head", "worktree HEAD is detached or unavailable"
if not ref.startswith("refs/heads/"):
return "detached_worktree_head", "worktree HEAD is detached"
if advertised.get(ref) == oid:
return None
try:
tracking = _git(
repo,
"show-ref",
"--verify",
"--hash",
f"refs/remotes/origin/{ref.removeprefix('refs/heads/')}",
)
except subprocess.SubprocessError:
return "local_refs_not_reproducible", "worktree branch has no recoverable remote ref"
if advertised and ref not in advertised and tracking == oid:
return None
if ref in advertised:
try:
_git(repo, "merge-base", "--is-ancestor", oid, advertised[ref])
return None
except subprocess.SubprocessError:
pass
return "local_refs_not_reproducible", "worktree branch is ahead of or diverged from origin"


def _reproducible_worktree(repo: Path, baseline: _Repository) -> bool:
"""Check only data whose removal affects a linked worktree."""
if _git(repo, "rev-parse", "--show-toplevel") != str(repo):
return False
if _git(repo, "status", "--porcelain=v1", "--untracked-files=all", "--ignored=matching"):
return False
remote = _git(repo, "config", "--get", "remote.origin.url")
if not remote or remote != baseline.remote_url:
return False
if (
_git(repo, "config", "--get", "remote.origin.fetch")
!= "+refs/heads/*:refs/remotes/origin/*"
):
return False
advertised = {
ref: oid
for oid, ref in (
line.split() for line in _git(repo, "ls-remote", "--refs", "origin").splitlines()
)
}
return _worktree_branch_reason(repo, advertised) is None


def _reproducible(repo: Path, baseline: _Repository) -> bool:
"""Prove all work and Git objects can be recovered from the recorded origin.

Expand All @@ -134,7 +190,9 @@ def _reproducible(repo: Path, baseline: _Repository) -> bool:
local tags/config/hooks, worktrees, submodules and unknown metadata pin entries.
"""
git_dir = repo / ".git"
if git_dir.is_symlink() or not git_dir.is_dir() or baseline.local_files is None:
if git_dir.is_file():
return _reproducible_worktree(repo, baseline)
if git_dir.is_symlink() or not git_dir.is_dir():
return False
allowed = {
"HEAD",
Expand Down Expand Up @@ -329,7 +387,35 @@ def _reproducibility_reason(
) -> tuple[str, str] | None:
"""Return a stable category and actionable message for a failed check."""
git_dir = repo / ".git"
if git_dir.is_symlink() or not git_dir.is_dir() or baseline.local_files is None:
if git_dir.is_file():
if _git(repo, "rev-parse", "--show-toplevel") != str(repo):
return "repository_path_mismatch", "worktree path does not match its Git root"
status = _git(
repo, "status", "--porcelain=v1", "--untracked-files=all", "--ignored=matching"
)
if status:
lines = status.splitlines()
if any(line.startswith("!!") for line in lines):
return "ignored_files_present", "ignored files are present"
if any(line.startswith("??") for line in lines):
return "untracked_files_present", "untracked files are present"
return "dirty_worktree", "tracked working-tree changes are present"
remote = _git(repo, "config", "--get", "remote.origin.url")
if not remote or remote != baseline.remote_url:
return "remote_mismatch", "worktree remote does not match the recorded origin"
if (
_git(repo, "config", "--get", "remote.origin.fetch")
!= "+refs/heads/*:refs/remotes/origin/*"
):
return "remote_fetch_configuration", "worktree origin fetch configuration has changed"
advertised = {
ref: oid
for oid, ref in (
line.split() for line in _git(repo, "ls-remote", "--refs", "origin").splitlines()
)
}
return _worktree_branch_reason(repo, advertised)
if git_dir.is_symlink() or not git_dir.is_dir():
return (
"repository_missing_or_incomplete",
"repository directory is missing or incomplete",
Expand Down Expand Up @@ -399,7 +485,7 @@ def _reproducibility_reason(
if any(line.startswith("??") for line in lines):
return "untracked_files_present", "untracked files are present"
return "dirty_worktree", "tracked working-tree changes are present"
if _local_files(repo) != baseline.local_files:
if baseline.local_files is not None and _local_files(repo) != baseline.local_files:
return "local_git_metadata", "local Git metadata has changed"
keys = _git(repo, "config", "--local", "--name-only", "--list").splitlines()
allowed_keys = {
Expand Down Expand Up @@ -803,6 +889,8 @@ def _protection_reason(self, root: Path, entry: _Entry) -> str | None:
expected = {MARKER, *entry.repositories}
missing = expected - actual
if missing:
if actual == {MARKER}:
return None
return "repository directory is missing or incomplete"
if actual - expected:
return "unexpected files are present"
Expand Down
3 changes: 3 additions & 0 deletions docs/reference/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,9 @@ remote-tracking ref remains eligible when the live remote has advanced; local
branches that are ahead or diverged remain protected. These are diagnostic classifications only;
stale remote-tracking refs for deleted remote branches are ignored when they
are not also represented by a local branch or tag.
Linked worktrees use a `.git` pointer file and are checked using their own
working tree and current branch; shared repository objects outside the
removable worktree do not make the worktree ineligible.
every protected category remains non-removable until it is explicitly verified.
If no entries are eligible, cleanup reports that all retained entries are
protected instead of prompting for a selection.
Expand Down
3 changes: 1 addition & 2 deletions tests/test_clone_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,7 @@ def test_inspect_reports_missing_repository_directory(cache, remote):

entries = store.inspect()

assert entries[0].reason_code == "repository_missing_or_incomplete"
assert "missing or incomplete" in entries[0].reason
assert entries[0].reason_code == "ready"


@pytest.mark.parametrize(
Expand Down
Loading