Skip to content
Open
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
19 changes: 19 additions & 0 deletions __tests__/foundation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,18 @@ function cleanupTempDir(dir: string): void {

describe('CodeGraph Foundation', () => {
let tempDir: string;
let savedLocalGitignore: string | undefined;

beforeEach(() => {
tempDir = createTempDir();
savedLocalGitignore = process.env.CODEGRAPH_LOCAL_GITIGNORE;
delete process.env.CODEGRAPH_LOCAL_GITIGNORE;
});

afterEach(() => {
cleanupTempDir(tempDir);
if (savedLocalGitignore === undefined) delete process.env.CODEGRAPH_LOCAL_GITIGNORE;
else process.env.CODEGRAPH_LOCAL_GITIGNORE = savedLocalGitignore;
});

describe('Initialization', () => {
Expand Down Expand Up @@ -62,6 +67,20 @@ describe('CodeGraph Foundation', () => {
cg.close();
});

it('can create a local-only .codegraph/.gitignore', () => {
process.env.CODEGRAPH_LOCAL_GITIGNORE = '1';
const cg = CodeGraph.initSync(tempDir);

const gitignorePath = path.join(getCodeGraphDir(tempDir), '.gitignore');
expect(fs.existsSync(gitignorePath)).toBe(true);

const content = fs.readFileSync(gitignorePath, 'utf-8');
expect(content).toContain('*');
expect(content).not.toContain('!.gitignore');

cg.close();
});

it('should throw if already initialized', () => {
const cg = CodeGraph.initSync(tempDir);
cg.close();
Expand Down
12 changes: 11 additions & 1 deletion src/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,9 +401,19 @@ const GITIGNORE_CONTENT = `# CodeGraph data files — local to each machine, not
!.gitignore
`;

const LOCAL_GITIGNORE_CONTENT = `# CodeGraph data files — local to this machine, not for committing.
# Ignore everything in .codegraph/, including this file itself.
*
`;

/** Header line that prefixes every .gitignore CodeGraph has auto-generated. */
const GITIGNORE_MARKER = '# CodeGraph data files';

function localGitignoreEnabled(): boolean {
const raw = process.env.CODEGRAPH_LOCAL_GITIGNORE?.trim().toLowerCase();
return raw === '1' || raw === 'true' || raw === 'yes';
}

/**
* Is `content` a stale CodeGraph-generated `.gitignore` that should be
* regenerated in place? True when it carries our header but predates the
Expand Down Expand Up @@ -434,7 +444,7 @@ function ensureGitignore(gitignorePath: string): boolean {
// Current default or a user-authored file: nothing to do.
if (existing !== null && !isStaleDefaultGitignore(existing)) return true;
try {
fs.writeFileSync(gitignorePath, GITIGNORE_CONTENT, 'utf-8');
fs.writeFileSync(gitignorePath, localGitignoreEnabled() ? LOCAL_GITIGNORE_CONTENT : GITIGNORE_CONTENT, 'utf-8');
return true;
} catch {
return false;
Expand Down