sync-worktrees 3.6.1 → 3.6.2

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/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", 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,