Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use getBranchParentSha cached result (#16724)
- Loading branch information
1 parent
a3c3f11
commit 9778ca6
Showing
4 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
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'; | ||
|
||
jest.mock('../cache/repository'); | ||
const repositoryCache = mocked(_repositoryCache); | ||
|
||
describe('util/git/parent-sha-cache', () => { | ||
let repoCache: RepoCacheData = {}; | ||
|
||
beforeEach(() => { | ||
repoCache = {}; | ||
repositoryCache.getCache.mockReturnValue(repoCache); | ||
}); | ||
|
||
describe('getCachedBranchParentShaResult', () => { | ||
it('returns null if cache is not populated', () => { | ||
expect(getCachedBranchParentShaResult('foo', '111')).toBeNull(); | ||
}); | ||
|
||
it('returns null if target key not found', () => { | ||
repositoryCache.getCache.mockReturnValue({ | ||
branches: [ | ||
{ | ||
branchName: 'not_foo', | ||
sha: '111', | ||
} as BranchCache, | ||
], | ||
}); | ||
expect(getCachedBranchParentShaResult('foo', '111')).toBeNull(); | ||
}); | ||
|
||
it('returns null if target key is null', () => { | ||
repositoryCache.getCache.mockReturnValue({ | ||
branches: [ | ||
{ | ||
branchName: 'not_foo', | ||
sha: null, | ||
} as BranchCache, | ||
], | ||
}); | ||
expect(getCachedBranchParentShaResult('foo', '111')).toBeNull(); | ||
}); | ||
|
||
it('returns null if target SHA has changed', () => { | ||
repositoryCache.getCache.mockReturnValue({ | ||
branches: [ | ||
{ | ||
branchName: 'foo', | ||
sha: '222', | ||
} as BranchCache, | ||
], | ||
}); | ||
expect(getCachedBranchParentShaResult('foo', '111')).toBeNull(); | ||
}); | ||
|
||
it('returns cached value', () => { | ||
repositoryCache.getCache.mockReturnValue({ | ||
branches: [ | ||
{ | ||
branchName: 'foo', | ||
sha: '111', | ||
parentSha: '000', | ||
} as BranchCache, | ||
], | ||
}); | ||
expect(getCachedBranchParentShaResult('foo', '111')).toBe('000'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { getCache } from '../cache/repository'; | ||
|
||
export function getCachedBranchParentShaResult( | ||
branchName: string, | ||
branchSha: string | null | ||
): string | null { | ||
const { branches } = getCache(); | ||
const branch = branches?.find((branch) => branch.branchName === branchName); | ||
|
||
if (branchSha === branch?.sha) { | ||
return branch.parentSha; | ||
} | ||
|
||
return null; | ||
} |