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 @@ -19,6 +19,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Adds an "alt" _Fetch_ command for the inline _Pull_ command on branches in views
- 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

### Changed

Expand Down
42 changes: 39 additions & 3 deletions src/commands/git/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { uncommitted, uncommittedStaged } from '../../git/models/constants';
import type { GitReference } from '../../git/models/reference';
import {
getNameWithoutRemote,
getReferenceFromBranch,
getReferenceLabel,
isBranchReference,
isRevisionReference,
Expand All @@ -37,6 +38,7 @@ import type { Deferred } from '../../system/promise';
import { pluralize, truncateLeft } from '../../system/string';
import { getWorkspaceFriendlyPath, openWorkspace, revealInFileExplorer } from '../../system/utils';
import type { ViewsWithRepositoryFolders } from '../../views/viewBase';
import { getSteps } from '../gitCommands.utils';
import type {
AsyncStepResultGenerator,
CustomStep,
Expand Down Expand Up @@ -101,7 +103,7 @@ interface CreateState {
skipWorktreeConfirmations?: boolean;
}

type DeleteFlags = '--force';
type DeleteFlags = '--force' | '--delete-branches';

interface DeleteState {
subcommand: 'delete';
Expand Down Expand Up @@ -842,7 +844,9 @@ export class WorktreeGitCommand extends QuickCommand<State> {
context.title = getTitle(state.subcommand);

const result = yield* pickWorktreesStep(state, context, {
filter: wt => !wt.main || !wt.opened, // Can't delete the main or opened worktree
// Can't delete the main or opened worktree
excludeOpened: true,
filter: wt => !wt.main,
includeStatus: true,
picked: state.uris?.map(uri => uri.toString()),
placeholder: 'Choose worktrees to delete',
Expand All @@ -862,16 +866,19 @@ export class WorktreeGitCommand extends QuickCommand<State> {

endSteps(state);

const branchesToDelete = [];

for (const uri of state.uris) {
let retry = false;
let skipHasChangesPrompt = false;
do {
retry = false;
const force = state.flags.includes('--force');
const deleteBranches = state.flags.includes('--delete-branches');

try {
const worktree = context.worktrees.find(wt => wt.uri.toString() === uri.toString());
if (force) {
const worktree = context.worktrees.find(wt => wt.uri.toString() === uri.toString());
let status;
try {
status = await worktree?.getStatus();
Expand All @@ -892,6 +899,9 @@ export class WorktreeGitCommand extends QuickCommand<State> {
}

await state.repo.deleteWorktree(uri, { force: force });
if (deleteBranches && worktree?.branch) {
branchesToDelete.push(getReferenceFromBranch(worktree?.branch));
}
} catch (ex) {
skipHasChangesPrompt = false;

Expand Down Expand Up @@ -922,16 +932,33 @@ export class WorktreeGitCommand extends QuickCommand<State> {
}
} while (retry);
}

if (branchesToDelete.length) {
yield* getSteps(
this.container,
{
command: 'branch',
state: {
subcommand: 'delete',
repo: state.repo,
references: branchesToDelete,
},
},
this.pickedVia,
);
}
}
}

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 branchesLabel = state.uris.length === 1 ? 'Branch' : 'Branches';
const description =
state.uris.length === 1
? `delete worktree in $(folder) ${getWorkspaceFriendlyPath(state.uris[0])}`
: 'delete worktrees';
const branchesDescription = state.uris.length === 1 ? `and its branch` : 'and corresponding branches';

const step: QuickPickStep<FlagsQuickPickItem<DeleteFlags>> = createConfirmStep(
appendReposToTitle(`Confirm ${context.title}`, state, context),
Expand All @@ -945,6 +972,15 @@ export class WorktreeGitCommand extends QuickCommand<State> {
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}`,
}),
],
context,
);
Expand Down
3 changes: 3 additions & 0 deletions src/commands/quickCommand.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1870,12 +1870,14 @@ export function* pickWorktreesStep<
state: State,
context: Context,
{
excludeOpened,
filter,
includeStatus,
picked,
placeholder,
titleContext,
}: {
excludeOpened?: boolean;
filter?: (b: GitWorktree) => boolean;
includeStatus?: boolean;
picked?: string | string[];
Expand All @@ -1885,6 +1887,7 @@ export function* pickWorktreesStep<
): StepResultGenerator<GitWorktree[]> {
const items = getWorktrees(context.worktrees ?? state.repo, {
buttons: [OpenInNewWindowQuickInputButton, RevealInSideBarQuickInputButton],
excludeOpened: excludeOpened,
filter: filter,
includeStatus: includeStatus,
picked: picked,
Expand Down
Loading