| 
 | 1 | +import type { Disposable } from 'vscode';  | 
 | 2 | +import type { CompareWithCommandArgs } from '../../../../commands/compareWith';  | 
 | 3 | +import type { Container } from '../../../../container';  | 
 | 4 | +import { cherryPick, merge, rebase } from '../../../../git/actions/repository';  | 
 | 5 | +import type { Repository } from '../../../../git/models/repository';  | 
 | 6 | +import { executeCommand } from '../../../../system/-webview/command';  | 
 | 7 | +import type { CliCommandRequest, CliCommandResponse, CliIpcServer } from './integration';  | 
 | 8 | + | 
 | 9 | +interface CliCommand {  | 
 | 10 | +	command: string;  | 
 | 11 | +	handler: (request: CliCommandRequest, repo?: Repository | undefined) => Promise<CliCommandResponse>;  | 
 | 12 | +}  | 
 | 13 | + | 
 | 14 | +const commandHandlers: CliCommand[] = [];  | 
 | 15 | +function command(command: string) {  | 
 | 16 | +	return function (  | 
 | 17 | +		target: unknown,  | 
 | 18 | +		contextOrKey?: string | ClassMethodDecoratorContext,  | 
 | 19 | +		descriptor?: PropertyDescriptor,  | 
 | 20 | +	) {  | 
 | 21 | +		// ES Decorator  | 
 | 22 | +		if (contextOrKey && typeof contextOrKey === 'object' && 'kind' in contextOrKey) {  | 
 | 23 | +			if (contextOrKey.kind !== 'method') {  | 
 | 24 | +				throw new Error('The command decorator can only be applied to methods');  | 
 | 25 | +			}  | 
 | 26 | + | 
 | 27 | +			commandHandlers.push({ command: command, handler: target as CliCommand['handler'] });  | 
 | 28 | +			return;  | 
 | 29 | +		}  | 
 | 30 | + | 
 | 31 | +		// TypeScript experimental decorator  | 
 | 32 | +		if (descriptor) {  | 
 | 33 | +			commandHandlers.push({ command: command, handler: descriptor.value as CliCommand['handler'] });  | 
 | 34 | +			return descriptor;  | 
 | 35 | +		}  | 
 | 36 | + | 
 | 37 | +		throw new Error('Invalid decorator usage');  | 
 | 38 | +	};  | 
 | 39 | +}  | 
 | 40 | + | 
 | 41 | +export class CliCommandHandlers implements Disposable {  | 
 | 42 | +	constructor(  | 
 | 43 | +		private readonly container: Container,  | 
 | 44 | +		private readonly server: CliIpcServer,  | 
 | 45 | +	) {  | 
 | 46 | +		for (const { command, handler } of commandHandlers) {  | 
 | 47 | +			this.server.registerHandler(command, rq => this.wrapHandler(rq, handler));  | 
 | 48 | +		}  | 
 | 49 | +	}  | 
 | 50 | + | 
 | 51 | +	dispose(): void {}  | 
 | 52 | + | 
 | 53 | +	private wrapHandler(  | 
 | 54 | +		request: CliCommandRequest,  | 
 | 55 | +		handler: (request: CliCommandRequest, repo?: Repository | undefined) => Promise<CliCommandResponse>,  | 
 | 56 | +	) {  | 
 | 57 | +		let repo: Repository | undefined;  | 
 | 58 | +		if (request?.cwd) {  | 
 | 59 | +			repo = this.container.git.getRepository(request.cwd);  | 
 | 60 | +		}  | 
 | 61 | + | 
 | 62 | +		return handler(request, repo);  | 
 | 63 | +	}  | 
 | 64 | + | 
 | 65 | +	@command('cherry-pick')  | 
 | 66 | +	async handleCherryPickCommand(  | 
 | 67 | +		_request: CliCommandRequest,  | 
 | 68 | +		repo?: Repository | undefined,  | 
 | 69 | +	): Promise<CliCommandResponse> {  | 
 | 70 | +		return cherryPick(repo);  | 
 | 71 | +	}  | 
 | 72 | + | 
 | 73 | +	@command('compare')  | 
 | 74 | +	async handleCompareCommand(  | 
 | 75 | +		_request: CliCommandRequest,  | 
 | 76 | +		repo?: Repository | undefined,  | 
 | 77 | +	): Promise<CliCommandResponse> {  | 
 | 78 | +		if (!repo || !_request.args?.length) {  | 
 | 79 | +			await executeCommand('gitlens.compareWith');  | 
 | 80 | +			return;  | 
 | 81 | +		}  | 
 | 82 | + | 
 | 83 | +		const [ref1, ref2] = _request.args;  | 
 | 84 | +		if (!ref1 || !ref2) {  | 
 | 85 | +			await executeCommand('gitlens.compareWith');  | 
 | 86 | +			return;  | 
 | 87 | +		}  | 
 | 88 | + | 
 | 89 | +		if (ref1) {  | 
 | 90 | +			if (!(await repo.git.refs().validateReference(ref1))) {  | 
 | 91 | +				// TODO: Send an error back to the CLI?  | 
 | 92 | +				await executeCommand('gitlens.compareWith');  | 
 | 93 | +				return;  | 
 | 94 | +			}  | 
 | 95 | +		}  | 
 | 96 | + | 
 | 97 | +		if (ref2) {  | 
 | 98 | +			if (!(await repo.git.refs().validateReference(ref2))) {  | 
 | 99 | +				// TODO: Send an error back to the CLI?  | 
 | 100 | +				await executeCommand<CompareWithCommandArgs>('gitlens.compareWith', { ref1: ref1 });  | 
 | 101 | +				return;  | 
 | 102 | +			}  | 
 | 103 | +		}  | 
 | 104 | + | 
 | 105 | +		await executeCommand<CompareWithCommandArgs>('gitlens.compareWith', { ref1: ref1, ref2: ref2 });  | 
 | 106 | +	}  | 
 | 107 | + | 
 | 108 | +	@command('graph')  | 
 | 109 | +	async handleGraphCommand(request: CliCommandRequest, repo?: Repository | undefined): Promise<CliCommandResponse> {  | 
 | 110 | +		if (!repo || !request.args?.length) {  | 
 | 111 | +			await executeCommand('gitlens.showGraphView');  | 
 | 112 | +			return;  | 
 | 113 | +		}  | 
 | 114 | + | 
 | 115 | +		const [ref] = request.args;  | 
 | 116 | +		const reference = await repo.git.refs().getReference(ref);  | 
 | 117 | +		if (ref && !reference) {  | 
 | 118 | +			// TODO: Send an error back to the CLI?  | 
 | 119 | +			await executeCommand('gitlens.showInCommitGraph', repo);  | 
 | 120 | +			return;  | 
 | 121 | +		}  | 
 | 122 | + | 
 | 123 | +		await executeCommand('gitlens.showInCommitGraph', { ref: reference });  | 
 | 124 | +	}  | 
 | 125 | + | 
 | 126 | +	@command('merge')  | 
 | 127 | +	async handleMergeCommand(request: CliCommandRequest, repo?: Repository | undefined): Promise<CliCommandResponse> {  | 
 | 128 | +		if (!repo || !request.args?.length) return merge(repo);  | 
 | 129 | + | 
 | 130 | +		const [ref] = request.args;  | 
 | 131 | +		const reference = await repo.git.refs().getReference(ref);  | 
 | 132 | +		if (ref && !reference) {  | 
 | 133 | +			// TODO: Send an error back to the CLI?  | 
 | 134 | +		}  | 
 | 135 | +		return merge(repo, reference);  | 
 | 136 | +	}  | 
 | 137 | + | 
 | 138 | +	@command('rebase')  | 
 | 139 | +	async handleRebaseCommand(request: CliCommandRequest, repo?: Repository | undefined): Promise<CliCommandResponse> {  | 
 | 140 | +		if (!repo || !request.args?.length) return rebase(repo);  | 
 | 141 | + | 
 | 142 | +		const [ref] = request.args;  | 
 | 143 | +		const reference = await repo.git.refs().getReference(ref);  | 
 | 144 | +		if (ref && !reference) {  | 
 | 145 | +			// TODO: Send an error back to the CLI?  | 
 | 146 | +		}  | 
 | 147 | + | 
 | 148 | +		return rebase(repo, reference);  | 
 | 149 | +	}  | 
 | 150 | +}  | 
0 commit comments