-
Couldn't load subscription status.
- Fork 1.6k
Allow the merge target to be changed or ignored #4258
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a221c19
Saves a merge base branch defined by user
sergeibbb c4d0e8a
Completes the change-merge-base command to let user pick a repo and b…
sergeibbb 2732583
Lets user reset their choice of Merge Target
sergeibbb 9af6104
Handles potential errors when retrieving the active repository
sergeibbb 31dd3ec
Describes changes in CHANGELOG
sergeibbb 85fff31
Improves description for a case when default target is undefined
sergeibbb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import type { CancellationToken } from 'vscode'; | ||
| import type { Container } from '../container'; | ||
| import type { GitBranch } from '../git/models/branch'; | ||
| import type { Repository } from '../git/models/repository'; | ||
| import { getSettledValue } from '../system/promise'; | ||
| import type { ViewsWithRepositoryFolders } from '../views/viewBase'; | ||
| import type { PartialStepState, StepGenerator, StepState } from './quickCommand'; | ||
| import { endSteps, QuickCommand, StepResultBreak } from './quickCommand'; | ||
| import { pickBranchStep, pickOrResetBranchStep, pickRepositoryStep } from './quickCommand.steps'; | ||
|
|
||
| interface Context { | ||
| repos: Repository[]; | ||
| title: string; | ||
| associatedView: ViewsWithRepositoryFolders; | ||
| } | ||
|
|
||
| type InitialState = { | ||
| repo: string | Repository; | ||
| branch: string; | ||
| mergeBranch: string | undefined; | ||
| }; | ||
|
|
||
| type State = { | ||
| repo: Repository; | ||
| branch: string; | ||
| mergeBranch: string | undefined; | ||
| }; | ||
| function assertState(state: PartialStepState<InitialState>): asserts state is StepState<State> { | ||
| if (!state.repo || typeof state.repo === 'string') { | ||
| throw new Error('Invalid state: repo should be a Repository instance'); | ||
| } | ||
| } | ||
|
|
||
| export interface ChangeBranchMergeTargetCommandArgs { | ||
| readonly command: 'changeBranchMergeTarget'; | ||
| state?: Partial<InitialState>; | ||
| } | ||
|
|
||
| export class ChangeBranchMergeTargetCommand extends QuickCommand { | ||
| constructor(container: Container, args?: ChangeBranchMergeTargetCommandArgs) { | ||
| super(container, 'changeBranchMergeTarget', 'changeBranchMergeTarget', 'Change Merge Target', { | ||
| description: 'Change Merge Target for a branch', | ||
| }); | ||
| let counter = 0; | ||
| if (args?.state?.repo) { | ||
| counter++; | ||
| } | ||
| if (args?.state?.branch) { | ||
| counter++; | ||
| } | ||
| this.initialState = { | ||
| counter: counter, | ||
| ...args?.state, | ||
| }; | ||
| } | ||
|
|
||
| protected async *steps(state: PartialStepState<InitialState>): StepGenerator { | ||
| const context: Context = { | ||
| repos: this.container.git.openRepositories, | ||
| title: this.title, | ||
| associatedView: this.container.views.branches, | ||
| }; | ||
|
|
||
| while (this.canStepsContinue(state)) { | ||
| if (state.counter < 1 || !state.repo || typeof state.repo === 'string') { | ||
| const result = yield* pickRepositoryStep(state, context); | ||
| if (result === StepResultBreak) { | ||
| break; | ||
| } | ||
|
|
||
| state.repo = result; | ||
| } | ||
|
|
||
| assertState(state); | ||
|
|
||
| if (state.counter < 2 || !state.branch) { | ||
| const branches = yield* pickBranchStep(state, context, { | ||
| picked: state.branch, | ||
| placeholder: 'Pick a branch to edit', | ||
| filter: (branch: GitBranch) => !branch.remote, | ||
| }); | ||
| if (branches === StepResultBreak) { | ||
| continue; | ||
| } | ||
|
|
||
| state.branch = branches.name; | ||
| } | ||
|
|
||
| if (!state.mergeBranch) { | ||
| state.mergeBranch = await this.container.git | ||
| .branches(state.repo.path) | ||
| .getBaseBranchName?.(state.branch); | ||
| } | ||
|
|
||
| const gitBranch = await state.repo.git.branches().getBranch(state.branch); | ||
| const detectedMergeTarget = gitBranch && (await getDetectedMergeTarget(this.container, gitBranch)); | ||
|
|
||
| const result = yield* pickOrResetBranchStep(state, context, { | ||
| picked: state.mergeBranch, | ||
| placeholder: 'Pick a merge target branch', | ||
| filter: (branch: GitBranch) => branch.remote && branch.name !== state.branch, | ||
| resetTitle: 'Reset Merge Target', | ||
| resetDescription: detectedMergeTarget ? `Reset to "${detectedMergeTarget}"` : '', | ||
| }); | ||
| if (result === StepResultBreak) { | ||
| continue; | ||
| } | ||
| if (state.branch) { | ||
| await this.container.git | ||
| .branches(state.repo.path) | ||
| .setUserMergeTargetBranchName?.(state.branch, result?.name); | ||
| } | ||
|
|
||
| endSteps(state); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function getDetectedMergeTarget( | ||
| container: Container, | ||
| branch: GitBranch, | ||
| options?: { cancellation?: CancellationToken }, | ||
| ): Promise<string | undefined> { | ||
| const [baseResult, defaultResult, targetResult] = await Promise.allSettled([ | ||
| container.git.branches(branch.repoPath).getBaseBranchName?.(branch.name), | ||
| container.git.branches(branch.repoPath).getDefaultBranchName(branch.getRemoteName()), | ||
| container.git.branches(branch.repoPath).getTargetBranchName?.(branch.name), | ||
| ]); | ||
|
|
||
| const baseBranchName = getSettledValue(baseResult); | ||
| const defaultBranchName = getSettledValue(defaultResult); | ||
| const targetMaybeResult = getSettledValue(targetResult); | ||
| const localValue = targetMaybeResult || baseBranchName || defaultBranchName; | ||
| if (localValue) { | ||
| return localValue; | ||
| } | ||
|
|
||
| // only if nothing found locally, try value from integration | ||
| return getIntegrationDefaultBranchName(container, branch.repoPath, options); | ||
| } | ||
|
|
||
| async function getIntegrationDefaultBranchName( | ||
| container: Container, | ||
| repoPath: string, | ||
| options?: { cancellation?: CancellationToken }, | ||
| ): Promise<string | undefined> { | ||
| const remote = await container.git.remotes(repoPath).getBestRemoteWithIntegration(); | ||
| if (remote == null) return undefined; | ||
|
|
||
| const integration = await remote.getIntegration(); | ||
| const defaultBranch = await integration?.getDefaultBranch?.(remote.provider.repoDesc, options); | ||
| return defaultBranch && `${remote.name}/${defaultBranch?.name}`; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.