Skip to content

Commit

Permalink
Closes #3138 adds create branch option to switch
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Feb 8, 2024
1 parent d3f2644 commit 1abe670
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### Added

- Adds a _Create New Branch..._ option to the _Git Switch to..._ command to easily create a new branch to switch to — closes [#3138](https://github.com/gitkraken/vscode-gitlens/issues/3138)

## [14.8.0] - 2024-02-08

### Added
Expand Down
4 changes: 3 additions & 1 deletion src/commands/git/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ interface CreateState {
reference: GitReference;
name: string;
flags: CreateFlags[];

suggestNameOnly?: boolean;
}

type DeleteFlags = '--force' | '--remotes';
Expand Down Expand Up @@ -151,7 +153,7 @@ export class BranchGitCommand extends QuickCommand {
counter++;
}

if (args.state.name != null) {
if (!args.state.suggestNameOnly && args.state.name != null) {
counter++;
}

Expand Down
21 changes: 21 additions & 0 deletions src/commands/git/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export class SwitchGitCommand extends QuickCommand<State> {
if (state.counter < 2 || state.reference == null) {
const result = yield* pickBranchOrTagStepMultiRepo(state as SwitchStepState, context, {
placeholder: context => `Choose a branch${context.showTags ? ' or tag' : ''} to switch to`,
allowCreate: state.repos.length === 1,
});
if (result === StepResultBreak) {
// If we skipped the previous step, make sure we back up past it
Expand All @@ -165,6 +166,26 @@ export class SwitchGitCommand extends QuickCommand<State> {
continue;
}

if (typeof result == 'string') {
yield* getSteps(
this.container,
{
command: 'branch',
state: {
subcommand: 'create',
repo: state.repos[0],
name: result,
suggestNameOnly: true,
flags: ['--switch'],
},
},
this.pickedVia,
);

endSteps(state);
return;
}

state.reference = result;
}

Expand Down
29 changes: 24 additions & 5 deletions src/commands/quickCommand.steps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { QuickInputButton, QuickPick } from 'vscode';
import type { QuickInputButton, QuickPick, QuickPickItem } from 'vscode';
import { ThemeIcon } from 'vscode';
import { Commands, GlyphChars, quickPickTitleMaxChars } from '../constants';
import { Container } from '../container';
Expand Down Expand Up @@ -931,28 +931,37 @@ export function* pickBranchOrTagStep<

export function* pickBranchOrTagStepMultiRepo<
State extends StepState & { repos: Repository[]; reference?: GitReference },
Context extends { repos: Repository[]; showTags?: boolean; title: string },
Context extends { allowCreate?: boolean; repos: Repository[]; showTags?: boolean; title: string },
>(
state: State,
context: Context,
{
allowCreate,
filter,
picked,
placeholder,
titleContext,
value,
}: {
allowCreate?: boolean;
filter?: { branches?: (b: GitBranch) => boolean; tags?: (t: GitTag) => boolean };
picked?: string | string[];
placeholder: string | ((context: Context) => string);
titleContext?: string;
value?: string;
},
): StepResultGenerator<GitReference> {
): StepResultGenerator<GitReference | string> {
context.showTags = state.repos.length === 1;

const showTagsButton = new ShowTagsToggleQuickInputButton(context.showTags);

const createNewBranchItem: QuickPickItem & { item: string } = {
label: 'Create New Branch...',
iconPath: new ThemeIcon('plus'),
alwaysShow: true,
item: '',
};

const getBranchesAndOrTagsFn = () => {
return getBranchesAndOrTags(state.repos, context.showTags ? ['branches', 'tags'] : ['branches'], {
buttons: [RevealInSideBarQuickInputButton],
Expand All @@ -965,10 +974,12 @@ export function* pickBranchOrTagStepMultiRepo<
const items = getBranchesAndOrTagsFn().then(branchesAndOrTags =>
branchesAndOrTags.length === 0
? [createDirectiveQuickPickItem(Directive.Back, true), createDirectiveQuickPickItem(Directive.Cancel)]
: branchesAndOrTags,
: allowCreate
? [createNewBranchItem, ...branchesAndOrTags]
: branchesAndOrTags,
);

const step = createPickStep<ReferencesQuickPickItem>({
const step = createPickStep<ReferencesQuickPickItem | typeof createNewBranchItem>({
title: appendReposToTitle(`${context.title}${titleContext ?? ''}`, state, context),
placeholder: count =>
!count
Expand All @@ -984,7 +995,13 @@ export function* pickBranchOrTagStepMultiRepo<
selectValueWhenShown: true,
items: items,
additionalButtons: [showTagsButton],
onDidChangeValue: quickpick => {
createNewBranchItem.item = quickpick.value;
return true;
},
onDidClickItemButton: (quickpick, button, { item }) => {
if (typeof item === 'string') return;

if (button === RevealInSideBarQuickInputButton) {
if (isBranchReference(item)) {
void BranchActions.reveal(item, { select: true, focus: false, expand: true });
Expand Down Expand Up @@ -1024,6 +1041,8 @@ export function* pickBranchOrTagStepMultiRepo<
},
keys: ['right', 'alt+right', 'ctrl+right'],
onDidPressKey: (_quickpick, _key, { item }) => {
if (typeof item === 'string') return;

if (isBranchReference(item)) {
void BranchActions.reveal(item, { select: true, focus: false, expand: true });
} else if (isTagReference(item)) {
Expand Down

0 comments on commit 1abe670

Please sign in to comment.