diff --git a/lib/workers/repository/update/branch/index.spec.ts b/lib/workers/repository/update/branch/index.spec.ts index ff9b3fac8a1a9895d7e40bd25543d5a42476ad69..da9c8329a42ebb281bf06f8124952fdee2c8d900 100644 --- a/lib/workers/repository/update/branch/index.spec.ts +++ b/lib/workers/repository/update/branch/index.spec.ts @@ -2145,5 +2145,27 @@ describe('workers/repository/update/branch/index', () => { expect(automerge.tryBranchAutomerge).not.toHaveBeenCalled(); expect(prWorker.ensurePr).toHaveBeenCalledTimes(1); }); + + it('checks out baseBranch after committing files', async () => { + getUpdated.getUpdatedPackageFiles.mockResolvedValueOnce({ + ...updatedPackageFiles, + }); + npmPostExtract.getAdditionalFiles.mockResolvedValueOnce({ + artifactErrors: [], + updatedArtifacts: [], + }); + config.baseBranch = 'main'; + await branchWorker.processBranch(config); + expect(git.checkoutBranch).toHaveBeenLastCalledWith('main'); + // Check that the last checkoutBranch call is after the only commitFilesToBranch call + const checkoutBranchCalledTimes = git.checkoutBranch.mock.calls.length; + expect( + commit.commitFilesToBranch.mock.invocationCallOrder[0] + ).toBeLessThan( + git.checkoutBranch.mock.invocationCallOrder[ + checkoutBranchCalledTimes - 1 + ] + ); + }); }); }); diff --git a/lib/workers/repository/update/branch/index.ts b/lib/workers/repository/update/branch/index.ts index b3e609ee104860ec7231605458cddf440fd28b71..b779a277aa134480e2b11aebe30a6df7d2a05c98 100644 --- a/lib/workers/repository/update/branch/index.ts +++ b/lib/workers/repository/update/branch/index.ts @@ -498,6 +498,9 @@ export async function processBranch( } commitSha = await commitFilesToBranch(config); + // Checkout to base branch to ensure that the next branch processing always starts with git being on the baseBranch + // baseBranch is not checked out at the start of processBranch() due to pull/16246 + await checkoutBranch(config.baseBranch); updatesVerified = true; } // istanbul ignore if