-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use isBranchBehindBase cached result (#16595)
- Loading branch information
1 parent
5b74dad
commit 93707c1
Showing
4 changed files
with
84 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { mocked } from '../../../test/util'; | ||
import * as _repositoryCache from '../cache/repository'; | ||
import type { BranchCache, RepoCacheData } from '../cache/repository/types'; | ||
import { getCachedBehindBaseResult } from './behind-base-branch-cache'; | ||
|
||
jest.mock('../cache/repository'); | ||
const repositoryCache = mocked(_repositoryCache); | ||
|
||
describe('util/git/behind-base-branch-cache', () => { | ||
let repoCache: RepoCacheData = {}; | ||
|
||
beforeEach(() => { | ||
repoCache = {}; | ||
repositoryCache.getCache.mockReturnValue(repoCache); | ||
}); | ||
|
||
describe('getCachedBehindBaseResult', () => { | ||
it('returns null if cache is not populated', () => { | ||
expect(getCachedBehindBaseResult('foo', '111')).toBeNull(); | ||
}); | ||
|
||
it('returns null if branch not found', () => { | ||
expect(getCachedBehindBaseResult('foo', '111')).toBeNull(); | ||
}); | ||
|
||
it('returns true if target SHA has changed', () => { | ||
repoCache.branches = [ | ||
{ branchName: 'foo', sha: 'aaa', parentSha: '222' } as BranchCache, | ||
]; | ||
expect(getCachedBehindBaseResult('foo', '111')).toBeTrue(); | ||
}); | ||
|
||
it('returns false if target SHA has not changed', () => { | ||
repoCache.branches = [ | ||
{ branchName: 'foo', sha: 'aaa', parentSha: '111' } as BranchCache, | ||
]; | ||
expect(getCachedBehindBaseResult('foo', '111')).toBeFalse(); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import { getCache } from '../cache/repository'; | ||
|
||
// Compare cached parent Sha of a branch to the fetched base-branch sha to determine whether the branch is behind the base | ||
// Since cache is updated after each run, this will be sufficient to determine whether a branch is behind its parent. | ||
export function getCachedBehindBaseResult( | ||
branchName: string, | ||
currentBaseBranchSha: string | ||
): boolean | null { | ||
const cache = getCache(); | ||
const { branches = [] } = cache; | ||
const cachedBranch = branches?.find( | ||
(branch) => branch.branchName === branchName | ||
); | ||
|
||
if (!cachedBranch) { | ||
return null; | ||
} | ||
|
||
return currentBaseBranchSha !== cachedBranch.parentSha; | ||
} |
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