INNER CODE UNIT · Python

apply_patch

kangarooking/cangjie-skill · scripts/apply_skill_patch.py:26

def apply_patch(target: Path, patch_dir: Path, snapshots: Path) -> int:
    patch_files = [p for p in patch_dir.rglob("*") if p.is_file()]
    if not patch_files:
        raise SystemExit(f"补丁目录为空: {patch_dir}")

    with WriterLock(target):
        snap = snapshot_dir(target, snapshots, "pre-patch")
        print(f"已快照: {snap}")
        diff_lines = []
        for pf in patch_files:
            rel = pf.relative_to(patch_dir)
            dest = target / rel
            old = dest.read_text(encoding="utf-8") if dest.exists() else ""
            new = pf.read_text(encoding="utf-8")
            diff_lines.append(f"### {rel}: {'修改' if dest.exists() else '新增'}({len(old)} → {len(new)} 字符)")
            dest.parent.mkdir(parents=True, exist_ok=True)
            shutil.copyfile(pf, dest)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…