Skip to content

Commit 837f57f

Browse files
committed
fix(init): merge scaffold entries into an existing .gitignore instead of overwriting it
Re-running agentex init on an existing project replaced the user's .gitignore. Keep the existing file and append only the scaffold entries that are missing, with a test for the merge. Claude-Session: https://claude.ai/code/session_01HCVKnA7LeJZ44nxZz1uzF3
1 parent eac56e8 commit 837f57f

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

‎src/agentex/lib/cli/commands/init.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,17 @@ def create_project_structure(
119119

120120
for template, output in root_templates.items():
121121
output_path = project_dir / output
122-
output_path.write_text(render_template(template, context, template_type))
122+
rendered = render_template(template, context, template_type)
123+
if output == ".gitignore" and output_path.exists():
124+
# Re-running init on an existing project: keep the user's rules and
125+
# append only the scaffold entries that are missing.
126+
existing = output_path.read_text()
127+
existing_lines = {line.strip() for line in existing.splitlines()}
128+
missing = [line for line in rendered.splitlines() if line.strip() and not line.startswith("#") and line.strip() not in existing_lines]
129+
if missing:
130+
output_path.write_text(existing.rstrip("\n") + "\n\n# Added by agentex init\n" + "\n".join(missing) + "\n")
131+
continue
132+
output_path.write_text(rendered)
123133

124134
console.print(f"\n[green]✓[/green] Created project structure at: {project_dir}")
125135

‎tests/lib/cli/test_init_templates.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,18 @@ def test_all_templates_ship_a_gitignore_that_excludes_env(tmp_path: Path, templa
148148
lines = gitignore.read_text().splitlines()
149149
assert ".env" in lines and ".venv/" in lines
150150
assert "!.env.example" in lines, "the example env file should stay committable"
151+
152+
153+
def test_rerunning_init_preserves_existing_gitignore_rules(tmp_path: Path):
154+
"""A pre-existing .gitignore keeps its own rules; missing scaffold entries are appended."""
155+
context = _context(TemplateType.SYNC)
156+
project_dir = tmp_path / context["project_name"]
157+
project_dir.mkdir(parents=True)
158+
(project_dir / ".gitignore").write_text("# mine\n*.log\n.env\n")
159+
160+
create_project_structure(tmp_path, context, TemplateType.SYNC, use_uv=True)
161+
162+
lines = (project_dir / ".gitignore").read_text().splitlines()
163+
assert "*.log" in lines, "user rule was dropped"
164+
assert lines.count(".env") == 1, "existing entry duplicated"
165+
assert ".venv/" in lines and "!.env.example" in lines, "scaffold entries not appended"

0 commit comments

Comments
 (0)