Skip to content
TypeScript

fix(server): onboarding reads a remote url on its section header line (#23747)

Wesley Smits8689c7a4f5e6
diff --git a/packages/shared/src/git.test.ts b/packages/shared/src/git.test.ts
index adf44d2289e..30a5ca2bd5d 100644
--- a/packages/shared/src/git.test.ts
+++ b/packages/shared/src/git.test.ts
@@ -113,6 +113,21 @@ describe("parseOriginUrlFromGitConfig", () => {
).toBe("https://github.com/acme/repo.git");
});
+ it("reads settings on the remote header line", () => {
+ const upstream = '\n[remote "upstream"]\n\turl = https://github.com/other/source.git\n';
+ expect(
+ parseOriginUrlFromGitConfig(`[remote "origin"] url = [email protected]:a/b.git${upstream}`),
+ ).toBe("[email protected]:a/b.git");
+ expect(parseOriginUrlFromGitConfig("[remote.Origin][email protected]:a/b.git\n")).toBe(
+ "[email protected]:a/b.git",
+ );
+ expect(
+ parseOriginUrlFromGitConfig(
+ `[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/*\n\turl = [email protected]:a/b.git${upstream}`,
+ ),
+ ).toBe("[email protected]:a/b.git");
+ });
+
it("falls back to the first remote when there is no origin", () => {
const config = [
'[remote "upstream"]',
diff --git a/packages/shared/src/git.ts b/packages/shared/src/git.ts
index 4fc86fbccc3..3277ea3b29d 100644
--- a/packages/shared/src/git.ts
+++ b/packages/shared/src/git.ts
@@ -214,15 +214,15 @@ export function parseOriginUrlFromGitConfig(configText: string): string | null {
let firstRemoteUrl: string | null = null;
const joined = configText.replace(/\\\r?\n[ \t]*/g, "");
for (const rawLine of joined.split(/\r?\n/)) {
- const line = rawLine.trim();
+ let line = rawLine.trim();
if (line.length === 0 || line.startsWith("#") || line.startsWith(";")) continue;
- // Git preserves case in quoted remote subsections but lowercases legacy dotted ones.
- const header = /^\[\s*remote(?:\s+"([^"]+)"|\.([^\]\s]+))\s*\](?:\s*[#;].*)?$/i.exec(line);
+ // Git preserves case in quoted remote subsections but lowercases legacy dotted ones,
+ // and accepts the first setting on the section header's own line.
+ const header = /^\[\s*remote(?:\s+"([^"]+)"|\.([^\]\s]+))\s*\](.*)$/i.exec(line);
if (header) {
section = header[1] ?? header[2]?.toLowerCase() ?? null;
- continue;
- }
- if (line.startsWith("[")) {
+ line = header[3]!.trim();
+ } else if (line.startsWith("[")) {
section = null;
continue;
}