Skip to content

fix: ensure newline before gitignore entry#6

Merged
niccokunzmann merged 1 commit into
mainfrom
prevent-gitignore-corruption
Jul 12, 2026
Merged

fix: ensure newline before gitignore entry#6
niccokunzmann merged 1 commit into
mainfrom
prevent-gitignore-corruption

Conversation

@niccokunzmann

Copy link
Copy Markdown
Member

Problem: the ignore was just appended, corrupting the .gitignore when there was no newline at the end

@qodo-code-review

Copy link
Copy Markdown

PR Summary by Qodo

Fix .gitignore corruption by inserting newline before appended entry
🐞 Bug fix 🧪 Tests 🕐 Less than 10 minutes

Grey Divider

Description

• Prevent appending ignore patterns onto the last line when .gitignore lacks a trailing newline.
• Preserve existing .gitignore content while safely adding the prompts directory ignore.
• Add regression test covering the missing-newline scenario.
Diagram

graph TD
  A["prepare_repository()"] --> B["Read .gitignore text"] --> C{"Ends with newline?"}
  C -->|No| D["Write '\n'"] --> E["Append '/.prompts/' entry"] --> F["Updated .gitignore"]
  C -->|Yes| E
  T["tests/test_prepare_repository.py"] --> A

  subgraph Legend
    direction LR
    _proc["Process"] ~~~ _dec{"Decision"} ~~~ _file[("File")]
  end
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Rewrite .gitignore via read/modify/write (instead of append mode)
  • ➕ Simplifies newline handling (always join lines with '\n' and end with '\n')
  • ➕ Easier to enforce formatting invariants (dedupe, sorting, trailing newline)
  • ➖ More invasive change; rewrites entire file even when only appending one line
  • ➖ Slightly higher risk of altering existing formatting/comments unintentionally
2. Use a dedicated helper to ensure trailing newline (utility function)
  • ➕ Makes newline-safety reusable for future ignore entries or other files
  • ➕ Improves readability and testability of edge-case handling
  • ➖ Adds indirection for a very small behavior change
  • ➖ May be overkill unless more call sites appear

Recommendation: Current approach (append mode + explicit trailing-newline check) is a good minimal fix: it preserves existing .gitignore bytes except for the necessary newline and new entry. Consider a read/modify/write approach only if future requirements include canonicalization (sorting/deduping) or multiple entries appended in different places.

Files changed (2) +12 / -1

Bug fix (1) +4 / -1
prepare_repository.pyInsert newline before appending prompts ignore entry +4/-1

Insert newline before appending prompts ignore entry

• Reads the full .gitignore text (when present) to detect missing trailing newline. When appending a new prompts-directory ignore pattern, it first writes a '\n' if the file has content but does not end with one, preventing the new pattern from being concatenated onto the last existing line.

ai_prompt_auto_commit/prepare_repository.py

Tests (1) +8 / -0
test_prepare_repository.pyAdd regression test for missing trailing newline in .gitignore +8/-0

Add regression test for missing trailing newline in .gitignore

• Introduces a test that creates a .gitignore without a trailing newline, runs prepare_repository, and asserts the original line remains intact and the prompts ignore entry is added as a separate line.

tests/test_prepare_repository.py

@qodo-code-review

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

@niccokunzmann niccokunzmann merged commit 213f7a5 into main Jul 12, 2026
6 checks passed
@niccokunzmann

Copy link
Copy Markdown
Member Author

Merging for progress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant