Skip to content

Commit f39b0a6

Browse files
committed
Cleans up parsers a bit
1 parent 1d65dde commit f39b0a6

File tree

7 files changed

+82
-91
lines changed

7 files changed

+82
-91
lines changed

src/git/gitService.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,8 @@ export class GitService implements Disposable {
10701070

10711071
let key: string;
10721072
let value: string;
1073-
let match: RegExpExecArray | null = null;
1073+
1074+
let match: RegExpExecArray | null;
10741075
do {
10751076
match = userConfigRegex.exec(data);
10761077
if (match == null) break;
@@ -1237,8 +1238,8 @@ export class GitService implements Disposable {
12371238
similarityThreshold: Container.config.advanced.similarityThreshold,
12381239
...options
12391240
});
1240-
const diff = GitDiffParser.parseNameStatus(data, repoPath);
1241-
return diff;
1241+
const files = GitDiffParser.parseNameStatus(data, repoPath);
1242+
return files === undefined || files.length === 0 ? undefined : files;
12421243
}
12431244
catch (ex) {
12441245
return undefined;
@@ -1995,7 +1996,7 @@ export class GitService implements Disposable {
19951996

19961997
try {
19971998
const data = await Git.remote(repoPath);
1998-
return GitRemoteParser.parse(data, repoPath, RemoteProviderFactory.factory(providers));
1999+
return GitRemoteParser.parse(data, repoPath, RemoteProviderFactory.factory(providers)) || [];
19992000
}
20002001
catch (ex) {
20012002
Logger.error(ex);

src/git/parsers/branchParser.ts

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,21 @@ export class GitBranchParser {
2323

2424
if (!data) return branches;
2525

26-
let match: RegExpExecArray | null;
27-
let ahead;
28-
let aheadStr;
29-
let behind;
30-
let behindStr;
3126
let current;
3227
let name;
28+
let tracking;
29+
let ahead;
30+
let behind;
3331
let ref;
32+
3433
let remote;
35-
let tracking;
34+
35+
let match: RegExpExecArray | null;
3636
do {
3737
match = branchWithTrackingRegex.exec(data);
3838
if (match == null) break;
3939

40-
[, current, name, tracking, aheadStr, behindStr, ref] = match;
41-
if (aheadStr !== undefined && aheadStr.length !== 0) {
42-
ahead = parseInt(aheadStr, 10);
43-
ahead = isNaN(ahead) ? 0 : ahead;
44-
}
45-
else {
46-
ahead = 0;
47-
}
48-
49-
if (behindStr !== undefined && behindStr.length !== 0) {
50-
behind = parseInt(behindStr, 10);
51-
behind = isNaN(behind) ? 0 : behind;
52-
}
53-
else {
54-
behind = 0;
55-
}
40+
[, current, name, tracking, ahead, behind, ref] = match;
5641

5742
if (name.startsWith('refs/remotes/')) {
5843
// Strip off refs/remotes/
@@ -72,11 +57,11 @@ export class GitBranchParser {
7257
remote,
7358
current.charCodeAt(0) === 42, // '*',
7459
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
75-
ref === undefined || ref.length === 0 ? undefined : ` ${ref}`.substr(1),
60+
ref == null || ref.length === 0 ? undefined : ` ${ref}`.substr(1),
7661
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
77-
tracking === undefined || tracking.length === 0 ? undefined : ` ${tracking}`.substr(1),
78-
ahead,
79-
behind
62+
tracking == null || tracking.length === 0 ? undefined : ` ${tracking}`.substr(1),
63+
Number(ahead) || 0,
64+
Number(behind) || 0
8065
)
8166
);
8267
} while (match != null);

src/git/parsers/diffParser.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,33 @@ export class GitDiffParser {
1313

1414
const hunks: GitDiffHunk[] = [];
1515

16-
let match: RegExpExecArray | null;
17-
let hunk;
18-
let currentStartStr;
19-
let currentStart;
20-
let currentCountStr;
21-
let currentCount;
22-
let previousStartStr;
2316
let previousStart;
24-
let previousCountStr;
2517
let previousCount;
18+
let currentStart;
19+
let currentCount;
20+
let hunk;
21+
22+
let match: RegExpExecArray | null;
2623
do {
2724
match = unifiedDiffRegex.exec(`${data}\n@@`);
2825
if (match == null) break;
2926

30-
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
31-
hunk = ` ${match[5]}`.substr(1);
27+
[, previousStart, previousCount, currentStart, currentCount, hunk] = match;
3228

33-
[, previousStartStr, previousCountStr, currentStartStr, currentCountStr] = match;
34-
previousStart = parseInt(previousStartStr, 10);
35-
previousCount = previousCountStr ? parseInt(previousCountStr, 10) : 0;
36-
currentStart = parseInt(currentStartStr, 10);
37-
currentCount = currentCountStr ? parseInt(currentCountStr, 10) : 0;
29+
previousStart = Number(previousStart) || 0;
30+
currentStart = Number(currentStart) || 0;
3831

3932
hunks.push(
4033
new GitDiffHunk(
41-
hunk,
34+
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
35+
` ${hunk}`.substr(1),
4236
{
4337
start: currentStart,
44-
end: currentStart + currentCount
38+
end: currentStart + (Number(currentCount) || 0)
4539
},
4640
{
4741
start: previousStart,
48-
end: previousStart + previousCount
42+
end: previousStart + (Number(previousCount) || 0)
4943
}
5044
)
5145
);
@@ -130,18 +124,17 @@ export class GitDiffParser {
130124

131125
const files: GitFile[] = [];
132126

133-
let rawStatus: string;
127+
let status: string;
134128
let fileName: string;
135129
let originalFileName: string;
136-
let match: RegExpExecArray | null = null;
130+
131+
let match: RegExpExecArray | null;
137132
do {
138133
match = nameStatusDiffRegex.exec(data);
139134
if (match == null) break;
140135

141-
[, rawStatus, fileName, originalFileName] = match;
136+
[, status, fileName, originalFileName] = match;
142137

143-
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
144-
const status = ` ${rawStatus}`.substr(1);
145138
files.push({
146139
repoPath: repoPath,
147140
status: (status[0] !== '.' ? status[0].trim() : '?') as GitFileStatus,
@@ -150,12 +143,13 @@ export class GitDiffParser {
150143
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
151144
fileName: ` ${fileName}`.substr(1),
152145
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
153-
originalFileName: originalFileName === undefined ? undefined : ` ${originalFileName}`.substr(1)
146+
originalFileName:
147+
originalFileName == null || originalFileName.length === 0
148+
? undefined
149+
: ` ${originalFileName}`.substr(1)
154150
});
155151
} while (match != null);
156152

157-
if (!files.length) return undefined;
158-
159153
return files;
160154
}
161155

@@ -173,6 +167,7 @@ export class GitDiffParser {
173167
insertions: insertions == null ? 0 : parseInt(insertions, 10),
174168
deletions: deletions == null ? 0 : parseInt(deletions, 10)
175169
};
170+
176171
return diffShortStat;
177172
}
178173
}

src/git/parsers/remoteParser.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,32 @@ export class GitRemoteParser {
5353
data: string,
5454
repoPath: string,
5555
providerFactory: (domain: string, path: string) => RemoteProvider | undefined
56-
): GitRemote[] {
57-
if (!data) return [];
56+
): GitRemote[] | undefined {
57+
if (!data) return undefined;
5858

5959
const remotes: GitRemote[] = [];
6060
const groups = Object.create(null);
6161

62-
let url: string;
63-
let scheme: string;
64-
let domain: string;
65-
let path: string;
66-
let uniqueness: string;
62+
let name;
63+
let url;
64+
let type;
65+
66+
let scheme;
67+
let domain;
68+
let path;
69+
70+
let uniqueness;
6771
let remote: GitRemote | undefined;
68-
let match: RegExpExecArray | null = null;
72+
73+
let match: RegExpExecArray | null;
6974
do {
7075
match = remoteRegex.exec(data);
7176
if (match == null) break;
7277

78+
[, name, url, type] = match;
79+
7380
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
74-
url = ` ${match[2]}`.substr(1);
81+
url = ` ${url}`.substr(1);
7582

7683
[scheme, domain, path] = this.parseGitUrl(url);
7784

@@ -84,25 +91,23 @@ export class GitRemoteParser {
8491
repoPath,
8592
uniqueness,
8693
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
87-
` ${match[1]}`.substr(1),
94+
` ${name}`.substr(1),
8895
scheme,
8996
provider !== undefined ? provider.domain : domain,
9097
provider !== undefined ? provider.path : path,
9198
provider,
9299
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
93-
[{ url: url, type: ` ${match[3]}`.substr(1) as GitRemoteType }]
100+
[{ url: url, type: ` ${type}`.substr(1) as GitRemoteType }]
94101
);
95102
remotes.push(remote);
96103
groups[uniqueness] = remote;
97104
}
98105
else {
99106
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
100-
remote.types.push({ url: url, type: ` ${match[3]}`.substr(1) as GitRemoteType });
107+
remote.types.push({ url: url, type: ` ${type}`.substr(1) as GitRemoteType });
101108
}
102109
} while (match != null);
103110

104-
if (!remotes.length) return [];
105-
106111
return remotes;
107112
}
108113

src/git/parsers/shortlogParser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ export class GitShortLogParser {
1414
let count;
1515
let name;
1616
let email;
17-
let match: RegExpExecArray | null = null;
17+
18+
let match: RegExpExecArray | null;
1819
do {
1920
match = shortlogRegex.exec(data);
2021
if (match == null) break;
2122

2223
[, count, name, email] = match;
24+
2325
contributors.push(
2426
new GitContributor(
2527
repoPath,
2628
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
2729
` ${name}`.substr(1),
2830
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
2931
` ${email}`.substr(1),
30-
parseInt(count, 10)
32+
Number(count) || 0
3133
)
3234
);
3335
} while (match != null);
3436

35-
if (!contributors.length) return undefined;
36-
3737
return { repoPath: repoPath, contributors: contributors };
3838
}
3939
}

src/git/parsers/tagParser.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,28 @@ export class GitTagParser {
1212

1313
const tags: GitTag[] = [];
1414

15-
let annotation;
1615
let name;
17-
let match: RegExpExecArray | null = null;
16+
let annotation;
17+
18+
let match: RegExpExecArray | null;
1819
do {
1920
match = tagWithAnnotationRegex.exec(data);
2021
if (match == null) break;
2122

2223
[, name, annotation] = match;
24+
2325
tags.push(
2426
new GitTag(
2527
repoPath,
2628
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
2729
` ${name}`.substr(1),
2830
undefined,
2931
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
30-
annotation === undefined ? undefined : ` ${annotation}`.substr(1)
32+
annotation == null || annotation.length === 0 ? undefined : ` ${annotation}`.substr(1)
3133
)
3234
);
3335
} while (match != null);
3436

35-
if (!tags.length) return undefined;
36-
3737
return tags;
3838
}
3939

@@ -42,14 +42,16 @@ export class GitTagParser {
4242

4343
const tags: GitTag[] = [];
4444

45-
let name;
4645
let sha;
47-
let match: RegExpExecArray | null = null;
46+
let name;
47+
48+
let match: RegExpExecArray | null;
4849
do {
4950
match = tagWithRefRegex.exec(data);
5051
if (match == null) break;
5152

5253
[, sha, name] = match;
54+
5355
tags.push(
5456
new GitTag(
5557
repoPath,
@@ -61,8 +63,6 @@ export class GitTagParser {
6163
);
6264
} while (match != null);
6365

64-
if (!tags.length) return undefined;
65-
6666
return tags;
6767
}
6868
}

src/git/parsers/treeParser.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,29 @@ export class GitTreeParser {
1212

1313
const trees: GitTree[] = [];
1414

15-
let match: RegExpExecArray | null = null;
15+
let type;
16+
let sha;
17+
let size;
18+
let filePath;
19+
20+
let match: RegExpExecArray | null;
1621
do {
1722
match = treeRegex.exec(data);
1823
if (match == null) break;
1924

20-
const [, type, commitSha, size, filePath] = match;
25+
[, type, sha, size, filePath] = match;
26+
2127
trees.push({
2228
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
23-
commitSha: commitSha === undefined ? emptyStr : ` ${commitSha}`.substr(1),
24-
path: filePath === undefined ? emptyStr : filePath,
25-
size: size === '-' ? 0 : Number(size || 0),
29+
commitSha: sha == null || sha.length === 0 ? emptyStr : ` ${sha}`.substr(1),
2630
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
27-
type: (type === undefined ? emptyStr : ` ${type}`.substr(1)) as 'blob' | 'tree'
31+
path: filePath == null || filePath.length === 0 ? emptyStr : ` ${filePath}`.substr(1),
32+
size: Number(size) || 0,
33+
// Stops excessive memory usage -- https://bugs.chromium.org/p/v8/issues/detail?id=2869
34+
type: (type == null || type.length === 0 ? emptyStr : ` ${type}`.substr(1)) as 'blob' | 'tree'
2835
});
2936
} while (match != null);
3037

31-
if (!trees.length) return undefined;
32-
3338
return trees;
3439
}
3540
}

0 commit comments

Comments
 (0)