Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Adds _Open Comparison on Remote_ command to branch comparisons in views
- Adds _Open in Worktree_ context menu command to branches and pull requests in views and the _Commit Graph_
- Adds an option to delete a worktree along with its branch from the _Git Delete Worktree_ command
- Adds a step to delete the worktree of a branch first if one exists when using the _Git Delete Branch_ command

### Changed

Expand Down
43 changes: 42 additions & 1 deletion src/commands/git/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Container } from '../../container';
import type { GitBranchReference, GitReference } from '../../git/models/reference';
import { getNameWithoutRemote, getReferenceLabel, isRevisionReference } from '../../git/models/reference';
import { Repository } from '../../git/models/repository';
import type { GitWorktree } from '../../git/models/worktree';
import type { QuickPickItemOfT } from '../../quickpicks/items/common';
import { createQuickPickSeparator } from '../../quickpicks/items/common';
import type { FlagsQuickPickItem } from '../../quickpicks/items/flags';
Expand Down Expand Up @@ -422,7 +423,10 @@ export class BranchGitCommand extends QuickCommand {
return canPickStepContinue(step, state, selection) ? selection[0].item : StepResultBreak;
}

private *deleteCommandSteps(state: DeleteStepState | PruneStepState, context: Context): StepResultGenerator<void> {
private async *deleteCommandSteps(
state: DeleteStepState | PruneStepState,
context: Context,
): AsyncStepResultGenerator<void> {
const prune = state.subcommand === 'prune';
if (state.flags == null) {
state.flags = [];
Expand Down Expand Up @@ -463,6 +467,29 @@ export class BranchGitCommand extends QuickCommand {
);

assertStateStepDeleteBranches(state);

const worktrees = await this.getWorktreesOfSelectedBranches(state);
if (worktrees.length) {
const result = yield* getSteps(
this.container,
{
command: 'worktree',
state: {
subcommand: 'delete',
repo: state.repo,
uris: worktrees.map(wt => wt.uri),
flags: ['--deleting-of-selected-branches'],
},
},
this.pickedVia,
);
if (result !== StepResultBreak) {
// we get here if it was a step back from the delete worktrees picker
state.counter--;
continue;
}
}

const result = yield* this.deleteCommandConfirmStep(state, context);
if (result === StepResultBreak) continue;

Expand All @@ -476,6 +503,20 @@ export class BranchGitCommand extends QuickCommand {
}
}

private async getWorktreesOfSelectedBranches(state: DeleteStepState | PruneStepState): Promise<GitWorktree[]> {
const worktrees = await state.repo.getWorktrees();

const refs = new Set<string>(
Array.isArray(state.references)
? state.references.map(r => r.name)
: state.references != null
? [state.references.name]
: undefined,
);

return worktrees.filter(wt => wt.branch && refs.has(wt.branch.name));
}

private *deleteCommandConfirmStep(
state:
| DeleteStepState<ExcludeSome<DeleteState, 'references', GitBranchReference>>
Expand Down
30 changes: 18 additions & 12 deletions src/commands/git/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ interface CreateState {
skipWorktreeConfirmations?: boolean;
}

type DeleteFlags = '--force' | '--delete-branches';
type DeleteFlags = '--force' | '--delete-branches' | '--deleting-of-selected-branches';

interface DeleteState {
subcommand: 'delete';
Expand Down Expand Up @@ -953,7 +953,9 @@ export class WorktreeGitCommand extends QuickCommand<State> {
private *deleteCommandConfirmStep(state: DeleteStepState, context: Context): StepResultGenerator<DeleteFlags[]> {
context.title = state.uris.length === 1 ? 'Delete Worktree' : 'Delete Worktrees';
const label = state.uris.length === 1 ? 'Delete Worktree' : 'Delete Worktrees';
const deletingOfSelectedBranches = state.flags.includes('--deleting-of-selected-branches');
const branchesLabel = state.uris.length === 1 ? 'Branch' : 'Branches';
const selectedBranchesLabelSuffix = !deletingOfSelectedBranches ? '' : ` of ${branchesLabel}`;
const description =
state.uris.length === 1
? `delete worktree in $(folder) ${getWorkspaceFriendlyPath(state.uris[0])}`
Expand All @@ -964,23 +966,27 @@ export class WorktreeGitCommand extends QuickCommand<State> {
appendReposToTitle(`Confirm ${context.title}`, state, context),
[
createFlagsQuickPickItem<DeleteFlags>(state.flags, [], {
label: label,
label: `${label}${selectedBranchesLabelSuffix}`,
detail: `Will ${description}`,
}),
createFlagsQuickPickItem<DeleteFlags>(state.flags, ['--force'], {
label: `Force ${label}`,
label: `Force ${label}${selectedBranchesLabelSuffix}`,
description: 'includes ANY UNCOMMITTED changes',
detail: `Will forcibly ${description}`,
}),
createFlagsQuickPickItem<DeleteFlags>(state.flags, ['--delete-branches'], {
label: `${label} & ${branchesLabel}`,
detail: `Will ${description} ${branchesDescription}`,
}),
createFlagsQuickPickItem<DeleteFlags>(state.flags, ['--force', '--delete-branches'], {
label: `Force ${label} & ${branchesLabel}`,
description: 'includes ANY UNCOMMITTED changes',
detail: `Will forcibly ${description} ${branchesDescription}`,
}),
...(deletingOfSelectedBranches
? []
: [
createFlagsQuickPickItem<DeleteFlags>(state.flags, ['--delete-branches'], {
label: `${label} & ${branchesLabel}`,
detail: `Will ${description} ${branchesDescription}`,
}),
createFlagsQuickPickItem<DeleteFlags>(state.flags, ['--force', '--delete-branches'], {
label: `Force ${label} & ${branchesLabel}`,
description: 'includes ANY UNCOMMITTED changes',
detail: `Will forcibly ${description} ${branchesDescription}`,
}),
]),
],
context,
);
Expand Down
Loading