From 1e00878d20c3f34b3091e332643b519360144461 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Wed, 16 Aug 2017 12:02:53 +0200 Subject: [PATCH] :lipstick: --- extensions/git/src/commands.ts | 38 ++++++++++++++++++++-- extensions/git/src/model.ts | 54 ++------------------------------ extensions/git/src/repository.ts | 9 +----- 3 files changed, 38 insertions(+), 63 deletions(-) diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 028678a4cf5..88c8be29701 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -441,7 +441,7 @@ export class CommandCenter { } const resources = scmResources.map(r => r.resourceUri); - await this.model.add(resources); + await this.runByRepository(resources, async (repository, resources) => repository.add(resources)); } @command('git.stageAll') @@ -561,7 +561,7 @@ export class CommandCenter { } const resources = scmResources.map(r => r.resourceUri); - await this.model.revert(resources); + await this.runByRepository(resources, async (repository, resources) => repository.revert(resources)); } @command('git.unstageAll') @@ -667,7 +667,7 @@ export class CommandCenter { } const resources = scmResources.map(r => r.resourceUri); - await this.model.clean(resources); + await this.runByRepository(resources, async (repository, resources) => repository.clean(resources)); } @command('git.cleanAll') @@ -1349,6 +1349,38 @@ export class CommandCenter { } } + private runByRepository(resources: Uri, fn: (repository: Repository, resources: Uri) => Promise): Promise; + private runByRepository(resources: Uri[], fn: (repository: Repository, resources: Uri[]) => Promise): Promise; + private async runByRepository(arg: Uri | Uri[], fn: (repository: Repository, resources: any) => Promise): Promise { + const resources = arg instanceof Uri ? [arg] : arg; + const isSingleResource = arg instanceof Uri; + + const groups = resources.reduce((result, resource) => { + const repository = this.model.getRepository(resource); + + // TODO@Joao: what should happen? + if (!repository) { + console.warn('Could not find git repository for ', resource); + return result; + } + + const tuple = result.filter(p => p[0] === repository)[0]; + + if (tuple) { + tuple.resources.push(resource); + } else { + result.push({ repository, resources: [resource] }); + } + + return result; + }, [] as { repository: Repository, resources: Uri[] }[]); + + const promises = groups + .map(({ repository, resources }) => fn(repository as Repository, isSingleResource ? resources[0] : resources)); + + return Promise.all(promises); + } + dispose(): void { this.disposables.forEach(d => d.dispose()); } diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index 1db5cda3767..6e3a92df692 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -6,7 +6,7 @@ 'use strict'; import { Uri, window, QuickPickItem, Disposable, SourceControlResourceGroup } from 'vscode'; -import { Repository, IRepository, State } from './repository'; +import { Repository, State } from './repository'; import { memoize } from './decorators'; import { toDisposable, filterEvent, once } from './util'; import * as path from 'path'; @@ -20,7 +20,7 @@ class RepositoryPick implements QuickPickItem { constructor(protected repositoryRoot: Uri, public readonly repository: Repository) { } } -export class Model implements IRepository { +export class Model { private repositories: Map = new Map(); @@ -84,54 +84,4 @@ export class Model implements IRepository { return undefined; } - - private runByRepository(resources: Uri, fn: (repository: Repository, resources: Uri) => Promise): Promise; - private runByRepository(resources: Uri[], fn: (repository: Repository, resources: Uri[]) => Promise): Promise; - private async runByRepository(arg: Uri | Uri[], fn: (repository: Repository, resources: any) => Promise): Promise { - const resources = arg instanceof Uri ? [arg] : arg; - const isSingleResource = arg instanceof Uri; - - const groups = resources.reduce((result, resource) => { - const repository = this.getRepository(resource); - - // TODO@Joao: what should happen? - if (!repository) { - console.warn('Could not find git repository for ', resource); - return result; - } - - const tuple = result.filter(p => p[0] === repository)[0]; - - if (tuple) { - tuple.resources.push(resource); - } else { - result.push({ repository, resources: [resource] }); - } - - return result; - }, [] as { repository: Repository, resources: Uri[] }[]); - - const promises = groups - .map(({ repository, resources }) => fn(repository as Repository, isSingleResource ? resources[0] : resources)); - - return Promise.all(promises); - } - - // IRepository - - async add(resources: Uri[]): Promise { - await this.runByRepository(resources, async (repository, resources) => repository.add(resources)); - } - - async stage(resource: Uri, contents: string): Promise { - await this.runByRepository(resource, async (repository, uri) => repository.stage(uri, contents)); - } - - async revert(resources: Uri[]): Promise { - await this.runByRepository(resources, async (repository, resources) => repository.revert(resources)); - } - - async clean(resources: Uri[]): Promise { - await this.runByRepository(resources, async (repository, resources) => repository.clean(resources)); - } } \ No newline at end of file diff --git a/extensions/git/src/repository.ts b/extensions/git/src/repository.ts index af429aad98d..b6fee704642 100644 --- a/extensions/git/src/repository.ts +++ b/extensions/git/src/repository.ts @@ -263,18 +263,11 @@ export interface CommitOptions { signCommit?: boolean; } -export interface IRepository { - add(resources: Uri[]): Promise; - stage(resource: Uri, contents: string): Promise; - revert(resources: Uri[]): Promise; - clean(resources: Uri[]): Promise; -} - export interface GitResourceGroup extends SourceControlResourceGroup { resourceStates: Resource[]; } -export class Repository implements IRepository, Disposable { +export class Repository implements Disposable { private _onDidChangeRepository = new EventEmitter(); readonly onDidChangeRepository: Event = this._onDidChangeRepository.event; -- GitLab