Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
All notable changes to this project are documented here, following
[Keep a Changelog](https://keepachangelog.com/) and semantic versioning.

## [0.1.4] - 2026-09-07

### Fixed

- Repair an unclosed managed ignore block in place instead of appending a
duplicate block on every run.

## [0.1.3] - 2026-09-06

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ctxtrim",
"version": "0.1.3",
"version": "0.1.4",
"description": "Trim what bloats your AI coding context. Scan a repo, find the high-cost/low-value files ballooning your Claude Code / Cursor / Codex context, and write ignore files to cut token cost. Zero dependencies.",
"type": "module",
"bin": {
Expand Down
8 changes: 7 additions & 1 deletion src/ignore.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,15 @@ export function block(patterns) {
/** Insert or replace the managed block in existing content (keeps the user's own lines). */
export function merge(existing, patterns) {
const b = block(patterns);
if (existing.includes(START) && existing.includes(END)) {
const startIndex = existing.indexOf(START);
const endIndex = existing.indexOf(END, startIndex + START.length);
if (startIndex !== -1 && endIndex !== -1) {
return existing.replace(new RegExp(escape(START) + "[\\s\\S]*?" + escape(END)), b).trimEnd() + "\n";
}
if (startIndex !== -1) {
const base = existing.slice(0, startIndex).trim();
return (base ? base + "\n\n" : "") + b + "\n";
}
const base = existing.trim();
return (base ? base + "\n\n" : "") + b + "\n";
}
Expand Down
16 changes: 16 additions & 0 deletions test/ctxtrim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ test("ignore block is idempotent (managed block replaced, user lines kept)", ()
assert.ok(!second.includes("package-lock.json"), "old managed pattern should be gone");
});

test("ignore merge repairs a managed block with no end marker", () => {
const existing = [
"# my own rule",
"*.log",
"# >>> ctxtrim (managed) >>>",
"old-generated-pattern/",
].join("\n");

const repaired = merge(existing, ["dist/", "coverage/"]);

assert.ok(repaired.includes("# my own rule\n*.log"));
assert.ok(repaired.includes(block(["dist/", "coverage/"])));
assert.ok(!repaired.includes("old-generated-pattern/"));
assert.equal((repaired.match(/ctxtrim \(managed\)/g) || []).length, 2);
});

test("clean repo (only source) reports nothing to trim", () => {
// scanning the src subdir alone = only source
const s = scanRepo(join(repo, "src"));
Expand Down
Loading