From dcb3c339c8d02aa8b218c3e8c89d313491f6cc2c Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Fri, 4 Jan 2019 16:07:15 +0100 Subject: [PATCH] preserve user input when creating branch from checkout fixes #60895 --- extensions/git/src/commands.ts | 81 ++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 42b23989051..eb45f107f4b 100755 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -179,31 +179,6 @@ function createCheckoutItems(repository: Repository): CheckoutItem[] { return [...heads, ...tags, ...remoteHeads]; } -async function getBranchNameFromUser(): Promise { - const config = workspace.getConfiguration('git'); - const branchWhitespaceChar = config.get('branchWhitespaceChar')!; - const branchValidationRegex = config.get('branchValidationRegex')!; - const sanitize = (name: string) => name ? - name.trim().replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$|\[|\]$/g, branchWhitespaceChar) - : name; - - const result = await window.showInputBox({ - placeHolder: localize('branch name', "Branch name"), - prompt: localize('provide branch name', "Please provide a branch name"), - ignoreFocusOut: true, - validateInput: (name: string) => { - const validateName = new RegExp(branchValidationRegex); - if (validateName.test(sanitize(name))) { - return null; - } - - return localize('branch name format invalid', "Branch name needs to match regex: {0}", branchValidationRegex); - } - }); - - return sanitize(result || ''); -} - enum PushType { Push, PushTo, @@ -1448,33 +1423,71 @@ export class CommandCenter { const picks = [createBranch, ...createCheckoutItems(repository)]; const placeHolder = localize('select a ref to checkout', 'Select a ref to checkout'); - const choice = await window.showQuickPick(picks, { placeHolder }); + + const quickpick = window.createQuickPick(); + quickpick.items = picks; + quickpick.placeholder = placeHolder; + quickpick.show(); + + const choice = await new Promise(c => quickpick.onDidAccept(() => c(quickpick.activeItems[0]))); + quickpick.hide(); if (!choice) { return false; } - await choice.run(repository); + if (choice === createBranch) { + await this._branch(repository, quickpick.value); + } else { + await (choice as CheckoutItem).run(repository); + } + return true; } @command('git.branch', { repository: true }) async branch(repository: Repository): Promise { - const picks = [new HEADItem(repository), ...createCheckoutItems(repository)]; - const placeHolder = localize('select a ref to create a new branch from', 'Select a ref to create a new branch from'); - const target = await window.showQuickPick(picks, { placeHolder }); + await this._branch(repository); + } - if (!target) { + private async _branch(repository: Repository, defaultName?: string): Promise { + const config = workspace.getConfiguration('git'); + const branchWhitespaceChar = config.get('branchWhitespaceChar')!; + const branchValidationRegex = config.get('branchValidationRegex')!; + const sanitize = (name: string) => name ? + name.trim().replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$|\[|\]$/g, branchWhitespaceChar) + : name; + + const rawBranchName = await window.showInputBox({ + value: defaultName, + placeHolder: localize('branch name', "Branch name"), + prompt: localize('provide branch name', "Please provide a branch name"), + ignoreFocusOut: true, + validateInput: (name: string) => { + const validateName = new RegExp(branchValidationRegex); + if (validateName.test(sanitize(name))) { + return null; + } + + return localize('branch name format invalid', "Branch name needs to match regex: {0}", branchValidationRegex); + } + }); + + const branchName = sanitize(rawBranchName || ''); + + if (!branchName) { return; } - const name = await getBranchNameFromUser(); + const picks = [new HEADItem(repository), ...createCheckoutItems(repository)]; + const placeHolder = localize('select a ref to create a new branch from', 'Select a ref to create a new branch from'); + const target = await window.showQuickPick(picks, { placeHolder }); - if (!name) { + if (!target) { return; } - await repository.branch(name, true, target.label); + await repository.branch(branchName, true, target.label); } @command('git.deleteBranch', { repository: true }) -- GitLab