Skip to content

Commit 566e71b

Browse files
authored
Revert "Git - parse config file for remotes (microsoft#164151)" (microsoft#164433)
This reverts commit 798cd3b.
1 parent 0b6f9df commit 566e71b

File tree

2 files changed

+34
-172
lines changed

2 files changed

+34
-172
lines changed

extensions/git/src/git.ts

Lines changed: 33 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -777,96 +777,59 @@ export interface Submodule {
777777
}
778778

779779
export function parseGitmodules(raw: string): Submodule[] {
780-
const result: Submodule[] = [];
781-
782-
for (const submoduleSection of parseGitConfig(raw, 'submodule')) {
783-
if (submoduleSection.label && submoduleSection.properties['path'] && submoduleSection.properties['url']) {
784-
result.push({
785-
name: submoduleSection.label,
786-
path: submoduleSection.properties['path'],
787-
url: submoduleSection.properties['url']
788-
});
789-
}
790-
}
791-
792-
return result;
793-
}
794-
795-
interface GitConfigSection {
796-
label?: string;
797-
properties: { [key: string]: string };
798-
}
799-
800-
function parseGitConfig(raw: string, section: string): Iterable<GitConfigSection> {
801-
const sections: GitConfigSection[] = [];
802-
803-
const sectionHeaderRegex = /^\[.+\]$/gm;
804-
const sectionRegex = new RegExp(`^\\s*\\[\\s*${section}\\s*("[^"]+")*\\]$`, 'm');
780+
const regex = /\r?\n/g;
781+
let position = 0;
782+
let match: RegExpExecArray | null = null;
805783

806-
const parseSectionProperties = (sectionRaw: string): { [key: string]: string } => {
807-
const properties: { [key: string]: string } = {};
784+
const result: Submodule[] = [];
785+
let submodule: Partial<Submodule> = {};
808786

809-
for (const propertyLine of sectionRaw.split(/\r?\n/)) {
810-
const propertyMatch = /^\s*(\w+)\s*=\s*(.*)$/.exec(propertyLine);
787+
function parseLine(line: string): void {
788+
const sectionMatch = /^\s*\[submodule "([^"]+)"\]\s*$/.exec(line);
811789

812-
if (!propertyMatch) {
813-
continue;
790+
if (sectionMatch) {
791+
if (submodule.name && submodule.path && submodule.url) {
792+
result.push(submodule as Submodule);
814793
}
815794

816-
const [, key, value] = propertyMatch;
795+
const name = sectionMatch[1];
817796

818-
if (properties[key]) {
819-
continue;
797+
if (name) {
798+
submodule = { name };
799+
return;
820800
}
821-
properties[key] = value;
822801
}
823802

824-
return properties;
825-
};
826-
827-
const parseSection = (sectionRaw: string) => {
828-
const sectionMatch = sectionRegex.exec(sectionRaw);
829-
if (!sectionMatch) {
803+
if (!submodule) {
830804
return;
831805
}
832806

833-
sections.push({
834-
label: sectionMatch.length === 2 ? sectionMatch[1].replaceAll('"', '') : undefined,
835-
properties: parseSectionProperties(sectionRaw.substring(sectionMatch[0].length))
836-
});
837-
};
807+
const propertyMatch = /^\s*(\w+)\s*=\s*(.*)$/.exec(line);
838808

839-
let position = 0;
840-
let match: RegExpExecArray | null = null;
809+
if (!propertyMatch) {
810+
return;
811+
}
841812

842-
while (match = sectionHeaderRegex.exec(raw)) {
843-
parseSection(raw.substring(position, match.index));
844-
position = match.index;
845-
}
846-
parseSection(raw.substring(position));
813+
const [, key, value] = propertyMatch;
847814

848-
return sections;
849-
}
815+
switch (key) {
816+
case 'path': submodule.path = value; break;
817+
case 'url': submodule.url = value; break;
818+
}
819+
}
850820

851-
export function parseGitRemotes(raw: string): Remote[] {
852-
const remotes: Remote[] = [];
821+
while (match = regex.exec(raw)) {
822+
parseLine(raw.substring(position, match.index));
823+
position = match.index + match[0].length;
824+
}
853825

854-
// Remote sections
855-
for (const remoteSection of parseGitConfig(raw, 'remote')) {
856-
if (!remoteSection.label) {
857-
continue;
858-
}
826+
parseLine(raw.substring(position));
859827

860-
remotes.push({
861-
name: remoteSection.label,
862-
fetchUrl: remoteSection.properties['url'],
863-
pushUrl: remoteSection.properties['pushurl'] ?? remoteSection.properties['url'],
864-
// https://github.com/microsoft/vscode/issues/45271
865-
isReadOnly: remoteSection.properties['pushurl'] === 'no_push'
866-
});
828+
if (submodule.name && submodule.path && submodule.url) {
829+
result.push(submodule as Submodule);
867830
}
868831

869-
return remotes;
832+
return result;
870833
}
871834

872835
const commitRegex = /([0-9a-f]{40})\n(.*)\n(.*)\n(.*)\n(.*)\n(.*)\n(.*)(?:\n([^]*?))?(?:\x00)/gm;
@@ -2201,20 +2164,6 @@ export class Repository {
22012164
}
22022165

22032166
async getRemotes(): Promise<Remote[]> {
2204-
try {
2205-
// Attempt to parse the config file
2206-
const remotes = await this.getRemotesFS();
2207-
if (remotes.length === 0) {
2208-
throw new Error('No remotes found in the git config file.');
2209-
}
2210-
2211-
return remotes;
2212-
}
2213-
catch (err) {
2214-
this.logger.warn(err.message);
2215-
}
2216-
2217-
// Fallback to using git to determine remotes
22182167
const result = await this.exec(['remote', '--verbose']);
22192168
const lines = result.stdout.trim().split('\n').filter(l => !!l);
22202169
const remotes: MutableRemote[] = [];
@@ -2246,10 +2195,6 @@ export class Repository {
22462195
return remotes;
22472196
}
22482197

2249-
async getRemotesFS(): Promise<Remote[]> {
2250-
return parseGitRemotes(await fs.readFile(path.join(this.dotGit.commonPath ?? this.dotGit.path, 'config'), 'utf8'));
2251-
}
2252-
22532198
async getBranch(name: string): Promise<Branch> {
22542199
if (name === 'HEAD') {
22552200
return this.getHEAD();

extensions/git/src/test/git.test.ts

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import 'mocha';
7-
import { GitStatusParser, parseGitCommits, parseGitmodules, parseLsTree, parseLsFiles, parseGitRemotes } from '../git';
7+
import { GitStatusParser, parseGitCommits, parseGitmodules, parseLsTree, parseLsFiles } from '../git';
88
import * as assert from 'assert';
99
import { splitInChunks } from '../util';
1010

@@ -265,89 +265,6 @@ This is a commit message.\x00`;
265265
});
266266
});
267267

268-
suite('parseGitRemotes', () => {
269-
test('empty', () => {
270-
assert.deepStrictEqual(parseGitRemotes(''), []);
271-
});
272-
273-
test('single remote', () => {
274-
const sample = `[remote "origin"]
275-
url = https://github.com/microsoft/vscode.git
276-
fetch = +refs/heads/*:refs/remotes/origin/*
277-
`;
278-
279-
assert.deepStrictEqual(parseGitRemotes(sample), [
280-
{ name: 'origin', fetchUrl: 'https://github.com/microsoft/vscode.git', pushUrl: 'https://github.com/microsoft/vscode.git', isReadOnly: false }
281-
]);
282-
});
283-
284-
test('single remote (read-only)', () => {
285-
const sample = `[remote "origin"]
286-
url = https://github.com/microsoft/vscode.git
287-
fetch = +refs/heads/*:refs/remotes/origin/*
288-
pushurl = no_push
289-
`;
290-
291-
assert.deepStrictEqual(parseGitRemotes(sample), [
292-
{ name: 'origin', fetchUrl: 'https://github.com/microsoft/vscode.git', pushUrl: 'no_push', isReadOnly: true }
293-
]);
294-
});
295-
296-
test('single remote (multiple urls)', () => {
297-
const sample = `[remote "origin"]
298-
url = https://github.com/microsoft/vscode.git
299-
url = https://github.com/microsoft/vscode2.git
300-
fetch = +refs/heads/*:refs/remotes/origin/*
301-
`;
302-
303-
assert.deepStrictEqual(parseGitRemotes(sample), [
304-
{ name: 'origin', fetchUrl: 'https://github.com/microsoft/vscode.git', pushUrl: 'https://github.com/microsoft/vscode.git', isReadOnly: false }
305-
]);
306-
});
307-
308-
test('multiple remotes', () => {
309-
const sample = `[remote "origin"]
310-
url = https://github.com/microsoft/vscode.git
311-
pushurl = https://github.com/microsoft/vscode1.git
312-
fetch = +refs/heads/*:refs/remotes/origin/*
313-
[remote "remote2"]
314-
url = https://github.com/microsoft/vscode2.git
315-
fetch = +refs/heads/*:refs/remotes/origin/*
316-
`;
317-
318-
assert.deepStrictEqual(parseGitRemotes(sample), [
319-
{ name: 'origin', fetchUrl: 'https://github.com/microsoft/vscode.git', pushUrl: 'https://github.com/microsoft/vscode1.git', isReadOnly: false },
320-
{ name: 'remote2', fetchUrl: 'https://github.com/microsoft/vscode2.git', pushUrl: 'https://github.com/microsoft/vscode2.git', isReadOnly: false }
321-
]);
322-
});
323-
324-
test('remotes (white space)', () => {
325-
const sample = ` [remote "origin"]
326-
url = https://github.com/microsoft/vscode.git
327-
pushurl=https://github.com/microsoft/vscode1.git
328-
fetch = +refs/heads/*:refs/remotes/origin/*
329-
[ remote"remote2"]
330-
url = https://github.com/microsoft/vscode2.git
331-
fetch = +refs/heads/*:refs/remotes/origin/*
332-
`;
333-
334-
assert.deepStrictEqual(parseGitRemotes(sample), [
335-
{ name: 'origin', fetchUrl: 'https://github.com/microsoft/vscode.git', pushUrl: 'https://github.com/microsoft/vscode1.git', isReadOnly: false },
336-
{ name: 'remote2', fetchUrl: 'https://github.com/microsoft/vscode2.git', pushUrl: 'https://github.com/microsoft/vscode2.git', isReadOnly: false }
337-
]);
338-
});
339-
340-
test('remotes (invalid section)', () => {
341-
const sample = `[remote "origin"
342-
url = https://github.com/microsoft/vscode.git
343-
pushurl = https://github.com/microsoft/vscode1.git
344-
fetch = +refs/heads/*:refs/remotes/origin/*
345-
`;
346-
347-
assert.deepStrictEqual(parseGitRemotes(sample), []);
348-
});
349-
});
350-
351268
suite('parseLsTree', function () {
352269
test('sample', function () {
353270
const input = `040000 tree 0274a81f8ee9ca3669295dc40f510bd2021d0043 - .vscode

0 commit comments

Comments
 (0)