@aiti-ai/aiti-git-ops 0.1.11 → 0.1.12

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.
@@ -10,7 +10,7 @@
10
10
  */
11
11
  import { test } from "node:test";
12
12
  import assert from "node:assert/strict";
13
- import { mkdtempSync, mkdirSync, writeFileSync, existsSync, readFileSync } from "node:fs";
13
+ import { mkdtempSync, mkdirSync, writeFileSync, existsSync, readFileSync, rmSync } from "node:fs";
14
14
  import { tmpdir } from "node:os";
15
15
  import { join } from "node:path";
16
16
  import { execFileSync } from "node:child_process";
@@ -469,3 +469,40 @@ test("cherry-pick:冲突 409 + 中间态文件清单 + abort 复原", async ()
469
469
  const st = await call(["git", "refs"], { cwd: repo });
470
470
  assert.equal(st.body.dirtyCount, 0); // 工作区干净(走出中间态)
471
471
  });
472
+
473
+ // ── folder "Add to .gitignore": trailing newline handling ───────────────────
474
+
475
+ test("ignore-add: prepend missing trailing newline, with idempotency / all-skipped / new-file cases", async () => {
476
+ const dir = mkdtempSync(join(tmpdir(), "git-ops-ignore-"));
477
+ const r = join(dir, "repo");
478
+ mkdirSync(r, { recursive: true });
479
+ execFileSync("git", ["init"], { cwd: r });
480
+ const gi = join(r, ".gitignore");
481
+
482
+ // No trailing newline: the new entry must not be glued to the last line
483
+ writeFileSync(gi, "keep.txt");
484
+ const first = await call(["git", "ignore-add"], { cwd: r, paths: ["new-dir"] });
485
+ assert.equal(first.status, 200);
486
+ assert.deepEqual(first.body.added, ["new-dir"]);
487
+ assert.equal(readFileSync(gi, "utf8"), "keep.txt\nnew-dir\n");
488
+
489
+ // Repeated add: no duplicate write, content unchanged
490
+ const again = await call(["git", "ignore-add"], { cwd: r, paths: ["new-dir"] });
491
+ assert.deepEqual(again.body.added, []);
492
+ assert.deepEqual(again.body.skipped, ["new-dir"]);
493
+ assert.equal(readFileSync(gi, "utf8"), "keep.txt\nnew-dir\n");
494
+
495
+ // All entries skipped but newline still missing: fix the newline anyway
496
+ writeFileSync(gi, "new-dir");
497
+ const fixNl = await call(["git", "ignore-add"], { cwd: r, paths: ["new-dir"] });
498
+ assert.equal(fixNl.status, 200);
499
+ assert.deepEqual(fixNl.body.skipped, ["new-dir"]);
500
+ assert.equal(readFileSync(gi, "utf8"), "new-dir\n");
501
+
502
+ // No .gitignore: the new file must not start with an empty line
503
+ rmSync(gi);
504
+ const fresh = await call(["git", "ignore-add"], { cwd: r, paths: ["fresh.txt"] });
505
+ assert.equal(fresh.status, 200);
506
+ assert.deepEqual(fresh.body.added, ["fresh.txt"]);
507
+ assert.equal(readFileSync(gi, "utf8"), "fresh.txt\n");
508
+ });
package/dev-pack.mjs CHANGED
@@ -77,7 +77,7 @@ export function agentsMdTemplate(extId) {
77
77
 
78
78
  /** 默认 tools/build.mjs(esbuild 单包零配置;扩展自带 tools/build.mjs 则不走到这里)。 */
79
79
  function buildMjsTemplate(extId, extRoot, clientOut, entryRel) {
80
- const repoHint = "/home/pi/workspace/pi-work"; // 现场构建探测源码仓 node_modules 用(三机各不同,缺失时走 npm exec)
80
+ // No hardcoded checkout path: candidates are resolved at run time (see repoCandidates below).
81
81
  return `/**
82
82
  * ${extId} · 现场构建入口(dev-pack 默认模板,esbuild 单包零配置)
83
83
  * 用法:node tools/build.mjs
@@ -96,12 +96,25 @@ const entryRel = ${JSON.stringify(entryRel || "")};
96
96
 
97
97
  // esbuild 解析:部署目录 node_modules → 源码仓 node_modules(本机开发场景)→ PATH 上的 esbuild → npm exec 现拉。
98
98
  // 注意:node_modules/esbuild/bin/esbuild 是原生二进制(ELF/Mach-O),必须直接执行(不能当 JS 跑)。
99
+ // Checkout roots are discovered by walking up from the extension dir (a root has node_modules/),
100
+ // plus AITI_REPO_ROOT when set — no machine-specific absolute paths baked into generated files.
101
+ function repoCandidates() {
102
+ const out = [];
103
+ if (process.env.AITI_REPO_ROOT) out.push(process.env.AITI_REPO_ROOT.split(pathSep).join("/") + "/node_modules/esbuild/bin/esbuild");
104
+ let d = extRoot;
105
+ for (let i = 0; i < 8; i++) {
106
+ out.push(d.split(pathSep).join("/") + "/node_modules/esbuild/bin/esbuild");
107
+ const up = dirname(d);
108
+ if (up === d) break;
109
+ d = up;
110
+ }
111
+ return out;
112
+ }
99
113
  function findEsbuild() {
100
114
  const home = (process.env.HOME || process.env.USERPROFILE || "").split(pathSep).join("/");
101
115
  const candidates = [
102
116
  extRoot + "/node_modules/esbuild/bin/esbuild",
103
- home + "/workspace/pi-work/node_modules/esbuild/bin/esbuild",
104
- "D:/workspace/pi-work/node_modules/esbuild/bin/esbuild", // 155 win(源码在 D 盘)
117
+ ...repoCandidates(),
105
118
  ];
106
119
  for (const c of candidates) {
107
120
  if (!existsSync(c)) continue;
package/git-engine.ts CHANGED
@@ -720,8 +720,9 @@ export async function gitIgnoreAdd(cwd: string, paths: string[]): Promise<{ ok:
720
720
  const lines = new Set(existing.split(/\r?\n/).map((l) => l.trim()).filter(Boolean));
721
721
  const added: string[] = [];
722
722
  const skipped: string[] = [];
723
- let content = existing;
724
- if (content && !content.endsWith("\n")) content += "\n";
723
+ // A file without a trailing newline would glue the first new entry onto its last line.
724
+ // Prepend the missing newline to the appended payload so the existing content stays untouched.
725
+ const needsNewline = existing.length > 0 && !existing.endsWith("\n");
725
726
  for (const rel of rels) {
726
727
  const entry = rel.replace(/\\/g, "/");
727
728
  if (lines.has(entry)) {
@@ -729,13 +730,12 @@ export async function gitIgnoreAdd(cwd: string, paths: string[]): Promise<{ ok:
729
730
  continue;
730
731
  }
731
732
  lines.add(entry);
732
- content += entry + "\n";
733
733
  added.push(entry);
734
734
  }
735
- if (added.length > 0) {
736
- await appendFile(giPath, added.map((e) => e + "\n").join(""), "utf8");
737
- await audit({ repo, cmd: "ignore-add", paths: added });
738
- changed(repo, "stage", `ignore +${added.length}`);
735
+ if (added.length > 0 || needsNewline) {
736
+ await appendFile(giPath, (needsNewline ? "\n" : "") + added.map((e) => e + "\n").join(""), "utf8");
737
+ if (added.length > 0) await audit({ repo, cmd: "ignore-add", paths: added });
738
+ changed(repo, "stage", added.length > 0 ? `ignore +${added.length}` : "ignore: trailing newline");
739
739
  }
740
740
  return { ok: true as const, added, skipped };
741
741
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiti-ai/aiti-git-ops",
3
- "version": "0.1.11",
3
+ "version": "0.1.12",
4
4
  "description": "Whitelisted git write engine — checkout precheck / stash / plan breakpoints / audit / changed topic, consumed by folder and session-manager",
5
5
  "soul": "一处写仓,处处安全。",
6
6
  "aitiExtProtocol": "1.0",
@@ -8,7 +8,7 @@
8
8
  "@aiti-ai/ext-shared": "^0.1.12"
9
9
  },
10
10
  "license": "MIT",
11
- "contentHash": "f27da5cb7cbd5917",
11
+ "contentHash": "98ec3c60db6ec2af",
12
12
  "repository": {
13
13
  "type": "git",
14
14
  "url": "git+https://github.com/aiti-ai/aiti-ai.git",