Skip to content

Commit 213f7a5

Browse files
Merge pull request #6 from pycalendar/prevent-gitignore-corruption
fix: ensure newline before gitignore entry
2 parents 03f5b44 + afc55c0 commit 213f7a5

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎ai_prompt_auto_commit/prepare_repository.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ def prepare_repository(
5656
# Add .prompts/ to the root .gitignore
5757
root_gitignore = repo_root / ".gitignore"
5858
pattern = f"/{prompts_directory}/"
59-
existing = root_gitignore.read_text(encoding="utf-8").splitlines() if root_gitignore.exists() else []
59+
existing_text = root_gitignore.read_text(encoding="utf-8") if root_gitignore.exists() else ""
60+
existing = existing_text.splitlines()
6061
if pattern not in existing:
6162
with root_gitignore.open("a", encoding="utf-8") as fh:
63+
if existing_text and not existing_text.endswith("\n"):
64+
fh.write("\n")
6265
fh.write(f"{pattern}\n")
6366
print(f"Added '{pattern}' to {root_gitignore}")
6467
else:

‎tests/test_prepare_repository.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def test_root_gitignore_existing_content_preserved(repo: Path) -> None:
4444
assert f"/{PROMPTS_DIRECTORY}/" in content
4545

4646

47+
def test_root_gitignore_newline_added_when_missing(repo: Path) -> None:
48+
(repo / ".gitignore").write_text("*.pyc", encoding="utf-8") # no trailing newline
49+
prepare_repository()
50+
lines = (repo / ".gitignore").read_text(encoding="utf-8").splitlines()
51+
assert f"/{PROMPTS_DIRECTORY}/" in lines
52+
assert lines[0] == "*.pyc"
53+
54+
4755
# ---------------------------------------------------------------------------
4856
# get_default_claude_settings
4957
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)