Skip to content

Commit

Permalink
feat(git-submodules): fetch default branch (#7391)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Oct 1, 2020
1 parent 78a3f60 commit 939f3d6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/manager/git-submodules/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ describe('lib/manager/gitsubmodules/extract', () => {
}
return git.raw(options);
},
listRemote(): Response<string> {
return partial<Response<string>>(
Promise.resolve(
'ref: refs/heads/main HEAD\n5701164b9f5edba1f6ca114c491a564ffb55a964 HEAD'
)
);
},
};
});
});
Expand All @@ -38,6 +45,7 @@ describe('lib/manager/gitsubmodules/extract', () => {
).toBeNull();
res = await extractPackageFile('', '.gitmodules.2', { localDir });
expect(res.deps).toHaveLength(1);
expect(res.deps[0].registryUrls[1]).toEqual('main');
res = await extractPackageFile('', '.gitmodules.3', { localDir });
expect(res.deps).toHaveLength(1);
res = await extractPackageFile('', '.gitmodules.4', { localDir });
Expand Down
18 changes: 15 additions & 3 deletions lib/manager/git-submodules/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ async function getUrl(
return URL.resolve(`${remoteUrl}/`, path);
}

const headRefRe = /ref: refs\/heads\/(?<branch>\w+)\s/;

async function getDefaultBranch(subModuleUrl: string): Promise<string> {
const val = await Git().listRemote(['--symref', subModuleUrl, 'HEAD']);
return headRefRe.exec(val)?.groups?.branch ?? 'master';
}

async function getBranch(
gitModulesPath: string,
submoduleName: string
submoduleName: string,
subModuleUrl: string
): Promise<string> {
return (
(await Git().raw([
Expand All @@ -45,7 +53,7 @@ async function getBranch(
gitModulesPath,
'--get',
`submodule.${submoduleName}.branch`,
])) || 'master'
])) || (await getDefaultBranch(subModuleUrl))
).trim();
}

Expand Down Expand Up @@ -99,8 +107,12 @@ export default async function extractPackageFile(
const [currentValue] = (await git.subModule(['status', path]))
.trim()
.split(/[+\s]/);
const submoduleBranch = await getBranch(gitModulesPath, name);
const subModuleUrl = await getUrl(git, gitModulesPath, name);
const submoduleBranch = await getBranch(
gitModulesPath,
name,
subModuleUrl
);
return {
depName: path,
registryUrls: [subModuleUrl, submoduleBranch],
Expand Down

0 comments on commit 939f3d6

Please sign in to comment.