Skip to content

Commit

Permalink
Passes container into all commands
Browse files Browse the repository at this point in the history
Should help with eventual testing
  • Loading branch information
eamodio committed Jan 20, 2022
1 parent df7dd9f commit e61e8cd
Show file tree
Hide file tree
Showing 82 changed files with 389 additions and 382 deletions.
6 changes: 3 additions & 3 deletions src/commands/addAuthors.ts
@@ -1,19 +1,19 @@
'use strict';
import { SourceControl } from 'vscode';
import { Container } from '../container';
import type { Container } from '../container';
import { command, Command, Commands } from './common';
import { executeGitCommand } from './gitCommands';

@command()
export class AddAuthorsCommand extends Command {
constructor() {
constructor(private readonly container: Container) {
super(Commands.AddAuthors);
}

async execute(sourceControl: SourceControl) {
let repo;
if (sourceControl?.rootUri != null) {
repo = await Container.instance.git.getRepository(sourceControl.rootUri);
repo = await this.container.git.getRepository(sourceControl.rootUri);
}

return executeGitCommand({
Expand Down
6 changes: 3 additions & 3 deletions src/commands/browseRepoAtRevision.ts
@@ -1,7 +1,7 @@
'use strict';
import { commands, TextEditor, Uri } from 'vscode';
import { BuiltInCommands } from '../constants';
import { Container } from '../container';
import type { Container } from '../container';
import { toGitLensFSUri } from '../git/fsProvider';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
Expand All @@ -26,7 +26,7 @@ export interface BrowseRepoAtRevisionCommandArgs {

@command()
export class BrowseRepoAtRevisionCommand extends ActiveEditorCommand {
constructor() {
constructor(private readonly container: Container) {
super([
Commands.BrowseRepoAtRevision,
Commands.BrowseRepoAtRevisionInNewWindow,
Expand Down Expand Up @@ -66,7 +66,7 @@ export class BrowseRepoAtRevisionCommand extends ActiveEditorCommand {
if (gitUri.sha == null) return;

const sha = args?.before
? await Container.instance.git.resolveReference(gitUri.repoPath!, `${gitUri.sha}^`)
? await this.container.git.resolveReference(gitUri.repoPath!, `${gitUri.sha}^`)
: gitUri.sha;
uri = toGitLensFSUri(sha, gitUri.repoPath!);
gitUri = GitUri.fromRevisionUri(uri);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/closeUnchangedFiles.ts
Expand Up @@ -2,7 +2,7 @@
import { commands, TextEditor, Uri, window } from 'vscode';
import { TextEditorComparer, UriComparer } from '../comparers';
import { BuiltInCommands } from '../constants';
import { Container } from '../container';
import type { Container } from '../container';
import { Logger } from '../logger';
import { Messages } from '../messages';
import { Functions } from '../system';
Expand All @@ -16,7 +16,7 @@ export interface CloseUnchangedFilesCommandArgs {
export class CloseUnchangedFilesCommand extends Command {
private _onEditorChangedFn: ((editor: TextEditor | undefined) => void) | undefined;

constructor() {
constructor(private readonly container: Container) {
super(Commands.CloseUnchangedFiles);
}

Expand All @@ -28,7 +28,7 @@ export class CloseUnchangedFilesCommand extends Command {
const repoPath = await getRepoPathOrPrompt('Close All Unchanged Files');
if (!repoPath) return;

const status = await Container.instance.git.getStatusForRepo(repoPath);
const status = await this.container.git.getStatusForRepo(repoPath);
if (status == null) {
void window.showWarningMessage('Unable to close unchanged files');

Expand Down
6 changes: 3 additions & 3 deletions src/commands/closeView.ts
@@ -1,11 +1,11 @@
'use strict';
import { ContextKeys, setContext, SyncedState } from '../constants';
import { Container } from '../container';
import type { Container } from '../container';
import { command, Command, CommandContext, Commands } from './common';

@command()
export class CloseViewCommand extends Command {
constructor() {
constructor(private readonly container: Container) {
super([Commands.CloseWelcomeView]);
}

Expand All @@ -16,7 +16,7 @@ export class CloseViewCommand extends Command {
async execute(command: Commands) {
switch (command) {
case Commands.CloseWelcomeView:
await Container.instance.context.globalState.update(SyncedState.WelcomeViewVisible, false);
await this.container.context.globalState.update(SyncedState.WelcomeViewVisible, false);
await setContext(ContextKeys.ViewsWelcomeVisible, false);
break;
}
Expand Down
9 changes: 3 additions & 6 deletions src/commands/common.ts
Expand Up @@ -2,7 +2,6 @@
import {
commands,
Disposable,
ExtensionContext,
GitTimelineItem,
SourceControlResourceGroup,
SourceControlResourceState,
Expand Down Expand Up @@ -218,7 +217,7 @@ export function executeEditorCommand<T>(command: Commands, uri: Uri | undefined,
}

interface CommandConstructor {
new (): Command;
new (container: Container): Command;
}

const registrableCommands: CommandConstructor[] = [];
Expand All @@ -229,10 +228,8 @@ export function command(): ClassDecorator {
};
}

export function registerCommands(context: ExtensionContext): void {
for (const c of registrableCommands) {
context.subscriptions.push(new c());
}
export function registerCommands(container: Container): Disposable[] {
return registrableCommands.map(c => new c(container));
}

export function getCommandUri(uri?: Uri, editor?: TextEditor): Uri | undefined {
Expand Down
8 changes: 4 additions & 4 deletions src/commands/compareWith.ts
@@ -1,6 +1,6 @@
'use strict';
import { TextEditor, Uri } from 'vscode';
import { Container } from '../container';
import type { Container } from '../container';
import { Logger } from '../logger';
import { Messages } from '../messages';
import {
Expand All @@ -19,7 +19,7 @@ export interface CompareWithCommandArgs {

@command()
export class CompareWithCommand extends ActiveEditorCommand {
constructor() {
constructor(private readonly container: Container) {
super([
Commands.CompareWith,
Commands.CompareHeadWith,
Expand Down Expand Up @@ -76,9 +76,9 @@ export class CompareWithCommand extends ActiveEditorCommand {
if (!repoPath) return;

if (args.ref1 != null && args.ref2 != null) {
void (await Container.instance.searchAndCompareView.compare(repoPath, args.ref1, args.ref2));
void (await this.container.searchAndCompareView.compare(repoPath, args.ref1, args.ref2));
} else {
Container.instance.searchAndCompareView.selectForCompare(repoPath, args.ref1, { prompt: true });
this.container.searchAndCompareView.selectForCompare(repoPath, args.ref1, { prompt: true });
}
} catch (ex) {
Logger.error(ex, 'CompareWithCommmand');
Expand Down
6 changes: 3 additions & 3 deletions src/commands/copyCurrentBranch.ts
@@ -1,13 +1,13 @@
'use strict';
import { env, TextEditor, Uri, window } from 'vscode';
import { Container } from '../container';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
import { ActiveEditorCommand, command, Commands, getCommandUri, getRepoPathOrActiveOrPrompt } from './common';

@command()
export class CopyCurrentBranchCommand extends ActiveEditorCommand {
constructor() {
constructor(private readonly container: Container) {
super(Commands.CopyCurrentBranch);
}

Expand All @@ -20,7 +20,7 @@ export class CopyCurrentBranchCommand extends ActiveEditorCommand {
if (!repoPath) return;

try {
const branch = await Container.instance.git.getBranch(repoPath);
const branch = await this.container.git.getBranch(repoPath);
if (branch?.name) {
await env.clipboard.writeText(branch.name);
}
Expand Down
14 changes: 7 additions & 7 deletions src/commands/copyMessageToClipboard.ts
@@ -1,6 +1,6 @@
'use strict';
import { env, TextEditor, Uri } from 'vscode';
import { Container } from '../container';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
import { Messages } from '../messages';
Expand All @@ -23,7 +23,7 @@ export interface CopyMessageToClipboardCommandArgs {

@command()
export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
constructor() {
constructor(private readonly container: Container) {
super(Commands.CopyMessageToClipboard);
}

Expand Down Expand Up @@ -53,10 +53,10 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
let repoPath;
// If we don't have an editor then get the message of the last commit to the branch
if (uri == null) {
repoPath = await Container.instance.git.getActiveRepoPath(editor);
repoPath = await this.container.git.getActiveRepoPath(editor);
if (!repoPath) return;

const log = await Container.instance.git.getLog(repoPath, { limit: 1 });
const log = await this.container.git.getLog(repoPath, { limit: 1 });
if (log == null) return;

args.message = Iterables.first(log.commits.values()).message;
Expand All @@ -70,12 +70,12 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {

try {
const blame = editor?.document.isDirty
? await Container.instance.git.getBlameForLineContents(
? await this.container.git.getBlameForLineContents(
gitUri,
blameline,
editor.document.getText(),
)
: await Container.instance.git.getBlameForLine(gitUri, blameline);
: await this.container.git.getBlameForLine(gitUri, blameline);
if (blame == null) return;

if (blame.commit.isUncommitted) return;
Expand All @@ -93,7 +93,7 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
}

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

args.message = commit.message;
Expand Down
18 changes: 7 additions & 11 deletions src/commands/copyShaToClipboard.ts
@@ -1,6 +1,6 @@
'use strict';
import { env, TextEditor, Uri } from 'vscode';
import { Container } from '../container';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { Logger } from '../logger';
import { Messages } from '../messages';
Expand All @@ -22,14 +22,14 @@ export interface CopyShaToClipboardCommandArgs {

@command()
export class CopyShaToClipboardCommand extends ActiveEditorCommand {
constructor() {
constructor(private readonly container: Container) {
super(Commands.CopyShaToClipboard);
}

protected override preExecute(context: CommandContext, args?: CopyShaToClipboardCommandArgs) {
if (isCommandContextViewNodeHasCommit(context)) {
args = { ...args };
args.sha = Container.instance.config.advanced.abbreviateShaOnCopy
args.sha = this.container.config.advanced.abbreviateShaOnCopy
? context.node.commit.shortSha
: context.node.commit.sha;
return this.execute(context.editor, context.node.commit.uri, args);
Expand All @@ -53,10 +53,10 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand {
try {
// If we don't have an editor then get the sha of the last commit to the branch
if (uri == null) {
const repoPath = await Container.instance.git.getActiveRepoPath(editor);
const repoPath = await this.container.git.getActiveRepoPath(editor);
if (!repoPath) return;

const log = await Container.instance.git.getLog(repoPath, { limit: 1 });
const log = await this.container.git.getLog(repoPath, { limit: 1 });
if (log == null) return;

args.sha = Iterables.first(log.commits.values()).sha;
Expand All @@ -67,12 +67,8 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand {
try {
const gitUri = await GitUri.fromUri(uri);
const blame = editor?.document.isDirty
? await Container.instance.git.getBlameForLineContents(
gitUri,
blameline,
editor.document.getText(),
)
: await Container.instance.git.getBlameForLine(gitUri, blameline);
? await this.container.git.getBlameForLineContents(gitUri, blameline, editor.document.getText())
: await this.container.git.getBlameForLine(gitUri, blameline);
if (blame == null) return;

args.sha = blame.commit.sha;
Expand Down
6 changes: 3 additions & 3 deletions src/commands/createPullRequestOnRemote.ts
@@ -1,5 +1,5 @@
'use strict';
import { Container } from '../container';
import type { Container } from '../container';
import { GitRemote } from '../git/models';
import { RemoteProvider, RemoteResource, RemoteResourceType } from '../git/remotes/provider';
import { Command, command, Commands, executeCommand } from './common';
Expand All @@ -16,14 +16,14 @@ export interface CreatePullRequestOnRemoteCommandArgs {

@command()
export class CreatePullRequestOnRemoteCommand extends Command {
constructor() {
constructor(private readonly container: Container) {
super(Commands.CreatePullRequestOnRemote);
}

async execute(args?: CreatePullRequestOnRemoteCommandArgs) {
if (args?.repoPath == null) return;

const repo = await Container.instance.git.getRepository(args.repoPath);
const repo = await this.container.git.getRepository(args.repoPath);
if (repo == null) return;

const compareRemote = await repo.getRemote(args.remote);
Expand Down
6 changes: 3 additions & 3 deletions src/commands/diffLineWithPrevious.ts
@@ -1,6 +1,6 @@
'use strict';
import { TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
import { Container } from '../container';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { GitCommit } from '../git/models';
import { Logger } from '../logger';
Expand All @@ -17,7 +17,7 @@ export interface DiffLineWithPreviousCommandArgs {

@command()
export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
constructor() {
constructor(private readonly container: Container) {
super(Commands.DiffLineWithPrevious);
}

Expand All @@ -33,7 +33,7 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
const gitUri = args.commit != null ? GitUri.fromCommit(args.commit) : await GitUri.fromUri(uri);

try {
const diffUris = await Container.instance.git.getPreviousLineDiffUris(
const diffUris = await this.container.git.getPreviousLineDiffUris(
gitUri.repoPath!,
gitUri,
args.line,
Expand Down
10 changes: 5 additions & 5 deletions src/commands/diffLineWithWorking.ts
@@ -1,6 +1,6 @@
'use strict';
import { TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
import { Container } from '../container';
import type { Container } from '../container';
import { GitUri } from '../git/gitUri';
import { GitCommit, GitRevision } from '../git/models';
import { Logger } from '../logger';
Expand All @@ -17,7 +17,7 @@ export interface DiffLineWithWorkingCommandArgs {

@command()
export class DiffLineWithWorkingCommand extends ActiveEditorCommand {
constructor() {
constructor(private readonly container: Container) {
super(Commands.DiffLineWithWorking);
}

Expand All @@ -38,8 +38,8 @@ export class DiffLineWithWorkingCommand extends ActiveEditorCommand {

try {
const blame = editor?.document.isDirty
? await Container.instance.git.getBlameForLineContents(gitUri, blameline, editor.document.getText())
: await Container.instance.git.getBlameForLine(gitUri, blameline);
? await this.container.git.getBlameForLineContents(gitUri, blameline, editor.document.getText())
: await this.container.git.getBlameForLine(gitUri, blameline);
if (blame == null) {
void Messages.showFileNotUnderSourceControlWarningMessage('Unable to open compare');

Expand All @@ -50,7 +50,7 @@ export class DiffLineWithWorkingCommand extends ActiveEditorCommand {

// If the line is uncommitted, change the previous commit
if (args.commit.isUncommitted) {
const status = await Container.instance.git.getStatusForFile(gitUri.repoPath!, gitUri.fsPath);
const status = await this.container.git.getStatusForFile(gitUri.repoPath!, gitUri.fsPath);
args.commit = args.commit.with({
sha: status?.indexStatus != null ? GitRevision.uncommittedStaged : args.commit.previousSha!,
fileName: args.commit.previousFileName!,
Expand Down

0 comments on commit e61e8cd

Please sign in to comment.