diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index 6df7303b275c57..763e6e1c87a580 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -768,6 +768,42 @@ export async function deleteBranch(branchName: string): Promise { delete config.branchCommits[branchName]; } +export async function mergeBranchWithoutPushing( + branchName: string +): Promise { + let status: StatusResult | undefined; + try { + await syncGit(); + await git.reset(ResetMode.HARD); + await gitRetry(() => + git.checkout(['-B', branchName, 'origin/' + branchName]) + ); + await gitRetry(() => + git.checkout([ + '-B', + config.currentBranch, + 'origin/' + config.currentBranch, + ]) + ); + status = await git.status(); + await gitRetry(() => git.merge([branchName])); + incLimitedValue('Commits'); + } catch (err) { + logger.debug( + { + baseBranch: config.currentBranch, + baseSha: config.currentBranchSha, + branchName, + branchSha: getBranchCommit(branchName), + status, + err, + }, + 'mergeBranch error' + ); + throw err; + } +} + export async function mergeBranch(branchName: string): Promise { let status: StatusResult | undefined; try { diff --git a/lib/workers/repository/onboarding/branch/index.ts b/lib/workers/repository/onboarding/branch/index.ts index 2add4def7375e7..8e7770bbe97e5b 100644 --- a/lib/workers/repository/onboarding/branch/index.ts +++ b/lib/workers/repository/onboarding/branch/index.ts @@ -8,7 +8,12 @@ import { } from '../../../../constants/error-messages'; import { logger } from '../../../../logger'; import { Pr, platform } from '../../../../modules/platform'; -import { checkoutBranch, setGitAuthor } from '../../../../util/git'; +import { scm } from '../../../../modules/platform/scm'; +import { + checkoutBranch, + mergeBranchWithoutPushing, + setGitAuthor, +} from '../../../../util/git'; import { extractAllDependencies } from '../../extract'; import { mergeRenovateConfig } from '../../init/merge'; import { OnboardingState } from '../common'; @@ -81,9 +86,16 @@ export async function checkOnboardingBranch( } } if (!GlobalConfig.get('dryRun')) { - logger.debug('Checkout onboarding branch.'); // TODO #7154 - await checkoutBranch(onboardingBranch!); + if ( + await scm.isBranchConflicted(config.baseBranch!, config.onboardingBranch!) + ) { + logger.debug('Checkout onboarding branch'); + await checkoutBranch(config.onboardingBranch!); + } else { + logger.debug('Merge onboarding branch in default branch'); + await mergeBranchWithoutPushing(onboardingBranch!); + } } // TODO #7154 const branchList = [onboardingBranch!];