11import type { Container } from '../container' ;
22import type { GitBranch } from '../git/models/branch' ;
33import 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
89interface 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+
1932export interface ChangeBranchMergeTargetCommandArgs {
2033 readonly command : 'changeBranchMergeTarget' ;
21- state ?: Partial < State > ;
34+ state ?: Partial < InitialState > ;
2235}
2336
2437export class ChangeBranchMergeTargetCommand extends QuickCommand {
2538 constructor ( container : Container , args ?: ChangeBranchMergeTargetCommandArgs ) {
2639 super ( container , 'changeBranchMergeTarget' , 'changeBranchMergeTarget' , '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 ChangeBranchMergeTargetCommand 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 . setUserMergeTargetBranchName ?.( state . branch , result . name ) ;
59103 }
60- }
61104
62- await Promise . resolve ( true ) ;
105+ endSteps ( state ) ;
106+ }
63107 }
64108}
0 commit comments