Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds squash merge detection and merge target actions #4125

Merged
merged 5 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 9 additions & 9 deletions src/commands/git/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface State {
reference: GitReference;
createBranch?: string;
fastForwardTo?: GitReference;
skipWorktreeConfirmations?: boolean;
worktreeDefaultOpen?: 'new' | 'current';
}

type ConfirmationChoice =
Expand Down Expand Up @@ -223,7 +223,7 @@ export class SwitchGitCommand extends QuickCommand<State> {
openOnly: true,
overrides: {
disallowBack: true,
confirmation: state.skipWorktreeConfirmations
confirmation: state.worktreeDefaultOpen
? undefined
: {
title: `Confirm Switch to Worktree \u2022 ${getReferenceLabel(
Expand All @@ -241,12 +241,12 @@ export class SwitchGitCommand extends QuickCommand<State> {
},
onWorkspaceChanging: state.onWorkspaceChanging,
repo: state.repos[0],
skipWorktreeConfirmations: state.skipWorktreeConfirmations,
worktreeDefaultOpen: state.worktreeDefaultOpen,
},
},
this.pickedVia,
);
if (worktreeResult === StepResultBreak && !state.skipWorktreeConfirmations) continue;
if (worktreeResult === StepResultBreak && !state.worktreeDefaultOpen) continue;

endSteps(state);
return;
Expand All @@ -263,7 +263,7 @@ export class SwitchGitCommand extends QuickCommand<State> {

state.createBranch = undefined;
context.promptToCreateBranch = false;
if (state.skipWorktreeConfirmations) {
if (state.worktreeDefaultOpen) {
state.reference = context.canSwitchToLocalBranch;
continue outer;
}
Expand All @@ -273,7 +273,7 @@ export class SwitchGitCommand extends QuickCommand<State> {
}

if (
state.skipWorktreeConfirmations ||
state.worktreeDefaultOpen ||
this.confirm(context.promptToCreateBranch || context.canSwitchToLocalBranch ? true : state.confirm)
) {
const result = yield* this.confirmStep(state as SwitchStepState, context);
Expand Down Expand Up @@ -330,12 +330,12 @@ export class SwitchGitCommand extends QuickCommand<State> {
result === 'switchToNewBranchViaWorktree' ? state.createBranch : undefined,
repo: state.repos[0],
onWorkspaceChanging: state.onWorkspaceChanging,
skipWorktreeConfirmations: state.skipWorktreeConfirmations,
worktreeDefaultOpen: state.worktreeDefaultOpen,
},
},
this.pickedVia,
);
if (worktreeResult === StepResultBreak && !state.skipWorktreeConfirmations) continue outer;
if (worktreeResult === StepResultBreak && !state.worktreeDefaultOpen) continue outer;

endSteps(state);
return;
Expand All @@ -355,7 +355,7 @@ export class SwitchGitCommand extends QuickCommand<State> {
const isRemoteBranch = isBranchReference(state.reference) && state.reference.remote;

type StepType = QuickPickItemOfT<ConfirmationChoice>;
if (state.skipWorktreeConfirmations && state.repos.length === 1) {
if (state.worktreeDefaultOpen && state.repos.length === 1) {
if (isLocalBranch) {
return 'switchViaWorktree';
} else if (!state.createBranch && context.canSwitchToLocalBranch != null) {
Expand Down
24 changes: 15 additions & 9 deletions src/commands/git/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ interface CreateState {
};

onWorkspaceChanging?: ((isNewWorktree?: boolean) => Promise<void>) | ((isNewWorktree?: boolean) => void);
skipWorktreeConfirmations?: boolean;
worktreeDefaultOpen?: 'new' | 'current';
}

type DeleteFlags = '--force' | '--delete-branches';
Expand Down Expand Up @@ -141,7 +141,7 @@ interface OpenState {

onWorkspaceChanging?: ((isNewWorktree?: boolean) => Promise<void>) | ((isNewWorktree?: boolean) => void);
isNewWorktree?: boolean;
skipWorktreeConfirmations?: boolean;
worktreeDefaultOpen?: 'new' | 'current';
}

interface CopyChangesState {
Expand Down Expand Up @@ -632,7 +632,7 @@ export class WorktreeGitCommand extends QuickCommand<State> {
openOnly: true,
overrides: { disallowBack: true },
isNewWorktree: true,
skipWorktreeConfirmations: state.skipWorktreeConfirmations,
worktreeDefaultOpen: state.worktreeDefaultOpen,
onWorkspaceChanging: state.onWorkspaceChanging,
} satisfies OpenStepState,
context,
Expand Down Expand Up @@ -739,7 +739,7 @@ export class WorktreeGitCommand extends QuickCommand<State> {

const confirmations: StepType[] = [];
if (!createDirectlyInFolder) {
if (state.skipWorktreeConfirmations) {
if (state.worktreeDefaultOpen) {
return [defaultOption.context, defaultOption.item];
}

Expand Down Expand Up @@ -1101,15 +1101,21 @@ export class WorktreeGitCommand extends QuickCommand<State> {
detail: 'Will open the worktree in a new window',
});

if (state.skipWorktreeConfirmations) {
const currentWindowItem = createFlagsQuickPickItem<OpenFlags>(state.flags, [], {
label: 'Open Worktree',
detail: 'Will open the worktree in the current window',
});

if (state.worktreeDefaultOpen === 'new') {
return newWindowItem.item;
}

if (state.worktreeDefaultOpen === 'current') {
return currentWindowItem.item;
}

const confirmations: StepType[] = [
createFlagsQuickPickItem<OpenFlags>(state.flags, [], {
label: 'Open Worktree',
detail: 'Will open the worktree in the current window',
}),
currentWindowItem,
newWindowItem,
createFlagsQuickPickItem<OpenFlags>(state.flags, ['--add-to-workspace'], {
label: `Add Worktree to Workspace`,
Expand Down
2 changes: 2 additions & 0 deletions src/constants.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ type InternalGraphWebviewCommands =
| 'gitlens.graph.skipPausedOperation';

type InternalHomeWebviewCommands =
| 'gitlens.home.deleteBranchOrWorktree'
| 'gitlens.home.pushBranch'
| 'gitlens.home.openMergeTargetComparison'
| 'gitlens.home.openPullRequestChanges'
| 'gitlens.home.openPullRequestComparison'
Expand Down
31 changes: 29 additions & 2 deletions src/env/node/git/sub-providers/branches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,39 @@ export class BranchesGitSubProvider implements GitBranchesSubProvider {
} catch {}

// Cherry-pick detection (handles cherry-picks, rebases, etc)
const data = await this.git.exec({ cwd: repoPath }, 'cherry', '--abbrev', '-v', into.name, branch.name);
let data = await this.git.exec({ cwd: repoPath }, 'cherry', '--abbrev', '-v', into.name, branch.name);
// Check if there are no lines or all lines startwith a `-` (i.e. likely merged)
if (!data || data.split('\n').every(l => l.startsWith('-'))) {
if (
!data ||
data
.trim()
.split('\n')
.every(l => l.startsWith('-'))
) {
return { merged: true, confidence: 'high' };
}

// Attempt to detect squash merges by checking if the diff of the branch can be cleanly removed from the target
const mergeBase = await this.getMergeBase(repoPath, into.name, branch.name);
data = await this.git.exec<string>({ cwd: repoPath }, 'diff', mergeBase, branch.name);
if (data?.length) {
// Create a temporary index file
await using disposableIndex = await this.provider.staging!.createTemporaryIndex(repoPath, into.name);
const { env } = disposableIndex;

data = await this.git.exec<string>(
{ cwd: repoPath, env: env, stdin: data },
'apply',
'--cached',
'--reverse',
'--check',
'-',
);
if (!data?.trim().length) {
return { merged: true, confidence: 'medium' };
}
}

return { merged: false };
} catch (ex) {
Logger.error(ex, scope);
Expand Down
4 changes: 2 additions & 2 deletions src/plus/launchpad/launchpad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ export class LaunchpadCommand extends QuickCommand<State> {
void this.container.launchpad.switchTo(state.item);
break;
case 'open-worktree':
void this.container.launchpad.switchTo(state.item, { skipWorktreeConfirmations: true });
void this.container.launchpad.switchTo(state.item, { openInWorktree: true });
break;
case 'switch-and-code-suggest':
case 'code-suggest':
Expand Down Expand Up @@ -900,7 +900,7 @@ export class LaunchpadCommand extends QuickCommand<State> {

case OpenWorktreeInNewWindowQuickInputButton:
this.sendItemActionTelemetry('open-worktree', item, group, context);
await this.container.launchpad.switchTo(item, { skipWorktreeConfirmations: true });
await this.container.launchpad.switchTo(item, { openInWorktree: true });
break;
}

Expand Down
4 changes: 2 additions & 2 deletions src/plus/launchpad/launchpadProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ export class LaunchpadProvider implements Disposable {
@log<LaunchpadProvider['switchTo']>({ args: { 0: i => `${i.id} (${i.provider.name} ${i.type})` } })
async switchTo(
item: LaunchpadItem,
options?: { skipWorktreeConfirmations?: boolean; startCodeSuggestion?: boolean },
options?: { openInWorktree?: boolean; startCodeSuggestion?: boolean },
): Promise<void> {
if (item.openRepository?.localBranch?.current) {
void showInspectView({
Expand All @@ -483,7 +483,7 @@ export class LaunchpadProvider implements Disposable {
item,
options?.startCodeSuggestion
? DeepLinkActionType.SwitchToAndSuggestPullRequest
: options?.skipWorktreeConfirmations
: options?.openInWorktree
? DeepLinkActionType.SwitchToPullRequestWorktree
: DeepLinkActionType.SwitchToPullRequest,
);
Expand Down
9 changes: 9 additions & 0 deletions src/uris/deepLinks/deepLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const DeepLinkCommandTypeToCommand = new Map<DeepLinkCommandType, GlComma
]);

export enum DeepLinkActionType {
DeleteBranch = 'delete-branch',
Switch = 'switch',
SwitchToPullRequest = 'switch-to-pr',
SwitchToPullRequestWorktree = 'switch-to-pr-worktree',
Expand Down Expand Up @@ -245,6 +246,7 @@ export const enum DeepLinkServiceState {
SwitchToRef,
RunCommand,
OpenAllPrChanges,
DeleteBranch,
}

export const enum DeepLinkServiceAction {
Expand Down Expand Up @@ -278,6 +280,7 @@ export const enum DeepLinkServiceAction {
OpenInspect,
OpenSwitch,
OpenAllPrChanges,
DeleteBranch,
}

export type DeepLinkRepoOpenType = 'clone' | 'folder' | 'workspace' | 'current';
Expand Down Expand Up @@ -396,6 +399,7 @@ export const deepLinkStateTransitionTable: Record<string, Record<string, DeepLin
[DeepLinkServiceAction.OpenFile]: DeepLinkServiceState.OpenFile,
[DeepLinkServiceAction.OpenSwitch]: DeepLinkServiceState.SwitchToRef,
[DeepLinkServiceAction.OpenComparison]: DeepLinkServiceState.OpenComparison,
[DeepLinkServiceAction.DeleteBranch]: DeepLinkServiceState.DeleteBranch,
[DeepLinkServiceAction.DeepLinkErrored]: DeepLinkServiceState.Idle,
[DeepLinkServiceAction.DeepLinkCancelled]: DeepLinkServiceState.Idle,
},
Expand Down Expand Up @@ -446,6 +450,11 @@ export const deepLinkStateTransitionTable: Record<string, Record<string, DeepLin
[DeepLinkServiceAction.DeepLinkErrored]: DeepLinkServiceState.Idle,
[DeepLinkServiceAction.DeepLinkCancelled]: DeepLinkServiceState.Idle,
},
[DeepLinkServiceState.DeleteBranch]: {
[DeepLinkServiceAction.DeepLinkResolved]: DeepLinkServiceState.Idle,
[DeepLinkServiceAction.DeepLinkErrored]: DeepLinkServiceState.Idle,
[DeepLinkServiceAction.DeepLinkCancelled]: DeepLinkServiceState.Idle,
},
};

export interface DeepLinkProgress {
Expand Down
50 changes: 48 additions & 2 deletions src/uris/deepLinks/deepLinkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ export class DeepLinkService implements Disposable {
return DeepLinkServiceAction.DeepLinkResolved;
}
}
case DeepLinkServiceState.GoToTarget: {
if (this._context.action === DeepLinkActionType.DeleteBranch) {
return DeepLinkServiceAction.DeleteBranch;
}

return DeepLinkServiceAction.DeepLinkErrored;
}
default:
return DeepLinkServiceAction.DeepLinkErrored;
}
Expand Down Expand Up @@ -1105,6 +1112,11 @@ export class DeepLinkService implements Disposable {
this._context.action === DeepLinkActionType.SwitchToAndSuggestPullRequest
) {
action = DeepLinkServiceAction.OpenSwitch;
} else if (
targetType === DeepLinkType.Branch &&
this._context.action === DeepLinkActionType.DeleteBranch
) {
action = DeepLinkServiceAction.DeleteBranch;
} else {
action = DeepLinkServiceAction.OpenGraph;
}
Expand Down Expand Up @@ -1344,8 +1356,10 @@ export class DeepLinkService implements Disposable {
repos: repo,
reference: ref,
onWorkspaceChanging: onWorkspaceChanging,
skipWorktreeConfirmations:
this._context.action === DeepLinkActionType.SwitchToPullRequestWorktree,
worktreeDefaultOpen:
this._context.action === DeepLinkActionType.SwitchToPullRequestWorktree
? 'new'
: undefined,
},
});

Expand Down Expand Up @@ -1462,6 +1476,38 @@ export class DeepLinkService implements Disposable {
action = DeepLinkServiceAction.DeepLinkResolved;
break;
}
case DeepLinkServiceState.DeleteBranch: {
if (!targetId || (!repo && !repoPath)) {
action = DeepLinkServiceAction.DeepLinkErrored;
message = 'Missing workspace id.';
break;
}

const repository = repo ?? this.container.git.getRepository(repoPath!);
if (!repository) {
action = DeepLinkServiceAction.DeepLinkErrored;
message = 'No matching repository found.';
break;
}

const branch = await repository.git.branches().getBranch(targetId);
if (!branch) {
action = DeepLinkServiceAction.DeepLinkErrored;
message = 'No matching branch found.';
break;
}

void executeGitCommand({
command: 'branch',
state: {
subcommand: 'delete',
repo: repoPath || repo!.uri.fsPath,
references: branch,
},
});
action = DeepLinkServiceAction.DeepLinkResolved;
break;
}
default: {
action = DeepLinkServiceAction.DeepLinkErrored;
message = 'Unknown state.';
Expand Down
2 changes: 1 addition & 1 deletion src/views/viewCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ export class ViewCommands implements Disposable {
state: {
repos: node.repo,
reference: node.branch,
skipWorktreeConfirmations: true,
worktreeDefaultOpen: 'new',
},
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/webviews/apps/plus/home/components/branch-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,9 @@ export abstract class GlBranchCardBase extends GlElement {
repoPath: this.repo,
branchId: this.branch.id,
branchName: this.branch.name,
worktree: this.branch.worktree
? { name: this.branch.worktree.name, isDefault: this.branch.worktree.isDefault }
: undefined,
};
}

Expand Down
Loading