Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/otomi-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading