Skip to content

Commit 0aa1371

Browse files
committed
Saves a merge base branch defined by user
(#4224, #4258)
1 parent 07a01fe commit 0aa1371

File tree

17 files changed

+181
-19
lines changed

17 files changed

+181
-19
lines changed

contributions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
"enablement": "gitlens:enabled && resourceScheme =~ /^(gitlens|pr)$/",
7272
"commandPalette": "!gitlens:hasVirtualFolders && gitlens:enabled && resourceScheme =~ /^(gitlens|git|pr)$/"
7373
},
74+
"gitlens.changeBranchMergeTarget": {
75+
"label": "Change Branch Merge Target",
76+
"commandPalette": "gitlens:enabled && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders"
77+
},
7478
"gitlens.clearFileAnnotations": {
7579
"label": "Clear File Annotations",
7680
"icon": "$(gitlens-gitlens-filled)",

docs/telemetry-events.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,14 @@ or
11601160
}
11611161
```
11621162

1163+
### home/changeBranchMergeTarget
1164+
1165+
> Sent when the user starts defining a user-specific merge target branch
1166+
1167+
```typescript
1168+
void
1169+
```
1170+
11631171
### home/command
11641172

11651173
> Sent when a Home command is executed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6098,6 +6098,11 @@
60986098
"icon": "$(folder-opened)",
60996099
"enablement": "gitlens:enabled && resourceScheme =~ /^(gitlens|pr)$/"
61006100
},
6101+
{
6102+
"command": "gitlens.changeBranchMergeTarget",
6103+
"title": "Change Branch Merge Target",
6104+
"category": "GitLens"
6105+
},
61016106
{
61026107
"command": "gitlens.clearFileAnnotations",
61036108
"title": "Clear File Annotations",
@@ -10383,6 +10388,10 @@
1038310388
"command": "gitlens.browseRepoBeforeRevisionInNewWindow",
1038410389
"when": "!gitlens:hasVirtualFolders && gitlens:enabled && resourceScheme =~ /^(gitlens|git|pr)$/"
1038510390
},
10391+
{
10392+
"command": "gitlens.changeBranchMergeTarget",
10393+
"when": "gitlens:enabled && !gitlens:readonly && !gitlens:untrusted && !gitlens:hasVirtualFolders"
10394+
},
1038610395
{
1038710396
"command": "gitlens.clearFileAnnotations",
1038810397
"when": "resource in gitlens:tabs:blameable && (gitlens:window:annotated || resource in gitlens:tabs:annotated)"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import type { Container } from '../container';
2+
import type { GitBranch } from '../git/models/branch';
3+
import type { Repository } from '../git/models/repository';
4+
import type { PartialStepState, StepGenerator } from './quickCommand';
5+
import { QuickCommand, StepResultBreak } from './quickCommand';
6+
import { pickBranchOrTagStep } from './quickCommand.steps';
7+
8+
interface Context {
9+
repos: Repository[];
10+
title: string;
11+
}
12+
13+
type State = {
14+
repo: string | Repository;
15+
branch: string;
16+
mergeBranch: string | undefined;
17+
};
18+
19+
export interface ChangeBranchMergeTargetCommandArgs {
20+
readonly command: 'changeBranchMergeTarget';
21+
state?: Partial<State>;
22+
}
23+
24+
export class ChangeBranchMergeTargetCommand extends QuickCommand {
25+
constructor(container: Container, args?: ChangeBranchMergeTargetCommandArgs) {
26+
super(container, 'changeBranchMergeTarget', 'changeBranchMergeTarget', 'Change Merge Target', {
27+
description: 'Change Merge Target for a branch',
28+
});
29+
this.initialState = {
30+
counter: 0,
31+
...args?.state,
32+
};
33+
}
34+
35+
protected async *steps(state: PartialStepState<State>): StepGenerator {
36+
const context: Context = {
37+
repos: this.container.git.openRepositories,
38+
title: this.title,
39+
};
40+
const repository = typeof state.repo === 'string' ? this.container.git.getRepository(state.repo) : state.repo;
41+
if (repository) {
42+
const result = yield* pickBranchOrTagStep({ counter: 0, repo: repository }, context, {
43+
picked: state.mergeBranch,
44+
placeholder: 'Pick a merge target branch',
45+
value: undefined,
46+
filter: {
47+
branches: (branch: GitBranch) => branch.remote && branch.name !== state.branch,
48+
tags: () => false,
49+
},
50+
});
51+
if (result === StepResultBreak) {
52+
return;
53+
}
54+
const ref = await this.container.git.branches(repository.path).getBranch(state.branch);
55+
if (ref && result && state.branch) {
56+
await this.container.git
57+
.branches(repository.path)
58+
.setUserMergeTargetBranchName?.(state.branch, result.name);
59+
}
60+
}
61+
62+
await Promise.resolve(true);
63+
}
64+
}

src/commands/quickWizard.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@ import type { Container } from '../container';
22
import type { LaunchpadCommandArgs } from '../plus/launchpad/launchpad';
33
import type { AssociateIssueWithBranchCommandArgs, StartWorkCommandArgs } from '../plus/startWork/startWork';
44
import { command } from '../system/-webview/command';
5+
import type { ChangeBranchMergeTargetCommandArgs } from './changeBranchMergeTarget';
56
import type { CommandContext } from './commandContext';
67
import type { QuickWizardCommandArgsWithCompletion } from './quickWizard.base';
78
import { QuickWizardCommandBase } from './quickWizard.base';
89

9-
export type QuickWizardCommandArgs = LaunchpadCommandArgs | StartWorkCommandArgs | AssociateIssueWithBranchCommandArgs;
10+
export type QuickWizardCommandArgs =
11+
| LaunchpadCommandArgs
12+
| StartWorkCommandArgs
13+
| AssociateIssueWithBranchCommandArgs
14+
| ChangeBranchMergeTargetCommandArgs;
1015

1116
@command()
1217
export class QuickWizardCommand extends QuickWizardCommandBase {
1318
constructor(container: Container) {
14-
super(container, ['gitlens.showLaunchpad', 'gitlens.startWork', 'gitlens.associateIssueWithBranch']);
19+
super(container, [
20+
'gitlens.showLaunchpad',
21+
'gitlens.startWork',
22+
'gitlens.associateIssueWithBranch',
23+
'gitlens.changeBranchMergeTarget',
24+
]);
1525
}
1626

1727
protected override preExecute(context: CommandContext, args?: QuickWizardCommandArgsWithCompletion): Promise<void> {
@@ -25,6 +35,9 @@ export class QuickWizardCommand extends QuickWizardCommandBase {
2535
case 'gitlens.associateIssueWithBranch':
2636
return this.execute({ command: 'associateIssueWithBranch', ...args });
2737

38+
case 'gitlens.changeBranchMergeTarget':
39+
return this.execute({ command: 'changeBranchMergeTarget', ...args });
40+
2841
default:
2942
return this.execute(args);
3043
}

src/commands/quickWizard.utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LaunchpadCommand } from '../plus/launchpad/launchpad';
55
import { AssociateIssueWithBranchCommand, StartWorkCommand } from '../plus/startWork/startWork';
66
import { configuration } from '../system/-webview/configuration';
77
import { getContext } from '../system/-webview/context';
8+
import { ChangeBranchMergeTargetCommand } from './changeBranchMergeTarget';
89
import { BranchGitCommand } from './git/branch';
910
import { CherryPickGitCommand } from './git/cherry-pick';
1011
import { CoAuthorsGitCommand } from './git/coauthors';
@@ -122,6 +123,10 @@ export class QuickWizardRootStep implements QuickPickStep<QuickCommand> {
122123
if (args?.command === 'associateIssueWithBranch') {
123124
this.hiddenItems.push(new AssociateIssueWithBranchCommand(container, args));
124125
}
126+
127+
if (args?.command === 'changeBranchMergeTarget') {
128+
this.hiddenItems.push(new ChangeBranchMergeTargetCommand(container, args));
129+
}
125130
}
126131

127132
private _command: QuickCommand | undefined;

src/constants.commands.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,7 @@ export type ContributedPaletteCommands =
619619
| 'gitlens.browseRepoAtRevisionInNewWindow'
620620
| 'gitlens.browseRepoBeforeRevision'
621621
| 'gitlens.browseRepoBeforeRevisionInNewWindow'
622+
| 'gitlens.changeBranchMergeTarget'
622623
| 'gitlens.clearFileAnnotations'
623624
| 'gitlens.closeUnchangedFiles'
624625
| 'gitlens.compareHeadWith'

src/constants.commands.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type InternalGraphWebviewCommands =
3333
| 'gitlens.graph.skipPausedOperation';
3434

3535
type InternalHomeWebviewCommands =
36+
| 'gitlens.home.changeBranchMergeTarget'
3637
| 'gitlens.home.deleteBranchOrWorktree'
3738
| 'gitlens.home.pushBranch'
3839
| 'gitlens.home.openMergeTargetComparison'
@@ -103,6 +104,7 @@ type InternalWalkthroughCommands =
103104

104105
type InternalGlCommands =
105106
| `gitlens.action.${string}`
107+
| 'gitlens.changeBranchMergeTarget'
106108
| 'gitlens.diffWith'
107109
| 'gitlens.openOnRemote'
108110
| 'gitlens.openWalkthrough'

src/constants.telemetry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ export interface TelemetryEvents extends WebviewShowAbortedEvents, WebviewShownE
157157
'home/createBranch': void;
158158
/** Sent when the user chooses to start work on an issue from the home view */
159159
'home/startWork': void;
160+
/** Sent when the user starts defining a user-specific merge target branch */
161+
'home/changeBranchMergeTarget': void;
160162

161163
/** Sent when the user takes an action on the Launchpad title bar */
162164
'launchpad/title/action': LaunchpadTitleActionEvent;

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export const enum CharCode {
4949

5050
export type GitConfigKeys =
5151
| `branch.${string}.${'gk' | 'vscode'}-merge-base`
52+
| `branch.${string}.gk-user-merge-target`
5253
| `branch.${string}.gk-target-base`
5354
| `branch.${string}.gk-associated-issues`
5455
| `branch.${string}.github-pr-owner-number`;

0 commit comments

Comments
 (0)