Skip to content

Commit

Permalink
Makes GitUri immutable & memoizes many members
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed May 5, 2019
1 parent 25d3895 commit 26cbee6
Show file tree
Hide file tree
Showing 24 changed files with 116 additions and 107 deletions.
5 changes: 2 additions & 3 deletions src/annotations/annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
GitBlameCommit,
GitCommit,
GitDiffHunkLine,
GitService,
GitUri
} from '../git/gitService';
import { Objects, Strings } from '../system';
Expand Down Expand Up @@ -66,7 +65,7 @@ export class Annotations {
): Promise<MarkdownString | undefined> {
let ref;
if (commit.isUncommitted) {
if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) {
if (uri.isUncommittedStaged) {
ref = uri.sha;
}
}
Expand Down Expand Up @@ -101,7 +100,7 @@ export class Annotations {

let message: string;
if (commit.isUncommitted) {
if (uri.sha !== undefined && GitService.isUncommittedStaged(uri.sha)) {
if (uri.isUncommittedStaged) {
message = `[\`Changes\`](${DiffWithCommand.getMarkdownCommandArgs(
commit,
editorLine
Expand Down
4 changes: 2 additions & 2 deletions src/codelens/codeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
}

const fileSymbol = new SymbolInformation(
gitUri.getFilename(),
gitUri.fileName,
SymbolKind.File,
'',
new Location(gitUri.documentUri(), new Range(0, 0, 0, blameRange.start.character))
Expand All @@ -247,7 +247,7 @@ export class GitCodeLensProvider implements CodeLensProvider {
}

const fileSymbol = new SymbolInformation(
gitUri.getFilename(),
gitUri.fileName,
SymbolKind.File,
'',
new Location(gitUri.documentUri(), new Range(0, 1, 0, blameRange.start.character))
Expand Down
10 changes: 6 additions & 4 deletions src/commands/copyMessageToClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
try {
args = { ...args };

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

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

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

args.sha = blame.commit.sha;
if (!gitUri.repoPath) {
gitUri.repoPath = blame.commit.repoPath;
if (!repoPath) {
repoPath = blame.commit.repoPath;
}
}
catch (ex) {
Expand All @@ -83,7 +85,7 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
}

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

args.message = commit.message;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/diffLineWithPrevious.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
import { commands, TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
import { Container } from '../container';
import { GitCommit, GitService, GitUri } from '../git/gitService';
import { GitCommit, GitUri } from '../git/gitService';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { ActiveEditorCommand, command, Commands, getCommandUri } from './common';
Expand Down Expand Up @@ -31,7 +31,7 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand {

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

if (gitUri.sha === undefined || GitService.isUncommitted(gitUri.sha)) {
if (gitUri.sha === undefined || gitUri.isUncommitted) {
const blame =
editor && editor.document.isDirty
? await Container.git.getBlameForLineContents(gitUri, args.line, editor.document.getText())
Expand Down
2 changes: 1 addition & 1 deletion src/commands/diffLineWithWorking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class DiffLineWithWorkingCommand extends ActiveEditorCommand {
args.line = editor == null ? 0 : editor.selection.active.line;
}

if (args.commit === undefined || GitService.isUncommitted(args.commit.sha)) {
if (args.commit === undefined || args.commit.isUncommitted) {
const blameline = args.line;
if (blameline < 0) return undefined;

Expand Down
11 changes: 6 additions & 5 deletions src/commands/diffWithWorking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,17 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
args.line = editor == null ? 0 : editor.selection.active.line;
}

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

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

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

try {
args.commit = await Container.git.getCommitForFile(gitUri.repoPath, gitUri.fsPath, {
ref: gitUri.sha,
ref: ref,
firstIfNotFound: true
});
if (args.commit === undefined) {
Expand All @@ -94,7 +95,7 @@ export class DiffWithWorkingCommand extends ActiveEditorCommand {
Logger.error(
ex,
'DiffWithWorkingCommand',
`getLogCommit(${gitUri.repoPath}, ${gitUri.fsPath}, ${gitUri.sha})`
`getLogCommit(${gitUri.repoPath}, ${gitUri.fsPath}, ${ref})`
);
return Messages.showGenericErrorMessage('Unable to open compare');
}
Expand Down
15 changes: 6 additions & 9 deletions src/commands/openFileInRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { commands, Range, TextEditor, Uri, window } from 'vscode';
import { GlyphChars } from '../constants';
import { Container } from '../container';
import { GitUri, RemoteResourceType } from '../git/gitService';
import { GitService, GitUri, RemoteResourceType } from '../git/gitService';
import { Logger } from '../logger';
import { CommandQuickPickItem, ReferencesQuickPick } from '../quickpicks';
import {
Expand All @@ -15,7 +15,6 @@ import {
isCommandViewContextWithCommit
} from './common';
import { OpenInRemoteCommandArgs } from './openInRemote';
import { Git } from '../git/git';
import { Strings } from '../system';

export interface OpenFileInRemoteCommandArgs {
Expand Down Expand Up @@ -67,7 +66,7 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand {
: undefined;
let sha = args.sha || gitUri.sha;

if (args.branch === undefined && sha !== undefined && !Git.isSha(sha) && remotes.length !== 0) {
if (args.branch === undefined && sha !== undefined && !GitService.isSha(sha) && remotes.length !== 0) {
const [remotePart, branchPart] = Strings.splitSingle(sha, '/');
if (branchPart !== undefined) {
if (remotes.some(r => r.name === remotePart)) {
Expand All @@ -82,10 +81,8 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand {
if (branch === undefined || branch.tracking === undefined) {
const pick = await new ReferencesQuickPick(gitUri.repoPath).show(
args.clipboard
? `Copy url for ${gitUri.getRelativePath()} to clipboard for which branch${
GlyphChars.Ellipsis
}`
: `Open ${gitUri.getRelativePath()} on remote for which branch${GlyphChars.Ellipsis}`,
? `Copy url for ${gitUri.relativePath} to clipboard for which branch${GlyphChars.Ellipsis}`
: `Open ${gitUri.relativePath} on remote for which branch${GlyphChars.Ellipsis}`,
{
autoPick: true,
filters: {
Expand All @@ -109,13 +106,13 @@ export class OpenFileInRemoteCommand extends ActiveEditorCommand {
? {
type: RemoteResourceType.File,
branch: args.branch || 'HEAD',
fileName: gitUri.getRelativePath(),
fileName: gitUri.relativePath,
range: range
}
: {
type: RemoteResourceType.Revision,
branch: args.branch || 'HEAD',
fileName: gitUri.getRelativePath(),
fileName: gitUri.relativePath,
range: range,
sha: sha
},
Expand Down
2 changes: 1 addition & 1 deletion src/git/fsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const emptyArray = new Uint8Array(0);

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

export function toGitLensFSUri(ref: string, repoPath: string): Uri {
Expand Down
14 changes: 9 additions & 5 deletions src/git/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,23 @@ export class Git {
}

static isSha(ref: string) {
return Git.shaRegex.test(ref);
return Git.isMatch(Git.shaRegex, ref);
}

static isShaLike(ref: string) {
return Git.shaLikeRegex.test(ref);
return Git.isMatch(Git.shaLikeRegex, ref);
}

static isShaParent(ref: string) {
return Git.shaParentRegex.test(ref);
return Git.isMatch(Git.shaParentRegex, ref);
}

static isUncommitted(ref: string | undefined) {
return ref ? Git.uncommittedRegex.test(ref) : false;
return Git.isMatch(Git.uncommittedRegex, ref);
}

static isUncommittedStaged(ref: string | undefined): boolean {
return ref ? Git.uncommittedStagedRegex.test(ref) : false;
return Git.isMatch(Git.uncommittedStagedRegex, ref);
}

static shortenSha(
Expand Down Expand Up @@ -309,6 +309,10 @@ export class Git {
return parseInt(gitMajor, 10) >= major && parseInt(gitMinor, 10) >= minor;
}

private static isMatch(regex: RegExp, ref: string | undefined) {
return ref == null || ref.length === 0 ? false : regex.test(ref);
}

// Git commands

static add(repoPath: string | undefined, pathspec: string) {
Expand Down
17 changes: 9 additions & 8 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1166,7 +1166,7 @@ export class GitService implements Disposable {

try {
let data;
if (ref1 !== undefined && ref2 === undefined && !GitService.isUncommittedStaged(ref1)) {
if (ref1 !== undefined && ref2 === undefined && !Git.isUncommittedStaged(ref1)) {
data = await Git.show__diff(root, file, ref1, originalFileName, {
similarityThreshold: Container.config.advanced.similarityThreshold
});
Expand Down Expand Up @@ -1251,7 +1251,7 @@ export class GitService implements Disposable {

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

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

const fileName = GitUri.getRelativePath(uri, repoPath);
const fileName = GitUri.relativeTo(uri, repoPath);

if (Git.isUncommittedStaged(ref)) {
return {
Expand Down Expand Up @@ -1688,7 +1688,7 @@ export class GitService implements Disposable {
filters = ['A'];
}

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

const fileName = GitUri.getRelativePath(uri, repoPath);
const fileName = GitUri.relativeTo(uri, repoPath);

// If the ref is missing (i.e. working tree), check the file status to see if there is anything staged
if ((ref === undefined || ref.length === 0) && editorLine === undefined) {
Expand All @@ -1754,7 +1754,7 @@ export class GitService implements Disposable {
}
}
}
else if (GitService.isUncommittedStaged(ref)) {
else if (Git.isUncommittedStaged(ref)) {
const current =
skip === 0
? GitUri.fromFile(fileName, repoPath, ref)
Expand Down Expand Up @@ -1796,7 +1796,7 @@ export class GitService implements Disposable {
skip++;
}

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

@log()
async getWorkingUri(repoPath: string, uri: Uri) {
let fileName = GitUri.getRelativePath(uri, repoPath);
let fileName = GitUri.relativeTo(uri, repoPath);

let data;
let ref;
Expand Down Expand Up @@ -2438,6 +2438,7 @@ export class GitService implements Disposable {
static deletedOrMissingSha = Git.deletedOrMissingSha;
static getGitPath = Git.getGitPath;
static getGitVersion = Git.getGitVersion;
static isSha = Git.isSha;
static isShaLike = Git.isShaLike;
static isShaParent = Git.isShaParent;
static isUncommitted = Git.isUncommitted;
Expand Down

0 comments on commit 26cbee6

Please sign in to comment.