🐛 fix(playbook): honor no_backup for sync
This commit is contained in:
+23
-14
@@ -2,7 +2,7 @@
|
||||
import sys
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
from shutil import copy2, copytree, which
|
||||
from shutil import copy2, copytree, rmtree, which
|
||||
import subprocess
|
||||
|
||||
try:
|
||||
@@ -683,8 +683,9 @@ def sync_rules_action(config: dict, context: dict) -> int:
|
||||
main_language = resolve_main_language(config, context)
|
||||
playbook_scripts = resolve_playbook_scripts(project_root, context)
|
||||
date_value = config.get("date") or datetime.now().strftime("%Y-%m-%d")
|
||||
no_backup = bool(config.get("no_backup", False))
|
||||
|
||||
backup_path(rules_dst, False)
|
||||
backup_path(rules_dst, no_backup)
|
||||
text = rules_src.read_text(encoding="utf-8")
|
||||
text = replace_placeholders(
|
||||
text, project_name, date_value, main_language, playbook_scripts
|
||||
@@ -879,16 +880,18 @@ def read_gitattributes_entries(path: Path) -> list[str]:
|
||||
return entries
|
||||
|
||||
|
||||
def sync_gitattributes_overwrite(src: Path, dst: Path) -> None:
|
||||
def sync_gitattributes_overwrite(src: Path, dst: Path, no_backup: bool) -> None:
|
||||
if src.resolve() == dst.resolve():
|
||||
log("Skip: .gitattributes source equals destination.")
|
||||
return
|
||||
backup_path(dst, False)
|
||||
backup_path(dst, no_backup)
|
||||
copy2(src, dst)
|
||||
log("Synced .gitattributes from standards (overwrite).")
|
||||
|
||||
|
||||
def sync_gitattributes_append(src: Path, dst: Path, source_note: str) -> None:
|
||||
def sync_gitattributes_append(
|
||||
src: Path, dst: Path, source_note: str, no_backup: bool
|
||||
) -> None:
|
||||
src_entries = read_gitattributes_entries(src)
|
||||
dst_entries: list[str] = []
|
||||
if dst.exists():
|
||||
@@ -899,7 +902,7 @@ def sync_gitattributes_append(src: Path, dst: Path, source_note: str) -> None:
|
||||
return
|
||||
|
||||
original = dst.read_text(encoding="utf-8") if dst.exists() else ""
|
||||
backup_path(dst, False)
|
||||
backup_path(dst, no_backup)
|
||||
header = f"# Added from playbook .gitattributes (source: {source_note})"
|
||||
content = original.rstrip("\n")
|
||||
if content:
|
||||
@@ -909,7 +912,7 @@ def sync_gitattributes_append(src: Path, dst: Path, source_note: str) -> None:
|
||||
log("Appended missing .gitattributes rules from standards.")
|
||||
|
||||
|
||||
def sync_gitattributes_block(src: Path, dst: Path) -> None:
|
||||
def sync_gitattributes_block(src: Path, dst: Path, no_backup: bool) -> None:
|
||||
begin = "# BEGIN playbook .gitattributes"
|
||||
end = "# END playbook .gitattributes"
|
||||
begin_old = "# BEGIN tsl-playbook .gitattributes"
|
||||
@@ -939,7 +942,7 @@ def sync_gitattributes_block(src: Path, dst: Path) -> None:
|
||||
if updated and updated[-1].strip():
|
||||
updated.append("")
|
||||
updated.extend(block_lines)
|
||||
backup_path(dst, False)
|
||||
backup_path(dst, no_backup)
|
||||
dst.write_text("\n".join(updated) + "\n", encoding="utf-8")
|
||||
else:
|
||||
dst.write_text("\n".join(block_lines) + "\n", encoding="utf-8")
|
||||
@@ -960,6 +963,7 @@ def sync_standards_action(config: dict, context: dict) -> int:
|
||||
agents_root = project_root / ".agents"
|
||||
ensure_dir(agents_root)
|
||||
|
||||
no_backup = bool(config.get("no_backup", False))
|
||||
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
||||
for lang in langs:
|
||||
src = PLAYBOOK_ROOT / "rulesets" / lang
|
||||
@@ -968,9 +972,12 @@ def sync_standards_action(config: dict, context: dict) -> int:
|
||||
return 2
|
||||
dst = agents_root / lang
|
||||
if dst.exists():
|
||||
backup = agents_root / f"{lang}.bak.{timestamp}"
|
||||
dst.rename(backup)
|
||||
log(f"Backed up existing {lang} agents -> {backup.name}")
|
||||
if no_backup:
|
||||
rmtree(dst)
|
||||
else:
|
||||
backup = agents_root / f"{lang}.bak.{timestamp}"
|
||||
dst.rename(backup)
|
||||
log(f"Backed up existing {lang} agents -> {backup.name}")
|
||||
copytree(src, dst)
|
||||
log(f"Synced .agents/{lang} from standards.")
|
||||
|
||||
@@ -1005,11 +1012,13 @@ def sync_standards_action(config: dict, context: dict) -> int:
|
||||
if mode == "skip":
|
||||
log("Skip: .gitattributes sync (mode=skip).")
|
||||
elif mode == "overwrite":
|
||||
sync_gitattributes_overwrite(gitattributes_src, gitattributes_dst)
|
||||
sync_gitattributes_overwrite(gitattributes_src, gitattributes_dst, no_backup)
|
||||
elif mode == "block":
|
||||
sync_gitattributes_block(gitattributes_src, gitattributes_dst)
|
||||
sync_gitattributes_block(gitattributes_src, gitattributes_dst, no_backup)
|
||||
else:
|
||||
sync_gitattributes_append(gitattributes_src, gitattributes_dst, source_note)
|
||||
sync_gitattributes_append(
|
||||
gitattributes_src, gitattributes_dst, source_note, no_backup
|
||||
)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
Reference in New Issue
Block a user