pi-config-sync 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -31,20 +31,30 @@ That is the whole setup — from here on, syncing is automatic.
31
31
  ```text
32
32
  session start → auto-sync (rate-limited, default every 5 min across sessions)
33
33
  ensure ignore rules → commit allowlisted changes → fetch
34
- → fast-forward or rebase → push
34
+ → fast-forward, or merge (settings.json merges key by key) → push
35
35
  → notifies you to /reload when remote changes were pulled
36
36
  session shutdown → best-effort commit + push (next start reconciles the rest)
37
37
  ```
38
38
 
39
39
  ### Machine-local settings
40
40
 
41
- `settings.json` uses a git clean/smudge filter: portable settings are committed, while each machine retains its own selected top-level values. By default this is `lastChangelogVersion`; machine values stay in your live settings file (mirrored to `.git-sync/settings.machine.json`) and are absent only from committed blobs. On an existing repository, the first sync after upgrading creates a one-time commit that strips those keys from the committed blob and normalizes JSON formatting — the live settings file is unchanged.
41
+ `settings.json` uses a git clean/smudge filter: portable settings are committed, while each machine retains its own selected top-level values. By default these are `lastChangelogVersion` and `extensionChangelogVersions` (pi's record of which pi and extension changelogs this machine has shown); machine values stay in your live settings file (mirrored to `.git-sync/settings.machine.json`) and are absent only from committed blobs. On an existing repository, the first sync after upgrading creates a one-time commit that strips those keys from the committed blob and normalizes JSON formatting — the live settings file is unchanged.
42
42
 
43
43
  The filter is fail-open: a checkout on a machine without pi-config-sync still works, but its machine-local values are not stripped until the package runs again. A newly linked machine gets its sidecar as pi writes its local settings.
44
44
 
45
+ ### Merging changes from two machines
46
+
47
+ When both machines committed changes, the sync merges once (no rebase). Conflicts never stop the sync and never delete anything:
48
+
49
+ - `settings.json` merges key by key, even nested keys, so changing the theme on one machine and the default model on another simply combine. Lists such as `packages` merge item by item: additions and removals from both machines apply. Machine-local keys are ignored by the merge, so older commits that still contain them do not conflict.
50
+ - Other files merge line by line as usual.
51
+ - When the same setting or the same lines changed differently on both machines, the newer change wins (by commit time of that file on each side; auto-sync commits shortly after you edit, even offline). The other version is saved on this machine under `.git-sync/conflicts/<time>/<file>`, which never syncs, and pi shows a notice naming what was kept. A newer deletion wins over an older edit the same way.
52
+
53
+ Machines on an older package version still use git's rebase and line-based merge, and can stop on a conflict until they update.
54
+
45
55
  The filter script runs under the Node.js that runs pi. When pi ships as a single bundled executable (for example through mise), that executable cannot run the script, so the package uses the first `node` found on `PATH` instead and falls back to a disabled filter with a warning. Set `nodePath` in `git-sync.jsonc` to pin a specific Node.js binary; the git filter configuration is corrected on the next sync.
46
56
 
47
- Commits look like `pi config: auto-sync from <hostname>`. Concurrent pi sessions coordinate through a pid-liveness lock, subagent child processes never sync, and a diverged repo aborts the rebase and asks you to resolve manually — nothing is ever forced.
57
+ Commits look like `pi config: auto-sync from <hostname>`. Concurrent pi sessions coordinate through a pid-liveness lock, subagent child processes never sync, and conflicting changes keep the newer version and save the other — nothing is ever forced or lost.
48
58
 
49
59
  ## Commands
50
60
 
@@ -53,7 +63,7 @@ Commits look like `pi config: auto-sync from <hostname>`. Concurrent pi sessions
53
63
  | `/gitsync init [name\|owner/name\|url]` | Create and connect the first-machine repository |
54
64
  | `/gitsync link [name\|owner/name\|url]` | Connect another machine |
55
65
  | `/gitsync status` | Show repository and tracked-secret status |
56
- | `/gitsync sync` | Commit, fetch/rebase, and push |
66
+ | `/gitsync sync` | Commit, fetch/merge, and push |
57
67
  | `/gitsync push` | Commit and push without pulling |
58
68
  | `/gitsync pull` | Fetch and integrate remote changes |
59
69
 
@@ -99,22 +109,20 @@ Create `~/.pi/agent/git-sync.jsonc`:
99
109
  "includeHostname": true,
100
110
  "extraPaths": ["my-safe-directory"],
101
111
  "warnOnPublicRemote": true,
102
- "machineLocalSettings": ["lastChangelogVersion"],
112
+ "machineLocalSettings": ["shellPath"],
103
113
  "nodePath": "/usr/local/bin/node"
104
114
  }
105
115
  ```
106
116
 
107
- Unsafe extra paths (secrets, cache names, absolute paths, or `..`) are rejected. To sync your user-level MCP server definitions (`pi mcp add`, `/mcp`), add `"mcp.json"` to `extraPaths`. It is opt-in because servers can be configured with inline API keys or headers; keep secrets in `${ENV_VAR}` references or OAuth before enabling it. `mcp-auth.json` and `mcp.log` never sync. Set `includeHostname` to `false` to omit the machine hostname from automatic commit messages. `machineLocalSettings` replaces the default list when set; it accepts top-level keys only, and `[]` disables stripping (the filter still normalizes committed JSON formatting). Useful candidates include `shellPath`, `externalEditor`, `npmCommand`, `sessionDir`, and `trackingId`. `nodePath` is optional and only needed when pi runs as a bundled executable and Node.js is not on `PATH`, or to pin a specific interpreter for the `settings.json` filter.
117
+ Unsafe extra paths (secrets, cache names, absolute paths, or `..`) are rejected. To sync your user-level MCP server definitions (`pi mcp add`, `/mcp`), add `"mcp.json"` to `extraPaths`. It is opt-in because servers can be configured with inline API keys or headers; keep secrets in `${ENV_VAR}` references or OAuth before enabling it. `mcp-auth.json` and `mcp.log` never sync. Set `includeHostname` to `false` to omit the machine hostname from automatic commit messages. `machineLocalSettings` adds keys to the built-in list (`lastChangelogVersion`, `extensionChangelogVersions`), which always applies; it accepts top-level keys only. Useful candidates include `shellPath`, `externalEditor`, `npmCommand`, `sessionDir`, and `trackingId`. `nodePath` is optional and only needed when pi runs as a bundled executable and Node.js is not on `PATH`, or to pin a specific interpreter for the `settings.json` filter.
108
118
 
109
119
  ## Conflicts and uninstall
110
120
 
111
- On a sync conflict, rebase is aborted and the repository is left untouched for manual resolution:
121
+ Conflicting changes resolve automatically (see [Merging changes from two machines](#merging-changes-from-two-machines)). To restore the version that was not kept, copy it back from `~/.pi/agent/.git-sync/conflicts/<time>/<file>`; the next sync shares it. Delete old folders there whenever you like.
112
122
 
113
- ```sh
114
- cd ~/.pi/agent
115
- git status
116
- git pull --rebase
117
- ```
123
+ If git itself fails during a pull, the merge is aborted, the repository is left as it was, and the error shows git's message. Check `git status` in `~/.pi/agent`, then run `/gitsync sync` again.
124
+
125
+ A machine stuck with `local and remote diverged with conflicts; rebase aborted` is running an older version: update with `pi update npm:pi-config-sync` and run `/reload`. The next sync recovers on its own.
118
126
 
119
127
  Remove the package with `pi remove npm:pi-config-sync`. Your local repository and remote remain until you delete them yourself.
120
128
 
@@ -11,7 +11,9 @@ const START = "# >>> pi-config-sync managed — do not edit inside this block";
11
11
  const END = "# <<< pi-config-sync managed";
12
12
  const LEGACY_MARKERS: Array<[string, string]> = [["# >>> pi-git-sync managed — do not edit inside this block", "# <<< pi-git-sync managed"]];
13
13
  const DEFAULT_PATHS = [".gitignore", ".gitattributes", "settings.json", "AGENTS.md", "git-sync.jsonc", "extensions", "chains", "prompts", "themes", "skills"];
14
- const DEFAULT_MACHINE_LOCAL = ["lastChangelogVersion"];
14
+ const DEFAULT_MACHINE_LOCAL = ["lastChangelogVersion", "extensionChangelogVersions"];
15
+ const ATTRIBUTES_LINE = "settings.json filter=pi-config-sync merge=pi-config-sync";
16
+ const LEGACY_ATTRIBUTES_LINES = ["settings.json filter=pi-config-sync"];
15
17
  const RUNTIME_LOG_DIR = "logs";
16
18
  const IGNORE_DENY = [`**/${RUNTIME_LOG_DIR}/`, "auth*", "mcp-auth*", "mcp.log*", "*token*", "*secret*", "*credential*", "*.env", "*.env.*", "*.local.json", "**/node_modules/", ".DS_Store"];
17
19
  const LIKELY_SYNC_REPO_NAMES = ["pi-agent-config", "my-pi-config", "pi-config", "dotfiles-pi"];
@@ -45,7 +47,7 @@ export default function gitSync(pi: ExtensionAPI) {
45
47
 
46
48
  function dirOf(deps?: Deps) { const value = deps?.dir ?? process.env.PI_CODING_AGENT_DIR?.trim(); return !value ? path.join(os.homedir(), ".pi", "agent") : value === "~" || value.startsWith("~/") ? path.join(os.homedir(), value.slice(2)) : path.resolve(value); }
47
49
  function gitEnv(dir: string) { return { ...process.env, GIT_TERMINAL_PROMPT: "0", GIT_CEILING_DIRECTORIES: path.dirname(dir) }; }
48
- function git(args: string[], dir: string, timeout = 10_000) { return exec("git", args, { cwd: dir, timeout, maxBuffer: 16 * 1024 * 1024, env: gitEnv(dir) }); }
50
+ function git(args: string[], dir: string, timeout = 10_000, env: Record<string, string> = {}) { return exec("git", args, { cwd: dir, timeout, maxBuffer: 16 * 1024 * 1024, env: { ...gitEnv(dir), ...env } }); }
49
51
  function gitRaw(args: string[], dir: string) { return exec("git", args, { cwd: dir, timeout: 10_000, maxBuffer: 64 * 1024 * 1024, encoding: "buffer", env: gitEnv(dir) }) as unknown as Promise<{ stdout: Buffer; stderr: Buffer }>; }
50
52
  function notify(ctx: Ctx | undefined, text: string, level: Level, deps?: Deps) { if (deps?.notify) deps.notify(text, level); else if (ctx?.hasUI) ctx.ui.notify(text, level); }
51
53
  const warnedOnce = new Set<string>();
@@ -59,24 +61,48 @@ function isSubagentChild() { return Number(process.env.PI_SUBAGENT_DEPTH ?? "0")
59
61
  export function isDenied(file: string) { const parts = file.toLowerCase().replaceAll("\\", "/").split("/").filter(Boolean); return parts.some(part => ["sessions", "state", "npm", "git", "bin", ".git-sync", "node_modules", RUNTIME_LOG_DIR].includes(part) || part.startsWith("auth") || part.startsWith("mcp-auth") || part.startsWith("mcp.log") || part.includes("token") || part.includes("secret") || part.includes("credential") || part === ".env" || part.includes(".env.") || part.endsWith(".env") || part.endsWith(".local.json")); }
60
62
  function validExtra(entry: string) { return entry.trim() !== "" && !entry.includes("..") && !path.isAbsolute(entry) && !isDenied(entry); }
61
63
  export function stripJsonComments(input: string) { let out = "", quote = "", escaped = false; for (let i = 0; i < input.length; i++) { const c = input[i]!, next = input[i + 1]; if (quote) { out += c; if (escaped) escaped = false; else if (c === "\\") escaped = true; else if (c === quote) quote = ""; continue; } if (c === '"' || c === "'") { quote = c; out += c; } else if (c === "/" && next === "/") { while (i < input.length && input[i] !== "\n") i++; out += "\n"; } else if (c === "/" && next === "*") { i += 2; while (i < input.length && !(input[i] === "*" && input[i + 1] === "/")) i++; i++; } else out += c; } return out; }
62
- export function machineKeys(config: GitSyncConfig) { return config.machineLocalSettings ?? DEFAULT_MACHINE_LOCAL; }
64
+ /** Built-in machine-local keys always apply; machineLocalSettings adds to them. */
65
+ export function machineKeys(config: GitSyncConfig) { return [...new Set([...DEFAULT_MACHINE_LOCAL, ...(config.machineLocalSettings ?? [])])]; }
63
66
  export async function readConfig(deps?: Deps, ctx?: Ctx): Promise<GitSyncConfig> { const dir = dirOf(deps); try { const raw = JSON.parse(stripJsonComments(await fs.readFile(path.join(dir, "git-sync.jsonc"), "utf8"))) as GitSyncConfig; const extras = (raw.extraPaths ?? []).filter(validExtra); if (extras.length !== (raw.extraPaths ?? []).length) warnOnce(ctx, "git-sync: unsafe extraPaths were ignored", deps, `${dir}|extras`); let machineLocalSettings: string[] | undefined; if (Array.isArray(raw.machineLocalSettings)) { machineLocalSettings = raw.machineLocalSettings.filter((key): key is string => typeof key === "string" && key.trim() !== ""); if (machineLocalSettings.length !== raw.machineLocalSettings.length) warnOnce(ctx, "git-sync: invalid machineLocalSettings entries were ignored", deps, `${dir}|machineKeys`); } let nodePath: string | undefined; if (raw.nodePath !== undefined) { if (typeof raw.nodePath === "string" && raw.nodePath.trim() !== "") nodePath = raw.nodePath.trim(); else warnOnce(ctx, "git-sync: invalid nodePath was ignored", deps, `${dir}|nodePath`); } return { ...raw, extraPaths: extras, machineLocalSettings, nodePath }; } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return {}; warnOnce(ctx, "git-sync: invalid git-sync.jsonc; using defaults", deps, `${dir}|invalid`); return {}; } }
64
67
  function allowRules(entry: string) { const segments = entry.replace(/\/+$/, "").split("/").filter(Boolean); const rules: string[] = []; let prefix = ""; for (let i = 0; i < segments.length; i++) { prefix = prefix ? `${prefix}/${segments[i]}` : segments[i]!; if (i < segments.length - 1) rules.push(`!${prefix}/`); else rules.push(`!${prefix}`, `!${prefix}/`, `!${prefix}/**`); } return rules; }
65
68
  export async function ensureIgnoreRules(dir = dirOf(), config: GitSyncConfig = {}) { const file = path.join(dir, ".gitignore"); let existing = ""; try { existing = await fs.readFile(file, "utf8"); } catch {} const paths = [...DEFAULT_PATHS, ...(config.extraPaths ?? []).filter(validExtra)]; const lines = [START, "*", ...paths.flatMap(allowRules), "# hard denylist — re-ignored even inside allowed dirs", ...IGNORE_DENY, END]; const block = lines.join("\n"); let base = existing, replaced = false; for (const [start, end] of [[START, END] as [string, string], ...LEGACY_MARKERS]) { const re = new RegExp(`${escapeRegExp(start)}[\\s\\S]*?${escapeRegExp(end)}\\n?`); if (re.test(base)) { base = base.replace(re, ""); replaced = true; } } await fs.mkdir(dir, { recursive: true }); await fs.writeFile(file, (replaced ? base : base.replace(/\s*$/, "\n")) + block + "\n"); }
66
69
  export async function ensureInfoExclude(dir = dirOf()) { const file = path.join(dir, ".git", "info", "exclude"); await fs.mkdir(path.dirname(file), { recursive: true }); let current = ""; try { current = await fs.readFile(file, "utf8"); } catch {} const missing = IGNORE_DENY.filter(rule => !current.split("\n").includes(rule)); if (missing.length) await fs.writeFile(file, current.replace(/\s*$/, "\n") + missing.join("\n") + "\n"); }
67
70
  function escapeRegExp(text: string) { return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); }
68
- export async function ensureAttributes(dir = dirOf()) { const file = path.join(dir, ".gitattributes"); let current = ""; try { current = await fs.readFile(file, "utf8"); } catch {} const line = "settings.json filter=pi-config-sync"; if (!current.split(/\r?\n/).includes(line)) await fs.writeFile(file, `${current.replace(/\s*$/, "")}\n${line}\n`.replace(/^\n/, "")); }
71
+ export async function ensureAttributes(dir = dirOf()) { const file = path.join(dir, ".gitattributes"); let current = ""; try { current = await fs.readFile(file, "utf8"); } catch {} const lines = current.split(/\r?\n/); if (lines.includes(ATTRIBUTES_LINE) && !lines.some(line => LEGACY_ATTRIBUTES_LINES.includes(line))) return; const kept = lines.filter(line => line !== ATTRIBUTES_LINE && !LEGACY_ATTRIBUTES_LINES.includes(line)).join("\n"); await fs.writeFile(file, `${kept.replace(/\s*$/, "")}\n${ATTRIBUTES_LINE}\n`.replace(/^\n/, "")); }
69
72
  function filterSource(keys: string[]) { return `import fs from "node:fs";
70
73
  const keys=${JSON.stringify(keys)};
71
74
  const chunks=[]; for await (const chunk of process.stdin) chunks.push(chunk); const raw=Buffer.concat(chunks);
72
75
  try { const value=JSON.parse(raw.toString("utf8")); if (!value || Array.isArray(value) || typeof value !== "object") throw new Error("not object"); if (process.argv[2] === "clean") { for (const key of keys) delete value[key]; process.stdout.write(JSON.stringify(value, null, 2)+"\\n"); } else if (process.argv[2] === "smudge") { const sidecar=JSON.parse(fs.readFileSync(new URL("settings.machine.json", import.meta.url), "utf8")); if (!sidecar || Array.isArray(sidecar) || typeof sidecar !== "object") throw new Error("bad sidecar"); process.stdout.write(JSON.stringify({...value, ...sidecar}, null, 2)+"\\n"); } else process.stdout.write(raw); } catch { process.stdout.write(raw); }
73
76
  `; }
77
+ /**
78
+ * Git merge driver for settings.json (`%O %A %B`): a key-level three-way JSON merge, so edits to different keys on
79
+ * different machines never conflict. Lists merge item by item (additions and removals from both sides apply).
80
+ * Machine-local keys are dropped from every side, since they never belong in the committed form. Keys changed
81
+ * differently on both sides are recorded in merge-conflicts.json; with PI_CONFIG_SYNC_PREFER=ours|theirs (set by the
82
+ * sync) that side's value is kept, otherwise (a manual git merge) it falls back to a text merge with conflict markers.
83
+ */
84
+ function mergeSource(keys: string[]) { return `import fs from "node:fs"; import { execFileSync } from "node:child_process";
85
+ const keys=${JSON.stringify(keys)};
86
+ const [basePath, oursPath, theirsPath] = process.argv.slice(2);
87
+ const isObject = value => value !== null && typeof value === "object" && !Array.isArray(value);
88
+ const equal = (a, b) => { if (a === b) return true; if (Array.isArray(a) && Array.isArray(b)) return a.length === b.length && a.every((value, i) => equal(value, b[i])); if (!isObject(a) || !isObject(b)) return false; const ka = Object.keys(a), kb = Object.keys(b); return ka.length === kb.length && ka.every(key => Object.hasOwn(b, key) && equal(a[key], b[key])); };
89
+ const read = file => { const value = JSON.parse(fs.readFileSync(file, "utf8")); if (!isObject(value)) throw new Error("not an object"); for (const key of keys) delete value[key]; return value; };
90
+ const conflicts = [], prefer = process.env.PI_CONFIG_SYNC_PREFER;
91
+ const mergeList = (base, ours, theirs) => { const id = value => JSON.stringify(value), inBase = new Set(base.map(id)), inTheirs = new Set(theirs.map(id)); const out = ours.filter(value => !inBase.has(id(value)) || inTheirs.has(id(value))), have = new Set(out.map(id)); for (const value of theirs) if (!inBase.has(id(value)) && !have.has(id(value))) { out.push(value); have.add(id(value)); } return out; };
92
+ const merge = (base, ours, theirs, at) => { if (equal(ours, theirs)) return ours; if (equal(base, ours)) return theirs; if (equal(base, theirs)) return ours; if (Array.isArray(ours) && Array.isArray(theirs) && (base === undefined || Array.isArray(base))) return mergeList(base ?? [], ours, theirs); if (isObject(ours) && isObject(theirs)) { const out = {}, from = isObject(base) ? base : {}; for (const key of new Set([...Object.keys(ours), ...Object.keys(theirs)])) { const value = merge(from[key], ours[key], theirs[key], [...at, key]); if (value !== undefined) out[key] = value; } return out; } conflicts.push(at.join(".") || "(whole file)"); return prefer === "theirs" ? theirs : ours; };
93
+ const textMerge = () => { try { execFileSync("git", ["merge-file", "-L", "ours", "-L", "base", "-L", "theirs", oursPath, basePath, theirsPath], { stdio: "ignore" }); return true; } catch { return false; } };
94
+ let merged;
95
+ try { let base = {}; try { base = read(basePath); } catch {} merged = merge(base, read(oursPath), read(theirsPath), []); } catch { process.exit(textMerge() ? 0 : 1); }
96
+ if (conflicts.length) { const report = new URL("merge-conflicts.json", import.meta.url); let seen = []; try { seen = JSON.parse(fs.readFileSync(report, "utf8")); } catch {} fs.writeFileSync(report, JSON.stringify([...new Set([...seen, ...conflicts])])+"\\n"); }
97
+ if (!conflicts.length || prefer === "ours" || prefer === "theirs") { fs.writeFileSync(oursPath, JSON.stringify(merged, null, 2)+"\\n"); process.exit(0); }
98
+ textMerge(); process.exit(1);
99
+ `; }
74
100
  /** pi shipped as a single executable (Node SEA or compiled Bun binary): process.execPath is pi itself and cannot run the filter script. */
75
101
  async function detectRuntime(): Promise<Runtime> { const execPath = process.execPath; if (process.versions.bun) return { bundled: !/^bun/i.test(path.basename(execPath)), execPath }; try { return { bundled: (await import("node:sea")).isSea(), execPath }; } catch { return { bundled: false, execPath }; } }
76
102
  export async function findOnPath(name: string, pathEnv = process.env.PATH ?? "", platform = process.platform) { const names = platform === "win32" ? [`${name}.exe`, `${name}.cmd`, name] : [name]; for (const directory of pathEnv.split(path.delimiter).filter(Boolean)) for (const candidate of names) { const full = path.join(directory, candidate); try { await fs.access(full, fs.constants.X_OK); return full; } catch {} } return undefined; }
77
103
  export async function filterInterpreter(config: GitSyncConfig, runtime: Runtime, ctx?: Ctx, deps?: Deps) { if (config.nodePath) return config.nodePath; if (!runtime.bundled) return runtime.execPath; const found = await findOnPath("node", runtime.pathEnv, runtime.platform); if (found) return found; warnOnce(ctx, "git-sync: pi runs as a bundled executable and no node was found on PATH; settings.json filter is disabled until nodePath is set in git-sync.jsonc", deps, "nodePath"); return "node"; }
78
104
  async function gitConfigValue(key: string, dir: string) { try { return (await git(["config", "--get", key], dir)).stdout.trim(); } catch { return undefined; } }
79
- export async function ensureFilter(dir = dirOf(), config: GitSyncConfig = {}, ctx?: Ctx, deps?: Deps) { if (!(await hasDotGit(dir))) return; await fs.mkdir(stateDir(dir), { recursive: true }); const script = path.join(stateDir(dir), "filter.mjs"), source = filterSource(machineKeys(config)); try { if (await fs.readFile(script, "utf8") !== source) await fs.writeFile(script, source); } catch { await fs.writeFile(script, source); } const interpreter = await filterInterpreter(config, deps?.runtime ?? await detectRuntime(), ctx, deps); const quoted = (value: string) => `'${value.replaceAll("'", "'\\''")}'`; const values: Record<string, string> = { "filter.pi-config-sync.clean": `${quoted(interpreter)} ${quoted(script)} clean`, "filter.pi-config-sync.smudge": `${quoted(interpreter)} ${quoted(script)} smudge`, "filter.pi-config-sync.required": "false" }; let configured = false; for (const [key, value] of Object.entries(values)) if (await gitConfigValue(key, dir) !== value) { await git(["config", key, value], dir); configured = true; } if (configured) try { await git(["add", "--renormalize", "settings.json"], dir); } catch {} }
105
+ export async function ensureFilter(dir = dirOf(), config: GitSyncConfig = {}, ctx?: Ctx, deps?: Deps) { if (!(await hasDotGit(dir))) return; await fs.mkdir(stateDir(dir), { recursive: true }); const script = path.join(stateDir(dir), "filter.mjs"), mergeScript = path.join(stateDir(dir), "merge.mjs"); let configured = await writeIfChanged(script, filterSource(machineKeys(config))); await writeIfChanged(mergeScript, mergeSource(machineKeys(config))); const interpreter = await filterInterpreter(config, deps?.runtime ?? await detectRuntime(), ctx, deps); const quoted = (value: string) => `'${value.replaceAll("'", "'\\''")}'`; const values: Record<string, string> = { "filter.pi-config-sync.clean": `${quoted(interpreter)} ${quoted(script)} clean`, "filter.pi-config-sync.smudge": `${quoted(interpreter)} ${quoted(script)} smudge`, "filter.pi-config-sync.required": "false", "merge.pi-config-sync.name": "pi-config-sync settings.json key-level merge", "merge.pi-config-sync.driver": `${quoted(interpreter)} ${quoted(mergeScript)} %O %A %B` }; for (const [key, value] of Object.entries(values)) if (await gitConfigValue(key, dir) !== value) { await git(["config", key, value], dir); configured = true; } if (configured) try { await git(["add", "--renormalize", "settings.json"], dir); } catch {} }
80
106
  export async function refreshMachineSidecar(dir = dirOf(), config: GitSyncConfig = {}) { try { const settings = JSON.parse(await fs.readFile(path.join(dir, "settings.json"), "utf8")) as Record<string, unknown>; if (!settings || Array.isArray(settings) || typeof settings !== "object") return; const sidecar = Object.fromEntries(machineKeys(config).filter(key => Object.hasOwn(settings, key)).map(key => [key, settings[key]])); const output = JSON.stringify(sidecar, null, 2)+"\n", file = path.join(stateDir(dir), "settings.machine.json"); await fs.mkdir(stateDir(dir), { recursive: true }); try { if (await fs.readFile(file, "utf8") === output) return; } catch {} await fs.writeFile(file, output); } catch {} }
81
107
  async function prepareCommit(deps?: Deps, ctx?: Ctx) { const config = await readConfig(deps, ctx), dir = dirOf(deps); await ensureIgnoreRules(dir, config); await ensureInfoExclude(dir); await ensureAttributes(dir); await ensureFilter(dir, config, ctx, deps); await refreshMachineSidecar(dir, config); return config; }
82
108
  export async function stagedSecretFiles(dir = dirOf()) { const { stdout } = await git(["diff", "--cached", "--name-only", "--diff-filter=d", "-z"], dir); return stdout.split("\0").filter(Boolean).filter(isDenied); }
@@ -96,7 +122,79 @@ async function isSyncableRepo(dir = dirOf()) { if (!(await hasDotGit(dir))) retu
96
122
  async function upstreamRef(dir: string) { try { return (await git(["rev-parse", "--abbrev-ref", "@{u}"], dir)).stdout.trim(); } catch { return undefined; } }
97
123
  async function counts(upstream: string, dir: string) { const { stdout } = await git(["rev-list", "--left-right", "--count", `${upstream}...HEAD`], dir); const [behind = "0", ahead = "0"] = stdout.trim().split(/\s+/); return { behind: Number(behind), ahead: Number(ahead) }; }
98
124
  async function fetchOrigin(dir: string) { try { await git(["fetch", "origin"], dir); return true; } catch { return false; } }
99
- async function integrate(upstream: string, dir: string) { try { await git(["merge", "--ff-only", upstream], dir); return true; } catch {} try { await git(["rebase", upstream], dir); return true; } catch { try { await git(["rebase", "--abort"], dir); } catch {} return false; } }
125
+ /** True when a write was needed. */
126
+ async function writeIfChanged(file: string, content: string) { try { if (await fs.readFile(file, "utf8") === content) return false; } catch {} await fs.writeFile(file, content); return true; }
127
+ /** Pulled settings.json from an older package version can still carry machine-local keys; re-run the clean filter and commit if that changes the blob. */
128
+ async function normalizeSettings(deps?: Deps, ctx?: Ctx) { const dir = dirOf(deps); try { await git(["add", "--renormalize", "--", "settings.json"], dir); } catch { return false; } return commitLocalChanges(deps, undefined, ctx); }
129
+ /** A conflict the sync resolved by keeping the newer side; the other side is saved under the integration's backupDir. */
130
+ export interface ResolvedConflict { file: string; kept: "local" | "remote"; keys: string[] }
131
+ export interface IntegrateResult { resolved: ResolvedConflict[]; backupDir?: string }
132
+ type Side = ResolvedConflict["kept"];
133
+ async function lastChange(ref: string, file: string, dir: string) { try { return Number((await git(["log", "-1", "--format=%ct", ref, "--", file], dir)).stdout.trim()) || 0; } catch { return 0; } }
134
+ /** The side whose latest commit touching the file is newer; ties keep the remote version, which other machines already have. */
135
+ async function newerSide(local: string, remote: string, file: string, dir: string): Promise<Side> { return (await lastChange(local, file, dir)) > (await lastChange(remote, file, dir)) ? "local" : "remote"; }
136
+ async function unmergedStages(dir: string) { const stages = new Map<string, Set<number>>(); for (const entry of (await git(["ls-files", "-u", "-z"], dir)).stdout.split("\0").filter(Boolean)) { const [meta, file] = entry.split("\t") as [string, string]; const stage = Number(meta.split(" ")[2]); if (!stages.has(file)) stages.set(file, new Set()); stages.get(file)!.add(stage); } return stages; }
137
+ /** Resolve one conflicted path toward `kept`: non-conflicting hunks from both sides still merge; whole-file fallback for binaries, deletions, and settings.json. */
138
+ async function resolveFile(file: string, kept: Side, stages: Set<number>, dir: string) {
139
+ const stage = kept === "local" ? 2 : 3;
140
+ if (!stages.has(stage)) { await git(["rm", "--quiet", "--", file], dir); return; }
141
+ if (file !== "settings.json" && stages.has(1) && stages.has(2) && stages.has(3)) {
142
+ const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "pi-config-sync-merge-"));
143
+ try {
144
+ const [base, ours, theirs] = [1, 2, 3].map(n => path.join(tmp, String(n)));
145
+ for (const [n, target] of [[1, base], [2, ours], [3, theirs]] as const) await fs.writeFile(target!, (await gitRaw(["show", `:${n}:${file}`], dir)).stdout);
146
+ await git(["merge-file", kept === "local" ? "--ours" : "--theirs", ours!, base!, theirs!], dir);
147
+ await fs.writeFile(path.join(dir, file), await fs.readFile(ours!));
148
+ await git(["add", "--", file], dir);
149
+ return;
150
+ } catch {} finally { await fs.rm(tmp, { recursive: true, force: true }); }
151
+ }
152
+ await git(["checkout", kept === "local" ? "--ours" : "--theirs", "--", file], dir);
153
+ await git(["add", "--", file], dir);
154
+ }
155
+ async function backupLosers(resolved: ResolvedConflict[], local: string, remote: string, dir: string) {
156
+ const backupDir = path.join(stateDir(dir), "conflicts", new Date().toISOString().replace(/[:.]/g, "-"));
157
+ for (const { file, kept } of resolved) {
158
+ let content: Buffer; try { content = (await gitRaw(["show", `${kept === "local" ? remote : local}:${file}`], dir)).stdout; } catch { continue; }
159
+ await fs.mkdir(path.dirname(path.join(backupDir, file)), { recursive: true }); await fs.writeFile(path.join(backupDir, file), content);
160
+ }
161
+ return backupDir;
162
+ }
163
+ /**
164
+ * Fast-forward when possible, otherwise one merge commit. A single merge resolves each conflict once and never checks
165
+ * out older remote settings.json blobs, which may still carry machine-local keys written by older versions. Anything
166
+ * changed differently on both machines keeps the newer side (per file, by latest commit time); the other side is saved
167
+ * under .git-sync/conflicts/ so nothing is lost. Throws, leaving the repository as it was, only if git itself fails.
168
+ */
169
+ async function integrate(upstream: string, dir: string): Promise<IntegrateResult> {
170
+ try { await git(["merge", "--ff-only", upstream], dir); return { resolved: [] }; } catch {}
171
+ const local = (await git(["rev-parse", "HEAD"], dir)).stdout.trim(), remote = (await git(["rev-parse", upstream], dir)).stdout.trim();
172
+ const report = path.join(stateDir(dir), "merge-conflicts.json"); await fs.rm(report, { force: true });
173
+ const settingsSide = await newerSide(local, remote, "settings.json", dir);
174
+ let mergeError: unknown;
175
+ try { await git(["merge", "--no-edit", "-m", `pi config: merge ${upstream}`, upstream], dir, 60_000, { PI_CONFIG_SYNC_PREFER: settingsSide === "local" ? "ours" : "theirs" }); } catch (error) { mergeError = error; }
176
+ let settingsKeys: string[] = []; try { settingsKeys = JSON.parse(await fs.readFile(report, "utf8")) as string[]; } catch {} await fs.rm(report, { force: true });
177
+ const resolved: ResolvedConflict[] = settingsKeys.length ? [{ file: "settings.json", kept: settingsSide, keys: settingsKeys }] : [];
178
+ if (mergeError) {
179
+ try {
180
+ const unmerged = await unmergedStages(dir);
181
+ if (!unmerged.size) throw mergeError;
182
+ for (const [file, stages] of unmerged) {
183
+ const kept = await newerSide(local, remote, file, dir);
184
+ await resolveFile(file, kept, stages, dir);
185
+ const existing = resolved.find(entry => entry.file === file);
186
+ if (existing) existing.kept = kept; else resolved.push({ file, kept, keys: [] });
187
+ }
188
+ await git(["commit", "--no-edit"], dir);
189
+ } catch (error) {
190
+ try { await git(["merge", "--abort"], dir); } catch {}
191
+ const detail = String((error as { stderr?: string }).stderr ?? message(error)).split("\n").map(line => line.trim()).find(line => /^(error|fatal):/.test(line)) ?? message(error);
192
+ throw new Error(`can't pull remote changes: ${detail}. Nothing was changed locally. Check cd ${dir} && git status, then run /gitsync sync again.`);
193
+ }
194
+ }
195
+ return resolved.length ? { resolved, backupDir: await backupLosers(resolved, local, remote, dir) } : { resolved };
196
+ }
197
+ export function resolvedMessage(result: IntegrateResult) { const items = result.resolved.map(({ file, kept, keys }) => `${file}${keys.length ? ` (${keys.map(key => `"${key}"`).join(", ")})` : ""}: kept the ${kept === "local" ? "local" : "remote"} version`); return `git-sync: changed differently on two machines, kept the newer change — ${items.join("; ")}. The other version is saved in ${result.backupDir}.`; }
100
198
  async function assertSafeToPush(dir: string) {
101
199
  const tracked = (await git(["ls-tree", "-r", "--name-only", "-z", "HEAD"], dir)).stdout;
102
200
  const denied = new Set(tracked.split("\0").filter(Boolean).filter(isDenied));
@@ -109,7 +207,7 @@ async function assertSafeToPush(dir: string) {
109
207
  }
110
208
  async function push(first: boolean, dir: string) { await assertSafeToPush(dir); try { await git(first ? ["push", "-u", "origin", "HEAD"] : ["push"], dir, 15_000); return true; } catch { return false; } }
111
209
  async function bestEffortPush(deps?: Deps) { const dir = dirOf(deps); const upstream = await upstreamRef(dir); if (!upstream) return push(true, dir); const { ahead, behind } = await counts(upstream, dir); return ahead > 0 && behind === 0 ? push(false, dir) : false; }
112
- export async function runSync(ctx: Ctx | undefined, options: { auto: boolean; push: boolean; skipPull?: boolean }, deps?: Deps) { const dir = dirOf(deps); if (!(await isSyncableRepo(dir))) { if (!options.auto) notify(ctx, `git-sync: no git repo with an 'origin' remote in ${dir}. Run /gitsync init.`, "warning", deps); return; } if (ctx?.hasUI) ctx.ui.setStatus(STATUS_KEY, "syncing"); try { await prepareCommit(deps, ctx); const tracked = await trackedSecretFiles(dir); if (tracked.length) notify(ctx, `git-sync: tracked sensitive files: ${tracked.join(", ")}. Remove with git rm --cached <file>.`, "warning", deps); const changed: string[] = []; if (await commitLocalChanges(deps, undefined, ctx)) changed.push("committed local changes"); for (const line of gapMessages(await syncGaps(dir, await readConfig(deps, ctx)))) warnOnce(ctx, `git-sync: ${line}`, deps, `${dir}|${line}`); if (!options.skipPull) { if (!(await fetchOrigin(dir))) { if (!options.auto) notify(ctx, "git-sync: fetch failed; skipped pull/push.", "warning", deps); return; } const upstream = await upstreamRef(dir); if (upstream && (await counts(upstream, dir)).behind > 0) { if (!(await integrate(upstream, dir))) throw new Error(`local and remote diverged with conflicts; rebase aborted in ${dir}`); changed.push("pulled updates"); } } if (options.push) { const upstream = await upstreamRef(dir); if (!upstream || (await counts(upstream, dir)).ahead > 0) { if (await push(!upstream, dir)) changed.push("pushed"); } } if (changed.some(x => x.startsWith("pulled"))) notify(ctx, `git-sync: ${changed.join(", ")}. Run /reload to apply pulled config.`, "info", deps); else if (!options.auto) notify(ctx, changed.length ? `git-sync: ${changed.join(", ")}.` : "git-sync: already up to date.", "info", deps); } finally { if (ctx?.hasUI) ctx.ui.setStatus(STATUS_KEY, undefined); } }
210
+ export async function runSync(ctx: Ctx | undefined, options: { auto: boolean; push: boolean; skipPull?: boolean }, deps?: Deps) { const dir = dirOf(deps); if (!(await isSyncableRepo(dir))) { if (!options.auto) notify(ctx, `git-sync: no git repo with an 'origin' remote in ${dir}. Run /gitsync init.`, "warning", deps); return; } if (ctx?.hasUI) ctx.ui.setStatus(STATUS_KEY, "syncing"); try { await prepareCommit(deps, ctx); const tracked = await trackedSecretFiles(dir); if (tracked.length) notify(ctx, `git-sync: tracked sensitive files: ${tracked.join(", ")}. Remove with git rm --cached <file>.`, "warning", deps); const changed: string[] = []; if (await commitLocalChanges(deps, undefined, ctx)) changed.push("committed local changes"); for (const line of gapMessages(await syncGaps(dir, await readConfig(deps, ctx)))) warnOnce(ctx, `git-sync: ${line}`, deps, `${dir}|${line}`); if (!options.skipPull) { if (!(await fetchOrigin(dir))) { if (!options.auto) notify(ctx, "git-sync: fetch failed; skipped pull/push.", "warning", deps); return; } const upstream = await upstreamRef(dir); if (upstream && (await counts(upstream, dir)).behind > 0) { const result = await integrate(upstream, dir); changed.push("pulled updates"); if (await normalizeSettings(deps, ctx)) changed.push("normalized settings.json"); if (result.resolved.length) notify(ctx, resolvedMessage(result), "warning", deps); } } if (options.push) { const upstream = await upstreamRef(dir); if (!upstream || (await counts(upstream, dir)).ahead > 0) { if (await push(!upstream, dir)) changed.push("pushed"); } } if (changed.some(x => x.startsWith("pulled"))) notify(ctx, `git-sync: ${changed.join(", ")}. Run /reload to apply pulled config.`, "info", deps); else if (!options.auto) notify(ctx, changed.length ? `git-sync: ${changed.join(", ")}.` : "git-sync: already up to date.", "info", deps); } finally { if (ctx?.hasUI) ctx.ui.setStatus(STATUS_KEY, undefined); } }
113
211
 
114
212
  export function parseRepoReference(input: string, fallbackOwner: string) { const raw = input.trim().replace(/\.git$/i, ""); if (!raw) return undefined; const ssh = raw.match(/^git@github\.com:([^/\s]+)\/([^/\s]+)$/i); if (ssh) return { owner: ssh[1]!, name: ssh[2]! }; try { const url = new URL(raw); if (["github.com", "www.github.com"].includes(url.hostname) && url.pathname.split("/").filter(Boolean).length === 2) { const [owner, name] = url.pathname.split("/").filter(Boolean); return { owner: owner!, name: name!.replace(/\.git$/i, "") }; } } catch {} const bits = raw.split("/").filter(Boolean); if (bits.length === 1) return { owner: fallbackOwner, name: bits[0]! }; if (bits.length === 2 && !raw.includes(":")) return { owner: bits[0]!, name: bits[1]! }; return undefined; }
115
213
  function remoteFromArg(arg: string, fallback: string) { if (/^(https?|ssh):\/\//.test(arg) || /^git@/.test(arg) || arg.startsWith("/") || arg.startsWith(".")) return arg; const ref = parseRepoReference(arg, fallback); return ref ? `https://github.com/${ref.owner}/${ref.name}.git` : undefined; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-config-sync",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/diogovdias/pi-config-sync.git"