mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-25 08:48:08 +02:00
fix(cli): survive ktx.yaml version skew and derive repo ownership from disk (#293)
* fix(cli): survive ktx.yaml version skew and derive repo ownership from disk Loading ktx.yaml is now tolerant of keys this ktx version does not recognize: they are stripped from the in-memory config (the file on disk is never rewritten) and reported by ktx status as non-blocking warnings, while invalid values on recognized fields still fail hard. Repo ownership is derived from observed state (a .git directory plus a root ktx.yaml) instead of a ktx.managed git-config marker, so projects created by any past or future ktx classify identically. initKtxProject now runs an explicit foreign-repo pre-check and writes ktx.yaml before initializing git, so an interrupted init leaves only recoverable residue instead of a bare .git misread as foreign. * style(cli): trim comment blocks to constraint-only notes * docs(agents): require constraint-only code comments
This commit is contained in:
parent
a278d2f7d0
commit
0689d709d2
14 changed files with 502 additions and 146 deletions
|
|
@ -6,10 +6,10 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
|||
import type { KtxCoreConfig } from '../../../src/context/core/config.js';
|
||||
import { GitService } from '../../../src/context/core/git.service.js';
|
||||
|
||||
// Regression for bootstrapping a marked ktx repo on a machine with no configured
|
||||
// Regression for bootstrapping a ktx-owned repo on a machine with no configured
|
||||
// git identity. A foreign pre-existing repo is rejected by the ownership rule;
|
||||
// this test covers the still-valid path where the repo is already ktx-managed
|
||||
// but has no HEAD yet.
|
||||
// this test covers the still-valid path where the repo is already ktx's own
|
||||
// (root ktx.yaml present) but has no HEAD yet.
|
||||
describe('GitService.initialize without a configured git identity', () => {
|
||||
let repoDir: string;
|
||||
let homeDir: string;
|
||||
|
|
@ -58,11 +58,7 @@ describe('GitService.initialize without a configured git identity', () => {
|
|||
}
|
||||
|
||||
execFileSync('git', ['init'], { cwd: repoDir, env: process.env, stdio: 'ignore' });
|
||||
execFileSync('git', ['config', '--local', 'ktx.managed', 'true'], {
|
||||
cwd: repoDir,
|
||||
env: process.env,
|
||||
stdio: 'ignore',
|
||||
});
|
||||
await writeFile(join(repoDir, 'ktx.yaml'), 'connections: {}\n', 'utf-8');
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
|
|
|
|||
|
|
@ -56,10 +56,11 @@ describe('GitService repository ownership', () => {
|
|||
git(parentDir, ['commit', '-m', 'parent baseline']);
|
||||
const parentHeadBefore = git(parentDir, ['rev-parse', 'HEAD']);
|
||||
|
||||
await writeFile(join(projectDir, 'ktx.yaml'), 'connections: {}\n', 'utf-8');
|
||||
const service = new GitService(coreConfig(projectDir));
|
||||
await service.onModuleInit();
|
||||
|
||||
expect(git(projectDir, ['config', '--local', '--get', 'ktx.managed'])).toBe('true');
|
||||
expect(await classifyKtxRepoOwnership(projectDir)).toBe('ktx-managed');
|
||||
expect(git(parentDir, ['rev-parse', 'HEAD'])).toBe(parentHeadBefore);
|
||||
expect(await realpath(git(projectDir, ['rev-parse', '--show-toplevel']))).toBe(await realpath(projectDir));
|
||||
|
||||
|
|
@ -83,18 +84,22 @@ describe('GitService repository ownership', () => {
|
|||
expect(await readFile(join(projectDir, '.git', 'config'), 'utf-8')).toBe(configBefore);
|
||||
});
|
||||
|
||||
it('rejects a gitfile at the project dir as foreign', async () => {
|
||||
it('rejects a gitfile at the project dir as foreign even when a ktx.yaml sits beside it', async () => {
|
||||
// A linked worktree is never ktx's own repo, whatever files live in it.
|
||||
const projectDir = join(tempDir, 'linked-worktree');
|
||||
await mkdir(projectDir, { recursive: true });
|
||||
await writeFile(join(projectDir, '.git'), 'gitdir: ../actual.git\n', 'utf-8');
|
||||
await writeFile(join(projectDir, 'ktx.yaml'), 'connections: {}\n', 'utf-8');
|
||||
|
||||
const service = new GitService(coreConfig(projectDir));
|
||||
|
||||
await expect(service.onModuleInit()).rejects.toThrow(/already a git repository that ktx did not create/);
|
||||
});
|
||||
|
||||
it('accepts a marked ktx repo and does not create a second bootstrap commit', async () => {
|
||||
it('re-initializes an existing ktx project repo without a second bootstrap commit', async () => {
|
||||
const projectDir = join(tempDir, 'owned');
|
||||
await mkdir(projectDir, { recursive: true });
|
||||
await writeFile(join(projectDir, 'ktx.yaml'), 'connections: {}\n', 'utf-8');
|
||||
const service = new GitService(coreConfig(projectDir));
|
||||
await service.onModuleInit();
|
||||
const before = await service.revParseHead();
|
||||
|
|
@ -103,7 +108,37 @@ describe('GitService repository ownership', () => {
|
|||
await second.onModuleInit();
|
||||
|
||||
expect(await second.revParseHead()).toBe(before);
|
||||
expect(git(projectDir, ['config', '--local', '--get', 'ktx.managed'])).toBe('true');
|
||||
});
|
||||
|
||||
it('accepts a project created by an older ktx: repo history plus an untracked root ktx.yaml', async () => {
|
||||
// Older projects have ktx commit history and an uncommitted root ktx.yaml
|
||||
// (it holds secret refs); the on-disk file is still the ownership signal.
|
||||
const projectDir = join(tempDir, 'legacy');
|
||||
await mkdir(join(projectDir, '.ktx'), { recursive: true });
|
||||
git(projectDir, ['init']);
|
||||
await writeFile(join(projectDir, '.ktx', '.gitignore'), 'secrets/\n', 'utf-8');
|
||||
git(projectDir, ['add', '.ktx/.gitignore']);
|
||||
git(projectDir, ['commit', '-m', 'Initialize KTX project: legacy']);
|
||||
await writeFile(join(projectDir, 'ktx.yaml'), 'connections: {}\n', 'utf-8');
|
||||
const headBefore = git(projectDir, ['rev-parse', 'HEAD']);
|
||||
|
||||
const service = new GitService(coreConfig(projectDir));
|
||||
await expect(service.onModuleInit()).resolves.toBeUndefined();
|
||||
|
||||
expect(await service.revParseHead()).toBe(headBefore);
|
||||
expect(git(projectDir, ['status', '--short'])).toContain('?? ktx.yaml');
|
||||
});
|
||||
|
||||
it('still rejects a user repo with history but no root ktx.yaml', async () => {
|
||||
const projectDir = join(tempDir, 'app-repo');
|
||||
await mkdir(projectDir, { recursive: true });
|
||||
git(projectDir, ['init']);
|
||||
await writeFile(join(projectDir, 'README.md'), '# App\n', 'utf-8');
|
||||
git(projectDir, ['add', 'README.md']);
|
||||
git(projectDir, ['commit', '-m', 'app baseline']);
|
||||
|
||||
const service = new GitService(coreConfig(projectDir));
|
||||
await expect(service.onModuleInit()).rejects.toThrow(/already a git repository that ktx did not create/);
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -132,9 +167,11 @@ describe('classifyKtxRepoOwnership', () => {
|
|||
expect(await classifyKtxRepoOwnership(nestedDir)).toBe('unowned');
|
||||
});
|
||||
|
||||
it('reports ktx-managed for a repo ktx initialized', async () => {
|
||||
it('reports ktx-managed for a repo with a root ktx.yaml (even untracked)', async () => {
|
||||
const dir = join(tempDir, 'owned');
|
||||
await new GitService(coreConfig(dir)).onModuleInit();
|
||||
await mkdir(dir, { recursive: true });
|
||||
git(dir, ['init']);
|
||||
await writeFile(join(dir, 'ktx.yaml'), 'connections: {}\n', 'utf-8');
|
||||
expect(await classifyKtxRepoOwnership(dir)).toBe('ktx-managed');
|
||||
});
|
||||
|
||||
|
|
@ -145,6 +182,16 @@ describe('classifyKtxRepoOwnership', () => {
|
|||
expect(await classifyKtxRepoOwnership(dir)).toBe('foreign');
|
||||
});
|
||||
|
||||
it('reports foreign for a non-ktx repo that has commits but no ktx.yaml', async () => {
|
||||
const dir = join(tempDir, 'app');
|
||||
await mkdir(dir, { recursive: true });
|
||||
git(dir, ['init']);
|
||||
await writeFile(join(dir, 'README.md'), '# App\n', 'utf-8');
|
||||
git(dir, ['add', 'README.md']);
|
||||
git(dir, ['commit', '-m', 'baseline']);
|
||||
expect(await classifyKtxRepoOwnership(dir)).toBe('foreign');
|
||||
});
|
||||
|
||||
it('reports foreign for a .git file (linked worktree)', async () => {
|
||||
const dir = join(tempDir, 'linked');
|
||||
await mkdir(dir, { recursive: true });
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
import { execFileSync } from 'node:child_process';
|
||||
import { mkdir, mkdtemp, readFile, realpath, rm, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { dirname, join } from 'node:path';
|
||||
|
|
@ -27,8 +26,12 @@ describe('GitService', () => {
|
|||
},
|
||||
};
|
||||
|
||||
// Mirror production: initKtxProject writes ktx.yaml before the git repo is
|
||||
// initialized (the root ktx.yaml is the ownership signal) and commits it.
|
||||
await writeFile(join(tempDir, 'ktx.yaml'), 'connections: {}\n', 'utf-8');
|
||||
service = new GitService(coreConfig);
|
||||
await service.onModuleInit();
|
||||
await service.commitFile('ktx.yaml', 'Initialize KTX project', 'Test', 'test@example.com');
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
|
|
@ -61,14 +64,9 @@ describe('GitService', () => {
|
|||
|
||||
describe('cold-start bootstrap commit', () => {
|
||||
it('writes an empty commit on init so HEAD always resolves', async () => {
|
||||
// beforeEach already ran onModuleInit() against an empty temp dir.
|
||||
// beforeEach already ran onModuleInit() against a fresh temp dir.
|
||||
const head = await service.revParseHead();
|
||||
expect(head).toMatch(/^[0-9a-f]{40}$/);
|
||||
const marker = execFileSync('git', ['config', '--local', '--get', 'ktx.managed'], {
|
||||
cwd: tempDir,
|
||||
encoding: 'utf-8',
|
||||
}).trim();
|
||||
expect(marker).toBe('true');
|
||||
});
|
||||
|
||||
it('does not double-commit when re-initialized', async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue