From 3866258617f626f5673f129a9331c935ffecf8e5 Mon Sep 17 00:00:00 2001 From: Rahul Sharma Date: Wed, 18 Nov 2026 16:19:34 +0530 Subject: [PATCH] feat: add pullMode option so pull can keep local-only files --- README.md | 11 +++++++++++ src/sync/apply.test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/sync/apply.ts | 21 +++++++++++++-------- src/sync/config.test.ts | 6 ++++++ src/sync/config.ts | 11 +++++++++++ src/sync/service.ts | 6 +++--- 6 files changed, 84 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index d371aee..6e59d28 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ Create `~/.config/opencode/opencode-synced.jsonc`: "includeModelFavorites": true, "includeOpencodeSkills": true, "includeAgentsDir": true, + "pullMode": "replace", "extraSecretPaths": [], "extraConfigPaths": [], } @@ -155,6 +156,16 @@ Create `~/.config/opencode/opencode-synced.jsonc`: Review it before syncing, keep the sync repository private when needed, or set `"includeAgentsDir": false` to opt out. +### Pull mode + +`pullMode` controls how `/sync-pull` (and the startup pull) applies synced **directories**: + +- `"replace"` (default): each synced directory is replaced by the repo copy, so files that exist + only on this machine inside that directory (for example a local-only agent, command or plugin) + are deleted. +- `"merge"`: repo files are copied over local ones and files that exist only locally are left in + place. Files deleted in the repo are not deleted locally either. + Disable default directory sync by setting: - `"includeOpencodeSkills": false` to skip `~/.config/opencode/skills/` - `"includeAgentsDir": false` to skip `~/.agents/` diff --git a/src/sync/apply.test.ts b/src/sync/apply.test.ts index 1d881c1..98b5d15 100644 --- a/src/sync/apply.test.ts +++ b/src/sync/apply.test.ts @@ -902,3 +902,43 @@ describe('MCP secret scrub round trip', () => { }); }); }); + +describe('syncRepoToLocal pull mode for directories', () => { + async function seedDirectories(root: string): Promise<{ plan: SyncPlan; localDir: string }> { + const repoRoot = path.join(root, 'repo'); + const homeDir = path.join(root, 'home'); + const repoDir = path.join(repoRoot, 'config', 'agents'); + const localDir = path.join(homeDir, '.config', 'opencode', 'agents'); + await fs.mkdir(repoDir, { recursive: true }); + await fs.mkdir(localDir, { recursive: true }); + await fs.writeFile(path.join(repoDir, 'shared.md'), 'from-repo', 'utf8'); + await fs.writeFile(path.join(localDir, 'shared.md'), 'stale-local', 'utf8'); + await fs.writeFile(path.join(localDir, 'local-only.md'), 'keep-me', 'utf8'); + const plan = createPlan(repoRoot, homeDir, [ + { localPath: localDir, repoPath: repoDir, type: 'dir', isSecret: false, isConfigFile: false }, + ]); + return { plan, localDir }; + } + + it('replaces the directory by default, removing local-only files', async () => { + await withTempDir(async (root) => { + const { plan, localDir } = await seedDirectories(root); + + await syncRepoToLocal(plan, null); + + expect(await fs.readFile(path.join(localDir, 'shared.md'), 'utf8')).toBe('from-repo'); + await expect(fs.access(path.join(localDir, 'local-only.md'))).rejects.toThrow(); + }); + }); + + it('keeps local-only files in merge mode while updating shared files', async () => { + await withTempDir(async (root) => { + const { plan, localDir } = await seedDirectories(root); + + await syncRepoToLocal(plan, null, { pullMode: 'merge' }); + + expect(await fs.readFile(path.join(localDir, 'shared.md'), 'utf8')).toBe('from-repo'); + expect(await fs.readFile(path.join(localDir, 'local-only.md'), 'utf8')).toBe('keep-me'); + }); + }); +}); diff --git a/src/sync/apply.ts b/src/sync/apply.ts index 0e0e9f7..4686f21 100644 --- a/src/sync/apply.ts +++ b/src/sync/apply.ts @@ -11,6 +11,7 @@ import { chmodIfExists, deepMerge, hasOwn, + type PullMode, parseJsonc, pathExists, stripOverrides, @@ -51,18 +52,19 @@ interface ExtraPathManifest { export async function syncRepoToLocal( plan: SyncPlan, overrides: Record | null, - options: { chunkOptions?: ChunkOptions } = {} + options: { chunkOptions?: ChunkOptions; pullMode?: PullMode } = {} ): Promise { + const merge = options.pullMode === 'merge'; for (const item of plan.items) { if (item.chunkLargeFiles) { await copyChunkableItemFromRepo(item, plan.repoRoot, options.chunkOptions); continue; } - await copyItem(item.repoPath, item.localPath, item.type); + await copyItem(item.repoPath, item.localPath, item.type, false, merge); } - await applyExtraPaths(plan, plan.extraConfigs); - await applyExtraPaths(plan, plan.extraSecrets); + await applyExtraPaths(plan, plan.extraConfigs, merge); + await applyExtraPaths(plan, plan.extraSecrets, merge); if (overrides && Object.keys(overrides).length > 0) { await applyOverridesToLocalConfig(plan, overrides); @@ -424,7 +426,8 @@ async function copyItem( sourcePath: string, destinationPath: string, type: SyncItem['type'], - removeWhenMissing = false + removeWhenMissing = false, + merge = false ): Promise { if ( type === 'file' && @@ -447,7 +450,9 @@ async function copyItem( return; } - await removePath(destinationPath); + if (!merge) { + await removePath(destinationPath); + } await copyDirRecursive(sourcePath, destinationPath); } @@ -584,7 +589,7 @@ async function removePath(targetPath: string): Promise { await fs.rm(targetPath, { recursive: true, force: true }); } -async function applyExtraPaths(plan: SyncPlan, extra: ExtraPathPlan): Promise { +async function applyExtraPaths(plan: SyncPlan, extra: ExtraPathPlan, merge = false): Promise { const allowlist = extra.allowlist; if (allowlist.length === 0) return; @@ -606,7 +611,7 @@ async function applyExtraPaths(plan: SyncPlan, extra: ExtraPathPlan): Promise { expect(normalized.includeAgentsDir).toBe(false); }); + it('defaults pullMode to replace and accepts merge', () => { + expect(normalizeSyncConfig({}).pullMode).toBe('replace'); + expect(normalizeSyncConfig({ pullMode: 'merge' }).pullMode).toBe('merge'); + expect(normalizeSyncConfig({ pullMode: 'bogus' as never }).pullMode).toBe('replace'); + }); + it('defaults extra path lists when omitted', () => { const normalized = normalizeSyncConfig({ includeSecrets: true }); expect(normalized.extraSecretPaths).toEqual([]); diff --git a/src/sync/config.ts b/src/sync/config.ts index d22e1cc..744a63c 100644 --- a/src/sync/config.ts +++ b/src/sync/config.ts @@ -26,6 +26,14 @@ export interface SecretsBackendConfig { export type SessionBackendType = 'git' | 'turso'; +/** + * How `pull` applies synced directories to the local machine. + * - `replace` (default): each synced directory is replaced by the repo copy, so local-only files + * inside it are deleted. + * - `merge`: repo files are copied over local ones and local-only files are left untouched. + */ +export type PullMode = 'replace' | 'merge'; + export interface TursoSessionBackendSettings { database?: string; url?: string; @@ -61,6 +69,7 @@ export interface SyncConfig { includeModelFavorites?: boolean; includeOpencodeSkills?: boolean; includeAgentsDir?: boolean; + pullMode?: PullMode; secretsBackend?: SecretsBackendConfig; extraSecretPaths?: string[]; extraConfigPaths?: string[]; @@ -75,6 +84,7 @@ export interface NormalizedSyncConfig extends SyncConfig { includeModelFavorites: boolean; includeOpencodeSkills: boolean; includeAgentsDir: boolean; + pullMode: PullMode; secretsBackend?: SecretsBackendConfig; extraSecretPaths: string[]; extraConfigPaths: string[]; @@ -188,6 +198,7 @@ export function normalizeSyncConfig(config: SyncConfig): NormalizedSyncConfig { includeModelFavorites, includeOpencodeSkills, includeAgentsDir, + pullMode: config.pullMode === 'merge' ? 'merge' : 'replace', secretsBackend: normalizeSecretsBackend(config.secretsBackend), extraSecretPaths: Array.isArray(config.extraSecretPaths) ? config.extraSecretPaths : [], extraConfigPaths: Array.isArray(config.extraConfigPaths) ? config.extraConfigPaths : [], diff --git a/src/sync/service.ts b/src/sync/service.ts index ba27d7a..69b290e 100644 --- a/src/sync/service.ts +++ b/src/sync/service.ts @@ -923,7 +923,7 @@ export function createSyncService(ctx: SyncServiceContext): SyncService { const overrides = await loadOverrides(locations); const plan = buildSyncPlan(config, locations, repoRoot); - await syncRepoToLocal(plan, overrides); + await syncRepoToLocal(plan, overrides, { pullMode: config.pullMode }); await updateState(locations, { lastPull: new Date().toISOString(), @@ -1020,7 +1020,7 @@ export function createSyncService(ctx: SyncServiceContext): SyncService { const overrides = await loadOverrides(locations); const plan = buildSyncPlan(config, locations, repoRoot); - await syncRepoToLocal(plan, overrides); + await syncRepoToLocal(plan, overrides, { pullMode: config.pullMode }); await runSecretsPullIfConfigured(config); await updateState(locations, { @@ -1479,7 +1479,7 @@ async function runStartup( log.info('Pulled remote changes', { branch }); const overrides = await loadOverrides(locations); const plan = buildSyncPlan(config, locations, repoRoot); - await syncRepoToLocal(plan, overrides); + await syncRepoToLocal(plan, overrides, { pullMode: config.pullMode }); await options.runSecretsPullIfConfigured(config); await updateState(locations, { lastPull: new Date().toISOString(),