Skip to content

Commit

Permalink
feat: validate git version (#9979)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins authored May 21, 2021
1 parent 878aa39 commit 1304bc9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/development/local-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ For example, if you think anything is unclear, or you think something needs to b

You need the following dependencies for local development:

- Git
- Git `>= 1.22.0`
- Node.js `>=14.15.4`
- Yarn `^1.22.5`
- C++ compiler
Expand Down
6 changes: 6 additions & 0 deletions lib/util/git/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ describe(getName(), () => {
await base.cleanup();
});

describe('validateGitVersion()', () => {
it('has a git version greater or equal to the minimum required', async () => {
const res = await git.validateGitVersion();
expect(res).toBeTrue();
});
});
describe('checkoutBranch(branchName)', () => {
it('sets the base branch as master', async () => {
await expect(git.checkoutBranch(defaultBranch)).resolves.not.toThrow();
Expand Down
36 changes: 36 additions & 0 deletions lib/util/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
import { logger } from '../../logger';
import { ExternalHostError } from '../../types/errors/external-host-error';
import { GitOptions, GitProtocol } from '../../types/git';
import { api as semver } from '../../versioning/semver';
import { Limit, incLimitedValue } from '../../workers/global/limits';
import { GitNoVerifyOption, getNoVerify } from './config';
import { configSigningKey, writePrivateKey } from './private-key';
Expand Down Expand Up @@ -145,6 +146,41 @@ let gitInitialized: boolean;

let privateKeySet = false;

export const GIT_MINIMUM_VERSION = '2.22.0'; // git show-current

export async function validateGitVersion(): Promise<boolean> {
let version: string;
const globalGit = Git();
try {
const raw = await globalGit.raw(['--version']);
for (const section of raw.split(/\s+/)) {
if (semver.isVersion(section)) {
version = section;
break;
}
}
} catch (err) /* istanbul ignore next */ {
logger.error({ err }, 'Error fetching git version');
return false;
}
// istanbul ignore if
if (
!(
version &&
(version === GIT_MINIMUM_VERSION ||
semver.isGreaterThan(version, GIT_MINIMUM_VERSION))
)
) {
logger.error(
{ detectedVersion: version, minimumVersion: GIT_MINIMUM_VERSION },
'Git version needs upgrading'
);
return false;
}
logger.debug(`Found valid git version: ${version}`);
return true;
}

async function fetchBranchCommits(): Promise<void> {
config.branchCommits = {};
const opts = ['ls-remote', '--heads', config.url];
Expand Down
20 changes: 20 additions & 0 deletions lib/workers/global/initialize.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getName, mocked } from '../../../test/util';
import * as _git from '../../util/git';
import { checkVersions } from './initialize';

jest.mock('../../util/git');

const git = mocked(_git);

describe(getName(), () => {
describe('checkVersions()', () => {
it('throws if invalid version', async () => {
git.validateGitVersion.mockResolvedValueOnce(false);
await expect(checkVersions()).rejects.toThrow();
});
it('returns if valid git version', async () => {
git.validateGitVersion.mockResolvedValueOnce(true);
await expect(checkVersions()).toResolve();
});
});
});
9 changes: 9 additions & 0 deletions lib/workers/global/initialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { logger } from '../../logger';
import { initPlatform } from '../../platform';
import * as packageCache from '../../util/cache/package';
import { setEmojiConfig } from '../../util/emoji';
import { validateGitVersion } from '../../util/git';
import { Limit, setMaxLimit } from './limits';

async function setDirectories(input: GlobalConfig): Promise<GlobalConfig> {
Expand Down Expand Up @@ -34,10 +35,18 @@ function limitCommitsPerRun(config: RenovateConfig): void {
setMaxLimit(Limit.Commits, limit);
}

export async function checkVersions(): Promise<void> {
const validGitVersion = await validateGitVersion();
if (!validGitVersion) {
throw new Error('Init: git version needs upgrading');
}
}

export async function globalInitialize(
config_: RenovateConfig
): Promise<RenovateConfig> {
let config = config_;
await checkVersions();
config = await initPlatform(config);
config = await setDirectories(config);
packageCache.init(config);
Expand Down

0 comments on commit 1304bc9

Please sign in to comment.