Skip to content

Commit 26cbee6

Browse files
committed
Makes GitUri immutable & memoizes many members
1 parent 25d3895 commit 26cbee6

24 files changed

+116
-107
lines changed

src/annotations/annotations.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
GitBlameCommit,
1818
GitCommit,
1919
GitDiffHunkLine,
20-
GitService,
2120
GitUri
2221
} from '../git/gitService';
2322
import { Objects, Strings } from '../system';
@@ -66,7 +65,7 @@ export class Annotations {
6665
): Promise<MarkdownString | undefined> {
6766
let ref;
6867
if (commit.isUncommitted) {
69-
if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) {
68+
if (uri.isUncommittedStaged) {
7069
ref = uri.sha;
7170
}
7271
}
@@ -101,7 +100,7 @@ export class Annotations {
101100

102101
let message: string;
103102
if (commit.isUncommitted) {
104-
if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) {
103+
if (uri.isUncommittedStaged) {
105104
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(
106105
commit,
107106
editorLine

src/codelens/codeLensProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
220220
}
221221

222222
const fileSymbol = new SymbolInformation(
223-
gitUri.getFilename(),
223+
gitUri.fileName,
224224
SymbolKind.File,
225225
'',
226226
new Location(gitUri.documentUri(), new Range(0, 0, 0, blameRange.start.character))
@@ -247,7 +247,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
247247
}
248248

249249
const fileSymbol = new SymbolInformation(
250-
gitUri.getFilename(),
250+
gitUri.fileName,
251251
SymbolKind.File,
252252
'',
253253
new Location(gitUri.documentUri(), new Range(0, 1, 0, blameRange.start.character))

src/commands/copyMessageToClipboard.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
4141
try {
4242
args = { ...args };
4343

44+
let repoPath;
4445
// If we don't have an editor then get the message of the last commit to the branch
4546
if (uri == null) {
46-
const repoPath = await Container.git.getActiveRepoPath(editor);
47+
repoPath = await Container.git.getActiveRepoPath(editor);
4748
if (!repoPath) return undefined;
4849

4950
const log = await Container.git.getLog(repoPath, { maxCount: 1 });
@@ -53,6 +54,7 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
5354
}
5455
else if (args.message === undefined) {
5556
const gitUri = await GitUri.fromUri(uri);
57+
repoPath = gitUri.repoPath;
5658

5759
if (args.sha === undefined) {
5860
const blameline = (editor && editor.selection.active.line) || 0;
@@ -72,8 +74,8 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
7274
if (blame.commit.isUncommitted) return undefined;
7375

7476
args.sha = blame.commit.sha;
75-
if (!gitUri.repoPath) {
76-
gitUri.repoPath = blame.commit.repoPath;
77+
if (!repoPath) {
78+
repoPath = blame.commit.repoPath;
7779
}
7880
}
7981
catch (ex) {
@@ -83,7 +85,7 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
8385
}
8486

8587
// Get the full commit message -- since blame only returns the summary
86-
const commit = await Container.git.getCommit(gitUri.repoPath!, args.sha);
88+
const commit = await Container.git.getCommit(repoPath!, args.sha);
8789
if (commit === undefined) return undefined;
8890

8991
args.message = commit.message;

src/commands/diffLineWithPrevious.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
import { commands, TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
33
import { Container } from '../container';
4-
import { GitCommit, GitService, GitUri } from '../git/gitService';
4+
import { GitCommit, GitUri } from '../git/gitService';
55
import { Logger } from '../logger';
66
import { Messages } from '../messages';
77
import { ActiveEditorCommand, command, Commands, getCommandUri } from './common';
@@ -31,7 +31,7 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
3131

3232
const gitUri = args.commit !== undefined ? GitUri.fromCommit(args.commit) : await GitUri.fromUri(uri);
3333

34-
if (gitUri.sha === undefined || GitService.isUncommitted(gitUri.sha)) {
34+
if (gitUri.sha === undefined || gitUri.isUncommitted) {
3535
const blame =
3636
editor && editor.document.isDirty
3737
? await Container.git.getBlameForLineContents(gitUri, args.line, editor.document.getText())

src/commands/diffLineWithWorking.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class DiffLineWithWorkingCommand extends ActiveEditorCommand {
3131
args.line = editor == null ? 0 : editor.selection.active.line;
3232
}
3333

34-
if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) {
34+
if (args.commit === undefined || args.commit.isUncommitted) {
3535
const blameline = args.line;
3636
if (blameline < 0) return undefined;
3737

src/commands/diffWithWorking.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,17 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
5050
args.line = editor == null ? 0 : editor.selection.active.line;
5151
}
5252

53-
if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) {
53+
if (args.commit === undefined || args.commit.isUncommitted) {
5454
// If the sha is missing, just let the user know the file matches
5555
if (gitUri.sha === undefined) return window.showInformationMessage('File matches the working tree');
5656
if (gitUri.sha === GitService.deletedOrMissingSha) {
5757
return window.showWarningMessage('Unable to open compare. File has been deleted from the working tree');
5858
}
5959

6060
// If we are a fake "staged" sha, check the status
61-
if (GitService.isUncommittedStaged(gitUri.sha!)) {
62-
gitUri.sha = undefined;
61+
let ref: string | undefined = gitUri.sha;
62+
if (gitUri.isUncommittedStaged) {
63+
ref = undefined;
6364

6465
const status = await Container.git.getStatusForFile(gitUri.repoPath!, gitUri.fsPath);
6566
if (status !== undefined && status.indexStatus !== undefined) {
@@ -83,7 +84,7 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
8384

8485
try {
8586
args.commit = await Container.git.getCommitForFile(gitUri.repoPath, gitUri.fsPath, {
86-
ref: gitUri.sha,
87+
ref: ref,
8788
firstIfNotFound: true
8889
});
8990
if (args.commit === undefined) {
@@ -94,7 +95,7 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
9495
Logger.error(
9596
ex,
9697
'DiffWithWorkingCommand',
97-
`getLogCommit(${gitUri.repoPath}, ${gitUri.fsPath}, ${gitUri.sha})`
98+
`getLogCommit(${gitUri.repoPath}, ${gitUri.fsPath}, ${ref})`
9899
);
99100
return Messages.showGenericErrorMessage('Unable to open compare');
100101
}

src/commands/openFileInRemote.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { commands, Range, TextEditor, Uri, window } from 'vscode';
33
import { GlyphChars } from '../constants';
44
import { Container } from '../container';
5-
import { GitUri, RemoteResourceType } from '../git/gitService';
5+
import { GitService, GitUri, RemoteResourceType } from '../git/gitService';
66
import { Logger } from '../logger';
77
import { CommandQuickPickItem, ReferencesQuickPick } from '../quickpicks';
88
import {
@@ -15,7 +15,6 @@ import {
1515
isCommandViewContextWithCommit
1616
} from './common';
1717
import { OpenInRemoteCommandArgs } from './openInRemote';
18-
import { Git } from '../git/git';
1918
import { Strings } from '../system';
2019

2120
export interface OpenFileInRemoteCommandArgs {
@@ -67,7 +66,7 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand {
6766
: undefined;
6867
let sha = args.sha || gitUri.sha;
6968

70-
if (args.branch === undefined && sha !== undefined && !Git.isSha(sha) && remotes.length !== 0) {
69+
if (args.branch === undefined && sha !== undefined && !GitService.isSha(sha) && remotes.length !== 0) {
7170
const [remotePart, branchPart] = Strings.splitSingle(sha, '/');
7271
if (branchPart !== undefined) {
7372
if (remotes.some(r => r.name === remotePart)) {
@@ -82,10 +81,8 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand {
8281
if (branch === undefined || branch.tracking === undefined) {
8382
const pick = await new ReferencesQuickPick(gitUri.repoPath).show(
8483
args.clipboard
85-
? `Copy url for ${gitUri.getRelativePath()} to clipboard for which branch${
86-
GlyphChars.Ellipsis
87-
}`
88-
: `Open ${gitUri.getRelativePath()} on remote for which branch${GlyphChars.Ellipsis}`,
84+
? `Copy url for ${gitUri.relativePath} to clipboard for which branch${GlyphChars.Ellipsis}`
85+
: `Open ${gitUri.relativePath} on remote for which branch${GlyphChars.Ellipsis}`,
8986
{
9087
autoPick: true,
9188
filters: {
@@ -109,13 +106,13 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand {
109106
? {
110107
type: RemoteResourceType.File,
111108
branch: args.branch || 'HEAD',
112-
fileName: gitUri.getRelativePath(),
109+
fileName: gitUri.relativePath,
113110
range: range
114111
}
115112
: {
116113
type: RemoteResourceType.Revision,
117114
branch: args.branch || 'HEAD',
118-
fileName: gitUri.getRelativePath(),
115+
fileName: gitUri.relativePath,
119116
range: range,
120117
sha: sha
121118
},

src/git/fsProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const emptyArray = new Uint8Array(0);
2121

2222
export function fromGitLensFSUri(uri: Uri): { path: string; ref: string; repoPath: string } {
2323
const gitUri = uri instanceof GitUri ? uri : GitUri.fromRevisionUri(uri);
24-
return { path: gitUri.getRelativePath(), ref: gitUri.sha!, repoPath: gitUri.repoPath! };
24+
return { path: gitUri.relativePath, ref: gitUri.sha!, repoPath: gitUri.repoPath! };
2525
}
2626

2727
export function toGitLensFSUri(ref: string, repoPath: string): Uri {

src/git/git.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,23 @@ export class Git {
236236
}
237237

238238
static isSha(ref: string) {
239-
return Git.shaRegex.test(ref);
239+
return Git.isMatch(Git.shaRegex, ref);
240240
}
241241

242242
static isShaLike(ref: string) {
243-
return Git.shaLikeRegex.test(ref);
243+
return Git.isMatch(Git.shaLikeRegex, ref);
244244
}
245245

246246
static isShaParent(ref: string) {
247-
return Git.shaParentRegex.test(ref);
247+
return Git.isMatch(Git.shaParentRegex, ref);
248248
}
249249

250250
static isUncommitted(ref: string | undefined) {
251-
return ref ? Git.uncommittedRegex.test(ref) : false;
251+
return Git.isMatch(Git.uncommittedRegex, ref);
252252
}
253253

254254
static isUncommittedStaged(ref: string | undefined): boolean {
255-
return ref ? Git.uncommittedStagedRegex.test(ref) : false;
255+
return Git.isMatch(Git.uncommittedStagedRegex, ref);
256256
}
257257

258258
static shortenSha(
@@ -309,6 +309,10 @@ export class Git {
309309
return parseInt(gitMajor, 10) >= major && parseInt(gitMinor, 10) >= minor;
310310
}
311311

312+
private static isMatch(regex: RegExp, ref: string | undefined) {
313+
return ref == null || ref.length === 0 ? false : regex.test(ref);
314+
}
315+
312316
// Git commands
313317

314318
static add(repoPath: string | undefined, pathspec: string) {

src/git/gitService.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,7 +1166,7 @@ export class GitService implements Disposable {
11661166

11671167
try {
11681168
let data;
1169-
if (ref1 !== undefined && ref2 === undefined && !GitService.isUncommittedStaged(ref1)) {
1169+
if (ref1 !== undefined && ref2 === undefined && !Git.isUncommittedStaged(ref1)) {
11701170
data = await Git.show__diff(root, file, ref1, originalFileName, {
11711171
similarityThreshold: Container.config.advanced.similarityThreshold
11721172
});
@@ -1251,7 +1251,7 @@ export class GitService implements Disposable {
12511251

12521252
@log()
12531253
async getFileStatusForCommit(repoPath: string, fileName: string, ref: string): Promise<GitFile | undefined> {
1254-
if (ref === GitService.deletedOrMissingSha || GitService.isUncommitted(ref)) return undefined;
1254+
if (ref === GitService.deletedOrMissingSha || Git.isUncommitted(ref)) return undefined;
12551255

12561256
const data = await Git.show__name_status(repoPath, fileName, ref);
12571257
if (!data) return undefined;
@@ -1636,7 +1636,7 @@ export class GitService implements Disposable {
16361636
// If we have no ref (or staged ref) there is no next commit
16371637
if (ref === undefined || ref.length === 0) return undefined;
16381638

1639-
const fileName = GitUri.getRelativePath(uri, repoPath);
1639+
const fileName = GitUri.relativeTo(uri, repoPath);
16401640

16411641
if (Git.isUncommittedStaged(ref)) {
16421642
return {
@@ -1688,7 +1688,7 @@ export class GitService implements Disposable {
16881688
filters = ['A'];
16891689
}
16901690

1691-
const fileName = GitUri.getRelativePath(uri, repoPath);
1691+
const fileName = GitUri.relativeTo(uri, repoPath);
16921692
let data = await Git.log__file(repoPath, fileName, ref, {
16931693
filters: filters,
16941694
format: GitLogParser.simpleFormat,
@@ -1732,7 +1732,7 @@ export class GitService implements Disposable {
17321732
): Promise<{ current: GitUri; previous: GitUri | undefined } | undefined> {
17331733
if (ref === GitService.deletedOrMissingSha) return undefined;
17341734

1735-
const fileName = GitUri.getRelativePath(uri, repoPath);
1735+
const fileName = GitUri.relativeTo(uri, repoPath);
17361736

17371737
// If the ref is missing (i.e. working tree), check the file status to see if there is anything staged
17381738
if ((ref === undefined || ref.length === 0) && editorLine === undefined) {
@@ -1754,7 +1754,7 @@ export class GitService implements Disposable {
17541754
}
17551755
}
17561756
}
1757-
else if (GitService.isUncommittedStaged(ref)) {
1757+
else if (Git.isUncommittedStaged(ref)) {
17581758
const current =
17591759
skip === 0
17601760
? GitUri.fromFile(fileName, repoPath, ref)
@@ -1796,7 +1796,7 @@ export class GitService implements Disposable {
17961796
skip++;
17971797
}
17981798

1799-
const fileName = GitUri.getRelativePath(uri, repoPath);
1799+
const fileName = GitUri.relativeTo(uri, repoPath);
18001800
const data = await Git.log__file(repoPath, fileName, ref, {
18011801
format: GitLogParser.simpleFormat,
18021802
maxCount: skip + 1,
@@ -2146,7 +2146,7 @@ export class GitService implements Disposable {
21462146

21472147
@log()
21482148
async getWorkingUri(repoPath: string, uri: Uri) {
2149-
let fileName = GitUri.getRelativePath(uri, repoPath);
2149+
let fileName = GitUri.relativeTo(uri, repoPath);
21502150

21512151
let data;
21522152
let ref;
@@ -2438,6 +2438,7 @@ export class GitService implements Disposable {
24382438
static deletedOrMissingSha = Git.deletedOrMissingSha;
24392439
static getGitPath = Git.getGitPath;
24402440
static getGitVersion = Git.getGitVersion;
2441+
static isSha = Git.isSha;
24412442
static isShaLike = Git.isShaLike;
24422443
static isShaParent = Git.isShaParent;
24432444
static isUncommitted = Git.isUncommitted;

0 commit comments

Comments
 (0)