Skip to content

Commit 892dd65

Browse files
jawwad-aliclaude
andauthored
fix(shared-infra): refresh_shared_templates preserves recovered user files (#3378)
* fix(shared-infra): refresh_shared_templates preserves recovered user files refresh_shared_templates skipped a shared template only when it was untracked or modified, ignoring the manifest's is_recovered marker that install_shared_infra already honors. So a pre-existing user template (adopted via record_existing(recovered=True), hence tracked and hash-unmodified) was silently overwritten with bundled content on refresh — the exact data-loss class that #2918 fixed for install_shared_infra. Add the is_recovered check to the skip predicate. Co-Authored-By: Claude Fable 5 <[email protected]> * docs(shared-infra): include recovered files in refresh skip warning The skip predicate now also skips recovered (pre-existing user) files, so the warning saying only 'modified or untracked' could mislead a user into thinking they edited a file that was simply recorded as recovered. Reword to 'modified, untracked, or preserved (recovered)'. Co-Authored-By: Claude Fable 5 <[email protected]> --------- Co-authored-by: Claude Fable 5 <[email protected]>
1 parent 54ed736 commit 892dd65

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎src/specify_cli/shared_infra.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ def refresh_shared_templates(
328328
_ensure_safe_shared_destination(project_path, dst)
329329
rel = dst.relative_to(project_path).as_posix()
330330
if dst.exists() and not force:
331-
if rel not in tracked_files or rel in modified:
331+
if rel not in tracked_files or rel in modified or manifest.is_recovered(rel):
332+
# Never overwrite a recovered (pre-existing user) file without
333+
# --force, matching install_shared_infra's is_recovered gate
334+
# (#2918). Without this, refresh clobbers user content.
332335
skipped_files.append(rel)
333336
continue
334337

@@ -344,7 +347,7 @@ def refresh_shared_templates(
344347

345348
if skipped_files:
346349
console.print(
347-
f"[yellow]⚠[/yellow] {len(skipped_files)} modified or untracked shared template file(s) were not updated:"
350+
f"[yellow]⚠[/yellow] {len(skipped_files)} modified, untracked, or preserved (recovered) shared template file(s) were not updated:"
348351
)
349352
for rel in skipped_files:
350353
console.print(f" {rel}")

‎tests/integrations/test_cli.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,3 +2073,44 @@ def test_catalog_remove_final_entry_restores_defaults(
20732073
assert listing.exit_code == 0, listing.output
20742074
assert "default" in listing.output
20752075
assert "community" in listing.output
2076+
2077+
2078+
def test_refresh_shared_templates_preserves_recovered_user_file(tmp_path):
2079+
"""refresh_shared_templates must not overwrite a recovered (pre-existing
2080+
user) template without --force, matching install_shared_infra's gate (#2918).
2081+
"""
2082+
from specify_cli.shared_infra import (
2083+
load_speckit_manifest,
2084+
refresh_shared_templates,
2085+
)
2086+
2087+
project = tmp_path / "proj"
2088+
templates_dir = project / ".specify" / "templates"
2089+
templates_dir.mkdir(parents=True)
2090+
user_file = templates_dir / "spec-template.md"
2091+
user_file.write_text("# USER CUSTOM CONTENT\n", encoding="utf-8")
2092+
2093+
# Record the pre-existing file as recovered (its hash was adopted, not written).
2094+
manifest = load_speckit_manifest(project, version="test", console=_NoopConsole())
2095+
rel = ".specify/templates/spec-template.md"
2096+
manifest.record_existing(rel, recovered=True)
2097+
manifest.save()
2098+
2099+
# Bundled source ships a different body for the same template.
2100+
core_pack = tmp_path / "core-pack"
2101+
src = core_pack / "templates"
2102+
src.mkdir(parents=True)
2103+
(src / "spec-template.md").write_text("# BUNDLED CONTENT v2\n", encoding="utf-8")
2104+
2105+
refresh_shared_templates(
2106+
project,
2107+
version="test",
2108+
core_pack=core_pack,
2109+
repo_root=tmp_path / "unused",
2110+
console=_NoopConsole(),
2111+
invoke_separator=".",
2112+
force=False,
2113+
)
2114+
2115+
# Recovered user content must survive (fail-before: replaced by bundled body).
2116+
assert user_file.read_text(encoding="utf-8") == "# USER CUSTOM CONTENT\n"

0 commit comments

Comments
 (0)