diff --git a/src/git.ts b/src/git.ts index dd673249..6b04f1d5 100644 --- a/src/git.ts +++ b/src/git.ts @@ -314,18 +314,26 @@ export class Git { // Remove local changes so that no conflict can happen debug('Removing local changes.') await this.git.reset(ResetMode.HARD) - debug(`Go to ${this.branch} branch`) - await this.git.checkout(this.branch) - debug('Removing local changes.') - await this.git.reset(ResetMode.HARD) - debug('Cleaning local values and directories.') - await this.git.clean(CleanOptions.FORCE, ['-d']) - debug('Get the latest branch from:', this.remote) - await this.git.fetch(this.remote, this.branch) - debug('Reconciling divergent branches.') - await this.git.merge([`${this.remote}/${this.branch}`, '--strategy-option=theirs']) - debug('Trying to remove upstream commits: ', this.remote) - await this.git.push([this.remote, this.branch, '--force']) + if (this.isRootClone()) { + debug(`Go to ${this.branch} branch`) + await this.git.checkout(this.branch) + debug('Removing local changes.') + await this.git.reset(ResetMode.HARD) + debug('Cleaning local values and directories.') + await this.git.clean(CleanOptions.FORCE, ['-d']) + debug('Get the latest branch from:', this.remote) + await this.git.fetch(this.remote, this.branch) + debug('Reconciling divergent branches.') + await this.git.merge([`${this.remote}/${this.branch}`, '--strategy-option=theirs']) + debug('Trying to remove upstream commits: ', this.remote) + await this.git.push([this.remote, this.branch, '--force']) + } else { + // Worktree recovery: rebase session branch on top of remote; our changes win conflicts + debug('Get the latest branch from:', this.remote) + await this.git.fetch(this.remote, this.branch) + debug('Rebasing session branch on top of remote.') + await this.git.raw(['rebase', `${this.remote}/${this.branch}`, '--strategy-option=ours']) + } } catch (error) { const errorMessage = getSanitizedErrorMessage(error) debug('Failed to remove upstream commits: ', errorMessage) diff --git a/src/otomi-stack.ts b/src/otomi-stack.ts index a7b40077..60a41588 100644 --- a/src/otomi-stack.ts +++ b/src/otomi-stack.ts @@ -293,6 +293,14 @@ export default class OtomiStack { ) } + // Pull latest changes so the worktree starts from up-to-date state + try { + debug(`Pulled latest changes before creating worktree for session ${this.sessionId}`) + await mainRepo.pull(true, true) + } catch (e) { + debug(`Warning: could not pull latest before creating worktree: ${getSanitizedErrorMessage(e)}`) + } + const worktreePath = this.getRepoPath() this.git = await getWorktreeRepo(mainRepo, worktreePath, env.GIT_BRANCH) this.fileStore = new FileStore()