|
| 1 | +import { window } from 'vscode'; |
| 2 | +import type { Sources } from '../constants.telemetry'; |
| 3 | +import type { Container } from '../container'; |
| 4 | +import { CommandQuickPickItem } from '../quickpicks/items/common'; |
| 5 | +import { ReferencesQuickPickIncludes, showReferencePicker } from '../quickpicks/referencePicker'; |
| 6 | +import { getBestRepositoryOrShowPicker } from '../quickpicks/repositoryPicker'; |
| 7 | +import { command, executeCommand } from '../system/-webview/command'; |
| 8 | +import { getNodeRepoPath } from '../views/nodes/abstract/viewNode'; |
| 9 | +import type { WebviewPanelShowCommandArgs } from '../webviews/webviewsController'; |
| 10 | +import { GlCommandBase } from './commandBase'; |
| 11 | +import type { CommandContext } from './commandContext'; |
| 12 | +import { isCommandContextViewNodeHasBranch } from './commandContext.utils'; |
| 13 | + |
| 14 | +export interface RecomposeBranchCommandArgs { |
| 15 | + repoPath?: string; |
| 16 | + branchName?: string; |
| 17 | + source?: Sources; |
| 18 | +} |
| 19 | + |
| 20 | +@command() |
| 21 | +export class RecomposeBranchCommand extends GlCommandBase { |
| 22 | + constructor(private readonly container: Container) { |
| 23 | + super(['gitlens.recomposeBranch', 'gitlens.recomposeBranch:views']); |
| 24 | + } |
| 25 | + |
| 26 | + protected override preExecute(context: CommandContext, args?: RecomposeBranchCommandArgs): Promise<void> { |
| 27 | + if (isCommandContextViewNodeHasBranch(context)) { |
| 28 | + args = { ...args }; |
| 29 | + args.repoPath = args.repoPath ?? getNodeRepoPath(context.node); |
| 30 | + args.branchName = args.branchName ?? context.node.branch.name; |
| 31 | + args.source = args.source ?? 'view'; |
| 32 | + } |
| 33 | + |
| 34 | + return this.execute(args); |
| 35 | + } |
| 36 | + |
| 37 | + async execute(args?: RecomposeBranchCommandArgs): Promise<void> { |
| 38 | + try { |
| 39 | + // Get repository path using picker fallback |
| 40 | + const repoPath = |
| 41 | + args?.repoPath ?? |
| 42 | + (await getBestRepositoryOrShowPicker(this.container, undefined, undefined, 'Recompose Branch'))?.path; |
| 43 | + if (!repoPath) return; |
| 44 | + |
| 45 | + args = { ...args }; |
| 46 | + |
| 47 | + // Get branch name using picker fallback |
| 48 | + let branchName = args.branchName; |
| 49 | + if (!branchName) { |
| 50 | + const pick = await showReferencePicker(repoPath, 'Recompose Branch', 'Choose a branch to recompose', { |
| 51 | + include: ReferencesQuickPickIncludes.Branches, |
| 52 | + sort: { branches: { current: true } }, |
| 53 | + }); |
| 54 | + if (pick == null || pick instanceof CommandQuickPickItem) return; |
| 55 | + |
| 56 | + if (pick.refType === 'branch') { |
| 57 | + branchName = pick.name; |
| 58 | + } else { |
| 59 | + return; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Validate that the repository exists |
| 64 | + const repo = this.container.git.getRepository(repoPath); |
| 65 | + if (!repo) { |
| 66 | + void window.showErrorMessage('Repository not found'); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + // Validate that the branch exists |
| 71 | + const branch = await repo.git.branches.getBranch(branchName); |
| 72 | + if (!branch) { |
| 73 | + void window.showErrorMessage(`Branch '${branchName}' not found`); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // Check if branch is remote-only |
| 78 | + if (branch.remote && !branch.upstream) { |
| 79 | + void window.showErrorMessage(`Cannot recompose remote-only branch '${branchName}'`); |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + // Open the composer with branch mode |
| 84 | + await executeCommand<WebviewPanelShowCommandArgs>('gitlens.showComposerPage', undefined, { |
| 85 | + repoPath: repoPath, |
| 86 | + source: args?.source, |
| 87 | + mode: 'preview', |
| 88 | + branchName: branchName, |
| 89 | + }); |
| 90 | + } catch (ex) { |
| 91 | + void window.showErrorMessage(`Failed to recompose branch: ${ex}`); |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments