From 24c05ae2f984fc0c77bc94b49ca0186e396ca10f Mon Sep 17 00:00:00 2001 From: Justin Horner Date: Thu, 10 Aug 2017 14:02:20 -0400 Subject: [PATCH] Implement rename branch command --- extensions/git/src/commands.ts | 38 ++++++++++++++++++++++++++++++++++ extensions/git/src/model.ts | 11 +++++++--- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 4648d79da96..1a672cd5710 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -843,6 +843,44 @@ export class CommandCenter { } } + @command('git.renameBranch') + async renameBranch(): Promise { + const placeHolder = localize('provide branch name', "Please provide a branch name"); + const name = await window.showInputBox({ placeHolder }); + + if (!name || name.trim().length === 0) { + return; + } + const run = force => this.model.renameBranch(name); + + try { + await run(name); + } catch (err) { + console.log(err); + if (err.gitErrorCode !== GitErrorCodes.InvalidBranchName && + err.gitErrorCode !== GitErrorCodes.BranchAlreadyExists) { + return err; + } + + let message = ''; + switch (err.gitErrorCode) { + case GitErrorCodes.InvalidBranchName: + message = localize('invalid branch name', 'Invalid branch name'); + break; + case GitErrorCodes.BranchAlreadyExists: + message = localize('branch already exists', `A branch named '${name}' already exists`); + break; + } + + if (!message) { + return; + } + + window.showErrorMessage(message); + } + + } + @command('git.merge') async merge(): Promise { const config = workspace.getConfiguration('git'); diff --git a/extensions/git/src/model.ts b/extensions/git/src/model.ts index 3f0177dde7d..8c1bcb53072 100644 --- a/extensions/git/src/model.ts +++ b/extensions/git/src/model.ts @@ -213,9 +213,10 @@ export enum Operation { Stage = 1 << 14, GetCommitTemplate = 1 << 15, DeleteBranch = 1 << 16, - Merge = 1 << 17, - Ignore = 1 << 18, - Tag = 1 << 19 + RenameBranch = 1 << 17, + Merge = 1 << 18, + Ignore = 1 << 19, + Tag = 1 << 20 } // function getOperationName(operation: Operation): string { @@ -463,6 +464,10 @@ export class Model implements Disposable { await this.run(Operation.DeleteBranch, () => this.repository.deleteBranch(name, force)); } + async renameBranch(name: string): Promise { + await this.run(Operation.RenameBranch, () => this.repository.renameBranch(name)); + } + async merge(ref: string): Promise { await this.run(Operation.Merge, () => this.repository.merge(ref)); }