@saeed42/worktree-worker 1.1.0 → 1.3.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 +58 -5
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -314,9 +314,14 @@ var GitService = class {
314
314
  return result.stdout.trim();
315
315
  }
316
316
  /**
317
- * Check if branch exists
317
+ * Check if branch exists (local or remote)
318
+ * For remote branches, pass "origin/branch-name"
318
319
  */
319
320
  async branchExists(branch, cwd) {
321
+ if (branch.includes("/")) {
322
+ const result2 = await this.exec(["show-ref", "--verify", "--quiet", `refs/remotes/${branch}`], cwd);
323
+ return result2.code === 0;
324
+ }
320
325
  const result = await this.exec(["show-ref", "--verify", "--quiet", `refs/heads/${branch}`], cwd);
321
326
  return result.code === 0;
322
327
  }
@@ -566,6 +571,10 @@ var RepoService = class {
566
571
  await gitService.exec(["config", "--local", "safe.directory", repoRoot], repoRoot);
567
572
  const cleanUrl = options.repoUrl.replace(/^https:\/\/[^@]+@/, "https://");
568
573
  await gitService.exec(["remote", "set-url", "origin", cleanUrl], repoRoot);
574
+ log.info("Fetching all remote refs");
575
+ await gitService.fetch("origin", repoRoot, auth);
576
+ await gitService.exec(["branch", `--set-upstream-to=origin/${branch}`, branch], repoRoot).catch(() => {
577
+ });
569
578
  const headSha = await gitService.getHeadSha(repoRoot);
570
579
  const remote = await gitService.getRemoteUrl("origin", repoRoot);
571
580
  log.info("Repository initialized", { branch, headSha });
@@ -750,9 +759,24 @@ var WorktreeService = class {
750
759
  branchName,
751
760
  baseBranch,
752
761
  baseRepoDir,
753
- hasToken: !!options.githubToken
762
+ hasToken: !!options.githubToken,
763
+ hasRepoUrl: !!options.repoUrl
754
764
  });
755
765
  await mkdir3(env.TRIALS_WORKSPACE_DIR, { recursive: true });
766
+ const repoStatus = await repoService.getStatus();
767
+ if (!repoStatus.initialized) {
768
+ if (!options.repoUrl) {
769
+ throw new Error(
770
+ "Main repository not initialized. Provide repoUrl to auto-initialize, or call /v1/repo/init first."
771
+ );
772
+ }
773
+ log.info("Main repo not initialized, auto-initializing", { repoUrl: options.repoUrl.replace(/ghp_[a-zA-Z0-9]+/, "ghp_***") });
774
+ await repoService.initRepo({
775
+ repoUrl: options.repoUrl,
776
+ branch: baseBranch,
777
+ githubToken: options.githubToken
778
+ });
779
+ }
756
780
  try {
757
781
  const stats = await stat3(worktreePath);
758
782
  if (stats.isDirectory()) {
@@ -792,7 +816,33 @@ var WorktreeService = class {
792
816
  await gitService.exec(["fetch", "origin", `${branchName}:${branchName}`], baseRepoDir);
793
817
  await gitService.execOrThrow(["worktree", "add", worktreePath, branchName], baseRepoDir);
794
818
  } else {
795
- const baseRef = `origin/${baseBranch}`;
819
+ let baseRef = `origin/${baseBranch}`;
820
+ const baseBranchExistsOnRemote = await gitService.branchExists(baseRef, baseRepoDir);
821
+ if (!baseBranchExistsOnRemote) {
822
+ log.warn("Base branch not found, detecting default branch", { tried: baseRef });
823
+ const fallbackBranches = ["origin/main", "origin/master", "origin/dev", "origin/develop"];
824
+ let found = false;
825
+ for (const fallback of fallbackBranches) {
826
+ const exists = await gitService.branchExists(fallback, baseRepoDir);
827
+ if (exists) {
828
+ baseRef = fallback;
829
+ found = true;
830
+ log.info("Found fallback base branch", { baseRef });
831
+ break;
832
+ }
833
+ }
834
+ if (!found) {
835
+ const currentBranch = await gitService.getCurrentBranch(baseRepoDir);
836
+ if (currentBranch && currentBranch !== "HEAD") {
837
+ baseRef = currentBranch;
838
+ log.info("Using current branch as base", { baseRef });
839
+ } else {
840
+ throw new Error(
841
+ `Cannot find base branch. Tried: origin/${baseBranch}, ${fallbackBranches.join(", ")}. Ensure the repository has been fetched properly.`
842
+ );
843
+ }
844
+ }
845
+ }
796
846
  log.info("Creating worktree with new branch from base", { baseRef, branchName });
797
847
  await gitService.addWorktreeWithNewBranch(worktreePath, branchName, baseRef, baseRepoDir);
798
848
  }
@@ -965,8 +1015,10 @@ var resetWorktreeSchema = z2.object({
965
1015
  baseBranch: z2.string().default("main"),
966
1016
  trialBranch: z2.string().optional(),
967
1017
  force: z2.boolean().default(false),
968
- githubToken: z2.string().optional()
1018
+ githubToken: z2.string().optional(),
969
1019
  // Required for private repos
1020
+ repoUrl: z2.string().url().optional()
1021
+ // Required if repo not yet initialized
970
1022
  });
971
1023
  var commitSchema = z2.object({
972
1024
  message: z2.string().min(1),
@@ -1005,7 +1057,8 @@ worktree.post("/trials/:trialId/worktree/reset", async (c) => {
1005
1057
  baseBranch: input.baseBranch,
1006
1058
  trialBranch: input.trialBranch,
1007
1059
  force: input.force,
1008
- githubToken: input.githubToken
1060
+ githubToken: input.githubToken,
1061
+ repoUrl: input.repoUrl
1009
1062
  });
1010
1063
  return c.json({ success: true, data: result });
1011
1064
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saeed42/worktree-worker",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Git worktree management service for AI agent trials",
5
5
  "type": "module",
6
6
  "main": "dist/main.js",