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
107 changes: 103 additions & 4 deletions infra/github-backup/bin/clone_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
re-running skips verified mirrors.
"""

import json, os, subprocess, sys, time
import json, os, shutil, subprocess, sys, time
from concurrent.futures import ThreadPoolExecutor

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
Expand All @@ -18,6 +18,65 @@
TIMEOUT = 5400


def clear_partial_dest(root, dest):
"""Remove a leftover partial clone destination (#3233).

A clone killed mid-flight (driver TIMEOUT, interrupt, laptop sleep)
leaves a non-empty destination that is not a valid mirror; git
refuses to clone into it ('already exists and is not an empty
directory'), so without this every rerun wedges on the same repo.

Safety: only the driver-managed mirror path itself is removed —
dest must resolve to <root>/<owner>/<repo>.git exactly as
mirror_path builds it. Anything else is left untouched and the
caller fails loudly. An empty directory is not partial (git clone
accepts it), and a valid mirror never reaches this helper.
Returns True when a partial destination was cleared.
"""
if not os.path.lexists(dest):
return False
if os.path.isdir(dest) and not os.path.islink(dest) and not os.listdir(dest):
return False
root_real = os.path.realpath(root)
dest_real = os.path.realpath(dest)
if os.path.dirname(os.path.dirname(dest_real)) != root_real:
return False
if os.path.isdir(dest) and not os.path.islink(dest):
shutil.rmtree(dest)
else:
os.remove(dest)
return True


def dest_is_confirmed_partial(dest):
"""True only when dest is positively known not to be a repository.

valid_mirror() returning False does not by itself prove a partial
clone: git also refuses intact mirrors for policy reasons (dubious
ownership after a restore, safe.bareRepository=explicit), and
deleting an intact backup because inspection failed would be far
worse than the wedge this fixes (Codex P1 on the #3233 PR). So:

- a non-directory at the mirror path is a stray file — partial;
- git answering at all (rc 0) means a real repository lives there
— never partial, whatever valid_mirror made of it;
- git saying 'not a git repository' is the killed-clone / junk
shape — partial;
- any other git failure is an inspection/policy error — NOT
confirmed partial, and the caller preserves the destination.
"""
if not os.path.isdir(dest):
return True
r = subprocess.run(
["git", "-C", dest, "rev-parse", "--is-bare-repository"],
capture_output=True,
text=True,
)
if r.returncode == 0:
return False
return "not a git repository" in (r.stderr or "")


def main():
cfg = load_config()
root = backup_root(cfg)
Expand Down Expand Up @@ -49,6 +108,37 @@ def clone_one(item, ref_ok):
"note": "already-present",
"disk_kb": disk_kb(dest),
}
# #3233: a destination that exists but is not a valid mirror may
# be the leftover of a killed clone — clear it and retry, or the
# clone below fails on 'already exists' forever. But only a
# CONFIRMED partial is cleared: if git can open the destination
# as a repository, or cannot inspect it for policy reasons
# (dubious ownership, safe.bareRepository), it may be an intact
# backup — preserve it and fail loudly instead. The status
# record carries cleared_partial so a cleared-partial retry is
# distinguishable from an ordinary failure in clone-status.jsonl.
cleared_partial = False
if os.path.lexists(dest) and not (
os.path.isdir(dest) and not os.listdir(dest)
):
if not dest_is_confirmed_partial(dest):
return {
"repo": repo,
"status": "fail",
"error": "destination exists and is not a valid "
"mirror, but is not a confirmed partial clone (git "
"can open it, or inspection failed for policy "
f"reasons); preserving it: {dest}",
}
cleared_partial = clear_partial_dest(root, dest)
if os.path.lexists(dest):
return {
"repo": repo,
"status": "fail",
"error": "partial destination is outside the "
"driver-managed mirror tree; refusing to remove "
f"it: {dest}",
}
os.makedirs(os.path.dirname(dest), exist_ok=True)
cmd = ["git", "clone", "--mirror"]
ref = item.get("reference")
Expand All @@ -62,27 +152,36 @@ def clone_one(item, ref_ok):
cmd, capture_output=True, text=True, timeout=TIMEOUT, env=env
)
except subprocess.TimeoutExpired:
return {
rec = {
"repo": repo,
"status": "fail",
"error": "timeout",
"seconds": round(time.time() - t0),
}
if cleared_partial:
rec["cleared_partial"] = True
return rec
dt = round(time.time() - t0)
if r.returncode == 0 and valid_mirror(dest):
return {
rec = {
"repo": repo,
"status": "ok",
"seconds": dt,
"disk_kb": disk_kb(dest),
"reference_used": ref if (ref and ref_ok) else None,
}
return {
if cleared_partial:
rec["cleared_partial"] = True
return rec
rec = {
"repo": repo,
"status": "fail",
"seconds": dt,
"error": (r.stderr or r.stdout)[-400:],
}
if cleared_partial:
rec["cleared_partial"] = True
return rec

state = {}
for p in plan:
Expand Down
Loading