From 385c5344bd87720d998f70ff6819a15deb4041e2 Mon Sep 17 00:00:00 2001 From: Scratch Date: Mon, 10 Nov 2025 22:42:08 +1100 Subject: [PATCH] version info: detect correct remote --- packages/version-info/index.js | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/version-info/index.js b/packages/version-info/index.js index a1f74968a..855fcd05a 100644 --- a/packages/version-info/index.js +++ b/packages/version-info/index.js @@ -48,11 +48,25 @@ export const getBranch = async () => { ?.trim(); } -export const getRemote = async () => { - let remote = (await readGit('.git/config')) - ?.split('\n') - ?.find(line => line.includes('url = ')) - ?.split('url = ')[1]; +export const getRemote = async (branch) => { + branch = branch ?? await getBranch(); + function fail(){ + throw 'could not parse remote'; + } + + const gitConfig = await readGit('.git/config') + const remoteName = gitConfig.match( + new RegExp(`\\[branch "${branch}"\\][\\s]*?remote = (.+)`) + ) + if (!remoteName) { + fail(); + } + const remoteURL = gitConfig.match( + new RegExp(`\\[remote "${remoteName[1].trim()}"\\][\\s]*?url = (.+)`) + ) + if (!remoteURL) fail(); + + let remote = remoteURL[1].trim() if (remote?.startsWith('git@')) { remote = remote.split(':')[1]; @@ -63,7 +77,7 @@ export const getRemote = async () => { remote = remote?.replace(/\.git$/, ''); if (!remote) { - throw 'could not parse remote'; + fail(); } return remote;