Git - cleanup getRemotes() (#169839)

* Git - cleanup getRemotes()

* Remove test for read-only remote as the code to determine that a remote is read-only has been moved out of the function that does the parsing
This commit is contained in:
Ladislau Szomoru 2023-01-09 14:24:35 +01:00 committed by GitHub
parent ed8b63d8bf
commit 65962c405e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 32 deletions

View file

@ -834,8 +834,8 @@ export function parseGitmodules(raw: string): Submodule[] {
return result;
}
export function parseGitRemotes(raw: string): Remote[] {
const remotes: Remote[] = [];
export function parseGitRemotes(raw: string): MutableRemote[] {
const remotes: MutableRemote[] = [];
for (const remoteSection of GitConfigParser.parse(raw).filter(s => s.name === 'remote')) {
if (remoteSection.subSectionName) {
@ -843,8 +843,7 @@ export function parseGitRemotes(raw: string): Remote[] {
name: remoteSection.subSectionName,
fetchUrl: remoteSection.properties['url'],
pushUrl: remoteSection.properties['pushurl'] ?? remoteSection.properties['url'],
// https://github.com/microsoft/vscode/issues/45271
isReadOnly: remoteSection.properties['pushurl'] === 'no_push'
isReadOnly: false
});
}
}
@ -2262,23 +2261,41 @@ export class Repository {
}
async getRemotes(): Promise<Remote[]> {
const remotes: MutableRemote[] = [];
try {
// Attempt to parse the config file
const remotes = await this.getRemotesFS();
if (remotes.length === 0) {
throw new Error('No remotes found in the git config file.');
}
remotes.push(...await this.getRemotesFS());
return remotes;
if (remotes.length === 0) {
this.logger.info('No remotes found in the git config file.');
}
}
catch (err) {
this.logger.warn(err.message);
this.logger.warn(`getRemotes() - ${err.message}`);
// Fallback to using git to get the remotes
remotes.push(...await this.getRemotesGit());
}
// Fallback to using git to determine remotes
for (const remote of remotes) {
// https://github.com/microsoft/vscode/issues/45271
remote.isReadOnly = remote.pushUrl === undefined || remote.pushUrl === 'no_push';
}
return remotes;
}
private async getRemotesFS(): Promise<MutableRemote[]> {
const raw = await fs.readFile(path.join(this.dotGit.commonPath ?? this.dotGit.path, 'config'), 'utf8');
return parseGitRemotes(raw);
}
private async getRemotesGit(): Promise<MutableRemote[]> {
const remotes: MutableRemote[] = [];
const result = await this.exec(['remote', '--verbose']);
const lines = result.stdout.trim().split('\n').filter(l => !!l);
const remotes: MutableRemote[] = [];
for (const line of lines) {
const parts = line.split(/\s/);
@ -2299,19 +2316,11 @@ export class Repository {
remote.fetchUrl = url;
remote.pushUrl = url;
}
// https://github.com/microsoft/vscode/issues/45271
remote.isReadOnly = remote.pushUrl === undefined || remote.pushUrl === 'no_push';
}
return remotes;
}
private async getRemotesFS(): Promise<Remote[]> {
const raw = await fs.readFile(path.join(this.dotGit.commonPath ?? this.dotGit.path, 'config'), 'utf8');
return parseGitRemotes(raw);
}
async getBranch(name: string): Promise<Branch> {
if (name === 'HEAD') {
return this.getHEAD();

View file

@ -213,18 +213,6 @@ suite('git', () => {
]);
});
test('single remote (read-only)', () => {
const sample = `[remote "origin"]
url = https://github.com/microsoft/vscode.git
fetch = +refs/heads/*:refs/remotes/origin/*
pushurl = no_push
`;
assert.deepStrictEqual(parseGitRemotes(sample), [
{ name: 'origin', fetchUrl: 'https://github.com/microsoft/vscode.git', pushUrl: 'no_push', isReadOnly: true }
]);
});
test('single remote (multiple urls)', () => {
const sample = `[remote "origin"]
url = https://github.com/microsoft/vscode.git