sync-worktrees 3.6.1 → 3.6.3

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/README.md CHANGED
@@ -113,6 +113,8 @@ Add the server to your MCP client config. Use `npx` if the package is not instal
113
113
 
114
114
  If installed globally, replace `command` with `sync-worktrees-mcp` and drop `args`. `SYNC_WORKTREES_CONFIG` is optional — without it the server runs in **auto-detect mode**: when the client's CWD sits inside a worktree managed by sync-worktrees, the server locates the bare repo, enumerates sibling worktrees, and enables per-worktree operations. Sync and initialize require a loaded config (or call `load_config` at runtime).
115
115
 
116
+ At session start, agents should call `detect_context` with `includeAllWorktrees: true`. With a loaded config, that returns config-driven `siblingRepositories` for every other configured repo, including nested `worktreeDir` paths, plus `allWorktreesByRepo` keyed by repository name. If a configured repo cannot be enumerated, `allWorktreeErrorsByRepo` carries the per-repo error.
117
+
116
118
  Client-specific locations:
117
119
 
118
120
  | Client | Config file |
@@ -125,10 +127,10 @@ Client-specific locations:
125
127
 
126
128
  | Tool | Purpose |
127
129
  |------|---------|
128
- | `detect_context` | Inspect a path, resolve the bare repo, enumerate sibling worktrees, report capabilities. |
129
- | `list_worktrees` | List worktrees with status label (`clean`/`dirty`/`stale`/`current`), divergence, `safeToRemove`, last sync. |
130
+ | `detect_context` | Inspect a path, resolve the bare repo, enumerate sibling worktrees, report config-driven sibling repositories and capabilities. Pass `includeAllWorktrees: true` to include every configured repo's worktrees keyed by repo name. |
131
+ | `list_worktrees` | List worktrees with status label (`clean`/`dirty`/`stale`/`current`), divergence, `safeToRemove`, last sync. Without `repoName` and with a loaded config, results are grouped across all configured repos. |
130
132
  | `get_worktree_status` | Detailed status for one worktree (dirty files, unpushed commits, stashes, operation in progress). |
131
- | `create_worktree` | Create a worktree for a branch; optionally create the branch from `baseBranch` and push. |
133
+ | `create_worktree` | Create a worktree for a branch; optionally create the branch from `baseBranch`. Newly created branches are pushed to origin unless `push=false`. |
132
134
  | `remove_worktree` | Remove a worktree after safety checks; `force=true` skips validation. |
133
135
  | `update_worktree` | Fast-forward one worktree to match upstream. |
134
136
  | `sync` | Full sync cycle (fetch, create, prune, update). Requires config. Streams progress notifications. |
@@ -142,6 +144,7 @@ All tools that target a single repo accept an optional `repoName`. When omitted,
142
144
 
143
145
  - `remove_worktree` refuses to delete worktrees with uncommitted changes, unpushed commits, stashes, or operations in progress (merge/rebase/cherry-pick/revert/bisect). Pass `force=true` to override.
144
146
  - `create_worktree` rejects sanitized-path collisions (e.g. `feature/foo` vs `feature-foo` both resolving to `feature-foo/`) before touching disk.
147
+ - Branches created by sync-worktrees use `--no-track` first, then publish with `git push -u origin <branch>`, so they do not inherit `origin/main` as their upstream.
145
148
  - Path-targeted tools verify the supplied path is a registered worktree of the selected repository.
146
149
 
147
150
  ## Options
package/dist/index.js CHANGED
@@ -3809,7 +3809,7 @@ var GitService = class {
3809
3809
  const bareGit = this.getCachedGit(this.bareRepoPath);
3810
3810
  const checkRef = async (ref) => {
3811
3811
  try {
3812
- await bareGit.raw(["show-ref", "--verify", "--quiet", ref]);
3812
+ await bareGit.raw(["show-ref", "--verify", ref]);
3813
3813
  return true;
3814
3814
  } catch {
3815
3815
  return false;
@@ -3826,10 +3826,22 @@ var GitService = class {
3826
3826
  const branches = await bareGit.branch();
3827
3827
  return branches.all;
3828
3828
  }
3829
+ async resolveCreateBranchBaseRef(bareGit, baseBranch) {
3830
+ const candidates = baseBranch.startsWith(GIT_CONSTANTS.REMOTE_PREFIX) || baseBranch.startsWith("refs/") ? [baseBranch] : [`${GIT_CONSTANTS.REMOTE_PREFIX}${baseBranch}`, baseBranch];
3831
+ for (const candidate of candidates) {
3832
+ try {
3833
+ await bareGit.revparse(["--verify", candidate]);
3834
+ return candidate;
3835
+ } catch {
3836
+ }
3837
+ }
3838
+ return candidates[0];
3839
+ }
3829
3840
  async createBranch(branchName, baseBranch) {
3830
3841
  const bareGit = this.getCachedGit(this.bareRepoPath);
3831
- await bareGit.raw(["branch", branchName, `origin/${baseBranch}`]);
3832
- this.logger.info(`Created branch '${branchName}' from '${baseBranch}'`);
3842
+ const baseRef = await this.resolveCreateBranchBaseRef(bareGit, baseBranch);
3843
+ await bareGit.raw(["branch", "--no-track", branchName, baseRef]);
3844
+ this.logger.info(`Created branch '${branchName}' from '${baseRef}'`);
3833
3845
  }
3834
3846
  async pushBranch(branchName) {
3835
3847
  const bareGit = this.getCachedGit(this.bareRepoPath);
@@ -3941,17 +3953,9 @@ var WorktreeSyncService = class {
3941
3953
  this.progressListeners.add(listener);
3942
3954
  return () => this.progressListeners.delete(listener);
3943
3955
  }
3944
- emitProgress(event) {
3945
- for (const listener of this.progressListeners) {
3946
- try {
3947
- listener(event);
3948
- } catch {
3949
- }
3950
- }
3951
- }
3952
- async sync() {
3956
+ async runExclusiveRepoOperation(operation) {
3953
3957
  if (this.syncInProgress) {
3954
- this.logger.warn("\u26A0\uFE0F Sync already in progress, skipping...");
3958
+ this.logger.warn("\u26A0\uFE0F Another repository operation is already in progress, skipping...");
3955
3959
  return { started: false, reason: "in_progress" };
3956
3960
  }
3957
3961
  const release = await this.acquireBareLock();
@@ -3960,36 +3964,55 @@ var WorktreeSyncService = class {
3960
3964
  return { started: false, reason: "locked" };
3961
3965
  }
3962
3966
  this.syncInProgress = true;
3963
- this.logger.info(`[${(/* @__PURE__ */ new Date()).toISOString()}] Starting worktree synchronization...`);
3964
- const totalTimer = new Timer();
3965
- const phaseTimer = new PhaseTimer();
3966
- const syncContext = { lfsSkipEnabled: false };
3967
- const retryOptions = this.createRetryOptions(syncContext);
3968
3967
  try {
3969
- await retry(() => this.runSyncAttempt(phaseTimer, syncContext), retryOptions);
3970
- } catch (error) {
3971
- this.logger.error("\n\u274C Error during worktree synchronization after all retry attempts:", error);
3972
- throw error;
3968
+ return { started: true, value: await operation() };
3973
3969
  } finally {
3974
- if (syncContext.lfsSkipEnabled && !this.config.skipLfs) {
3975
- this.gitService.setLfsSkipEnabled(false);
3976
- }
3977
3970
  this.syncInProgress = false;
3978
3971
  try {
3979
3972
  await release();
3980
3973
  } catch (releaseError) {
3981
3974
  this.logger.warn(`Failed to release sync lock: ${getErrorMessage(releaseError)}`);
3982
3975
  }
3983
- this.logger.info(`[${(/* @__PURE__ */ new Date()).toISOString()}] Synchronization finished.
3984
- `);
3985
- if (this.config.debug) {
3986
- const totalDuration = totalTimer.stop();
3987
- const phaseResults = phaseTimer.getResults();
3988
- const repoName = this.config.name;
3989
- this.logger.table(formatTimingTable(totalDuration, phaseResults, repoName));
3976
+ }
3977
+ }
3978
+ emitProgress(event) {
3979
+ for (const listener of this.progressListeners) {
3980
+ try {
3981
+ listener(event);
3982
+ } catch {
3990
3983
  }
3991
3984
  }
3992
- return { started: true };
3985
+ }
3986
+ async sync() {
3987
+ const result = await this.runExclusiveRepoOperation(async () => {
3988
+ if (!this.isInitialized()) {
3989
+ await this.initialize();
3990
+ }
3991
+ this.logger.info(`[${(/* @__PURE__ */ new Date()).toISOString()}] Starting worktree synchronization...`);
3992
+ const totalTimer = new Timer();
3993
+ const phaseTimer = new PhaseTimer();
3994
+ const syncContext = { lfsSkipEnabled: false };
3995
+ const retryOptions = this.createRetryOptions(syncContext);
3996
+ try {
3997
+ await retry(() => this.runSyncAttempt(phaseTimer, syncContext), retryOptions);
3998
+ } catch (error) {
3999
+ this.logger.error("\n\u274C Error during worktree synchronization after all retry attempts:", error);
4000
+ throw error;
4001
+ } finally {
4002
+ if (syncContext.lfsSkipEnabled && !this.config.skipLfs) {
4003
+ this.gitService.setLfsSkipEnabled(false);
4004
+ }
4005
+ this.logger.info(`[${(/* @__PURE__ */ new Date()).toISOString()}] Synchronization finished.
4006
+ `);
4007
+ if (this.config.debug) {
4008
+ const totalDuration = totalTimer.stop();
4009
+ const phaseResults = phaseTimer.getResults();
4010
+ const repoName = this.config.name;
4011
+ this.logger.table(formatTimingTable(totalDuration, phaseResults, repoName));
4012
+ }
4013
+ }
4014
+ });
4015
+ return result.started ? { started: true } : result;
3993
4016
  }
3994
4017
  async acquireBareLock() {
3995
4018
  if (process.env.NODE_ENV === ENV_CONSTANTS.NODE_ENV_TEST) {
@@ -4001,15 +4024,9 @@ var WorktreeSyncService = class {
4001
4024
  };
4002
4025
  }
4003
4026
  const barePath = this.gitService.getBareRepoPath();
4004
- const lockTarget = path8.join(barePath, "HEAD");
4005
- try {
4006
- await fs6.access(lockTarget);
4007
- } catch {
4008
- return async () => {
4009
- };
4010
- }
4027
+ await fs6.mkdir(barePath, { recursive: true });
4011
4028
  try {
4012
- const release = await lockfile.lock(lockTarget, {
4029
+ const release = await lockfile.lock(barePath, {
4013
4030
  stale: DEFAULT_CONFIG.LOCK_STALE_MS,
4014
4031
  update: DEFAULT_CONFIG.LOCK_UPDATE_MS,
4015
4032
  retries: 0,