Git - remove hard coded remote name when detecting the default branch (#237423)

pull/237404/head
Ladislau Szomoru 2025-01-07 20:07:07 +01:00 committed by GitHub
parent e2e9a7306b
commit cbb7f99ba8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 3 deletions

View File

@ -2779,8 +2779,8 @@ export class Repository {
return Promise.reject<Branch>(new Error(`No such branch: ${name}.`));
}
async getDefaultBranch(): Promise<Branch> {
const result = await this.exec(['symbolic-ref', '--short', 'refs/remotes/origin/HEAD']);
async getDefaultBranch(remoteName: string): Promise<Branch> {
const result = await this.exec(['symbolic-ref', '--short', `refs/remotes/${remoteName}/HEAD`]);
if (!result.stdout || result.stderr) {
throw new Error('No default branch');
}

View File

@ -1593,7 +1593,12 @@ export class Repository implements Disposable {
private async getDefaultBranch(): Promise<Branch | undefined> {
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) {