From 9f35e5c3e0018341605e92837a8bbb28ad241e10 Mon Sep 17 00:00:00 2001 From: Jan Bicker Date: Wed, 21 Aug 2019 13:45:36 +0000 Subject: [PATCH] The current Workspace can be initialized as a Git Repository now. Signed-off-by: Jan Bicker --- packages/git/src/browser/git-contribution.ts | 22 +++++++++++-- .../git/src/browser/git-quick-open-service.ts | 32 ++++++++++++++++++- packages/git/src/common/git.ts | 1 - packages/scm/src/browser/scm-widget.tsx | 4 +-- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/git/src/browser/git-contribution.ts b/packages/git/src/browser/git-contribution.ts index 8c3e2cf0bdfd2..cfc4cb6d676da 100644 --- a/packages/git/src/browser/git-contribution.ts +++ b/packages/git/src/browser/git-contribution.ts @@ -181,6 +181,12 @@ export namespace GIT_COMMANDS { iconClass: 'fa fa-refresh', category: 'Git' }; + export const INIT_REPOSITORY = { + id: 'git-init', + label: 'Initialize Repository', + iconClass: 'fa fa-plus', + category: 'Git' + }; } @injectable() @@ -438,6 +444,11 @@ export class GitContribution implements CommandContribution, MenuContribution, T execute: () => this.quickOpenService.dropStash(), isEnabled: () => !!this.repositoryTracker.selectedRepository }); + registry.registerCommand(GIT_COMMANDS.INIT_REPOSITORY, { + execute: () => this.quickOpenService.initRepository(), + isEnabled: widget => (!widget || widget instanceof ScmWidget) && !this.repositoryProvider.selectedRepository, + isVisible: widget => (!widget || widget instanceof ScmWidget) && !this.repositoryProvider.selectedRepository + }); } protected withProgress(task: () => Promise): Promise { @@ -455,6 +466,11 @@ export class GitContribution implements CommandContribution, MenuContribution, T command: GIT_COMMANDS.OPEN_CHANGES.id, tooltip: GIT_COMMANDS.OPEN_CHANGES.label }); + registry.registerItem({ + id: GIT_COMMANDS.INIT_REPOSITORY.id, + command: GIT_COMMANDS.INIT_REPOSITORY.id, + tooltip: GIT_COMMANDS.INIT_REPOSITORY.label + }); const registerItem = (item: Mutable) => { const commandId = item.command; @@ -463,12 +479,14 @@ export class GitContribution implements CommandContribution, MenuContribution, T this.commands.registerCommand({ id, iconClass: command && command.iconClass }, { execute: (widget, ...args) => widget instanceof ScmWidget && this.commands.executeCommand(commandId, ...args), isEnabled: (widget, ...args) => widget instanceof ScmWidget && this.commands.isEnabled(commandId, ...args), - isVisible: (widget, ...args) => widget instanceof ScmWidget && this.commands.isVisible(commandId, ...args), + isVisible: (widget, ...args) => + widget instanceof ScmWidget && + this.commands.isVisible(commandId, ...args) && + !!this.repositoryProvider.selectedRepository }); item.command = id; registry.registerItem(item); }; - registerItem({ id: GIT_COMMANDS.COMMIT.id, command: GIT_COMMANDS.COMMIT.id, diff --git a/packages/git/src/browser/git-quick-open-service.ts b/packages/git/src/browser/git-quick-open-service.ts index 5cb080e6ff522..fb35cd7d721db 100644 --- a/packages/git/src/browser/git-quick-open-service.ts +++ b/packages/git/src/browser/git-quick-open-service.ts @@ -21,9 +21,11 @@ import { Git, Repository, Branch, BranchType, Tag, Remote, StashEntry } from '.. import { GitRepositoryProvider } from './git-repository-provider'; import { MessageService } from '@theia/core/lib/common/message-service'; import { WorkspaceService } from '@theia/workspace/lib/browser/workspace-service'; -import { FileSystem } from '@theia/filesystem/lib/common'; +import { FileSystem, FileStat } from '@theia/filesystem/lib/common'; import { GitErrorHandler } from './git-error-handler'; import { ProgressService } from '@theia/core/lib/common/progress-service'; +import URI from '@theia/core/lib/common/uri'; +import { LabelProvider } from '@theia/core/lib/browser'; export enum GitAction { PULL, @@ -40,6 +42,7 @@ export class GitQuickOpenService { @inject(GitErrorHandler) protected readonly gitErrorHandler: GitErrorHandler; @inject(ProgressService) protected readonly progressService: ProgressService; + @inject(LabelProvider) protected readonly labelProvider: LabelProvider; constructor( @inject(Git) protected readonly git: Git, @@ -474,6 +477,33 @@ export class GitQuickOpenService { }); } + async initRepository(): Promise { + const wsRoots = await this.workspaceService.roots; + if (wsRoots && wsRoots.length > 1) { + const placeholder = 'Choose workspace root to initialize git repo in'; + const items = wsRoots.map>(root => this.toRepositoryPathQuickOpenItem(root)); + this.open(items, placeholder); + } else { + const rootUri = new URI(wsRoots[0].uri); + this.doInitRepository(rootUri.toString()); + } + } + + private async doInitRepository(uri: string): Promise { + this.withProgress(async () => this.git.exec({localUri: uri}, ['init'])); + } + + private toRepositoryPathQuickOpenItem(root: FileStat): GitQuickOpenItem { + const rootUri = new URI(root.uri); + const toLabel = (item: GitQuickOpenItem) => this.labelProvider.getName(item.ref); + const toDescription = (item: GitQuickOpenItem) => this.labelProvider.getLongName(item.ref.parent); + const execute = async (item: GitQuickOpenItem) => { + const wsRoot = item.ref.toString(); + this.doInitRepository(wsRoot); + }; + return new GitQuickOpenItem(rootUri, execute, toLabel, toDescription); + } + private open(items: QuickOpenItem | QuickOpenItem[], placeholder: string): void { this.quickOpenService.open(this.getModel(Array.isArray(items) ? items : [items]), this.getOptions(placeholder)); } diff --git a/packages/git/src/common/git.ts b/packages/git/src/common/git.ts index 198c35a116db4..2e5f2be1b1394 100644 --- a/packages/git/src/common/git.ts +++ b/packages/git/src/common/git.ts @@ -840,7 +840,6 @@ export interface Git extends Disposable { */ // tslint:disable-next-line:no-any lsFiles(repository: Repository, uri: string, options?: Git.Options.LsFiles): Promise; - } /** diff --git a/packages/scm/src/browser/scm-widget.tsx b/packages/scm/src/browser/scm-widget.tsx index 63635f1be4df7..3825b736fdb11 100644 --- a/packages/scm/src/browser/scm-widget.tsx +++ b/packages/scm/src/browser/scm-widget.tsx @@ -94,7 +94,7 @@ export class ScmWidget extends ReactWidget implements StatefulWidget { this.toDisposeOnRefresh.dispose(); this.toDispose.push(this.toDisposeOnRefresh); const repository = this.scmService.selectedRepository; - this.title.label = repository ? repository.provider.label : 'no repository opened'; + this.title.label = repository ? repository.provider.label : 'no repository found'; this.title.caption = this.title.label; this.update(); if (repository) { @@ -145,7 +145,7 @@ export class ScmWidget extends ReactWidget implements StatefulWidget { if (!repository) { return ; } const input = repository.input;