From 0e40dd740f26dfcf1b7d2f4cecd0e2bdecf50561 Mon Sep 17 00:00:00 2001 From: Maik Riechert Date: Wed, 3 May 2017 22:53:26 +0100 Subject: [PATCH] add git delete branch command --- extensions/git/package.json | 9 ++++++++ extensions/git/package.nls.json | 1 + extensions/git/src/commands.ts | 39 +++++++++++++++++++++++++++++++++ extensions/git/src/git.ts | 5 +++++ extensions/git/src/model.ts | 7 +++++- 5 files changed, 60 insertions(+), 1 deletion(-) diff --git a/extensions/git/package.json b/extensions/git/package.json index bd3d175c58a..90d477cf594 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -172,6 +172,11 @@ "title": "%command.branch%", "category": "Git" }, + { + "command": "git.deleteBranch", + "title": "%command.deleteBranch%", + "category": "Git" + }, { "command": "git.pull", "title": "%command.pull%", @@ -298,6 +303,10 @@ "command": "git.branch", "when": "config.git.enabled && scmProvider == git && gitState == idle" }, + { + "command": "git.deleteBranch", + "when": "config.git.enabled && scmProvider == git && gitState == idle" + }, { "command": "git.pull", "when": "config.git.enabled && scmProvider == git && gitState == idle" diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index 11c7347fa07..9844e060659 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -21,6 +21,7 @@ "command.undoCommit": "Undo Last Commit", "command.checkout": "Checkout to...", "command.branch": "Create Branch...", + "command.deleteBranch": "Delete Branch...", "command.pull": "Pull", "command.pullRebase": "Pull (Rebase)", "command.push": "Push", diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 399cfd10a16..001532ff9dd 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -60,6 +60,26 @@ class CheckoutRemoteHeadItem extends CheckoutItem { } } +class BranchDeleteItem implements QuickPickItem { + + protected get shortCommit(): string { return (this.ref.commit || '').substr(0, 8); } + protected get treeish(): string | undefined { return this.ref.name; } + get label(): string { return this.ref.name || this.shortCommit; } + get description(): string { return this.shortCommit; } + + constructor(protected ref: Ref) { } + + async run(model: Model): Promise { + const ref = this.treeish; + + if (!ref) { + return; + } + + await model.deleteBranch(ref); + } +} + interface Command { commandId: string; key: string; @@ -671,6 +691,25 @@ export class CommandCenter { await this.model.branch(name); } + @command('git.deleteBranch') + async deleteBranch(branchName: string): Promise { + if (typeof branchName === 'string') { + return await this.model.deleteBranch(branchName); + } + const currentHead = this.model.HEAD && this.model.HEAD.name; + const heads = this.model.refs.filter(ref => ref.type === RefType.Head && ref.name !== currentHead) + .map(ref => new BranchDeleteItem(ref)); + + const placeHolder = 'Select a branch to delete'; + const choice = await window.showQuickPick(heads, { placeHolder }); + + if (!choice) { + return; + } + + await choice.run(this.model); + } + @command('git.pull') async pull(): Promise { const remotes = this.model.remotes; diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 4adc617a3eb..a242d034654 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -650,6 +650,11 @@ export class Repository { await this.run(args); } + async deleteBranch(name: string): Promise { + const args = ['branch', '-d', name]; + await this.run(args); + } + async clean(paths: string[]): Promise { const pathsByGroup = groupBy(paths, p => path.dirname(p)); const groups = Object.keys(pathsByGroup).map(k => pathsByGroup[k]); diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index 1d74a87d3a3..0446a3c5e09 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -211,7 +211,8 @@ export enum Operation { Init = 1 << 12, Show = 1 << 13, Stage = 1 << 14, - GetCommitTemplate = 1 << 15 + GetCommitTemplate = 1 << 15, + DeleteBranch = 1 << 16 } // function getOperationName(operation: Operation): string { @@ -454,6 +455,10 @@ export class Model implements Disposable { await this.run(Operation.Branch, () => this.repository.branch(name, true)); } + async deleteBranch(name: string): Promise { + await this.run(Operation.DeleteBranch, () => this.repository.deleteBranch(name)); + } + async checkout(treeish: string): Promise { await this.run(Operation.Checkout, () => this.repository.checkout(treeish, [])); }