From 62e95161edaa0351ffd8f1744944587bd72a567d Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Tue, 11 Feb 2020 13:00:43 +0100 Subject: [PATCH] fix(git-submodules): allow module and path be different (#5437) --- .../git-submodules/__fixtures__/.gitmodules.5 | 9 +++ .../__snapshots__/extract.spec.ts.snap | 32 +++++++++++ lib/manager/git-submodules/extract.spec.ts | 6 ++ lib/manager/git-submodules/extract.ts | 55 +++++++++++++------ 4 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 lib/manager/git-submodules/__fixtures__/.gitmodules.5 diff --git a/lib/manager/git-submodules/__fixtures__/.gitmodules.5 b/lib/manager/git-submodules/__fixtures__/.gitmodules.5 new file mode 100644 index 00000000000000..06da8880e1e1e9 --- /dev/null +++ b/lib/manager/git-submodules/__fixtures__/.gitmodules.5 @@ -0,0 +1,9 @@ +[submodule "renovate"] + path = deps/renovate + url = https://github.com/renovatebot/renovate.git +[submodule "renovate-pro"] + path = deps/renovate-pro + url = https://github.com/renovatebot/pro.git +[submodule "renovate-config"] + path = deps/renovate-config + url = https://github.com/renovatebot/renovate-config.git diff --git a/lib/manager/git-submodules/__snapshots__/extract.spec.ts.snap b/lib/manager/git-submodules/__snapshots__/extract.spec.ts.snap index 208537bf1dea79..329fc9cff20908 100644 --- a/lib/manager/git-submodules/__snapshots__/extract.spec.ts.snap +++ b/lib/manager/git-submodules/__snapshots__/extract.spec.ts.snap @@ -28,6 +28,38 @@ Array [ ] `; +exports[`lib/manager/gitsubmodules/extract extractPackageFile() extract name path mismatch 1`] = ` +Array [ + Object { + "currentDigest": "4b825dc642cb6eb9a060e54bf8d69288fbee4904", + "currentValue": "4b825dc642cb6eb9a060e54bf8d69288fbee4904", + "depName": "deps/renovate", + "registryUrls": Array [ + "https://github.com/renovatebot/renovate.git", + "master", + ], + }, + Object { + "currentDigest": "4b825dc642cb6eb9a060e54bf8d69288fbee4904", + "currentValue": "4b825dc642cb6eb9a060e54bf8d69288fbee4904", + "depName": "deps/renovate-pro", + "registryUrls": Array [ + "https://github.com/renovatebot/pro.git", + "master", + ], + }, + Object { + "currentDigest": "4b825dc642cb6eb9a060e54bf8d69288fbee4904", + "currentValue": "4b825dc642cb6eb9a060e54bf8d69288fbee4904", + "depName": "deps/renovate-config", + "registryUrls": Array [ + "https://github.com/renovatebot/renovate-config.git", + "master", + ], + }, +] +`; + exports[`lib/manager/gitsubmodules/extract extractPackageFile() extract relative URL 1`] = ` Array [ Object { diff --git a/lib/manager/git-submodules/extract.spec.ts b/lib/manager/git-submodules/extract.spec.ts index 7edb63cd4a7eca..ed9a4f051d3951 100644 --- a/lib/manager/git-submodules/extract.spec.ts +++ b/lib/manager/git-submodules/extract.spec.ts @@ -42,5 +42,11 @@ describe('lib/manager/gitsubmodules/extract', () => { expect(res.deps).toMatchSnapshot(); expect(res.deps).toHaveLength(1); }); + + it('extract name path mismatch', async () => { + const res = await extractPackageFile('', '.gitmodules.5', { localDir }); + expect(res.deps).toMatchSnapshot(); + expect(res.deps).toHaveLength(3); + }); }); }); diff --git a/lib/manager/git-submodules/extract.ts b/lib/manager/git-submodules/extract.ts index dca6d6e5523a38..497e913de73aca 100644 --- a/lib/manager/git-submodules/extract.ts +++ b/lib/manager/git-submodules/extract.ts @@ -5,6 +5,11 @@ import URL from 'url'; import { ManagerConfig, PackageFile } from '../common'; import { DATASOURCE_GIT_SUBMODULES } from '../../constants/data-binary-source'; +type GitModule = { + name: string; + path: string; +}; + async function getUrl( git: Git.SimpleGit, gitModulesPath: string, @@ -43,15 +48,11 @@ async function getBranch( ).trim(); } -export default async function extractPackageFile( - content: string, - fileName: string, - config: ManagerConfig -): Promise { - const git = Git(config.localDir); - const gitModulesPath = upath.join(config.localDir, fileName); - - const depNames = ( +async function getModules( + git: Git.SimpleGit, + gitModulesPath: string +): Promise { + const modules = ( (await git.raw([ 'config', '--file', @@ -61,22 +62,42 @@ export default async function extractPackageFile( ])) || '' ) .trim() - .split(/[\n\s]/) - .filter((_e: string, i: number) => i % 2); + .split(/\n/) + .filter(s => !!s); + + const res: GitModule[] = []; + + for (const line of modules) { + const [, name, path] = line.split(/submodule\.(.+?)\.path\s(.+)/); + res.push({ name, path }); + } + + return res; +} + +export default async function extractPackageFile( + content: string, + fileName: string, + config: ManagerConfig +): Promise { + const git = Git(config.localDir); + const gitModulesPath = upath.join(config.localDir, fileName); + + const depNames = await getModules(git, gitModulesPath); if (!depNames.length) { return null; } const deps = await Promise.all( - depNames.map(async depName => { - const currentValue = (await git.subModule(['status', depName])) + depNames.map(async ({ name, path }) => { + const [currentValue] = (await git.subModule(['status', path])) .trim() - .split(/[+\s]/)[0]; - const submoduleBranch = await getBranch(gitModulesPath, depName); - const subModuleUrl = await getUrl(git, gitModulesPath, depName); + .split(/[+\s]/); + const submoduleBranch = await getBranch(gitModulesPath, name); + const subModuleUrl = await getUrl(git, gitModulesPath, name); return { - depName, + depName: path, registryUrls: [subModuleUrl, submoduleBranch], currentValue, currentDigest: currentValue,