feat(git): initialise submodules when cloning repos (#4353)

See https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---recurse-submodulesltpathspec

Fixes #1356
This commit is contained in:
Jamie Magee 2019-09-25 11:58:52 +02:00 committed by Rhys Arkins
parent 8554df5c61
commit e792268cbb
3 changed files with 44 additions and 3 deletions

View file

@ -110,7 +110,10 @@ export class Storage {
const cloneStart = process.hrtime();
try {
// clone only the default branch
await this._git.clone(config.url, '.', ['--depth=2']);
await this._git.clone(config.url, '.', [
'--depth=2',
'--recurse-submodules',
]);
} catch (err) /* istanbul ignore next */ {
logger.debug({ err }, 'git clone error');
throw new Error('platform-failure');
@ -241,7 +244,19 @@ export class Storage {
if (!exists) {
return [];
}
const files = await this._git!.raw([
const submodules: string[] = (
(await this._git!.raw([
'config',
'--file',
'.gitmodules',
'--get-regexp',
'path',
])) || ''
)
.trim()
.split(/[\n\s]/)
.filter((_e: string, i: number) => i % 2);
const files: string = await this._git!.raw([
'ls-tree',
'-r',
'--name-only',
@ -251,7 +266,12 @@ export class Storage {
if (!files) {
return [];
}
return files.split('\n').filter(Boolean);
return files
.split('\n')
.filter(Boolean)
.filter((file: string) =>
submodules.every((submodule: string) => !file.startsWith(submodule))
);
}
async branchExists(branchName: string) {

View file

@ -11,6 +11,15 @@ Array [
exports[`platform/git/storage getFile(filePath, branchName) returns null for 404 1`] = `[Error: repository-changed]`;
exports[`platform/git/storage getFileList() should exclude submodules 1`] = `
Array [
".gitmodules",
"file_to_delete",
"master_file",
"past_file",
]
`;
exports[`platform/git/storage getFileList() should return the correct files 1`] = `
Array [
"file_to_delete",

View file

@ -84,6 +84,18 @@ describe('platform/git/storage', () => {
expect(await git.getFileList('renovate/future_branch')).toMatchSnapshot();
expect(await git.getFileList()).toMatchSnapshot();
});
it('should exclude submodules', async () => {
const repo = await Git(base.path).silent(true);
await repo.submoduleAdd(base.path, 'submodule');
await repo.commit('Add submodule');
await git.initRepo({
localDir: tmpDir.path,
url: base.path,
});
expect(await fs.exists(tmpDir.path + '/.gitmodules')).toBeTruthy();
expect(await git.getFileList()).toMatchSnapshot();
repo.reset(['--hard', 'HEAD^']);
});
});
describe('branchExists(branchName)', () => {
it('should return true if found', async () => {