agent-gear 2.15.0 → 2.17.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.
@@ -100,6 +100,11 @@ function normalizeCommitter(committer?: WorktreeCommitter): WorktreeCommitter |
100
100
  return coAuthor ? { name, email, ...coAuthor } : { name, email };
101
101
  }
102
102
 
103
+ export interface WorktreeCoAuthorResult {
104
+ success: boolean;
105
+ error?: string;
106
+ }
107
+
103
108
  export interface WorktreeRemoveResult {
104
109
  success: boolean;
105
110
  error?: string;
@@ -198,6 +203,52 @@ export class WorktreeHandler {
198
203
  }
199
204
  }
200
205
 
206
+ /**
207
+ * 更新已有 Worktree 的共同作者配置(不改变作者身份)。
208
+ *
209
+ * 设置页勾选/取消只影响「之后创建的 Worktree」时,用户在旧 Worktree 上提交会看不到变化;
210
+ * 本方法在已有 Worktree 上重新同步:`coAuthor` 缺省 = 移除受管 trailer 配置(仅清理本平台写入的值)。
211
+ */
212
+ async configureCoAuthor(
213
+ worktreePath: string,
214
+ coAuthor?: { name: string; email: string },
215
+ ): Promise<WorktreeCoAuthorResult> {
216
+ try {
217
+ const resolvedPath = expandTilde(worktreePath);
218
+ const normalized = coAuthor
219
+ ? normalizeCoAuthor({
220
+ coAuthorName: coAuthor.name,
221
+ coAuthorEmail: coAuthor.email,
222
+ })
223
+ : undefined;
224
+ if (normalized) {
225
+ await this.applyWorktreeCoAuthor(
226
+ resolvedPath,
227
+ normalized.coAuthorName,
228
+ normalized.coAuthorEmail,
229
+ );
230
+ } else {
231
+ await this.clearWorktreeCoAuthor(resolvedPath);
232
+ }
233
+ return { success: true };
234
+ } catch (err) {
235
+ this.log.warn({ err }, '更新 worktree 共同作者配置失败(忽略)');
236
+ return { success: false, error: (err as Error).message };
237
+ }
238
+ }
239
+
240
+ /** Worktree 共享 git 目录下的受管 hook 目录(多 worktree 共用同一份 hook 文件)。 */
241
+ private async resolveHookDir(worktreePath: string): Promise<string> {
242
+ const pathArg = shellQuote(worktreePath);
243
+ const { stdout } = await execAsync(
244
+ `git -C ${pathArg} rev-parse --path-format=absolute --git-common-dir`,
245
+ );
246
+ const commonDir = stdout.trim();
247
+ if (!commonDir) throw new Error('git-common-dir is empty');
248
+ // `--path-format=absolute` 保证绝对路径;兼容旧 git 的相对输出时以 worktree 为基准解析。
249
+ return join(resolve(worktreePath, commonDir), CO_AUTHOR_HOOK_DIR);
250
+ }
251
+
201
252
  /**
202
253
  * 安装 worktree 级共同作者 hook(仅作用于该 worktree)。
203
254
  *
@@ -214,13 +265,7 @@ export class WorktreeHandler {
214
265
  const pathArg = shellQuote(worktreePath);
215
266
  let hookDir: string;
216
267
  try {
217
- const { stdout } = await execAsync(
218
- `git -C ${pathArg} rev-parse --path-format=absolute --git-common-dir`,
219
- );
220
- const commonDir = stdout.trim();
221
- if (!commonDir) throw new Error('git-common-dir is empty');
222
- // `--path-format=absolute` 保证绝对路径;兼容旧 git 的相对输出时以 worktree 为基准解析。
223
- hookDir = join(resolve(worktreePath, commonDir), CO_AUTHOR_HOOK_DIR);
268
+ hookDir = await this.resolveHookDir(worktreePath);
224
269
  mkdirSync(hookDir, { recursive: true });
225
270
  const hookPath = join(hookDir, CO_AUTHOR_HOOK_NAME);
226
271
  writeFileSync(hookPath, renderCoAuthorHook(), { mode: 0o755 });
@@ -232,6 +277,8 @@ export class WorktreeHandler {
232
277
  }
233
278
 
234
279
  const commands = [
280
+ // 已有 Worktree 可能由旧版本/手工创建,补齐 worktree config 开关后 `--worktree` 才生效。
281
+ `git -C ${pathArg} config extensions.worktreeConfig true`,
235
282
  `git -C ${pathArg} config --worktree core.hooksPath ${shellQuote(hookDir)}`,
236
283
  `git -C ${pathArg} config --worktree zag.coAuthorName ${shellQuote(coAuthorName)}`,
237
284
  `git -C ${pathArg} config --worktree zag.coAuthorEmail ${shellQuote(coAuthorEmail)}`,
@@ -246,6 +293,40 @@ export class WorktreeHandler {
246
293
  }
247
294
  }
248
295
 
296
+ /**
297
+ * 移除 worktree 级共同作者配置(幂等)。
298
+ *
299
+ * - 仅当该 worktree 的 `core.hooksPath` 恰为受管目录时才 unset,用户自建 hooks 零触碰;
300
+ * - 共享 hook 文件保留(其他 worktree 可能仍在引用),仅移除本 worktree 的配置。
301
+ */
302
+ private async clearWorktreeCoAuthor(worktreePath: string): Promise<void> {
303
+ const pathArg = shellQuote(worktreePath);
304
+ let hookDir: string;
305
+ try {
306
+ hookDir = await this.resolveHookDir(worktreePath);
307
+ } catch (err) {
308
+ this.log.warn({ err }, '读取 worktree 共同作者配置失败(忽略)');
309
+ return;
310
+ }
311
+
312
+ try {
313
+ const { stdout } = await execAsync(`git -C ${pathArg} config --worktree --get core.hooksPath`);
314
+ if (stdout.trim() === hookDir) {
315
+ await execAsync(`git -C ${pathArg} config --worktree --unset core.hooksPath`);
316
+ }
317
+ } catch {
318
+ // key 不存在(git 退出码 1)或未启用 worktree config:本就没有可清理的受管路径。
319
+ }
320
+
321
+ for (const key of ['zag.coAuthorName', 'zag.coAuthorEmail']) {
322
+ try {
323
+ await execAsync(`git -C ${pathArg} config --worktree --unset ${key}`);
324
+ } catch {
325
+ // key 不存在 = 已清理,无需告警
326
+ }
327
+ }
328
+ }
329
+
249
330
  async list(workspacePath: string): Promise<WorktreeListResult> {
250
331
  try {
251
332
  const resolvedPath = expandTilde(workspacePath);