commands.ts 16.4 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, computeDiff, Range } from 'vscode';
J
Joao Moreno 已提交
9
import { Ref, RefType } from './git';
J
Joao Moreno 已提交
10
import { Model, Resource, Status, CommitOptions } from './model';
J
Joao Moreno 已提交
11
import * as staging from './staging';
J
Joao Moreno 已提交
12
import * as path from 'path';
J
Joao Moreno 已提交
13 14 15
import * as nls from 'vscode-nls';

const localize = nls.loadMessageBundle();
J
Joao Moreno 已提交
16

J
Joao Moreno 已提交
17 18 19 20
function resolveGitURI(uri: Uri): SCMResource | SCMResourceGroup | undefined {
	if (uri.authority !== 'git') {
		return;
	}
J
Joao Moreno 已提交
21

J
Joao Moreno 已提交
22
	return scm.getResourceFromURI(uri);
J
Joao Moreno 已提交
23 24
}

J
Joao Moreno 已提交
25 26
function resolveGitResource(uri: Uri): Resource | undefined {
	const resource = resolveGitURI(uri);
J
Joao Moreno 已提交
27

J
Joao Moreno 已提交
28 29 30
	if (!(resource instanceof Resource)) {
		return;
	}
J
Joao Moreno 已提交
31

J
Joao Moreno 已提交
32
	return resource;
J
Joao Moreno 已提交
33 34
}

J
Joao Moreno 已提交
35 36 37 38 39 40 41
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; }

J
Joao Moreno 已提交
42
	constructor(protected ref: Ref) { }
J
Joao Moreno 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56

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

		if (!ref) {
			return;
		}

		await model.checkout(ref);
	}
}

class CheckoutTagItem extends CheckoutItem {

J
Joao Moreno 已提交
57 58 59
	get description(): string {
		return localize('tag at', "Tag at {0}", this.shortCommit);
	}
J
Joao Moreno 已提交
60 61 62 63
}

class CheckoutRemoteHeadItem extends CheckoutItem {

J
Joao Moreno 已提交
64 65 66
	get description(): string {
		return localize('remote branch at', "Remote branch at {0}", this.shortCommit);
	}
J
Joao Moreno 已提交
67 68 69 70 71 72 73 74 75 76 77

	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 已提交
78
const Commands: { commandId: string; method: Function; }[] = [];
J
Joao Moreno 已提交
79

J
Joao Moreno 已提交
80 81
function command(commandId: string): Function {
	return (target: any, key: string, descriptor: any) => {
J
Joao Moreno 已提交
82 83 84 85
		if (!(typeof descriptor.value === 'function')) {
			throw new Error('not supported');
		}

J
Joao Moreno 已提交
86 87 88
		Commands.push({ commandId, method: descriptor.value });
	};
}
J
Joao Moreno 已提交
89

J
Joao Moreno 已提交
90
export class CommandCenter {
J
Joao Moreno 已提交
91

J
Joao Moreno 已提交
92
	private model: Model;
J
Joao Moreno 已提交
93
	private disposables: Disposable[];
J
Joao Moreno 已提交
94

J
Joao Moreno 已提交
95
	constructor(
J
Joao Moreno 已提交
96
		model: Model | undefined,
J
Joao Moreno 已提交
97 98
		private outputChannel: OutputChannel
	) {
J
Joao Moreno 已提交
99 100 101 102
		if (model) {
			this.model = model;
		}

J
Joao Moreno 已提交
103 104
		this.disposables = Commands
			.map(({ commandId, method }) => commands.registerCommand(commandId, this.createCommand(method)));
J
Joao Moreno 已提交
105 106
	}

J
Joao Moreno 已提交
107
	@command('git.refresh')
J
Joao Moreno 已提交
108
	async refresh(): Promise<void> {
J
Joao Moreno 已提交
109
		await this.model.status();
J
Joao Moreno 已提交
110
	}
J
Joao Moreno 已提交
111

J
Joao Moreno 已提交
112 113 114 115 116
	async open(resource: Resource): Promise<void> {
		const left = this.getLeftResource(resource);
		const right = this.getRightResource(resource);
		const title = this.getTitle(resource);

J
Joao Moreno 已提交
117 118 119 120 121
		if (!right) {
			// TODO
			console.error('oh no');
			return;
		}
J
Joao Moreno 已提交
122

J
Joao Moreno 已提交
123
		if (!left) {
J
Joao Moreno 已提交
124
			return await commands.executeCommand<void>('vscode.open', right);
J
Joao Moreno 已提交
125 126
		}

J
Joao Moreno 已提交
127
		return await commands.executeCommand<void>('vscode.diff', left, right, title);
J
Joao Moreno 已提交
128 129 130 131 132 133
	}

	private getLeftResource(resource: Resource): Uri | undefined {
		switch (resource.type) {
			case Status.INDEX_MODIFIED:
			case Status.INDEX_RENAMED:
J
Joao Moreno 已提交
134
				return resource.original.with({ scheme: 'git', query: 'HEAD' });
J
Joao Moreno 已提交
135 136

			case Status.MODIFIED:
137
				return resource.uri.with({ scheme: 'git', query: '~' });
J
Joao Moreno 已提交
138
		}
J
Joao Moreno 已提交
139
	}
J
Joao Moreno 已提交
140

J
Joao Moreno 已提交
141 142 143 144 145
	private getRightResource(resource: Resource): Uri | undefined {
		switch (resource.type) {
			case Status.INDEX_MODIFIED:
			case Status.INDEX_ADDED:
			case Status.INDEX_COPIED:
J
Joao Moreno 已提交
146 147
				return resource.uri.with({ scheme: 'git' });

J
Joao Moreno 已提交
148 149 150 151 152 153 154 155 156 157
			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:
J
Joao Moreno 已提交
158 159 160 161 162 163 164 165 166
				const uriString = resource.uri.toString();
				const [indexStatus] = this.model.indexGroup.resources.filter(r => r.uri.toString() === uriString);

				if (indexStatus && indexStatus.rename) {
					return indexStatus.rename;
				}

				return resource.uri;

J
Joao Moreno 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
			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 '';
	}

J
Joao Moreno 已提交
187 188 189 190 191
	@command('git.init')
	async init(): Promise<void> {
		await this.model.init();
	}

J
Joao Moreno 已提交
192
	@command('git.openFile')
J
Joao Moreno 已提交
193
	async openFile(uri: Uri): Promise<void> {
J
Joao Moreno 已提交
194
		const scmResource = resolveGitResource(uri);
J
Joao Moreno 已提交
195

J
Joao Moreno 已提交
196 197
		if (scmResource) {
			return await commands.executeCommand<void>('vscode.open', scmResource.uri);
J
Joao Moreno 已提交
198 199
		}

J
Joao Moreno 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
		return await commands.executeCommand<void>('vscode.open', uri.with({ scheme: 'file' }));
	}

	@command('git.openChange')
	async openChange(uri: Uri): Promise<void> {
		const scmResource = resolveGitResource(uri);

		if (scmResource) {
			return await this.open(scmResource);
		}

		if (uri.scheme === 'file') {
			const uriString = uri.toString();
			const resource = this.model.workingTreeGroup.resources.filter(r => r.uri.toString() === uriString)[0]
				|| this.model.indexGroup.resources.filter(r => r.uri.toString() === uriString)[0];

			if (resource) {
				return await this.open(resource);
			}
		}
J
Joao Moreno 已提交
220 221
	}

J
Joao Moreno 已提交
222
	@command('git.stage')
J
Joao Moreno 已提交
223 224
	async stage(uri: Uri): Promise<void> {
		const resource = resolveGitResource(uri);
J
Joao Moreno 已提交
225

J
Joao Moreno 已提交
226 227 228
		if (!resource) {
			return;
		}
J
Joao Moreno 已提交
229

J
Joao Moreno 已提交
230
		return await this.model.add(resource);
J
Joao Moreno 已提交
231 232
	}

J
Joao Moreno 已提交
233
	@command('git.stageAll')
J
Joao Moreno 已提交
234
	async stageAll(): Promise<void> {
J
Joao Moreno 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
		return await this.model.add();
	}

	@command('git.stageSelectedRanges')
	async stageSelectedRanges(): Promise<void> {
		const textEditor = window.activeTextEditor;

		if (!textEditor) {
			return;
		}

		const modifiedDocument = textEditor.document;
		const modifiedUri = modifiedDocument.uri;
		const modifiedUriString = modifiedUri.toString();
		const resource = this.model.workingTreeGroup.resources.filter(r => r.uri.toString() === modifiedUriString)[0];
		const originalUri = this.getLeftResource(resource);

		if (!originalUri) {
			return;
		}

		const originalDocument = await workspace.openTextDocument(originalUri);
		const diffs = await computeDiff(originalDocument, modifiedDocument);
		const selections = textEditor.selections;

		const selectedDiffs = diffs.filter(diff => {
			const modifiedRange = diff.modifiedEndLineNumber === 0
				? new Range(diff.modifiedStartLineNumber - 1, 0, diff.modifiedStartLineNumber - 1, 0)
				: new Range(modifiedDocument.lineAt(diff.modifiedStartLineNumber - 1).range.start, modifiedDocument.lineAt(diff.modifiedEndLineNumber - 1).range.end);

			return selections.some(selection => !!selection.intersection(modifiedRange));
		});

		if (!selectedDiffs.length) {
			return;
		}

		const result = staging.applyChanges(originalDocument, modifiedDocument, selectedDiffs);
		await this.model.stage(modifiedUri, result);
J
Joao Moreno 已提交
274
	}
J
Joao Moreno 已提交
275

J
Joao Moreno 已提交
276
	@command('git.unstage')
J
Joao Moreno 已提交
277 278
	async unstage(uri: Uri): Promise<void> {
		const resource = resolveGitResource(uri);
J
Joao Moreno 已提交
279

J
Joao Moreno 已提交
280
		if (!resource) {
J
Joao Moreno 已提交
281 282 283
			return;
		}

J
Joao Moreno 已提交
284
		return await this.model.revertFiles(resource);
J
Joao Moreno 已提交
285 286
	}

J
Joao Moreno 已提交
287
	@command('git.unstageAll')
J
Joao Moreno 已提交
288
	async unstageAll(): Promise<void> {
J
Joao Moreno 已提交
289
		return await this.model.revertFiles();
J
Joao Moreno 已提交
290
	}
J
Joao Moreno 已提交
291

J
Joao Moreno 已提交
292
	@command('git.clean')
J
Joao Moreno 已提交
293 294 295 296
	async clean(uri: Uri): Promise<void> {
		const resource = resolveGitResource(uri);

		if (!resource) {
J
Joao Moreno 已提交
297 298
			return;
		}
J
Joao Moreno 已提交
299

J
Joao Moreno 已提交
300
		const basename = path.basename(resource.uri.fsPath);
J
Joao Moreno 已提交
301
		const message = localize('confirm clean', "Are you sure you want to clean changes in {0}?", basename);
J
Joao Moreno 已提交
302 303
		const yes = localize('clean', "Clean Changes");
		const pick = await window.showWarningMessage(message, { modal: true }, yes);
J
Joao Moreno 已提交
304

J
Joao Moreno 已提交
305 306 307 308
		if (pick !== yes) {
			return;
		}

J
Joao Moreno 已提交
309
		await this.model.clean(resource);
J
Joao Moreno 已提交
310
	}
J
Joao Moreno 已提交
311

J
Joao Moreno 已提交
312
	@command('git.cleanAll')
J
Joao Moreno 已提交
313
	async cleanAll(): Promise<void> {
J
Joao Moreno 已提交
314
		const message = localize('confirm clean all', "Are you sure you want to clean all changes?");
J
Joao Moreno 已提交
315 316
		const yes = localize('clean', "Clean Changes");
		const pick = await window.showWarningMessage(message, { modal: true }, yes);
J
Joao Moreno 已提交
317 318 319 320 321

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

J
Joao Moreno 已提交
322
		await this.model.clean(...this.model.workingTreeGroup.resources);
J
Joao Moreno 已提交
323 324
	}

J
Joao Moreno 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338
	private async smartCommit(
		getCommitMessage: () => Promise<string>,
		opts?: CommitOptions
	): Promise<boolean> {
		if (!opts) {
			opts = { all: this.model.indexGroup.resources.length === 0 };
		}

		if (
			// no changes
			(this.model.indexGroup.resources.length === 0 && this.model.workingTreeGroup.resources.length === 0)
			// or no staged changes and not `all`
			|| (!opts.all && this.model.indexGroup.resources.length === 0)
		) {
J
Joao Moreno 已提交
339 340 341 342
			window.showInformationMessage(localize('no changes', "There are no changes to commit."));
			return false;
		}

J
Joao Moreno 已提交
343
		const message = await getCommitMessage();
J
Joao Moreno 已提交
344 345 346 347 348 349

		if (!message) {
			// TODO@joao: show modal dialog to confirm empty message commit
			return false;
		}

J
Joao Moreno 已提交
350
		await this.model.commit(message, opts);
J
Joao Moreno 已提交
351 352 353 354

		return true;
	}

J
Joao Moreno 已提交
355
	private async commitWithAnyInput(opts?: CommitOptions): Promise<void> {
356
		const message = scm.inputBox.value;
J
Joao Moreno 已提交
357
		const getCommitMessage = async () => {
J
Joao Moreno 已提交
358 359 360 361 362 363 364 365
			if (message) {
				return message;
			}

			return await window.showInputBox({
				placeHolder: localize('commit message', "Commit message"),
				prompt: localize('provide commit message', "Please provide a commit message")
			});
J
Joao Moreno 已提交
366 367 368
		};

		const didCommit = await this.smartCommit(getCommitMessage, opts);
J
Joao Moreno 已提交
369 370

		if (message && didCommit) {
371
			scm.inputBox.value = '';
J
Joao Moreno 已提交
372
		}
J
Joao Moreno 已提交
373 374
	}

J
Joao Moreno 已提交
375
	@command('git.commit')
J
Joao Moreno 已提交
376 377 378 379
	async commit(): Promise<void> {
		await this.commitWithAnyInput();
	}

J
Joao Moreno 已提交
380
	@command('git.commitWithInput')
J
Joao Moreno 已提交
381
	async commitWithInput(): Promise<void> {
J
Joao Moreno 已提交
382
		const didCommit = await this.smartCommit(async () => scm.inputBox.value);
J
Joao Moreno 已提交
383 384

		if (didCommit) {
385
			scm.inputBox.value = '';
J
Joao Moreno 已提交
386
		}
J
Joao Moreno 已提交
387 388
	}

J
Joao Moreno 已提交
389
	@command('git.commitStaged')
J
Joao Moreno 已提交
390
	async commitStaged(): Promise<void> {
J
Joao Moreno 已提交
391
		await this.commitWithAnyInput({ all: false });
J
Joao Moreno 已提交
392 393
	}

J
Joao Moreno 已提交
394
	@command('git.commitStagedSigned')
J
Joao Moreno 已提交
395
	async commitStagedSigned(): Promise<void> {
J
Joao Moreno 已提交
396
		await this.commitWithAnyInput({ all: false, signoff: true });
J
Joao Moreno 已提交
397 398
	}

J
Joao Moreno 已提交
399
	@command('git.commitAll')
J
Joao Moreno 已提交
400
	async commitAll(): Promise<void> {
J
Joao Moreno 已提交
401
		await this.commitWithAnyInput({ all: true });
J
Joao Moreno 已提交
402 403
	}

J
Joao Moreno 已提交
404
	@command('git.commitAllSigned')
J
Joao Moreno 已提交
405
	async commitAllSigned(): Promise<void> {
J
Joao Moreno 已提交
406
		await this.commitWithAnyInput({ all: true, signoff: true });
J
Joao Moreno 已提交
407 408
	}

J
Joao Moreno 已提交
409
	@command('git.undoCommit')
J
Joao Moreno 已提交
410
	async undoCommit(): Promise<void> {
J
Joao Moreno 已提交
411 412 413 414 415 416 417 418 419
		const HEAD = this.model.HEAD;

		if (!HEAD || !HEAD.commit) {
			return;
		}

		const commit = await this.model.getCommit('HEAD');
		await this.model.reset('HEAD~');
		scm.inputBox.value = commit.message;
J
Joao Moreno 已提交
420 421
	}

J
Joao Moreno 已提交
422
	@command('git.checkout')
J
Joao Moreno 已提交
423 424
	async checkout(): Promise<void> {
		const config = workspace.getConfiguration('git');
J
Joao Moreno 已提交
425
		const checkoutType = config.get<string>('checkoutType') || 'all';
J
Joao Moreno 已提交
426 427 428 429 430 431 432 433 434 435 436 437
		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 已提交
438 439 440
		const picks = [...heads, ...tags, ...remoteHeads];
		const placeHolder = 'Select a ref to checkout';
		const choice = await window.showQuickPick<CheckoutItem>(picks, { placeHolder });
J
Joao Moreno 已提交
441 442 443 444 445 446

		if (!choice) {
			return;
		}

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

J
Joao Moreno 已提交
449
	@command('git.branch')
J
Joao Moreno 已提交
450 451
	async branch(): Promise<void> {
		const result = await window.showInputBox({
J
Joao Moreno 已提交
452 453
			placeHolder: localize('branch name', "Branch name"),
			prompt: localize('provide branch name', "Please provide a branch name")
J
Joao Moreno 已提交
454
		});
J
Joao Moreno 已提交
455

J
Joao Moreno 已提交
456 457 458
		if (!result) {
			return;
		}
J
Joao Moreno 已提交
459

J
Joao Moreno 已提交
460 461
		const name = result.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$/g, '-');
		await this.model.branch(name);
J
Joao Moreno 已提交
462 463
	}

J
Joao Moreno 已提交
464
	@command('git.pull')
J
Joao Moreno 已提交
465
	async pull(): Promise<void> {
J
Joao Moreno 已提交
466 467 468 469 470 471 472 473
		const remotes = this.model.remotes;

		if (remotes.length === 0) {
			window.showWarningMessage(localize('no remotes to pull', "Your repository has no remotes configured to pull from."));
			return;
		}

		await this.model.pull();
J
Joao Moreno 已提交
474 475
	}

J
Joao Moreno 已提交
476
	@command('git.pullRebase')
J
Joao Moreno 已提交
477
	async pullRebase(): Promise<void> {
J
Joao Moreno 已提交
478 479 480 481 482 483 484 485
		const remotes = this.model.remotes;

		if (remotes.length === 0) {
			window.showWarningMessage(localize('no remotes to pull', "Your repository has no remotes configured to pull from."));
			return;
		}

		await this.model.pull(true);
J
Joao Moreno 已提交
486 487
	}

J
Joao Moreno 已提交
488
	@command('git.push')
J
Joao Moreno 已提交
489
	async push(): Promise<void> {
J
Joao Moreno 已提交
490 491 492 493 494 495 496 497
		const remotes = this.model.remotes;

		if (remotes.length === 0) {
			window.showWarningMessage(localize('no remotes to push', "Your repository has no remotes configured to push to."));
			return;
		}

		await this.model.push();
J
Joao Moreno 已提交
498 499
	}

J
Joao Moreno 已提交
500
	@command('git.pushTo')
J
Joao Moreno 已提交
501
	async pushTo(): Promise<void> {
J
Joao Moreno 已提交
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
		const remotes = this.model.remotes;

		if (remotes.length === 0) {
			window.showWarningMessage(localize('no remotes to push', "Your repository has no remotes configured to push to."));
			return;
		}

		if (!this.model.HEAD || !this.model.HEAD.name) {
			window.showWarningMessage(localize('nobranch', "Please check out a branch to push to a remote."));
			return;
		}

		const branchName = this.model.HEAD.name;
		const picks = remotes.map(r => ({ label: r.name, description: r.url }));
		const placeHolder = localize('pick remote', "Pick a remote to publish the branch '{0}' to:", branchName);
		const pick = await window.showQuickPick(picks, { placeHolder });

		if (!pick) {
			return;
		}

		this.model.push(pick.label, branchName);
J
Joao Moreno 已提交
524 525
	}

J
Joao Moreno 已提交
526
	@command('git.sync')
J
Joao Moreno 已提交
527
	async sync(): Promise<void> {
J
Joao Moreno 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
		const HEAD = this.model.HEAD;

		if (!HEAD || !HEAD.upstream) {
			return;
		}

		const config = workspace.getConfiguration('git');
		const shouldPrompt = config.get<boolean>('confirmSync') === true;

		if (shouldPrompt) {
			const message = localize('sync is unpredictable', "This action will push and pull commits to and from '{0}'.", HEAD.upstream);
			const yes = localize('ok', "OK");
			const neverAgain = localize('never again', "OK, Never Show Again");
			const pick = await window.showWarningMessage(message, { modal: true }, yes, neverAgain);

			if (pick === neverAgain) {
				await config.update('confirmSync', false, true);
			} else if (pick !== yes) {
				return;
			}
		}

J
Joao Moreno 已提交
550 551 552
		await this.model.sync();
	}

J
Joao Moreno 已提交
553
	@command('git.publish')
J
Joao Moreno 已提交
554
	async publish(): Promise<void> {
J
Joao Moreno 已提交
555 556 557 558 559 560 561
		const remotes = this.model.remotes;

		if (remotes.length === 0) {
			window.showWarningMessage(localize('no remotes to publish', "Your repository has no remotes configured to publish to."));
			return;
		}

J
Joao Moreno 已提交
562 563
		const branchName = this.model.HEAD && this.model.HEAD.name || '';
		const picks = this.model.remotes.map(r => r.name);
J
Joao Moreno 已提交
564
		const placeHolder = localize('pick remote', "Pick a remote to publish the branch '{0}' to:", branchName);
J
Joao Moreno 已提交
565 566 567 568 569 570 571 572 573
		const choice = await window.showQuickPick(picks, { placeHolder });

		if (!choice) {
			return;
		}

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

J
Joao Moreno 已提交
574
	@command('git.showOutput')
J
Joao Moreno 已提交
575 576 577 578
	showOutput(): void {
		this.outputChannel.show();
	}

J
Joao Moreno 已提交
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	private createCommand(method: Function): (...args: any[]) => any {
		return (...args) => {
			if (!this.model) {
				window.showInformationMessage(localize('disabled', "Git is either disabled or not supported in this workspace"));
				return;
			}

			const result = Promise.resolve(method.apply(this, args));

			return result.catch(async err => {
				let message: string;

				switch (err.gitErrorCode) {
					case 'DirtyWorkTree':
						message = localize('clean repo', "Please clean your repository working tree before checkout.");
						break;
					default:
						const lines = (err.stderr || err.message || String(err))
							.replace(/^error: /, '')
							.split(/[\r\n]/)
							.filter(line => !!line);

						message = lines[0] || 'Git error';
						break;
				}

				if (!message) {
					console.error(err);
					return;
				}

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

				if (choice === openOutputChannelChoice) {
					outputChannel.show();
				}
			});
		};
	}

J
Joao Moreno 已提交
621 622 623
	dispose(): void {
		this.disposables.forEach(d => d.dispose());
	}
J
Joao Moreno 已提交
624
}