Skip to content

Commit a5a86fc

Browse files
fix: concurrent git operations (#944)
Co-authored-by: svcAPLBot <174728082+svcAPLBot@users.noreply.github.com>
1 parent b46710e commit a5a86fc

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

src/git.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,26 @@ export class Git {
314314
// Remove local changes so that no conflict can happen
315315
debug('Removing local changes.')
316316
await this.git.reset(ResetMode.HARD)
317-
debug(`Go to ${this.branch} branch`)
318-
await this.git.checkout(this.branch)
319-
debug('Removing local changes.')
320-
await this.git.reset(ResetMode.HARD)
321-
debug('Cleaning local values and directories.')
322-
await this.git.clean(CleanOptions.FORCE, ['-d'])
323-
debug('Get the latest branch from:', this.remote)
324-
await this.git.fetch(this.remote, this.branch)
325-
debug('Reconciling divergent branches.')
326-
await this.git.merge([`${this.remote}/${this.branch}`, '--strategy-option=theirs'])
327-
debug('Trying to remove upstream commits: ', this.remote)
328-
await this.git.push([this.remote, this.branch, '--force'])
317+
if (this.isRootClone()) {
318+
debug(`Go to ${this.branch} branch`)
319+
await this.git.checkout(this.branch)
320+
debug('Removing local changes.')
321+
await this.git.reset(ResetMode.HARD)
322+
debug('Cleaning local values and directories.')
323+
await this.git.clean(CleanOptions.FORCE, ['-d'])
324+
debug('Get the latest branch from:', this.remote)
325+
await this.git.fetch(this.remote, this.branch)
326+
debug('Reconciling divergent branches.')
327+
await this.git.merge([`${this.remote}/${this.branch}`, '--strategy-option=theirs'])
328+
debug('Trying to remove upstream commits: ', this.remote)
329+
await this.git.push([this.remote, this.branch, '--force'])
330+
} else {
331+
// Worktree recovery: rebase session branch on top of remote; our changes win conflicts
332+
debug('Get the latest branch from:', this.remote)
333+
await this.git.fetch(this.remote, this.branch)
334+
debug('Rebasing session branch on top of remote.')
335+
await this.git.raw(['rebase', `${this.remote}/${this.branch}`, '--strategy-option=ours'])
336+
}
329337
} catch (error) {
330338
const errorMessage = getSanitizedErrorMessage(error)
331339
debug('Failed to remove upstream commits: ', errorMessage)

src/otomi-stack.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,14 @@ export default class OtomiStack {
293293
)
294294
}
295295

296+
// Pull latest changes so the worktree starts from up-to-date state
297+
try {
298+
debug(`Pulled latest changes before creating worktree for session ${this.sessionId}`)
299+
await mainRepo.pull(true, true)
300+
} catch (e) {
301+
debug(`Warning: could not pull latest before creating worktree: ${getSanitizedErrorMessage(e)}`)
302+
}
303+
296304
const worktreePath = this.getRepoPath()
297305
this.git = await getWorktreeRepo(mainRepo, worktreePath, env.GIT_BRANCH)
298306
this.fileStore = new FileStore()

0 commit comments

Comments
 (0)