diff options
author | Ladislau Szomoru <3372902+lszomoru@users.noreply.github.com> | 2022-11-10 20:04:35 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 20:04:35 +0300 |
commit | 624f4ac076f7ced1cbb52c65f281f0f00e4b1421 (patch) | |
tree | 77c2b1b44f4e77706dfb53ca9ee0a26e5f631db1 /extensions | |
parent | ddb50a07c511d1805571b8db52a5326b81f4fa0e (diff) |
Git - Tweak git config parser (#166022)
Tweak git config parser
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/git/src/git.ts | 56 |
1 files changed, 24 insertions, 32 deletions
diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 8a897fcd2d5..5a7660aa6d5 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -695,45 +695,39 @@ interface GitConfigSection { } class GitConfigParser { - private static readonly _lineSeparator = /\r?\n/; + private static readonly _lineSeparator = /\r?\n/g; - private static readonly _commentRegex = /^\s*[#;].*/; - private static readonly _emptyLineRegex = /^\s*$/; private static readonly _propertyRegex = /^\s*(\w+)\s*=\s*(.*)$/; private static readonly _sectionRegex = /^\s*\[\s*([^\]]+?)\s*(\"[^"]+\")*\]\s*$/; - static parse(raw: string, sectionName: string): GitConfigSection[] { - let section: GitConfigSection | undefined; + static parse(raw: string): GitConfigSection[] { const config: { sections: GitConfigSection[] } = { sections: [] }; + let section: GitConfigSection = { name: 'DEFAULT', properties: {} }; const addSection = (section?: GitConfigSection) => { if (!section) { return; } config.sections.push(section); }; - for (const configFileLine of raw.split(GitConfigParser._lineSeparator)) { - // Ignore empty lines and comments - if (GitConfigParser._emptyLineRegex.test(configFileLine) || - GitConfigParser._commentRegex.test(configFileLine)) { - continue; - } + let position = 0; + let match: RegExpExecArray | null = null; - // Section - const sectionMatch = configFileLine.match(GitConfigParser._sectionRegex); + while (match = GitConfigParser._lineSeparator.exec(raw)) { + const line = raw.substring(position, match.index); + position = match.index + match[0].length; + + const sectionMatch = line.match(GitConfigParser._sectionRegex); if (sectionMatch?.length === 3) { addSection(section); - section = sectionMatch[1] === sectionName ? - { name: sectionMatch[1], subSectionName: sectionMatch[2]?.replaceAll('"', ''), properties: {} } : undefined; + section = { name: sectionMatch[1], subSectionName: sectionMatch[2]?.replaceAll('"', ''), properties: {} }; continue; } // Properties - if (section) { - const propertyMatch = configFileLine.match(GitConfigParser._propertyRegex); - if (propertyMatch?.length === 3 && !Object.keys(section.properties).includes(propertyMatch[1])) { - section.properties[propertyMatch[1]] = propertyMatch[2]; - } + const propertyMatch = line.match(GitConfigParser._propertyRegex); + if (propertyMatch?.length === 3 && !Object.keys(section.properties).includes(propertyMatch[1])) { + section.properties[propertyMatch[1]] = propertyMatch[2]; } } @@ -818,7 +812,7 @@ export interface Submodule { export function parseGitmodules(raw: string): Submodule[] { const result: Submodule[] = []; - for (const submoduleSection of GitConfigParser.parse(raw, 'submodule')) { + for (const submoduleSection of GitConfigParser.parse(raw).filter(s => s.name === 'submodule')) { if (submoduleSection.subSectionName && submoduleSection.properties['path'] && submoduleSection.properties['url']) { result.push({ name: submoduleSection.subSectionName, @@ -834,18 +828,16 @@ export function parseGitmodules(raw: string): Submodule[] { export function parseGitRemotes(raw: string): Remote[] { const remotes: Remote[] = []; - for (const remoteSection of GitConfigParser.parse(raw, 'remote')) { - if (!remoteSection.subSectionName) { - continue; + for (const remoteSection of GitConfigParser.parse(raw).filter(s => s.name === 'remote')) { + if (remoteSection.subSectionName) { + remotes.push({ + 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' + }); } - - remotes.push({ - 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' - }); } return remotes; |