diff --git a/lib/util/cache/repository/types.ts b/lib/util/cache/repository/types.ts index c08081a3e358a49c1721dae3452561b6395f6d51..2512773be5924fef77cd46d9cc43bda0532a29c4 100644 --- a/lib/util/cache/repository/types.ts +++ b/lib/util/cache/repository/types.ts @@ -58,7 +58,7 @@ export interface BranchCache { /** * Parent commit of branch sha */ - parentSha: string | null; + parentSha?: string | null; /** * Pr nunber of PR created from this branch */ diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index 2bb96cad02095504afe9c92e093d1606653559b3..46bf23e45190898dd355fc7c26ce51f104731e98 100644 --- a/lib/util/git/index.spec.ts +++ b/lib/util/git/index.spec.ts @@ -315,23 +315,6 @@ describe('util/git/index', () => { }); }); - describe('getBranchParentSha(branchName)', () => { - it('should return sha if found', async () => { - const parentSha = await git.getBranchParentSha('renovate/future_branch'); - expect(parentSha).toHaveLength(40); - expect(parentSha).toEqual(git.getBranchCommit(defaultBranch)); - }); - - it('should return null if not found', async () => { - expect(await git.getBranchParentSha('not_found')).toBeNull(); - }); - - it('should return cached value', async () => { - parentShaCache.getCachedBranchParentShaResult.mockReturnValueOnce('111'); - expect(await git.getBranchParentSha('not_found')).toBe('111'); - }); - }); - describe('getBranchFiles(branchName)', () => { it('detects changed files compared to current base branch', async () => { const file: FileChange = { diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index df78425ec8ab9eb427f71fcb9f38125e311e3ba1..860e1be2fc7d0415d212babd48a03c6f2f2a25f1 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -45,7 +45,7 @@ import { getCachedModifiedResult, setCachedModifiedResult, } from './modified-cache'; -import { getCachedBranchParentShaResult } from './parent-sha-cache'; +import { deleteCachedBranchParentShaResult } from './parent-sha-cache'; import { configSigningKey, writePrivateKey } from './private-key'; import type { CommitFilesConfig, @@ -490,29 +490,6 @@ export function getBranchCommit(branchName: string): CommitSha | null { return config.branchCommits[branchName] || null; } -// Return the parent commit SHA for a branch -export async function getBranchParentSha( - branchName: string -): Promise<CommitSha | null> { - const branchSha = getBranchCommit(branchName); - let parentSha = getCachedBranchParentShaResult(branchName, branchSha); - if (parentSha !== null) { - logger.debug( - `branch.getBranchParentSha(): using cached result "${parentSha}"` - ); - return parentSha; - } - - try { - // TODO: branchSha can be null (#7154) - parentSha = await git.revparse([`${branchSha!}^`]); - return parentSha; - } catch (err) { - logger.debug({ err }, 'Error getting branch parent sha'); - return null; - } -} - export async function getCommitMessages(): Promise<string[]> { await syncGit(); logger.debug('getCommitMessages'); @@ -686,6 +663,7 @@ export async function isBranchModified(branchName: string): Promise<boolean> { ); config.branchIsModified[branchName] = true; setCachedModifiedResult(branchName, true); + deleteCachedBranchParentShaResult(branchName); return true; } diff --git a/lib/util/git/parent-sha-cache.spec.ts b/lib/util/git/parent-sha-cache.spec.ts index 9e88085d832086a3a90f17d4b0e0ea4351ba392c..6c7f1abfd71cadc15916a50e7719bdb2d54b2907 100644 --- a/lib/util/git/parent-sha-cache.spec.ts +++ b/lib/util/git/parent-sha-cache.spec.ts @@ -1,7 +1,10 @@ import { mocked } from '../../../test/util'; import * as _repositoryCache from '../cache/repository'; import type { BranchCache, RepoCacheData } from '../cache/repository/types'; -import { getCachedBranchParentShaResult } from './parent-sha-cache'; +import { + deleteCachedBranchParentShaResult, + getCachedBranchParentShaResult, +} from './parent-sha-cache'; jest.mock('../cache/repository'); const repositoryCache = mocked(_repositoryCache); @@ -68,4 +71,18 @@ describe('util/git/parent-sha-cache', () => { expect(getCachedBranchParentShaResult('foo', '111')).toBe('000'); }); }); + + describe('deleteCachedBranchParentShaResult', () => { + it('returns null if cache is not populated', () => { + repoCache.branches = [ + { + branchName: 'foo', + sha: '111', + parentSha: 'sha', + } as BranchCache, + ]; + deleteCachedBranchParentShaResult('foo'); + expect(repoCache.branches[0].parentSha).toBeUndefined(); + }); + }); }); diff --git a/lib/util/git/parent-sha-cache.ts b/lib/util/git/parent-sha-cache.ts index bec5e8253e93babb8cd08f91e6dcc9f7b2da1d47..f5842c378057767127cafe74c1e5f425f4ac8f5d 100644 --- a/lib/util/git/parent-sha-cache.ts +++ b/lib/util/git/parent-sha-cache.ts @@ -13,3 +13,12 @@ export function getCachedBranchParentShaResult( return null; } + +export function deleteCachedBranchParentShaResult(branchName: string): void { + const { branches } = getCache(); + const branch = branches?.find((branch) => branch.branchName === branchName); + + if (branch?.parentSha) { + delete branch.parentSha; + } +} diff --git a/lib/workers/repository/cache.ts b/lib/workers/repository/cache.ts index 2b76554c02111404d5e50ba8b8d118c1911fde91..90ee99d101737e0c4e69005d1922ad64612f4961 100644 --- a/lib/workers/repository/cache.ts +++ b/lib/workers/repository/cache.ts @@ -9,10 +9,10 @@ import type { } from '../../util/cache/repository/types'; import { getBranchCommit, - getBranchParentSha, isBranchBehindBase, isBranchModified, } from '../../util/git'; +import { getCachedBranchParentShaResult } from '../../util/git/parent-sha-cache'; import type { BranchConfig, BranchUpgradeConfig } from '../types'; function generateBranchUpgradeCache( @@ -57,7 +57,7 @@ async function generateBranchCache( let isModified = false; let isBehindBase = false; if (sha) { - parentSha = await getBranchParentSha(branchName); + parentSha = getCachedBranchParentShaResult(branchName, sha); const branchPr = await platform.getBranchPr(branchName); if (branchPr) { prNo = branchPr.number;