#!/usr/bin/env python3 """Archive safe current-worktree source, including non-ignored untracked files.""" import datetime import subprocess import sys import zipfile from pathlib import Path root = Path(sys.argv[1]).resolve() deny_dirs = {".git", ".test-worktrees", "snapshots", "node_modules", ".venv", "venv", "__pycache__", "dist", "build", ".pytest_cache"} deny_suffixes = (".db", ".sqlite", ".sqlite3", ".pem", ".key", ".p12", ".pfx") def git(*args): return subprocess.check_output(["git", "-C", str(root), *args], text=True).strip() def allowed(path): parts = Path(path).parts name = parts[-1] return not (set(parts) & deny_dirs or any(p.startswith(".env") for p in parts) or name.endswith(deny_suffixes) or "credential" in name.lower() or "secret" in name.lower()) paths = subprocess.check_output(["git", "-C", str(root), "ls-files", "-co", "--exclude-standard", "-z"]).decode().split("\0") selected = sorted({p for p in paths if p and allowed(p) and (root / p).is_file() and not (root / p).is_symlink()}) out_dir = root / "snapshots" out_dir.mkdir(exist_ok=True) stamp = datetime.datetime.now(datetime.timezone.utc).strftime("%Y%m%dT%H%M%SZ") out = out_dir / f"{root.name}-chatgpt-{stamp}-{git('rev-parse', '--short', 'HEAD')}.zip" with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as archive: for path in selected: archive.write(root / path, path) print(f"{out}\nbranch: {git('branch', '--show-current')}\nuncommitted: {bool(git('status', '--porcelain'))}\nfiles: {len(selected)}")