diff --git a/src/plus/webviews/patchDetails/patchDetailsWebview.ts b/src/plus/webviews/patchDetails/patchDetailsWebview.ts index 678d14ceeaf83..6e5b73f41f3ec 100644 --- a/src/plus/webviews/patchDetails/patchDetailsWebview.ts +++ b/src/plus/webviews/patchDetails/patchDetailsWebview.ts @@ -13,7 +13,7 @@ import { createReference } from '../../../git/models/reference'; import { isRepository } from '../../../git/models/repository'; import type { CreateDraftChange, Draft, DraftPatch, DraftPatchFileChange, LocalDraft } from '../../../gk/models/drafts'; import type { GkRepositoryId } from '../../../gk/models/repositoryIdentities'; -import { showBranchPicker } from '../../../quickpicks/branchPicker'; +import { showNewOrSelectBranchPicker } from '../../../quickpicks/branchPicker'; import { executeCommand, registerCommand } from '../../../system/command'; import { configuration } from '../../../system/configuration'; import { setContext } from '../../../system/context'; @@ -348,9 +348,9 @@ export class PatchDetailsWebviewProvider if (shouldPickBranch) { const repo = commit.getRepository(); - const branch = await showBranchPicker( + const branch = await showNewOrSelectBranchPicker( `Choose a Branch ${GlyphChars.Dot} ${repo?.name}`, - 'Choose a branch to apply the Cloud Patch to', + // 'Choose a branch to apply the Cloud Patch to', repo, ); @@ -360,9 +360,11 @@ export class PatchDetailsWebviewProvider ); continue; } + + const isString = typeof branch === 'string'; options = { - branchName: branch.ref, - createBranchIfNeeded: true, + branchName: isString ? branch : branch.ref, + createBranchIfNeeded: isString, }; } diff --git a/src/quickpicks/branchPicker.ts b/src/quickpicks/branchPicker.ts index a31a73c46e37d..b098dc2c985ff 100644 --- a/src/quickpicks/branchPicker.ts +++ b/src/quickpicks/branchPicker.ts @@ -1,4 +1,4 @@ -import type { Disposable } from 'vscode'; +import type { Disposable, QuickPickItem } from 'vscode'; import { window } from 'vscode'; import { getBranches } from '../commands/quickCommand.steps'; import type { Repository } from '../git/models/repository'; @@ -49,3 +49,102 @@ export async function showBranchPicker( disposables.forEach(d => void d.dispose()); } } + +export async function showNewBranchPicker( + title: string | undefined, + placeholder?: string, + _repository?: Repository, +): Promise { + const input = window.createInputBox(); + input.ignoreFocusOut = true; + + const disposables: Disposable[] = []; + + let newBranchName: string | undefined; + try { + newBranchName = await new Promise(resolve => { + disposables.push( + input.onDidHide(() => resolve(undefined)), + input.onDidAccept(() => { + const value = input.value.trim(); + if (value == null) { + input.validationMessage = 'Please enter a valid branch name'; + return; + } + + resolve(value); + }), + ); + + input.title = title; + input.placeholder = placeholder; + input.prompt = 'Enter a name for the new branch'; + + input.show(); + }); + } finally { + input.dispose(); + disposables.forEach(d => void d.dispose()); + } + + return newBranchName; +} + +export async function showNewOrSelectBranchPicker( + title: string | undefined, + repository?: Repository, +): Promise { + if (repository == null) { + return undefined; + } + + // TODO: needs updating + const createNewBranch = { + label: 'Create new branch', + description: + 'Creates a branch to apply the Cloud Patch to. (Typing an existing branch name will use that branch.)', + }; + const selectExistingBranch = { + label: 'Select existing branch', + description: 'Selects an existing branch to apply the Cloud Patch to.', + }; + + const items: QuickPickItem[] = [createNewBranch, selectExistingBranch]; + + const quickpick = window.createQuickPick(); + quickpick.ignoreFocusOut = getQuickPickIgnoreFocusOut(); + + const disposables: Disposable[] = []; + + try { + const pick = await new Promise(resolve => { + disposables.push( + quickpick.onDidHide(() => resolve(undefined)), + quickpick.onDidAccept(() => { + if (quickpick.activeItems.length !== 0) { + resolve(quickpick.activeItems[0]); + } + }), + ); + + quickpick.title = title; + quickpick.placeholder = 'Select a branch option'; + quickpick.matchOnDescription = true; + quickpick.matchOnDetail = true; + quickpick.items = items; + + quickpick.show(); + }); + + if (pick === createNewBranch) { + return showNewBranchPicker(title, 'Enter a name for the new branch', repository); + } else if (pick === selectExistingBranch) { + return showBranchPicker(title, 'Select an existing branch', repository); + } + + return undefined; + } finally { + quickpick.dispose(); + disposables.forEach(d => void d.dispose()); + } +} diff --git a/src/webviews/apps/plus/patchDetails/components/gl-draft-details.ts b/src/webviews/apps/plus/patchDetails/components/gl-draft-details.ts index 0e57c3c569f6a..57c88aebb0d35 100644 --- a/src/webviews/apps/plus/patchDetails/components/gl-draft-details.ts +++ b/src/webviews/apps/plus/patchDetails/components/gl-draft-details.ts @@ -355,7 +355,7 @@ export class GlDraftDetails extends GlTreeBase { > - Apply to new branch + Apply to a Branch