@saeed42/worktree-worker 1.4.0 → 1.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.
Files changed (2) hide show
  1. package/dist/main.js +59 -4
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -466,6 +466,48 @@ var GitService = class {
466
466
  const result = await this.exec(["remote", "get-url", remote], cwd);
467
467
  return result.code === 0 ? result.stdout.trim() : null;
468
468
  }
469
+ /**
470
+ * Configure git credential helper with token (temporary, for operations like worktree add)
471
+ * SECURITY: Token is stored in /tmp and should be cleared after operation
472
+ */
473
+ async configureCredentials(githubToken) {
474
+ const log = logger.child({ service: "git", operation: "configureCredentials" });
475
+ log.debug("Configuring git credentials");
476
+ await this.exec(["config", "--global", "credential.helper", "store --file=/tmp/.git-credentials"]);
477
+ await this.exec(["config", "--global", "core.askPass", ""]);
478
+ const { writeFile: writeFile2 } = await import("fs/promises");
479
+ const credLine = `https://x-access-token:${githubToken}@github.com
480
+ `;
481
+ await writeFile2("/tmp/.git-credentials", credLine, { mode: 384 });
482
+ log.debug("Git credentials configured");
483
+ }
484
+ /**
485
+ * Clear git credentials (call after operations complete)
486
+ */
487
+ async clearCredentials() {
488
+ const log = logger.child({ service: "git", operation: "clearCredentials" });
489
+ log.debug("Clearing git credentials");
490
+ const { unlink: unlink2 } = await import("fs/promises");
491
+ try {
492
+ await unlink2("/tmp/.git-credentials");
493
+ } catch {
494
+ }
495
+ await this.exec(["config", "--global", "--unset", "credential.helper"]).catch(() => {
496
+ });
497
+ log.debug("Git credentials cleared");
498
+ }
499
+ /**
500
+ * Run an operation with temporary credentials
501
+ * SECURITY: Credentials are cleared after operation completes (success or failure)
502
+ */
503
+ async withCredentials(githubToken, operation) {
504
+ await this.configureCredentials(githubToken);
505
+ try {
506
+ return await operation();
507
+ } finally {
508
+ await this.clearCredentials();
509
+ }
510
+ }
469
511
  };
470
512
  var gitService = new GitService();
471
513
 
@@ -1016,13 +1058,24 @@ var WorktreeService = class {
1016
1058
  await gitService.pruneWorktrees(baseRepoDir);
1017
1059
  const localBranchExists = await gitService.branchExists(branchName, baseRepoDir);
1018
1060
  const remoteBranchExists = await gitService.branchExists(`origin/${branchName}`, baseRepoDir);
1061
+ const createWorktreeWithCreds = async (operation) => {
1062
+ if (options.githubToken) {
1063
+ await gitService.withCredentials(options.githubToken, operation);
1064
+ } else {
1065
+ await operation();
1066
+ }
1067
+ };
1019
1068
  if (localBranchExists) {
1020
1069
  log.info("Creating worktree from existing local branch", { branchName });
1021
- await gitService.execOrThrow(["worktree", "add", worktreePath, branchName], baseRepoDir);
1070
+ await createWorktreeWithCreds(async () => {
1071
+ await gitService.execOrThrow(["worktree", "add", worktreePath, branchName], baseRepoDir);
1072
+ });
1022
1073
  } else if (remoteBranchExists) {
1023
1074
  log.info("Creating worktree from remote branch", { branchName });
1024
- await gitService.exec(["fetch", "origin", `${branchName}:${branchName}`], baseRepoDir);
1025
- await gitService.execOrThrow(["worktree", "add", worktreePath, branchName], baseRepoDir);
1075
+ await createWorktreeWithCreds(async () => {
1076
+ await gitService.exec(["fetch", "origin", `${branchName}:${branchName}`], baseRepoDir);
1077
+ await gitService.execOrThrow(["worktree", "add", worktreePath, branchName], baseRepoDir);
1078
+ });
1026
1079
  } else {
1027
1080
  let baseRef = `origin/${baseBranch}`;
1028
1081
  const baseBranchExistsOnRemote = await gitService.branchExists(baseRef, baseRepoDir);
@@ -1058,7 +1111,9 @@ var WorktreeService = class {
1058
1111
  }
1059
1112
  }
1060
1113
  log.info("Creating worktree with new branch from base", { baseRef, branchName });
1061
- await gitService.addWorktreeWithNewBranch(worktreePath, branchName, baseRef, baseRepoDir);
1114
+ await createWorktreeWithCreds(async () => {
1115
+ await gitService.addWorktreeWithNewBranch(worktreePath, branchName, baseRef, baseRepoDir);
1116
+ });
1062
1117
  }
1063
1118
  await gitService.exec(["config", "--local", "user.name", "origin-agent[bot]"], worktreePath).catch(() => {
1064
1119
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saeed42/worktree-worker",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Git worktree management service for AI agent trials",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",