diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index bc244991a5b..4d54d04c780 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -5,7 +5,7 @@ 'use strict'; -import { Uri, commands, scm, Disposable, window, workspace, QuickPickItem, OutputChannel, Range, WorkspaceEdit, Position, LineChange, SourceControl, SourceControlResourceGroup, SourceControlResourceState, TextDocumentShowOptions, ViewColumn } from 'vscode'; +import { Uri, commands, scm, Disposable, window, workspace, QuickPickItem, OutputChannel, Range, WorkspaceEdit, Position, LineChange, SourceControlResourceState, TextDocumentShowOptions, ViewColumn } from 'vscode'; import { Ref, RefType, Git, GitErrorCodes, Branch } from './git'; import { Repository, Resource, Status, CommitOptions, ResourceGroupType } from './repository'; import { Model } from './model'; @@ -145,22 +145,8 @@ export class CommandCenter { }); } - @command('git.refresh') - async refresh(sourceControl?: SourceControl): Promise { - let repository: Repository | undefined = undefined; - - if (sourceControl) { - repository = this.model.getRepositoryFromSourceControl(sourceControl); - } - - if (!repository) { - repository = await this.model.pickRepository(); - } - - if (!repository) { - return; - } - + @command('git.refresh', { repository: true }) + async refresh(repository: Repository): Promise { await repository.status(); } @@ -458,22 +444,8 @@ export class CommandCenter { await this.runByRepository(resources, async (repository, resources) => repository.add(resources)); } - @command('git.stageAll') - async stageAll(group?: SourceControlResourceGroup): Promise { - let repository: Repository | undefined = undefined; - - if (group) { - repository = this.model.getRepositoryFromResourceGroup(group); - } - - if (!repository) { - repository = await this.model.pickRepository(); - } - - if (!repository) { - return; - } - + @command('git.stageAll', { repository: true }) + async stageAll(repository: Repository): Promise { await repository.add([]); } @@ -576,22 +548,8 @@ export class CommandCenter { await this.runByRepository(resources, async (repository, resources) => repository.revert(resources)); } - @command('git.unstageAll') - async unstageAll(group?: SourceControlResourceGroup): Promise { - let repository: Repository | undefined = undefined; - - if (group) { - repository = this.model.getRepositoryFromResourceGroup(group); - } - - if (!repository) { - repository = await this.model.pickRepository(); - } - - if (!repository) { - return; - } - + @command('git.unstageAll', { repository: true }) + async unstageAll(repository: Repository): Promise { await repository.revert([]); } @@ -681,22 +639,8 @@ export class CommandCenter { await this.runByRepository(resources, async (repository, resources) => repository.clean(resources)); } - @command('git.cleanAll') - async cleanAll(group?: SourceControlResourceGroup): Promise { - let repository: Repository | undefined = undefined; - - if (group) { - repository = this.model.getRepositoryFromResourceGroup(group); - } - - if (!repository) { - repository = await this.model.pickRepository(); - } - - if (!repository) { - return; - } - + @command('git.cleanAll', { repository: true }) + async cleanAll(repository: Repository): Promise { const config = workspace.getConfiguration('git'); let scope = config.get('discardAllScope') || 'prompt'; let resources = repository.workingTreeGroup.resourceStates; @@ -1263,21 +1207,16 @@ export class CommandCenter { private createCommand(id: string, key: string, method: Function, options: CommandOptions): (...args: any[]) => any { const result = (...args) => { - // if (!skipModelCheck && !this.model) { - // window.showInformationMessage(localize('disabled', "Git is either disabled or not supported in this workspace")); - // return; - // } - let result: Promise; if (!options.repository) { result = Promise.resolve(method.apply(this, args)); } else { - console.log(args[0]); - // if (args[0] instanceof SourceControlResourceGroup) { - // } + // try to guess the repository based on the first argument + const repository = this.model.getRepository(args[0]); + const repositoryPromise = repository ? Promise.resolve(repository) : this.model.pickRepository(); - result = this.model.pickRepository().then(repository => { + result = repositoryPromise.then(repository => { if (!repository) { return Promise.reject(localize('modelnotfound', "Git model not found")); } diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index af41e5f8a6c..33d71d2127c 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -60,34 +60,35 @@ export class Model { return pick && pick.repository; } - getRepositoryFromSourceControl(sourceControl: SourceControl): Repository | undefined { - for (let [, repository] of this.repositories) { - if (sourceControl === repository.sourceControl) { - return repository; - } + getRepository(sourceControl: SourceControl): Repository | undefined; + getRepository(resourceGroup: SourceControlResourceGroup): Repository | undefined; + getRepository(resource: Uri): Repository | undefined; + getRepository(hint: any): Repository | undefined { + if (!hint) { + return undefined; } - return undefined; - } + if (hint instanceof Uri) { + const resourcePath = hint.fsPath; - getRepositoryFromResourceGroup(resourceGroup: SourceControlResourceGroup): Repository | undefined { - for (let [, repository] of this.repositories) { - if (resourceGroup === repository.mergeGroup || resourceGroup === repository.indexGroup || resourceGroup === repository.workingTreeGroup) { - return repository; + for (let [root, repository] of this.repositories) { + const repositoryRootPath = root.fsPath; + const relativePath = path.relative(repositoryRootPath, resourcePath); + + if (!/^\./.test(relativePath)) { + return repository; + } } + + return undefined; } - return undefined; - } + for (let [, repository] of this.repositories) { + if (hint === repository.sourceControl) { + return repository; + } - getRepository(resource: Uri): Repository | undefined { - const resourcePath = resource.fsPath; - - for (let [root, repository] of this.repositories) { - const repositoryRootPath = root.fsPath; - const relativePath = path.relative(repositoryRootPath, resourcePath); - - if (!/^\./.test(relativePath)) { + if (hint === repository.mergeGroup || hint === repository.indexGroup || hint === repository.workingTreeGroup) { return repository; } }