From cbb7f99ba8e15177d7c5cc26339707ef8c086d66 Mon Sep 17 00:00:00 2001 From: Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> Date: Tue, 7 Jan 2025 20:07:07 +0100 Subject: [PATCH] Git - remove hard coded remote name when detecting the default branch (#237423) --- extensions/git/src/git.ts | 4 ++-- extensions/git/src/repository.ts | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 327903aa95920..b000245a0f2ec 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -2779,8 +2779,8 @@ export class Repository { return Promise.reject(new Error(`No such branch: ${name}.`)); } - async getDefaultBranch(): Promise { - const result = await this.exec(['symbolic-ref', '--short', 'refs/remotes/origin/HEAD']); + async getDefaultBranch(remoteName: string): Promise { + const result = await this.exec(['symbolic-ref', '--short', `refs/remotes/${remoteName}/HEAD`]); if (!result.stdout || result.stderr) { throw new Error('No default branch'); } diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index 2a722502b75c9..00419ef85fc2b 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -1593,7 +1593,12 @@ export class Repository implements Disposable { private async getDefaultBranch(): Promise { try { - const defaultBranch = await this.repository.getDefaultBranch(); + if (this.remotes.length === 0) { + return undefined; + } + + const remote = this.remotes.find(r => r.name === 'origin') ?? this.remotes[0]; + const defaultBranch = await this.repository.getDefaultBranch(remote.name); return defaultBranch; } catch (err) {