sticky-note-cli 3.1.39 → 3.1.40

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sticky-note-cli",
3
- "version": "3.1.39",
3
+ "version": "3.1.40",
4
4
  "description": "Human-to-human handoff for AI coding assistants. Git-backed shared memory for Claude Code and Copilot CLI.",
5
5
  "license": "MIT",
6
6
  "bin": {
@@ -25,6 +25,7 @@
25
25
  },
26
26
  "files": [
27
27
  "bin/",
28
+ "src/",
28
29
  "templates/",
29
30
  "sticky-server/"
30
31
  ],
@@ -35,6 +36,6 @@
35
36
  "test": "node test/smoke.test.js"
36
37
  },
37
38
  "devDependencies": {
38
- "sticky-note-cli": "^3.1.5"
39
+ "sticky-note-cli": "^3.1.38"
39
40
  }
40
41
  }
@@ -0,0 +1,23 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { execFileSync } = require("child_process");
4
+
5
+ function getActiveStickyDir(cwd) {
6
+ const root = cwd || process.cwd();
7
+ try {
8
+ const gitDir = execFileSync("git", ["rev-parse", "--absolute-git-dir"], {
9
+ encoding: "utf-8", timeout: 3000, stdio: ["pipe", "pipe", "pipe"], cwd: root,
10
+ }).trim();
11
+ const dataBranchDir = path.join(gitDir, "sticky-note");
12
+ if (fs.existsSync(path.join(dataBranchDir, "sticky-note.json"))) {
13
+ return dataBranchDir;
14
+ }
15
+ } catch (_) {
16
+ // not a git repo or git unavailable
17
+ }
18
+ return path.join(root, ".sticky-note");
19
+ }
20
+
21
+ module.exports = {
22
+ getActiveStickyDir,
23
+ };
package/src/index.js ADDED
@@ -0,0 +1,57 @@
1
+ const ui = require("./utils/ui");
2
+ const fileIo = require("./utils/file-io");
3
+ const semver = require("./utils/semver");
4
+ const git = require("./utils/git");
5
+ const hook = require("./utils/hook");
6
+ const pkg = require("./utils/package");
7
+ const mcpConfig = require("./mcp/config");
8
+ const mcpDetection = require("./mcp/detection");
9
+ const stickyDir = require("./core/sticky-dir");
10
+
11
+ module.exports = {
12
+ ui: {
13
+ print: ui.print,
14
+ debugLog: ui.debugLog,
15
+ printBanner: ui.printBanner,
16
+ ask: ui.ask,
17
+ VERSION: ui.VERSION,
18
+ },
19
+ fileIo: {
20
+ mkdirSafe: fileIo.mkdirSafe,
21
+ copyFile: fileIo.copyFile,
22
+ readTemplate: fileIo.readTemplate,
23
+ readJsonSafe: fileIo.readJsonSafe,
24
+ readEnvSticky: fileIo.readEnvSticky,
25
+ makeExecutable: fileIo.makeExecutable,
26
+ updateInstructionSection: fileIo.updateInstructionSection,
27
+ countJsonlLines: fileIo.countJsonlLines,
28
+ },
29
+ semver: {
30
+ parseSemver: semver.parseSemver,
31
+ compareSemver: semver.compareSemver,
32
+ bumpMinVersion: semver.bumpMinVersion,
33
+ parseIntOr: semver.parseIntOr,
34
+ },
35
+ git: {
36
+ isGitRepo: git.isGitRepo,
37
+ getGitUserName: git.getGitUserName,
38
+ },
39
+ hook: {
40
+ resolveHookPaths: hook.resolveHookPaths,
41
+ findStaleHookPaths: hook.findStaleHookPaths,
42
+ installGitHook: hook.installGitHook,
43
+ },
44
+ pkg: {
45
+ pinCliInPackageJson: pkg.pinCliInPackageJson,
46
+ },
47
+ mcp: {
48
+ getCopilotCliConfigDir: mcpConfig.getCopilotCliConfigDir,
49
+ getCopilotCliMcpConfigPath: mcpConfig.getCopilotCliMcpConfigPath,
50
+ ensureMcpInCopilotCliConfig: mcpConfig.ensureMcpInCopilotCliConfig,
51
+ detectMcpServers: mcpDetection.detectMcpServers,
52
+ detectSkills: mcpDetection.detectSkills,
53
+ },
54
+ core: {
55
+ getActiveStickyDir: stickyDir.getActiveStickyDir,
56
+ },
57
+ };
@@ -0,0 +1,55 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+ function getCopilotCliConfigDir() {
5
+ if (process.env.COPILOT_HOME) return process.env.COPILOT_HOME;
6
+ const home = process.env.HOME || process.env.USERPROFILE;
7
+ if (!home) return null;
8
+ return path.join(home, ".copilot");
9
+ }
10
+
11
+ function getCopilotCliMcpConfigPath() {
12
+ const dir = getCopilotCliConfigDir();
13
+ return dir ? path.join(dir, "mcp-config.json") : null;
14
+ }
15
+
16
+ function ensureMcpInCopilotCliConfig(serverName, serverConfig) {
17
+ try {
18
+ const configDir = getCopilotCliConfigDir();
19
+ if (!configDir || !fs.existsSync(configDir)) return false;
20
+
21
+ const configPath = path.join(configDir, "mcp-config.json");
22
+ let config = {};
23
+ if (fs.existsSync(configPath)) {
24
+ try {
25
+ config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
26
+ } catch (_) {
27
+ config = {};
28
+ }
29
+ }
30
+
31
+ config.mcpServers = config.mcpServers || {};
32
+ const existing = config.mcpServers[serverName];
33
+ if (existing) {
34
+ if (!existing.tools) {
35
+ existing.tools = ["*"];
36
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
37
+ return true;
38
+ }
39
+ return false;
40
+ }
41
+
42
+ if (!serverConfig.tools) serverConfig.tools = ["*"];
43
+ config.mcpServers[serverName] = serverConfig;
44
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2) + "\n");
45
+ return true;
46
+ } catch (_) {
47
+ return false;
48
+ }
49
+ }
50
+
51
+ module.exports = {
52
+ getCopilotCliConfigDir,
53
+ getCopilotCliMcpConfigPath,
54
+ ensureMcpInCopilotCliConfig,
55
+ };
@@ -0,0 +1,127 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const os = require("os");
4
+ const { getCopilotCliMcpConfigPath } = require("./config");
5
+ const { debugLog } = require("../utils/ui");
6
+
7
+ function detectMcpServers() {
8
+ const servers = new Map();
9
+
10
+ const mcpJsonPath = path.join(process.cwd(), ".mcp.json");
11
+ if (fs.existsSync(mcpJsonPath)) {
12
+ try {
13
+ const mcpJson = JSON.parse(fs.readFileSync(mcpJsonPath, "utf-8"));
14
+ const mcpServers = mcpJson.mcpServers || {};
15
+ for (const [name, config] of Object.entries(mcpServers)) {
16
+ const entry = {
17
+ name,
18
+ type: config.type || config.transport || "unknown",
19
+ source: ".mcp.json",
20
+ };
21
+ if (config.command) entry.command = config.command;
22
+ if (config.args) entry.args = config.args;
23
+ if (config.env) entry.env = config.env;
24
+ if (config.url) entry.url = config.url;
25
+ servers.set(name, entry);
26
+ }
27
+ } catch (err) { debugLog("detectMcpServers .mcp.json: " + (err.message || err)); }
28
+ }
29
+
30
+ const copilotMcpPath = getCopilotCliMcpConfigPath();
31
+ if (copilotMcpPath && fs.existsSync(copilotMcpPath)) {
32
+ try {
33
+ const copilotMcp = JSON.parse(fs.readFileSync(copilotMcpPath, "utf-8"));
34
+ const mcpServers = copilotMcp.mcpServers || {};
35
+ for (const [name, config] of Object.entries(mcpServers)) {
36
+ if (servers.has(name)) continue;
37
+ const entry = {
38
+ name,
39
+ type: config.type || config.transport || "unknown",
40
+ source: "~/.copilot/mcp-config.json",
41
+ };
42
+ if (config.command) entry.command = config.command;
43
+ if (config.args) entry.args = config.args;
44
+ if (config.env) entry.env = config.env;
45
+ if (config.url) entry.url = config.url;
46
+ servers.set(name, entry);
47
+ }
48
+ } catch (err) { debugLog("detectMcpServers copilot mcp-config: " + (err.message || err)); }
49
+ }
50
+
51
+ const globalClaudeSettingsPath = path.join(os.homedir(), ".claude", "settings.json");
52
+ if (fs.existsSync(globalClaudeSettingsPath)) {
53
+ try {
54
+ const globalSettings = JSON.parse(fs.readFileSync(globalClaudeSettingsPath, "utf-8"));
55
+ const globalMcp = globalSettings.mcpServers || {};
56
+ for (const [name, config] of Object.entries(globalMcp)) {
57
+ if (servers.has(name)) continue;
58
+ if (config.type === "permission-detected") continue;
59
+ const entry = {
60
+ name,
61
+ type: config.type || config.transport || "stdio",
62
+ source: "~/.claude/settings.json",
63
+ };
64
+ if (config.command) entry.command = config.command;
65
+ if (config.args) entry.args = config.args;
66
+ if (config.env) entry.env = config.env;
67
+ if (config.url) entry.url = config.url;
68
+ servers.set(name, entry);
69
+ }
70
+ } catch (err) { debugLog("detectMcpServers global settings: " + (err.message || err)); }
71
+ }
72
+
73
+ const localSettingsPath = path.join(process.cwd(), ".claude", "settings.local.json");
74
+ if (fs.existsSync(localSettingsPath)) {
75
+ try {
76
+ const localSettings = JSON.parse(fs.readFileSync(localSettingsPath, "utf-8"));
77
+ const permissions = [
78
+ ...(localSettings.allow || []),
79
+ ...(localSettings.permissions?.allow || []),
80
+ ];
81
+ for (const perm of permissions) {
82
+ if (typeof perm === "string" && perm.startsWith("mcp__")) {
83
+ const serverName = perm.split("__")[1];
84
+ if (serverName && !servers.has(serverName)) {
85
+ servers.set(serverName, {
86
+ name: serverName,
87
+ type: "account-mcp",
88
+ source: "settings.local.json",
89
+ });
90
+ }
91
+ }
92
+ }
93
+ } catch (err) { debugLog("detectMcpServers settings.local.json: " + (err.message || err)); }
94
+ }
95
+
96
+ return Array.from(servers.values());
97
+ }
98
+
99
+ function detectSkills() {
100
+ const skills = new Set();
101
+
102
+ const localSettingsPath = path.join(process.cwd(), ".claude", "settings.local.json");
103
+ if (fs.existsSync(localSettingsPath)) {
104
+ try {
105
+ const localSettings = JSON.parse(fs.readFileSync(localSettingsPath, "utf-8"));
106
+ const permissions = [
107
+ ...(localSettings.allow || []),
108
+ ...(localSettings.permissions?.allow || []),
109
+ ];
110
+ for (const perm of permissions) {
111
+ if (typeof perm === "string") {
112
+ const match = perm.match(/^Skill\(([^)]+)\)/);
113
+ if (match) {
114
+ skills.add(match[1]);
115
+ }
116
+ }
117
+ }
118
+ } catch (err) { debugLog("detectSkills: " + (err.message || err)); }
119
+ }
120
+
121
+ return Array.from(skills);
122
+ }
123
+
124
+ module.exports = {
125
+ detectMcpServers,
126
+ detectSkills,
127
+ };
@@ -0,0 +1,96 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+ function mkdirSafe(dir) {
5
+ if (!fs.existsSync(dir)) {
6
+ fs.mkdirSync(dir, { recursive: true });
7
+ }
8
+ }
9
+
10
+ function copyFile(src, dest) {
11
+ fs.copyFileSync(src, dest);
12
+ }
13
+
14
+ function readTemplate(name, templatesDir) {
15
+ return fs.readFileSync(path.join(templatesDir, name), "utf-8");
16
+ }
17
+
18
+ function readJsonSafe(filePath, fallback) {
19
+ try {
20
+ return JSON.parse(fs.readFileSync(filePath, "utf-8"));
21
+ } catch (err) {
22
+ return fallback;
23
+ }
24
+ }
25
+
26
+ function readEnvSticky(cwd) {
27
+ try {
28
+ const raw = fs.readFileSync(path.join(cwd, ".env.sticky"), "utf-8");
29
+ let url = "", key = "";
30
+ for (const line of raw.split(/\r?\n/)) {
31
+ const t = line.trim();
32
+ if (t.startsWith("STICKY_URL=")) url = t.slice("STICKY_URL=".length).trim();
33
+ else if (t.startsWith("STICKY_API_KEY=")) key = t.slice("STICKY_API_KEY=".length).trim();
34
+ }
35
+ return { url, key };
36
+ } catch (_) {
37
+ return { url: "", key: "" };
38
+ }
39
+ }
40
+
41
+ function makeExecutable(filePath) {
42
+ try {
43
+ fs.chmodSync(filePath, 0o755);
44
+ } catch {
45
+ // Windows doesn't support chmod
46
+ }
47
+ }
48
+
49
+ const SECTION_START = "<!-- sticky-note:start";
50
+ const SECTION_END = "<!-- sticky-note:end -->";
51
+
52
+ function updateInstructionSection(destPath, templateContent, heading) {
53
+ if (!fs.existsSync(destPath)) {
54
+ fs.writeFileSync(destPath, templateContent);
55
+ return true;
56
+ }
57
+
58
+ const existing = fs.readFileSync(destPath, "utf-8");
59
+ const startIdx = existing.indexOf(SECTION_START);
60
+ const endIdx = existing.indexOf(SECTION_END);
61
+
62
+ if (startIdx !== -1 && endIdx !== -1) {
63
+ const before = existing.slice(0, startIdx);
64
+ const after = existing.slice(endIdx + SECTION_END.length);
65
+ fs.writeFileSync(destPath, before + templateContent.trim() + after);
66
+ } else if (heading && existing.includes(heading)) {
67
+ const headingIdx = existing.indexOf(heading);
68
+ const before = existing.slice(0, headingIdx);
69
+ fs.writeFileSync(destPath, before + templateContent);
70
+ } else {
71
+ const sep = existing.endsWith("\n") ? "\n" : "\n\n";
72
+ fs.writeFileSync(destPath, existing + sep + templateContent);
73
+ }
74
+ return true;
75
+ }
76
+
77
+ function countJsonlLines(filePath) {
78
+ if (!fs.existsSync(filePath)) return 0;
79
+ try {
80
+ const content = fs.readFileSync(filePath, "utf-8");
81
+ return content.split(/\r?\n/).filter((l) => l.trim()).length;
82
+ } catch {
83
+ return 0;
84
+ }
85
+ }
86
+
87
+ module.exports = {
88
+ mkdirSafe,
89
+ copyFile,
90
+ readTemplate,
91
+ readJsonSafe,
92
+ readEnvSticky,
93
+ makeExecutable,
94
+ updateInstructionSection,
95
+ countJsonlLines,
96
+ };
@@ -0,0 +1,27 @@
1
+ const { execFileSync } = require("child_process");
2
+
3
+ function isGitRepo() {
4
+ try {
5
+ execFileSync("git", ["rev-parse", "--is-inside-work-tree"], { stdio: "pipe" });
6
+ return true;
7
+ } catch {
8
+ return false;
9
+ }
10
+ }
11
+
12
+ function getGitUserName() {
13
+ try {
14
+ const name = execFileSync("git", ["config", "user.name"], {
15
+ encoding: "utf-8",
16
+ stdio: "pipe",
17
+ }).trim();
18
+ return name || null;
19
+ } catch {
20
+ return null;
21
+ }
22
+ }
23
+
24
+ module.exports = {
25
+ isGitRepo,
26
+ getGitUserName,
27
+ };
@@ -0,0 +1,74 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+ function resolveHookPaths(_obj, _rootDir) {
5
+ return;
6
+ }
7
+
8
+ function findStaleHookPaths(settings) {
9
+ const stale = [];
10
+ const hooks = (settings && settings.hooks) || {};
11
+ for (const entries of Object.values(hooks)) {
12
+ for (const entry of Array.isArray(entries) ? entries : []) {
13
+ for (const h of entry.hooks || []) {
14
+ const cmd = h && h.command;
15
+ if (typeof cmd !== "string") continue;
16
+ if (
17
+ /["'][A-Za-z]:[\\/]/.test(cmd) ||
18
+ /["']\/(?:Users|home|root|tmp)\//.test(cmd) ||
19
+ cmd.includes("$(git rev-parse")
20
+ ) {
21
+ stale.push(cmd);
22
+ }
23
+ if (/^npx\s+sticky-note\s+run-hook\s+\S+/.test(cmd)) {
24
+ stale.push(cmd);
25
+ }
26
+ if (/^node\s+"?\.claude\/hooks\//.test(cmd)) {
27
+ stale.push(cmd);
28
+ }
29
+ }
30
+ }
31
+ }
32
+ return stale;
33
+ }
34
+
35
+ function installGitHook(hookName, templatesDir) {
36
+ try {
37
+ const gitDir = path.join(process.cwd(), ".git");
38
+ if (!fs.existsSync(gitDir)) return false;
39
+ const hooksDir = path.join(gitDir, "hooks");
40
+
41
+ if (!fs.existsSync(hooksDir)) {
42
+ fs.mkdirSync(hooksDir, { recursive: true });
43
+ }
44
+
45
+ const src = path.join(templatesDir, "hooks", hookName + ".js");
46
+ if (!fs.existsSync(src)) return false;
47
+
48
+ if (process.platform === "win32") {
49
+ const shimContent =
50
+ `@echo off\r\nnode "%~dp0..\\..\\templates\\hooks\\${hookName}.js" %*\r\n`;
51
+ const dest = path.join(hooksDir, hookName);
52
+ fs.writeFileSync(dest, shimContent, "utf-8");
53
+ } else {
54
+ const shimContent =
55
+ `#!/bin/sh\nnode "$(dirname "$0")/../../templates/hooks/${hookName}.js" "$@"\n`;
56
+ const dest = path.join(hooksDir, hookName);
57
+ fs.writeFileSync(dest, shimContent, "utf-8");
58
+ try {
59
+ fs.chmodSync(dest, 0o755);
60
+ } catch {
61
+ // Windows doesn't support chmod
62
+ }
63
+ }
64
+ return true;
65
+ } catch (err) {
66
+ return false;
67
+ }
68
+ }
69
+
70
+ module.exports = {
71
+ resolveHookPaths,
72
+ findStaleHookPaths,
73
+ installGitHook,
74
+ };
@@ -0,0 +1,41 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const { compareSemver } = require("./semver");
4
+ const { print } = require("./ui");
5
+ const { VERSION } = require("./ui");
6
+
7
+ function pinCliInPackageJson() {
8
+ const pkgPath = path.join(process.cwd(), "package.json");
9
+ if (!fs.existsSync(pkgPath)) {
10
+ print(" ⏭️ No package.json found — skipping devDependency pin");
11
+ return;
12
+ }
13
+ let pkg;
14
+ try {
15
+ pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
16
+ } catch (err) {
17
+ print(` ⏭️ Could not parse package.json: ${err.message}`);
18
+ return;
19
+ }
20
+ pkg.devDependencies = pkg.devDependencies || {};
21
+ const existing = pkg.devDependencies["sticky-note-cli"];
22
+ const desired = `^${VERSION}`;
23
+ if (existing) {
24
+ const existingClean = String(existing).replace(/^[\^~]/, "");
25
+ if (compareSemver(existingClean, VERSION) >= 0) {
26
+ print(` ⏭️ package.json devDependencies already pin sticky-note-cli ${existing}`);
27
+ return;
28
+ }
29
+ }
30
+ pkg.devDependencies["sticky-note-cli"] = desired;
31
+ pkg.devDependencies = Object.fromEntries(
32
+ Object.entries(pkg.devDependencies).sort(([a], [b]) => a.localeCompare(b))
33
+ );
34
+ fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
35
+ print(` [OK] package.json devDependencies: sticky-note-cli ${desired}`);
36
+ print(" Run `npm install` to lock the version for your team.");
37
+ }
38
+
39
+ module.exports = {
40
+ pinCliInPackageJson,
41
+ };
@@ -0,0 +1,34 @@
1
+ function parseSemver(v) {
2
+ if (typeof v !== "string") return null;
3
+ const m = v.trim().replace(/^v/, "").match(/^(\d+)\.(\d+)(?:\.(\d+))?/);
4
+ if (!m) return null;
5
+ return [Number(m[1]), Number(m[2]), Number(m[3] || 0)];
6
+ }
7
+
8
+ function compareSemver(a, b) {
9
+ const pa = parseSemver(a), pb = parseSemver(b);
10
+ if (!pa && !pb) return 0;
11
+ if (!pa) return 1;
12
+ if (!pb) return -1;
13
+ for (let i = 0; i < 3; i++) {
14
+ if (pa[i] !== pb[i]) return pa[i] < pb[i] ? -1 : 1;
15
+ }
16
+ return 0;
17
+ }
18
+
19
+ function bumpMinVersion(existing, current) {
20
+ if (!existing) return current;
21
+ return compareSemver(existing, current) >= 0 ? existing : current;
22
+ }
23
+
24
+ function parseIntOr(str, fallback) {
25
+ const n = parseInt(str, 10);
26
+ return Number.isNaN(n) ? fallback : n;
27
+ }
28
+
29
+ module.exports = {
30
+ parseSemver,
31
+ compareSemver,
32
+ bumpMinVersion,
33
+ parseIntOr,
34
+ };
@@ -0,0 +1,38 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+ const VERSION = require("../../package.json").version;
5
+
6
+ function print(msg) {
7
+ process.stdout.write(msg + "\n");
8
+ }
9
+
10
+ function debugLog(msg) {
11
+ if (process.env.STICKY_DEBUG) {
12
+ process.stderr.write(`[sticky-note] ${msg}\n`);
13
+ }
14
+ }
15
+
16
+ function printBanner() {
17
+ print("");
18
+ print(` 📌 sticky-note v${VERSION}`);
19
+ print(" Human-to-human handoff for AI coding assistants");
20
+ print("");
21
+ }
22
+
23
+ function ask(rl, question, defaultVal) {
24
+ return new Promise((resolve) => {
25
+ const suffix = defaultVal !== undefined ? ` (${defaultVal})` : "";
26
+ rl.question(` ${question}${suffix}: `, (answer) => {
27
+ resolve(answer.trim() || (defaultVal !== undefined ? String(defaultVal) : ""));
28
+ });
29
+ });
30
+ }
31
+
32
+ module.exports = {
33
+ print,
34
+ debugLog,
35
+ printBanner,
36
+ ask,
37
+ VERSION,
38
+ };
@@ -41,11 +41,23 @@ function readFileFromBranch(ref, filePath) {
41
41
  }
42
42
  }
43
43
 
44
+ /** Returns all file paths in a git ref as an array. */
45
+ function listFilesInBranch(ref) {
46
+ try {
47
+ const out = execFileSync("git", ["ls-tree", "-r", "--name-only", ref], GIT_OPTS);
48
+ return out.trim().split(/\r?\n/).filter(Boolean);
49
+ } catch (_) {
50
+ return [];
51
+ }
52
+ }
53
+
44
54
  /**
45
55
  * Commit { relativePath: content } to a branch using git plumbing.
46
56
  * Does not touch the working tree or the main index. Returns the new commit SHA.
57
+ * explicitParentSha: if provided, use this SHA as the parent instead of the
58
+ * current local branch tip (used during retry to parent off the remote SHA).
47
59
  */
48
- function commitFilesToBranch(branchName, fileMap) {
60
+ function commitFilesToBranch(branchName, fileMap, explicitParentSha) {
49
61
  const branchRef = "refs/heads/" + branchName;
50
62
  const tmpIndex = path.join(
51
63
  os.tmpdir(),
@@ -53,13 +65,15 @@ function commitFilesToBranch(branchName, fileMap) {
53
65
  );
54
66
 
55
67
  try {
56
- let parentSha = null;
57
- try {
58
- parentSha = execFileSync("git", ["rev-parse", "--verify", branchRef], {
59
- encoding: "utf-8", timeout: 3000, stdio: ["pipe", "pipe", "pipe"],
60
- }).trim();
61
- } catch (_) {
62
- // Branch doesn't exist yet — first commit creates it as orphan
68
+ let parentSha = explicitParentSha || null;
69
+ if (!parentSha) {
70
+ try {
71
+ parentSha = execFileSync("git", ["rev-parse", "--verify", branchRef], {
72
+ encoding: "utf-8", timeout: 3000, stdio: ["pipe", "pipe", "pipe"],
73
+ }).trim();
74
+ } catch (_) {
75
+ // Branch doesn't exist yet — first commit creates it as orphan
76
+ }
63
77
  }
64
78
 
65
79
  const indexEnv = { ...process.env, GIT_INDEX_FILE: tmpIndex };
@@ -162,15 +176,40 @@ function pushDataBranch(remote, branchName, maxRetries, localMemPath, loadJsonFn
162
176
  return { ok: false, error: err.message };
163
177
  }
164
178
 
165
- // Fetch, merge, and re-commit so the next push attempt advances the ref
179
+ // Fetch remote, build a merged file map (local files + remote files +
180
+ // merged sticky-note.json), then re-commit parented off the remote SHA
181
+ // so the next push attempt is a fast-forward.
166
182
  try {
167
183
  const fetchResult = fetchDataBranch(remote, branchName);
168
- if (fetchResult.ok && fetchResult.remoteRef && localMemPath) {
169
- const remoteContent = readFileFromBranch(fetchResult.remoteRef, "sticky-note.json");
170
- if (remoteContent) {
171
- mergeAndSaveFromRemote(localMemPath, remoteContent, loadJsonFn, saveJsonFn);
172
- const mergedContent = fs.readFileSync(localMemPath, "utf-8");
173
- commitFilesToBranch(branchName, { "sticky-note.json": mergedContent });
184
+ if (fetchResult.ok && fetchResult.remoteRef) {
185
+ // Get the remote SHA to use as parent for the re-commit
186
+ const remoteSha = execFileSync(
187
+ "git", ["rev-parse", fetchResult.remoteRef], GIT_OPTS
188
+ ).trim();
189
+
190
+ // Build merged file map: start with remote files as base, then
191
+ // overlay local files so this user's audit/presence are preserved
192
+ const fileMap = {};
193
+ const localRef = "refs/heads/" + branchName;
194
+ for (const f of listFilesInBranch(fetchResult.remoteRef)) {
195
+ const content = readFileFromBranch(fetchResult.remoteRef, f);
196
+ if (content !== null) fileMap[f] = content;
197
+ }
198
+ for (const f of listFilesInBranch(localRef)) {
199
+ const content = readFileFromBranch(localRef, f);
200
+ if (content !== null) fileMap[f] = content;
201
+ }
202
+
203
+ // Merge sticky-note.json from both sides
204
+ if (localMemPath && fileMap["sticky-note.json"]) {
205
+ mergeAndSaveFromRemote(
206
+ localMemPath, fileMap["sticky-note.json"], loadJsonFn, saveJsonFn
207
+ );
208
+ fileMap["sticky-note.json"] = fs.readFileSync(localMemPath, "utf-8");
209
+ }
210
+
211
+ if (Object.keys(fileMap).length > 0) {
212
+ commitFilesToBranch(branchName, fileMap, remoteSha);
174
213
  }
175
214
  }
176
215
  } catch (_) {}
@@ -193,7 +193,11 @@ async function main() {
193
193
  const toolInput = hookInput.tool_input || hookInput.input || hookInput.toolArgs || {};
194
194
  const cmd = typeof toolInput === "string" ? toolInput : (toolInput.command || "");
195
195
  if (/\bgit\s+(pull|merge|rebase|fetch)\b/.test(cmd)) {
196
- try { syncStickyNote({}); } catch (_) {}
196
+ try {
197
+ syncStickyNote({});
198
+ } catch (err) {
199
+ _emit(`[STICKY-NOTE] ⚠️ Auto-sync before git operation failed: ${err && err.message ? err.message : err}\nSticky-note files may be dirty — run \`npx sticky-note pull\` manually to avoid conflicts.`);
200
+ }
197
201
  }
198
202
  }
199
203