commands.ts 8.9 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

J
Joao Moreno 已提交
8
import { Uri, commands, scm, Disposable, SCMResourceGroup, SCMResource, window, workspace, QuickPickItem, OutputChannel } from 'vscode';
J
Joao Moreno 已提交
9
import { IRef, RefType } from './git';
J
Joao Moreno 已提交
10
import { Model, Resource, Status } from './model';
J
Joao Moreno 已提交
11
import { decorate } from 'core-decorators';
J
Joao Moreno 已提交
12
import * as path from 'path';
J
Joao Moreno 已提交
13

J
Joao Moreno 已提交
14
function catchErrors(fn: (...args) => Promise<any>): (...args) => void {
J
Joao Moreno 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	return (...args) => fn.call(this, ...args).catch(async err => {
		if (err.gitErrorCode) {
			let message: string;

			switch (err.gitErrorCode) {
				case 'DirtyWorkTree':
					message = 'Please clean your repository working tree before checkout.';
					break;
				default:
					message = (err.stderr || err.message).replace(/^error: /, '');
					break;
			}

			const outputChannel = this.outputChannel as OutputChannel;
			const openOutputChannelChoice = 'Open Git Log';
			const choice = await window.showErrorMessage(message, openOutputChannelChoice);

			if (choice === openOutputChannelChoice) {
				outputChannel.show();
			}
		} else {
			console.error(err);
		}
	});
J
Joao Moreno 已提交
39
}
J
Joao Moreno 已提交
40

J
Joao Moreno 已提交
41 42 43 44
function resolveGitURI(uri: Uri): SCMResource | SCMResourceGroup | undefined {
	if (uri.authority !== 'git') {
		return;
	}
J
Joao Moreno 已提交
45

J
Joao Moreno 已提交
46
	return scm.getResourceFromURI(uri);
J
Joao Moreno 已提交
47 48
}

J
Joao Moreno 已提交
49 50
function resolveGitResource(uri: Uri): Resource | undefined {
	const resource = resolveGitURI(uri);
J
Joao Moreno 已提交
51

J
Joao Moreno 已提交
52 53 54
	if (!(resource instanceof Resource)) {
		return;
	}
J
Joao Moreno 已提交
55

J
Joao Moreno 已提交
56
	return resource;
J
Joao Moreno 已提交
57 58
}

J
Joao Moreno 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
class CheckoutItem implements QuickPickItem {

	protected get shortCommit(): string { return (this.ref.commit || '').substr(0, 8); }
	protected get treeish(): string | undefined { return this.ref.name; }
	get label(): string { return this.ref.name || this.shortCommit; }
	get description(): string { return this.shortCommit; }

	constructor(protected ref: IRef) { }

	async run(model: Model): Promise<void> {
		const ref = this.treeish;

		if (!ref) {
			return;
		}

		await model.checkout(ref);
	}
}

class CheckoutTagItem extends CheckoutItem {

	get description(): string { return `Tag at ${this.shortCommit}`; }
}

class CheckoutRemoteHeadItem extends CheckoutItem {

	get description(): string { return `Remote branch at ${this.shortCommit}`; }

	protected get treeish(): string | undefined {
		if (!this.ref.name) {
			return;
		}

		const match = /^[^/]+\/(.*)$/.exec(this.ref.name);
		return match ? match[1] : this.ref.name;
	}
}

J
Joao Moreno 已提交
98
export class CommandCenter {
J
Joao Moreno 已提交
99 100 101

	private disposables: Disposable[] = [];

J
Joao Moreno 已提交
102
	constructor(private model: Model, private outputChannel: OutputChannel) {
J
Joao Moreno 已提交
103 104 105 106 107 108 109 110 111 112
		this.disposables.push(
			commands.registerCommand('git.refresh', this.refresh, this),
			commands.registerCommand('git.openChange', this.openChange, this),
			commands.registerCommand('git.openFile', this.openFile, this),
			commands.registerCommand('git.stage', this.stage, this),
			commands.registerCommand('git.stageAll', this.stageAll, this),
			commands.registerCommand('git.unstage', this.unstage, this),
			commands.registerCommand('git.unstageAll', this.unstageAll, this),
			commands.registerCommand('git.clean', this.clean, this),
			commands.registerCommand('git.cleanAll', this.cleanAll, this),
J
Joao Moreno 已提交
113
			commands.registerCommand('git.checkout', this.checkout, this),
J
Joao Moreno 已提交
114
			commands.registerCommand('git.sync', this.sync, this),
J
Joao Moreno 已提交
115
			commands.registerCommand('git.publish', this.publish, this),
J
Joao Moreno 已提交
116
			commands.registerCommand('git.showOutput', this.showOutput, this),
J
Joao Moreno 已提交
117 118 119 120 121 122 123
		);
	}

	@decorate(catchErrors)
	async refresh(): Promise<void> {
		return await this.model.update();
	}
J
Joao Moreno 已提交
124

J
Joao Moreno 已提交
125 126
	@decorate(catchErrors)
	async openChange(uri: Uri): Promise<void> {
J
Joao Moreno 已提交
127
		const resource = resolveGitResource(uri);
J
Joao Moreno 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

		if (!resource) {
			return;
		}

		return this.open(resource);
	}

	async open(resource: Resource): Promise<void> {
		const left = this.getLeftResource(resource);
		const right = this.getRightResource(resource);
		const title = this.getTitle(resource);

		if (!left) {
			if (!right) {
				// TODO
				console.error('oh no');
				return;
			}

			return commands.executeCommand<void>('vscode.open', right);
		}

		return commands.executeCommand<void>('vscode.diff', left, right, title);
	}

	private getLeftResource(resource: Resource): Uri | undefined {
		switch (resource.type) {
			case Status.INDEX_MODIFIED:
			case Status.INDEX_RENAMED:
				return resource.uri.with({ scheme: 'git', query: 'HEAD' });

			case Status.MODIFIED:
				const uriString = resource.uri.toString();
				const [indexStatus] = this.model.indexGroup.resources.filter(r => r.uri.toString() === uriString);
				const query = indexStatus ? '~' : 'HEAD';
				return resource.uri.with({ scheme: 'git', query });
		}
J
Joao Moreno 已提交
166
	}
J
Joao Moreno 已提交
167

J
Joao Moreno 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
	private getRightResource(resource: Resource): Uri | undefined {
		switch (resource.type) {
			case Status.INDEX_MODIFIED:
			case Status.INDEX_ADDED:
			case Status.INDEX_COPIED:
			case Status.INDEX_RENAMED:
				return resource.uri.with({ scheme: 'git' });

			case Status.INDEX_DELETED:
			case Status.DELETED:
				return resource.uri.with({ scheme: 'git', query: 'HEAD' });

			case Status.MODIFIED:
			case Status.UNTRACKED:
			case Status.IGNORED:
			case Status.BOTH_MODIFIED:
				return resource.uri;
		}
	}

	private getTitle(resource: Resource): string {
		const basename = path.basename(resource.uri.fsPath);

		switch (resource.type) {
			case Status.INDEX_MODIFIED:
			case Status.INDEX_RENAMED:
				return `${basename} (Index)`;

			case Status.MODIFIED:
				return `${basename} (Working Tree)`;
		}

		return '';
	}

	@decorate(catchErrors)
	async openFile(uri: Uri): Promise<void> {
J
Joao Moreno 已提交
205
		const resource = resolveGitResource(uri);
J
Joao Moreno 已提交
206 207 208 209 210 211

		if (!resource) {
			return;
		}

		return commands.executeCommand<void>('vscode.open', resource.uri);
J
Joao Moreno 已提交
212 213
	}

J
Joao Moreno 已提交
214 215 216
	@decorate(catchErrors)
	async stage(uri: Uri): Promise<void> {
		const resource = resolveGitResource(uri);
J
Joao Moreno 已提交
217

J
Joao Moreno 已提交
218 219 220
		if (!resource) {
			return;
		}
J
Joao Moreno 已提交
221

J
Joao Moreno 已提交
222
		return await this.model.stage(resource);
J
Joao Moreno 已提交
223 224
	}

J
Joao Moreno 已提交
225 226 227 228
	@decorate(catchErrors)
	async stageAll(): Promise<void> {
		return await this.model.stage();
	}
J
Joao Moreno 已提交
229

J
Joao Moreno 已提交
230 231 232
	@decorate(catchErrors)
	async unstage(uri: Uri): Promise<void> {
		const resource = resolveGitResource(uri);
J
Joao Moreno 已提交
233

J
Joao Moreno 已提交
234
		if (!resource) {
J
Joao Moreno 已提交
235 236 237
			return;
		}

J
Joao Moreno 已提交
238 239 240 241 242 243 244
		return await this.model.unstage(resource);
	}

	@decorate(catchErrors)
	async unstageAll(): Promise<void> {
		return await this.model.unstage();
	}
J
Joao Moreno 已提交
245

J
Joao Moreno 已提交
246 247 248 249 250
	@decorate(catchErrors)
	async clean(uri: Uri): Promise<void> {
		const resource = resolveGitResource(uri);

		if (!resource) {
J
Joao Moreno 已提交
251 252
			return;
		}
J
Joao Moreno 已提交
253

J
Joao Moreno 已提交
254 255 256 257 258
		const basename = path.basename(resource.uri.fsPath);
		const message = `Are you sure you want to clean changes in ${basename}?`;
		const yes = 'Yes';
		const no = 'No, keep them';
		const pick = await window.showQuickPick([no, yes], { placeHolder: message });
J
Joao Moreno 已提交
259

J
Joao Moreno 已提交
260 261 262 263 264 265
		if (pick !== yes) {
			return;
		}

		return await this.model.clean(resource);
	}
J
Joao Moreno 已提交
266

J
Joao Moreno 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280
	@decorate(catchErrors)
	async cleanAll(): Promise<void> {
		const message = `Are you sure you want to clean all changes?`;
		const yes = 'Yes';
		const no = 'No, keep them';
		const pick = await window.showQuickPick([no, yes], { placeHolder: message });

		if (pick !== yes) {
			return;
		}

		return await this.model.clean(...this.model.workingTreeGroup.resources);
	}

J
Joao Moreno 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
	@decorate(catchErrors)
	async checkout(): Promise<void> {
		const config = workspace.getConfiguration('git');
		const checkoutType = config.get<string>('checkoutType');
		const includeTags = checkoutType === 'all' || checkoutType === 'tags';
		const includeRemotes = checkoutType === 'all' || checkoutType === 'remote';

		const heads = this.model.refs.filter(ref => ref.type === RefType.Head)
			.map(ref => new CheckoutItem(ref));

		const tags = (includeTags ? this.model.refs.filter(ref => ref.type === RefType.Tag) : [])
			.map(ref => new CheckoutTagItem(ref));

		const remoteHeads = (includeRemotes ? this.model.refs.filter(ref => ref.type === RefType.RemoteHead) : [])
			.map(ref => new CheckoutRemoteHeadItem(ref));

J
Joao Moreno 已提交
297 298 299
		const picks = [...heads, ...tags, ...remoteHeads];
		const placeHolder = 'Select a ref to checkout';
		const choice = await window.showQuickPick<CheckoutItem>(picks, { placeHolder });
J
Joao Moreno 已提交
300 301 302 303 304 305

		if (!choice) {
			return;
		}

		await choice.run(this.model);
J
Joao Moreno 已提交
306 307
	}

J
Joao Moreno 已提交
308 309 310 311 312
	@decorate(catchErrors)
	async sync(): Promise<void> {
		await this.model.sync();
	}

J
Joao Moreno 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326
	@decorate(catchErrors)
	async publish(): Promise<void> {
		const branchName = this.model.HEAD && this.model.HEAD.name || '';
		const picks = this.model.remotes.map(r => r.name);
		const placeHolder = `Pick a remote to publish the branch '${branchName}' to:`;
		const choice = await window.showQuickPick(picks, { placeHolder });

		if (!choice) {
			return;
		}

		await this.model.push(choice, branchName, { setUpstream: true });
	}

J
Joao Moreno 已提交
327 328 329 330
	showOutput(): void {
		this.outputChannel.show();
	}

J
Joao Moreno 已提交
331 332 333
	dispose(): void {
		this.disposables.forEach(d => d.dispose());
	}
J
Joao Moreno 已提交
334
}