From f39b0a6c928fdad18806d8017c3ba6ed5f0404b9 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Wed, 15 May 2019 00:51:21 -0400 Subject: [PATCH] Cleans up parsers a bit --- src/git/gitService.ts | 9 ++++---- src/git/parsers/branchParser.ts | 37 +++++++++--------------------- src/git/parsers/diffParser.ts | 47 +++++++++++++++++---------------------- src/git/parsers/remoteParser.ts | 33 +++++++++++++++------------ src/git/parsers/shortlogParser.ts | 8 +++---- src/git/parsers/tagParser.ts | 18 +++++++-------- src/git/parsers/treeParser.ts | 21 ++++++++++------- 7 files changed, 82 insertions(+), 91 deletions(-) diff --git a/src/git/gitService.ts b/src/git/gitService.ts index 6e2feef..f700a28 100644 --- a/src/git/gitService.ts +++ b/src/git/gitService.ts @@ -1070,7 +1070,8 @@ export class GitService implements Disposable { let key: string; let value: string; - let match: RegExpExecArray | null = null; + + let match: RegExpExecArray | null; do { match = userConfigRegex.exec(data); if (match == null) break; @@ -1237,8 +1238,8 @@ export class GitService implements Disposable { similarityThreshold: Container.config.advanced.similarityThreshold, ...options }); - const diff = GitDiffParser.parseNameStatus(data, repoPath); - return diff; + const files = GitDiffParser.parseNameStatus(data, repoPath); + return files === undefined || files.length === 0 ? undefined : files; } catch (ex) { return undefined; @@ -1995,7 +1996,7 @@ export class GitService implements Disposable { try { const data = await Git.remote(repoPath); - return GitRemoteParser.parse(data, repoPath, RemoteProviderFactory.factory(providers)); + return GitRemoteParser.parse(data, repoPath, RemoteProviderFactory.factory(providers)) || []; } catch (ex) { Logger.error(ex); diff --git a/src/git/parsers/branchParser.ts b/src/git/parsers/branchParser.ts index 006727e..6b79fec 100644 --- a/src/git/parsers/branchParser.ts +++ b/src/git/parsers/branchParser.ts @@ -23,36 +23,21 @@ export class GitBranchParser { if (!data) return branches; - let match: RegExpExecArray | null; - let ahead; - let aheadStr; - let behind; - let behindStr; let current; let name; + let tracking; + let ahead; + let behind; let ref; + let remote; - let tracking; + + let match: RegExpExecArray | null; do { match = branchWithTrackingRegex.exec(data); if (match == null) break; - [, current, name, tracking, aheadStr, behindStr, ref] = match; - if (aheadStr !== undefined && aheadStr.length !== 0) { - ahead = parseInt(aheadStr, 10); - ahead = isNaN(ahead) ? 0 : ahead; - } - else { - ahead = 0; - } - - if (behindStr !== undefined && behindStr.length !== 0) { - behind = parseInt(behindStr, 10); - behind = isNaN(behind) ? 0 : behind; - } - else { - behind = 0; - } + [, current, name, tracking, ahead, behind, ref] = match; if (name.startsWith('refs/remotes/')) { // Strip off refs/remotes/ @@ -72,11 +57,11 @@ export class GitBranchParser { remote, current.charCodeAt(0) === 42, // '*', // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - ref === undefined || ref.length === 0 ? undefined : ` ${ref}`.substr(1), + ref == null || ref.length === 0 ? undefined : ` ${ref}`.substr(1), // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - tracking === undefined || tracking.length === 0 ? undefined : ` ${tracking}`.substr(1), - ahead, - behind + tracking == null || tracking.length === 0 ? undefined : ` ${tracking}`.substr(1), + Number(ahead) || 0, + Number(behind) || 0 ) ); } while (match != null); diff --git a/src/git/parsers/diffParser.ts b/src/git/parsers/diffParser.ts index 70f9d3e..cb6e990 100644 --- a/src/git/parsers/diffParser.ts +++ b/src/git/parsers/diffParser.ts @@ -13,39 +13,33 @@ export class GitDiffParser { const hunks: GitDiffHunk[] = []; - let match: RegExpExecArray | null; - let hunk; - let currentStartStr; - let currentStart; - let currentCountStr; - let currentCount; - let previousStartStr; let previousStart; - let previousCountStr; let previousCount; + let currentStart; + let currentCount; + let hunk; + + let match: RegExpExecArray | null; do { match = unifiedDiffRegex.exec(`${data}\n@@`); if (match == null) break; - // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - hunk = ` ${match[5]}`.substr(1); + [, previousStart, previousCount, currentStart, currentCount, hunk] = match; - [, previousStartStr, previousCountStr, currentStartStr, currentCountStr] = match; - previousStart = parseInt(previousStartStr, 10); - previousCount = previousCountStr ? parseInt(previousCountStr, 10) : 0; - currentStart = parseInt(currentStartStr, 10); - currentCount = currentCountStr ? parseInt(currentCountStr, 10) : 0; + previousStart = Number(previousStart) || 0; + currentStart = Number(currentStart) || 0; hunks.push( new GitDiffHunk( - hunk, + // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 + ` ${hunk}`.substr(1), { start: currentStart, - end: currentStart + currentCount + end: currentStart + (Number(currentCount) || 0) }, { start: previousStart, - end: previousStart + previousCount + end: previousStart + (Number(previousCount) || 0) } ) ); @@ -130,18 +124,17 @@ export class GitDiffParser { const files: GitFile[] = []; - let rawStatus: string; + let status: string; let fileName: string; let originalFileName: string; - let match: RegExpExecArray | null = null; + + let match: RegExpExecArray | null; do { match = nameStatusDiffRegex.exec(data); if (match == null) break; - [, rawStatus, fileName, originalFileName] = match; + [, status, fileName, originalFileName] = match; - // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - const status = ` ${rawStatus}`.substr(1); files.push({ repoPath: repoPath, status: (status[0] !== '.' ? status[0].trim() : '?') as GitFileStatus, @@ -150,12 +143,13 @@ export class GitDiffParser { // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 fileName: ` ${fileName}`.substr(1), // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - originalFileName: originalFileName === undefined ? undefined : ` ${originalFileName}`.substr(1) + originalFileName: + originalFileName == null || originalFileName.length === 0 + ? undefined + : ` ${originalFileName}`.substr(1) }); } while (match != null); - if (!files.length) return undefined; - return files; } @@ -173,6 +167,7 @@ export class GitDiffParser { insertions: insertions == null ? 0 : parseInt(insertions, 10), deletions: deletions == null ? 0 : parseInt(deletions, 10) }; + return diffShortStat; } } diff --git a/src/git/parsers/remoteParser.ts b/src/git/parsers/remoteParser.ts index 3f76f0f..546024d 100644 --- a/src/git/parsers/remoteParser.ts +++ b/src/git/parsers/remoteParser.ts @@ -53,25 +53,32 @@ export class GitRemoteParser { data: string, repoPath: string, providerFactory: (domain: string, path: string) => RemoteProvider | undefined - ): GitRemote[] { - if (!data) return []; + ): GitRemote[] | undefined { + if (!data) return undefined; const remotes: GitRemote[] = []; const groups = Object.create(null); - let url: string; - let scheme: string; - let domain: string; - let path: string; - let uniqueness: string; + let name; + let url; + let type; + + let scheme; + let domain; + let path; + + let uniqueness; let remote: GitRemote | undefined; - let match: RegExpExecArray | null = null; + + let match: RegExpExecArray | null; do { match = remoteRegex.exec(data); if (match == null) break; + [, name, url, type] = match; + // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - url = ` ${match[2]}`.substr(1); + url = ` ${url}`.substr(1); [scheme, domain, path] = this.parseGitUrl(url); @@ -84,25 +91,23 @@ export class GitRemoteParser { repoPath, uniqueness, // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - ` ${match[1]}`.substr(1), + ` ${name}`.substr(1), scheme, provider !== undefined ? provider.domain : domain, provider !== undefined ? provider.path : path, provider, // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - [{ url: url, type: ` ${match[3]}`.substr(1) as GitRemoteType }] + [{ url: url, type: ` ${type}`.substr(1) as GitRemoteType }] ); remotes.push(remote); groups[uniqueness] = remote; } else { // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - remote.types.push({ url: url, type: ` ${match[3]}`.substr(1) as GitRemoteType }); + remote.types.push({ url: url, type: ` ${type}`.substr(1) as GitRemoteType }); } } while (match != null); - if (!remotes.length) return []; - return remotes; } diff --git a/src/git/parsers/shortlogParser.ts b/src/git/parsers/shortlogParser.ts index 5f902e4..56b113e 100644 --- a/src/git/parsers/shortlogParser.ts +++ b/src/git/parsers/shortlogParser.ts @@ -14,12 +14,14 @@ export class GitShortLogParser { let count; let name; let email; - let match: RegExpExecArray | null = null; + + let match: RegExpExecArray | null; do { match = shortlogRegex.exec(data); if (match == null) break; [, count, name, email] = match; + contributors.push( new GitContributor( repoPath, @@ -27,13 +29,11 @@ export class GitShortLogParser { ` ${name}`.substr(1), // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 ` ${email}`.substr(1), - parseInt(count, 10) + Number(count) || 0 ) ); } while (match != null); - if (!contributors.length) return undefined; - return { repoPath: repoPath, contributors: contributors }; } } diff --git a/src/git/parsers/tagParser.ts b/src/git/parsers/tagParser.ts index de33824..9a8de0f 100644 --- a/src/git/parsers/tagParser.ts +++ b/src/git/parsers/tagParser.ts @@ -12,14 +12,16 @@ export class GitTagParser { const tags: GitTag[] = []; - let annotation; let name; - let match: RegExpExecArray | null = null; + let annotation; + + let match: RegExpExecArray | null; do { match = tagWithAnnotationRegex.exec(data); if (match == null) break; [, name, annotation] = match; + tags.push( new GitTag( repoPath, @@ -27,13 +29,11 @@ export class GitTagParser { ` ${name}`.substr(1), undefined, // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - annotation === undefined ? undefined : ` ${annotation}`.substr(1) + annotation == null || annotation.length === 0 ? undefined : ` ${annotation}`.substr(1) ) ); } while (match != null); - if (!tags.length) return undefined; - return tags; } @@ -42,14 +42,16 @@ export class GitTagParser { const tags: GitTag[] = []; - let name; let sha; - let match: RegExpExecArray | null = null; + let name; + + let match: RegExpExecArray | null; do { match = tagWithRefRegex.exec(data); if (match == null) break; [, sha, name] = match; + tags.push( new GitTag( repoPath, @@ -61,8 +63,6 @@ export class GitTagParser { ); } while (match != null); - if (!tags.length) return undefined; - return tags; } } diff --git a/src/git/parsers/treeParser.ts b/src/git/parsers/treeParser.ts index 3622694..16d64fa 100644 --- a/src/git/parsers/treeParser.ts +++ b/src/git/parsers/treeParser.ts @@ -12,24 +12,29 @@ export class GitTreeParser { const trees: GitTree[] = []; - let match: RegExpExecArray | null = null; + let type; + let sha; + let size; + let filePath; + + let match: RegExpExecArray | null; do { match = treeRegex.exec(data); if (match == null) break; - const [, type, commitSha, size, filePath] = match; + [, type, sha, size, filePath] = match; + trees.push({ // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - commitSha: commitSha === undefined ? emptyStr : ` ${commitSha}`.substr(1), - path: filePath === undefined ? emptyStr : filePath, - size: size === '-' ? 0 : Number(size || 0), + commitSha: sha == null || sha.length === 0 ? emptyStr : ` ${sha}`.substr(1), // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 - type: (type === undefined ? emptyStr : ` ${type}`.substr(1)) as 'blob' | 'tree' + path: filePath == null || filePath.length === 0 ? emptyStr : ` ${filePath}`.substr(1), + size: Number(size) || 0, + // Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869 + type: (type == null || type.length === 0 ? emptyStr : ` ${type}`.substr(1)) as 'blob' | 'tree' }); } while (match != null); - if (!trees.length) return undefined; - return trees; } }