From cc71202224b1b71e906d6762cff23872bb7f7570 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Mon, 20 Feb 2017 15:54:22 +0100 Subject: [PATCH] git: clone command --- extensions/git/package.json | 5 +++++ extensions/git/package.nls.json | 3 ++- extensions/git/src/commands.ts | 32 ++++++++++++++++++++++++++++++++ extensions/git/src/git.ts | 8 ++++++++ extensions/git/src/model.ts | 10 +++++++--- 5 files changed, 54 insertions(+), 4 deletions(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index 7a4111aaad0..f091be69379 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -21,6 +21,11 @@ }, "contributes": { "commands": [ + { + "command": "git.clone", + "title": "%command.clone%", + "category": "Git" + }, { "command": "git.init", "title": "%command.init%", diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index b4cb85e4de8..97ae29eae20 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -1,5 +1,6 @@ { - "command.init": "Initialize Git Repository", + "command.clone": "Clone", + "command.init": "Initialize Repository", "command.refresh": "Refresh", "command.openChange": "Open Change", "command.openFile": "Open File", diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index fb9becbcc22..4506b3a339f 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -10,6 +10,7 @@ import { Ref, RefType } from './git'; import { Model, Resource, Status, CommitOptions } from './model'; import * as staging from './staging'; import * as path from 'path'; +import * as os from 'os'; import * as nls from 'vscode-nls'; const localize = nls.loadMessageBundle(); @@ -184,6 +185,37 @@ export class CommandCenter { return ''; } + @command('git.clone') + async clone(): Promise { + const url = await window.showInputBox({ + prompt: localize('repourl', "Repository URL") + }); + + if (!url) { + return; + } + + const parentPath = await window.showInputBox({ + prompt: localize('parent', "Parent Directory"), + value: os.homedir() + }); + + if (!parentPath) { + return; + } + + const clonePromise = this.model.git.clone(url, parentPath); + window.setStatusBarMessage(localize('cloning', "Cloning git repository..."), clonePromise); + const repositoryPath = await clonePromise; + + const open = localize('openrepo', "Open Repository"); + const result = await window.showInformationMessage(localize('proposeopen', "Would you like to open the cloned repository?"), open); + + if (result === open) { + commands.executeCommand('vscode.openFolder', Uri.file(repositoryPath)); + } + } + @command('git.init') async init(): Promise { await this.model.init(); diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 26017c0f141..0bd99841935 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -289,6 +289,14 @@ export class Git { return new Repository(this, repository, env); } + async clone(url: string, parentPath: string): Promise { + const folderName = url.replace(/^.*\//, '').replace(/\.git$/, '') || 'repository'; + const folderPath = path.join(parentPath, folderName); + + await this.exec(parentPath, ['clone', url, folderPath]); + return folderPath; + } + async getRepositoryRoot(path: string): Promise { const result = await this.exec(path, ['rev-parse', '--show-toplevel']); return result.stdout.trim(); diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index bc831b9bc0d..4cc6eb1e72f 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -249,6 +249,10 @@ export class Model implements Disposable { return anyEvent(this.onRunOperation as Event, this.onDidRunOperation as Event); } + get git(): Git { + return this._git; + } + private _mergeGroup = new MergeGroup([]); get mergeGroup(): MergeGroup { return this._mergeGroup; } @@ -314,7 +318,7 @@ export class Model implements Disposable { private disposables: Disposable[] = []; constructor( - private git: Git, + private _git: Git, private rootPath: string, private askpass: Askpass ) { @@ -497,9 +501,9 @@ export class Model implements Disposable { this.repositoryDisposable.dispose(); const disposables: Disposable[] = []; - const repositoryRoot = await this.git.getRepositoryRoot(this.rootPath); + const repositoryRoot = await this._git.getRepositoryRoot(this.rootPath); const askpassEnv = await this.askpass.getEnv(); - this.repository = this.git.open(repositoryRoot, askpassEnv); + this.repository = this._git.open(repositoryRoot, askpassEnv); const dotGitPath = path.join(repositoryRoot, '.git'); const { event: onRawGitChange, disposable: watcher } = watch(dotGitPath); -- GitLab