Skip to content

Commit 1200370

Browse files
dvsergeibbb
authored andcommitted
Completes the change-merge-base command to let user pick a repo and branch
(#4224)
1 parent ad2261d commit 1200370

File tree

1 file changed

+60
-16
lines changed

1 file changed

+60
-16
lines changed
Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,90 @@
11
import type { Container } from '../container';
22
import type { GitBranch } from '../git/models/branch';
33
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';
4+
import type { ViewsWithRepositoryFolders } from '../views/viewBase';
5+
import type { PartialStepState, StepGenerator, StepState } from './quickCommand';
6+
import { endSteps, QuickCommand, StepResultBreak } from './quickCommand';
7+
import { pickBranchOrTagStep, pickBranchStep, pickRepositoryStep } from './quickCommand.steps';
78

89
interface Context {
910
repos: Repository[];
1011
title: string;
12+
associatedView: ViewsWithRepositoryFolders;
1113
}
1214

13-
type State = {
15+
type InitialState = {
1416
repo: string | Repository;
1517
branch: string;
1618
mergeBranch: string | undefined;
1719
};
1820

21+
type State = {
22+
repo: Repository;
23+
branch: string;
24+
mergeBranch: string | undefined;
25+
};
26+
function assertState(state: PartialStepState<InitialState>): asserts state is StepState<State> {
27+
if (!state.repo || typeof state.repo === 'string') {
28+
throw new Error('Invalid state: repo should be a Repository instance');
29+
}
30+
}
31+
1932
export interface ChangeUserDefinedMergeBaseCommandArgs {
2033
readonly command: 'changeUserDefinedMergeBase';
21-
state?: Partial<State>;
34+
state?: Partial<InitialState>;
2235
}
2336

2437
export class ChangeUserDefinedMergeBaseCommand extends QuickCommand {
2538
constructor(container: Container, args?: ChangeUserDefinedMergeBaseCommandArgs) {
2639
super(container, 'changeUserDefinedMergeBase', 'changeUserDefinedMergeBase', 'Change Merge Target', {
2740
description: 'Change Merge Target for a branch',
2841
});
42+
let counter = 0;
43+
if (args?.state?.repo) {
44+
counter++;
45+
}
46+
if (args?.state?.branch) {
47+
counter++;
48+
}
2949
this.initialState = {
30-
counter: 0,
50+
counter: counter,
3151
...args?.state,
3252
};
3353
}
3454

35-
protected async *steps(state: PartialStepState<State>): StepGenerator {
55+
protected async *steps(state: PartialStepState<InitialState>): StepGenerator {
3656
const context: Context = {
3757
repos: this.container.git.openRepositories,
3858
title: this.title,
59+
associatedView: this.container.views.branches,
3960
};
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, {
61+
62+
while (this.canStepsContinue(state)) {
63+
if (state.counter < 1 || !state.repo || typeof state.repo === 'string') {
64+
const result = yield* pickRepositoryStep(state, context);
65+
if (result === StepResultBreak) {
66+
break;
67+
}
68+
69+
state.repo = result;
70+
}
71+
72+
assertState(state);
73+
74+
if (state.counter < 2 || !state.branch) {
75+
const branches = yield* pickBranchStep(state, context, {
76+
picked: state.branch,
77+
placeholder: 'Pick a branch to edit',
78+
filter: (branch: GitBranch) => !branch.remote,
79+
});
80+
if (branches === StepResultBreak) {
81+
continue;
82+
}
83+
84+
state.branch = branches.name;
85+
}
86+
87+
const result = yield* pickBranchOrTagStep(state, context, {
4388
picked: state.mergeBranch,
4489
placeholder: 'Pick a merge target branch',
4590
value: undefined,
@@ -49,16 +94,15 @@ export class ChangeUserDefinedMergeBaseCommand extends QuickCommand {
4994
},
5095
});
5196
if (result === StepResultBreak) {
52-
return;
97+
continue;
5398
}
54-
const ref = await this.container.git.branches(repository.path).getBranch(state.branch);
55-
if (ref && result && state.branch) {
99+
if (result && state.branch) {
56100
await this.container.git
57-
.branches(repository.path)
101+
.branches(state.repo.path)
58102
.setUserDefinedBaseBranchName?.(state.branch, result.name);
59103
}
60-
}
61104

62-
await Promise.resolve(true);
105+
endSteps(state);
106+
}
63107
}
64108
}

0 commit comments

Comments
 (0)