Skip to content

Commit

Permalink
feat: add pristine in branchCache (#18478)
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulGautamSingh committed Nov 20, 2022
1 parent 516b56e commit 987421b
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 148 deletions.
4 changes: 2 additions & 2 deletions lib/util/cache/repository/types.ts
Expand Up @@ -59,9 +59,9 @@ export interface BranchCache {
*/
isModified?: boolean;
/**
* Parent commit of branch sha
*
*/
parentSha?: string | null;
pristine?: boolean;
/**
* Pr nunber of PR created from this branch
*/
Expand Down
4 changes: 0 additions & 4 deletions lib/util/git/index.spec.ts
Expand Up @@ -11,21 +11,18 @@ import { newlineRegex, regEx } from '../regex';
import * as _behindBaseCache from './behind-base-branch-cache';
import * as _conflictsCache from './conflicts-cache';
import * as _modifiedCache from './modified-cache';
import * as _parentShaCache from './parent-sha-cache';
import type { FileChange } from './types';
import * as git from '.';
import { setNoVerify } from '.';

jest.mock('./conflicts-cache');
jest.mock('./behind-base-branch-cache');
jest.mock('./modified-cache');
jest.mock('./parent-sha-cache');
jest.mock('delay');
jest.mock('../cache/repository');
const behindBaseCache = mocked(_behindBaseCache);
const conflictsCache = mocked(_conflictsCache);
const modifiedCache = mocked(_modifiedCache);
const parentShaCache = mocked(_parentShaCache);
// Class is no longer exported
const SimpleGit = Git().constructor as { prototype: ReturnType<typeof Git> };

Expand Down Expand Up @@ -116,7 +113,6 @@ describe('util/git/index', () => {
// override some local git settings for better testing
const local = Git(tmpDir.path);
await local.addConfig('commit.gpgsign', 'false');
parentShaCache.getCachedBranchParentShaResult.mockReturnValue(null);
behindBaseCache.getCachedBehindBaseResult.mockReturnValue(null);
});

Expand Down
2 changes: 0 additions & 2 deletions lib/util/git/index.ts
Expand Up @@ -45,7 +45,6 @@ import {
getCachedModifiedResult,
setCachedModifiedResult,
} from './modified-cache';
import { deleteCachedBranchParentShaResult } from './parent-sha-cache';
import { configSigningKey, writePrivateKey } from './private-key';
import type {
CommitFilesConfig,
Expand Down Expand Up @@ -663,7 +662,6 @@ export async function isBranchModified(branchName: string): Promise<boolean> {
);
config.branchIsModified[branchName] = true;
setCachedModifiedResult(branchName, true);
deleteCachedBranchParentShaResult(branchName);
return true;
}

Expand Down
88 changes: 0 additions & 88 deletions lib/util/git/parent-sha-cache.spec.ts

This file was deleted.

24 changes: 0 additions & 24 deletions lib/util/git/parent-sha-cache.ts

This file was deleted.

37 changes: 37 additions & 0 deletions lib/util/git/pristine.spec.ts
@@ -0,0 +1,37 @@
import { mocked, partial } from '../../../test/util';
import * as _repositoryCache from '../cache/repository';
import type { BranchCache, RepoCacheData } from '../cache/repository/types';
import { getCachedPristineResult } from './pristine';

jest.mock('../cache/repository');
const repositoryCache = mocked(_repositoryCache);

describe('util/git/pristine', () => {
let repoCache: RepoCacheData = {};

beforeEach(() => {
repoCache = {};
repositoryCache.getCache.mockReturnValue(repoCache);
});

describe('getCachedPristineResult', () => {
it('returns false if cache is not populated', () => {
expect(getCachedPristineResult('foo')).toBeFalse();
});

it('returns false if branch not found', () => {
repoCache.branches = [partial<BranchCache>({ branchName: 'not_foo' })];
expect(getCachedPristineResult('foo')).toBeFalse();
});

it('returns true', () => {
repoCache.branches = [
partial<BranchCache>({
branchName: 'foo',
pristine: true,
}),
];
expect(getCachedPristineResult('foo')).toBeTrue();
});
});
});
10 changes: 10 additions & 0 deletions lib/util/git/pristine.ts
@@ -0,0 +1,10 @@
import { getCache } from '../cache/repository';

export function getCachedPristineResult(branchName: string): boolean {
const cache = getCache();
const branch = cache.branches?.find(
(branch) => branch.branchName === branchName
);

return branch?.pristine ?? false;
}
10 changes: 5 additions & 5 deletions lib/util/git/set-branch-commit.spec.ts
Expand Up @@ -16,7 +16,7 @@ describe('util/git/set-branch-commit', () => {
});

describe('setBranchCommit', () => {
it('sets new branch in cache', () => {
it('sets new branch in cache if it does not exist', () => {
git.getBranchCommit.mockReturnValueOnce('base_SHA');
setBranchNewCommit('branch_name', 'base_branch', 'SHA');
expect(logger.logger.debug).toHaveBeenCalledWith(
Expand All @@ -31,7 +31,7 @@ describe('util/git/set-branch-commit', () => {
isBehindBase: false,
isConflicted: false,
isModified: false,
parentSha: 'base_SHA',
pristine: true,
},
]);
});
Expand All @@ -44,10 +44,10 @@ describe('util/git/set-branch-commit', () => {
baseBranch: 'base_branch',
sha: 'SHA',
baseBranchSha: 'base_SHA',
isBehindBase: true,
isBehindBase: false,
isModified: true,
pristine: false,
isConflicted: true,
parentSha: 'base_SHA',
}),
],
};
Expand All @@ -63,7 +63,7 @@ describe('util/git/set-branch-commit', () => {
isBehindBase: false,
isModified: false,
isConflicted: false,
parentSha: 'base_SHA',
pristine: true,
},
]);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/util/git/set-branch-commit.ts
Expand Up @@ -32,6 +32,6 @@ export function setBranchNewCommit(
branch.isBehindBase = false;
branch.isConflicted = false;
branch.isModified = false;
branch.parentSha = baseBranchSha;
branch.pristine = true;
branch.sha = commitSha;
}
7 changes: 3 additions & 4 deletions lib/workers/repository/cache.ts
Expand Up @@ -13,7 +13,7 @@ import {
isBranchConflicted,
isBranchModified,
} from '../../util/git';
import { getCachedBranchParentShaResult } from '../../util/git/parent-sha-cache';
import { getCachedPristineResult } from '../../util/git/pristine';
import type { BranchConfig, BranchUpgradeConfig } from '../types';

function generateBranchUpgradeCache(
Expand Down Expand Up @@ -53,13 +53,12 @@ async function generateBranchCache(
try {
const sha = getBranchCommit(branchName) ?? null;
const baseBranchSha = getBranchCommit(baseBranch);
const pristine = getCachedPristineResult(branchName);
let prNo = null;
let parentSha = null;
let isModified = false;
let isBehindBase = false;
let isConflicted = false;
if (sha) {
parentSha = getCachedBranchParentShaResult(branchName, sha);
const branchPr = await platform.getBranchPr(branchName);
if (branchPr) {
prNo = branchPr.number;
Expand All @@ -82,7 +81,7 @@ async function generateBranchCache(
isBehindBase,
isConflicted,
isModified,
parentSha,
pristine,
prNo,
sha,
upgrades,
Expand Down
6 changes: 3 additions & 3 deletions lib/workers/repository/finalise/repository-statistics.spec.ts
Expand Up @@ -87,21 +87,21 @@ describe('workers/repository/finalise/repository-statistics', () => {

it('processes cache with baseBranches and branches', () => {
const sha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const parentSha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const baseBranchSha = '793221454914cdc422e1a8f0ca27b96fe39ff9ad';
const baseBranch = 'base-branch';
const baseCache = partial<BaseBranchCache>({ sha });
const branchCache = partial<BranchCache>({
sha,
parentSha,
baseBranch,
baseBranchSha,
isModified: false,
automerge: false,
});
const expectedMeta = {
automerge: branchCache.automerge,
isModified: branchCache.isModified,
baseBranch,
baseBranchSha: parentSha,
baseBranchSha,
branchSha: sha,
};
const branches: BranchCache[] = [
Expand Down
2 changes: 1 addition & 1 deletion lib/workers/repository/finalise/repository-statistics.ts
Expand Up @@ -44,7 +44,7 @@ function branchCacheToMetadata({
branchName,
sha: branchSha,
baseBranch,
parentSha: baseBranchSha,
baseBranchSha,
automerge,
isModified,
}: BranchCache): BranchMetadata {
Expand Down

0 comments on commit 987421b

Please sign in to comment.