|
| 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 ChangeUserDefinedMergeBaseCommandArgs { |
| 20 | + readonly command: 'changeUserDefinedMergeBase'; |
| 21 | + state?: Partial<State>; |
| 22 | +} |
| 23 | + |
| 24 | +export class ChangeUserDefinedMergeBaseCommand extends QuickCommand { |
| 25 | + constructor(container: Container, args?: ChangeUserDefinedMergeBaseCommandArgs) { |
| 26 | + super(container, 'changeUserDefinedMergeBase', 'changeUserDefinedMergeBase', '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 | + .setUserDefinedBaseBranchName?.(state.branch, result.name); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + await Promise.resolve(true); |
| 63 | + } |
| 64 | +} |
0 commit comments