Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

Fix initialization of nested clone destinations (#7591)

Nick Gomez4fa23f3752f9
diff --git a/.changeset/clone-initialization-directory.md b/.changeset/clone-initialization-directory.md
new file mode 100644
index 000000000..0273e6540
--- /dev/null
+++ b/.changeset/clone-initialization-directory.md
@@ -0,0 +1,5 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Ensure cloning into an existing OpenKnowledge project initializes the new checkout without changing the enclosing project's files.
diff --git a/packages/cli/src/commands/clone-init.test.ts b/packages/cli/src/commands/clone-init.test.ts
new file mode 100644
index 000000000..099f525f2
--- /dev/null
+++ b/packages/cli/src/commands/clone-init.test.ts
@@ -0,0 +1,98 @@
+import { execFileSync } from 'node:child_process';
+import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { pathToFileURL } from 'node:url';
+import { pathspecArgs } from '@inkeep/open-knowledge-core';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+import { configureTestGitRepository } from '../../../../test-support/configure-git-fixture.test-helper.ts';
+import { loadConfig } from '../config/loader.ts';
+import { runClone } from './clone.ts';
+
+const fixtures: string[] = [];
+
+afterEach(() => {
+ vi.restoreAllMocks();
+ vi.unstubAllEnvs();
+ for (const fixture of fixtures.splice(0)) rmSync(fixture, { recursive: true, force: true });
+});
+
+function git(cwd: string, args: string[]): string {
+ return execFileSync('git', args, { cwd, encoding: 'utf8', stdio: 'pipe' });
+}
+
+function initializeRepository(directory: string): void {
+ mkdirSync(directory, { recursive: true });
+ git(directory, ['init', '--initial-branch=main']);
+ configureTestGitRepository(directory);
+}
+
+describe('clone initialization', () => {
+ it('initializes the checkout without changing its enclosing project', async () => {
+ const fixture = mkdtempSync(join(tmpdir(), 'ok-clone-init-'));
+ fixtures.push(fixture);
+ const home = join(fixture, 'home');
+ const template = join(fixture, 'empty-template');
+ mkdirSync(home);
+ mkdirSync(template);
+
+ for (const name of Object.keys(process.env)) {
+ if (name.startsWith('GIT_')) vi.stubEnv(name, undefined);
+ }
+ vi.stubEnv('HOME', home);
+ vi.stubEnv('USERPROFILE', home);
+ vi.stubEnv('XDG_CONFIG_HOME', join(home, '.config'));
+ vi.stubEnv('OK_CHANNEL', 'stable');
+ vi.stubEnv('DO_NOT_TRACK', '1');
+ vi.stubEnv('GIT_CONFIG_NOSYSTEM', '1');
+ vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
+
+ writeFileSync(
+ join(home, '.gitconfig'),
+ `[user]\n name = Fixture\n email = [email protected]\n` +
+ `[commit]\n gpgsign = false\n[maintenance]\n auto = false\n[gc]\n auto = 0\n`,
+ );
+ git(fixture, ['config', '--file', join(home, '.gitconfig'), 'init.templateDir', template]);
+
+ const source = join(fixture, 'source');
+ initializeRepository(source);
+ writeFileSync(join(source, 'README.md'), '# Cloned content\n');
+ git(source, ['add', ...pathspecArgs(['README.md'])]);
+ git(source, ['commit', '-m', 'Seed source']);
+
+ const ancestor = join(home, 'outer');
+ initializeRepository(ancestor);
+ mkdirSync(join(ancestor, '.ok'));
+ writeFileSync(join(ancestor, '.ok', 'config.yml'), 'content:\n dir: .\n');
+ const ignore = 'user-rule/\n.claude/skills/open-knowledge/\n.cursor/skills/open-knowledge/\n';
+ const localIgnore = 'local/\n';
+ writeFileSync(join(ancestor, '.gitignore'), ignore);
+ writeFileSync(join(ancestor, '.ok', '.gitignore'), localIgnore);
+ git(ancestor, ['add', ...pathspecArgs(['.gitignore', '.ok/config.yml', '.ok/.gitignore'])]);
+ git(ancestor, ['commit', '-m', 'Seed enclosing project']);
+
+ const url = 'http://127.0.0.1:1/fixture/source.git';
+ vi.stubEnv('GIT_CONFIG_COUNT', '1');
+ vi.stubEnv('GIT_CONFIG_KEY_0', `url.${pathToFileURL(source).href}.insteadOf`);
+ vi.stubEnv('GIT_CONFIG_VALUE_0', url);
+
+ const target = join(ancestor, 'cloned');
+ const result = await runClone(
+ url,
+ {
+ dir: target,
+ json: true,
+ _detectGhFn: () => ({ available: true, token: 'fixture-only' }),
+ },
+ loadConfig(ancestor).config,
+ ancestor,
+ );
+ configureTestGitRepository(result);
+
+ expect(result).toBe(target);
+ expect(readFileSync(join(target, 'README.md'), 'utf8')).toBe('# Cloned content\n');
+ expect.soft(readFileSync(join(ancestor, '.gitignore'), 'utf8')).toBe(ignore);
+ expect.soft(readFileSync(join(ancestor, '.ok', '.gitignore'), 'utf8')).toBe(localIgnore);
+ expect.soft(existsSync(join(target, '.ok', 'config.yml'))).toBe(true);
+ });
+});
diff --git a/packages/cli/src/commands/clone.ts b/packages/cli/src/commands/clone.ts
index a69fb4de3..42ce2d8cb 100644
--- a/packages/cli/src/commands/clone.ts
+++ b/packages/cli/src/commands/clone.ts
@@ -331,7 +331,7 @@ export async function runClone(
try {
const { runInit } = await import('./init.ts');
- const initResult = await runInit({ cwd: targetDir, mcp: false });
+ const initResult = await runInit({ cwd: targetDir, projectRootMode: 'cwd', mcp: false });
if (initResult.contentUpdated.length > 0) {
const msg = `auto-init: updated ${initResult.contentUpdated.join(', ')}`;
if (opts.json) emit(true, { type: 'warning', message: msg });
diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts
index 4cb834b55..909e4cf8d 100644
--- a/packages/cli/src/commands/init.ts
+++ b/packages/cli/src/commands/init.ts
@@ -59,7 +59,11 @@ import { isCollection, parseDocument, stringify as stringifyYaml } from 'yaml';
import { CONFIG_FILENAME, OK_DIR } from '../constants.ts';
import { formatPreviewBlock, type PreviewResult } from '../content/preview.ts';
import { buildPiExtensionSource, makePiManagedFileEntry } from '../integrations/pi-extension.ts';
-import { isHomeDir, resolveProjectRoot } from '../integrations/resolve-project-root.ts';
+import {
+ isHomeDir,
+ type ResolveProjectRootResult,
+ resolveProjectRoot,
+} from '../integrations/resolve-project-root.ts';
import {
assertProjectPathSafe,
type ProjectSkillResult,
@@ -578,6 +582,7 @@ interface ProjectConfigResult {
interface InitCommandOptions {
cwd?: string;
+ projectRootMode?: 'discover' | 'cwd';
mcp?: boolean;
devMcp?: boolean;
editors?: EditorId[];
@@ -1166,7 +1171,15 @@ export function classifyExistingMcpEntry(
export async function runInit(options: InitCommandOptions = {}): Promise<InitCommandResult> {
const cwd = resolve(options.cwd ?? process.cwd());
- const resolution = resolveProjectRoot(cwd, { homeDir: options.home });
+ const resolution =
+ options.projectRootMode === 'cwd'
+ ? ({
+ projectRoot: cwd,
+ defaultContentDir: '.',
+ ancestorPromoted: false,
+ gitRootPromoted: false,
+ } satisfies ResolveProjectRootResult)
+ : resolveProjectRoot(cwd, { homeDir: options.home });
const projectRoot = resolution.projectRoot;
if (isHomeDir(projectRoot, options.home ?? homedir())) {
throw new HomeProjectRootError(projectRoot);