commands.ts 32.1 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, window, workspace, QuickPickItem, OutputChannel, Range, WorkspaceEdit, Position, LineChange, SourceControlResourceState, TextDocumentShowOptions, ViewColumn } from 'vscode';
J
Joao Moreno 已提交
9
import { Ref, RefType, Git, GitErrorCodes, Branch } from './git';
10
import { Model, Resource, Status, CommitOptions, WorkingTreeGroup, IndexGroup, MergeGroup } from './model';
J
Joao Moreno 已提交
11
import { toGitUri, fromGitUri } from './uri';
12
import { applyLineChanges, intersectDiffWithRange, toLineRanges, invertLineChange } from './staging';
J
Joao Moreno 已提交
13
import * as path from 'path';
J
Joao Moreno 已提交
14
import * as os from 'os';
J
Joao Moreno 已提交
15
import TelemetryReporter from 'vscode-extension-telemetry';
J
Joao Moreno 已提交
16 17 18
import * as nls from 'vscode-nls';

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

J
Joao Moreno 已提交
20 21 22 23 24 25 26
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 已提交
27
	constructor(protected ref: Ref) { }
J
Joao Moreno 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41

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

		if (!ref) {
			return;
		}

		await model.checkout(ref);
	}
}

class CheckoutTagItem extends CheckoutItem {

J
Joao Moreno 已提交
42 43 44
	get description(): string {
		return localize('tag at', "Tag at {0}", this.shortCommit);
	}
J
Joao Moreno 已提交
45 46 47 48
}

class CheckoutRemoteHeadItem extends CheckoutItem {

J
Joao Moreno 已提交
49 50 51
	get description(): string {
		return localize('remote branch at', "Remote branch at {0}", this.shortCommit);
	}
J
Joao Moreno 已提交
52 53 54 55 56 57 58 59 60 61 62

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

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

M
Maik Riechert 已提交
63 64
class BranchDeleteItem implements QuickPickItem {

65 66 67
	private get shortCommit(): string { return (this.ref.commit || '').substr(0, 8); }
	get branchName(): string | undefined { return this.ref.name; }
	get label(): string { return this.branchName || ''; }
M
Maik Riechert 已提交
68 69
	get description(): string { return this.shortCommit; }

70
	constructor(private ref: Ref) { }
M
Maik Riechert 已提交
71

72 73
	async run(model: Model, force?: boolean): Promise<void> {
		if (!this.branchName) {
M
Maik Riechert 已提交
74 75
			return;
		}
76
		await model.deleteBranch(this.branchName, force);
M
Maik Riechert 已提交
77 78 79
	}
}

80 81 82 83 84 85
class MergeItem implements QuickPickItem {

	get label(): string { return this.ref.name || ''; }
	get description(): string { return this.ref.name || ''; }

	constructor(protected ref: Ref) { }
J
Joao Moreno 已提交
86 87

	async run(model: Model): Promise<void> {
J
Joao Moreno 已提交
88
		await model.merge(this.ref.name! || this.ref.commit!);
J
Joao Moreno 已提交
89
	}
90 91
}

J
Joao Moreno 已提交
92 93 94 95 96 97 98 99 100 101
class CreateBranchItem implements QuickPickItem {

	get label(): string { return localize('create branch', '$(plus) Create new branch'); }
	get description(): string { return ''; }

	async run(model: Model): Promise<void> {
		await commands.executeCommand('git.branch');
	}
}

102 103 104 105 106 107 108 109 110
interface Command {
	commandId: string;
	key: string;
	method: Function;
	skipModelCheck: boolean;
	requiresDiffInformation: boolean;
}

const Commands: Command[] = [];
J
Joao Moreno 已提交
111

112
function command(commandId: string, skipModelCheck = false, requiresDiffInformation = false): Function {
J
Joao Moreno 已提交
113
	return (target: any, key: string, descriptor: any) => {
J
Joao Moreno 已提交
114 115 116 117
		if (!(typeof descriptor.value === 'function')) {
			throw new Error('not supported');
		}

118
		Commands.push({ commandId, key, method: descriptor.value, skipModelCheck, requiresDiffInformation });
J
Joao Moreno 已提交
119 120
	};
}
J
Joao Moreno 已提交
121

J
Joao Moreno 已提交
122
export class CommandCenter {
J
Joao Moreno 已提交
123

J
Joao Moreno 已提交
124
	private model: Model;
J
Joao Moreno 已提交
125
	private disposables: Disposable[];
J
Joao Moreno 已提交
126

J
Joao Moreno 已提交
127
	constructor(
J
Joao Moreno 已提交
128
		private git: Git,
J
Joao Moreno 已提交
129
		model: Model | undefined,
J
Joao Moreno 已提交
130 131
		private outputChannel: OutputChannel,
		private telemetryReporter: TelemetryReporter
J
Joao Moreno 已提交
132
	) {
J
Joao Moreno 已提交
133 134 135 136
		if (model) {
			this.model = model;
		}

J
Joao Moreno 已提交
137
		this.disposables = Commands
138 139 140 141 142 143 144 145 146
			.map(({ commandId, key, method, skipModelCheck, requiresDiffInformation }) => {
				const command = this.createCommand(commandId, key, method, skipModelCheck);

				if (requiresDiffInformation) {
					return commands.registerDiffInformationCommand(commandId, command);
				} else {
					return commands.registerCommand(commandId, command);
				}
			});
J
Joao Moreno 已提交
147 148
	}

J
Joao Moreno 已提交
149
	@command('git.refresh')
J
Joao Moreno 已提交
150
	async refresh(): Promise<void> {
J
Joao Moreno 已提交
151
		await this.model.status();
J
Joao Moreno 已提交
152
	}
J
Joao Moreno 已提交
153

J
Joao Moreno 已提交
154 155 156 157 158 159
	@command('git.openResource')
	async openResource(resource: Resource): Promise<void> {
		await this._openResource(resource);
	}

	private async _openResource(resource: Resource): Promise<void> {
J
Joao Moreno 已提交
160 161 162 163
		const left = this.getLeftResource(resource);
		const right = this.getRightResource(resource);
		const title = this.getTitle(resource);

J
Joao Moreno 已提交
164 165 166 167 168
		if (!right) {
			// TODO
			console.error('oh no');
			return;
		}
J
Joao Moreno 已提交
169

J
Joao Moreno 已提交
170 171
		const viewColumn = window.activeTextEditor && window.activeTextEditor.viewColumn || ViewColumn.One;

J
Joao Moreno 已提交
172
		if (!left) {
J
Joao Moreno 已提交
173
			return await commands.executeCommand<void>('vscode.open', right, viewColumn);
J
Joao Moreno 已提交
174 175
		}

J
Joao Moreno 已提交
176 177 178 179
		const opts: TextDocumentShowOptions = {
			viewColumn
		};

J
Joao Moreno 已提交
180 181 182 183 184 185
		const activeTextEditor = window.activeTextEditor;

		if (activeTextEditor && activeTextEditor.document.uri.toString() === right.toString()) {
			opts.selection = activeTextEditor.selection;
		}

J
Joao Moreno 已提交
186
		return await commands.executeCommand<void>('vscode.diff', left, right, title, opts);
J
Joao Moreno 已提交
187 188 189 190 191 192
	}

	private getLeftResource(resource: Resource): Uri | undefined {
		switch (resource.type) {
			case Status.INDEX_MODIFIED:
			case Status.INDEX_RENAMED:
J
Joao Moreno 已提交
193
				return toGitUri(resource.original, 'HEAD');
J
Joao Moreno 已提交
194 195

			case Status.MODIFIED:
J
Joao Moreno 已提交
196
				return toGitUri(resource.resourceUri, '~');
J
Joao Moreno 已提交
197
		}
J
Joao Moreno 已提交
198
	}
J
Joao Moreno 已提交
199

J
Joao Moreno 已提交
200 201 202 203 204 205
	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:
J
Joao Moreno 已提交
206
				return toGitUri(resource.resourceUri, '');
J
Joao Moreno 已提交
207 208 209

			case Status.INDEX_DELETED:
			case Status.DELETED:
J
Joao Moreno 已提交
210
				return toGitUri(resource.resourceUri, 'HEAD');
J
Joao Moreno 已提交
211 212 213 214

			case Status.MODIFIED:
			case Status.UNTRACKED:
			case Status.IGNORED:
J
Joao Moreno 已提交
215 216
				const uriString = resource.resourceUri.toString();
				const [indexStatus] = this.model.indexGroup.resources.filter(r => r.resourceUri.toString() === uriString);
J
Joao Moreno 已提交
217

J
Joao Moreno 已提交
218 219
				if (indexStatus && indexStatus.renameResourceUri) {
					return indexStatus.renameResourceUri;
J
Joao Moreno 已提交
220 221
				}

J
Joao Moreno 已提交
222
				return resource.resourceUri;
J
Joao Moreno 已提交
223

J
Joao Moreno 已提交
224
			case Status.BOTH_MODIFIED:
J
Joao Moreno 已提交
225
				return resource.resourceUri;
J
Joao Moreno 已提交
226 227 228 229
		}
	}

	private getTitle(resource: Resource): string {
J
Joao Moreno 已提交
230
		const basename = path.basename(resource.resourceUri.fsPath);
J
Joao Moreno 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243

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

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

		return '';
	}

244 245
	@command('git.clone', true)
	async clone(): Promise<void> {
J
Joao Moreno 已提交
246
		const url = await window.showInputBox({
J
Joao Moreno 已提交
247 248
			prompt: localize('repourl', "Repository URL"),
			ignoreFocusOut: true
J
Joao Moreno 已提交
249 250 251
		});

		if (!url) {
252 253
			this.telemetryReporter.sendTelemetryEvent('clone', { outcome: 'no_URL' });
			return;
J
Joao Moreno 已提交
254 255
		}

256
		const config = workspace.getConfiguration('git');
J
Joao Moreno 已提交
257
		const value = config.get<string>('defaultCloneDirectory') || os.homedir();
258

J
Joao Moreno 已提交
259 260
		const parentPath = await window.showInputBox({
			prompt: localize('parent', "Parent Directory"),
J
Joao Moreno 已提交
261
			value,
J
Joao Moreno 已提交
262
			ignoreFocusOut: true
J
Joao Moreno 已提交
263 264 265
		});

		if (!parentPath) {
266 267
			this.telemetryReporter.sendTelemetryEvent('clone', { outcome: 'no_directory' });
			return;
J
Joao Moreno 已提交
268 269
		}

J
Joao Moreno 已提交
270
		const clonePromise = this.git.clone(url, parentPath);
J
Joao Moreno 已提交
271 272
		window.setStatusBarMessage(localize('cloning', "Cloning git repository..."), clonePromise);

273
		try {
274 275 276 277 278 279 280 281 282 283
			const repositoryPath = await clonePromise;

			const open = localize('openrepo', "Open Repository");
			const result = await window.showInformationMessage(localize('proposeopen', "Would you like to open the cloned repository?"), open);

			const openFolder = result === open;
			this.telemetryReporter.sendTelemetryEvent('clone', { outcome: 'success' }, { openFolder: openFolder ? 1 : 0 });
			if (openFolder) {
				commands.executeCommand('vscode.openFolder', Uri.file(repositoryPath));
			}
284 285
		} catch (err) {
			if (/already exists and is not an empty directory/.test(err && err.stderr || '')) {
286 287 288
				this.telemetryReporter.sendTelemetryEvent('clone', { outcome: 'directory_not_empty' });
			} else {
				this.telemetryReporter.sendTelemetryEvent('clone', { outcome: 'error' });
289 290
			}
			throw err;
J
Joao Moreno 已提交
291 292 293
		}
	}

J
Joao Moreno 已提交
294 295 296 297 298
	@command('git.init')
	async init(): Promise<void> {
		await this.model.init();
	}

J
Joao Moreno 已提交
299
	@command('git.openFile')
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
	async openFile(arg?: Resource | Uri): Promise<void> {
		let uri: Uri | undefined;

		if (arg instanceof Uri) {
			if (arg.scheme === 'git') {
				uri = Uri.file(fromGitUri(arg).path);
			} else if (arg.scheme === 'file') {
				uri = arg;
			}
		} else {
			let resource = arg;

			if (!(resource instanceof Resource)) {
				// can happen when called from a keybinding
				resource = this.getSCMResource();
			}

			if (resource) {
				uri = resource.resourceUri;
			}
J
Joao Moreno 已提交
320 321
		}

322
		if (!uri) {
J
Joao Moreno 已提交
323
			return;
J
Joao Moreno 已提交
324 325
		}

J
Joao Moreno 已提交
326 327 328 329
		const activeTextEditor = window.activeTextEditor && window.activeTextEditor;
		const isSameUri = activeTextEditor && activeTextEditor.document.uri.toString() === uri.toString();
		const selections = activeTextEditor && activeTextEditor.selections;
		const viewColumn = activeTextEditor && activeTextEditor.viewColumn || ViewColumn.One;
J
Joao Moreno 已提交
330

J
Joao Moreno 已提交
331 332 333 334 335
		await commands.executeCommand<void>('vscode.open', uri, viewColumn);

		if (isSameUri && selections && window.activeTextEditor) {
			window.activeTextEditor.selections = selections;
		}
J
Joao Moreno 已提交
336 337
	}

J
Joao Moreno 已提交
338 339
	@command('git.openHEADFile')
	async openHEADFile(arg?: Resource | Uri): Promise<void> {
D
Duroktar 已提交
340 341 342 343 344 345 346 347 348 349 350 351 352 353
		let resource: Resource | undefined = undefined;

		if (arg instanceof Resource) {
			resource = arg;
		} else if (arg instanceof Uri) {
			resource = this.getSCMResource(arg);
		} else {
			resource = this.getSCMResource();
		}

		if (!resource) {
			return;
		}

J
Joao Moreno 已提交
354
		const HEAD = this.getLeftResource(resource);
D
Duroktar 已提交
355

J
Joao Moreno 已提交
356 357 358
		if (!HEAD) {
			window.showWarningMessage(localize('HEAD not available', "HEAD version of '{0}' is not available.", path.basename(resource.resourceUri.fsPath)));
			return;
D
Duroktar 已提交
359
		}
J
Joao Moreno 已提交
360 361

		return await commands.executeCommand<void>('vscode.open', HEAD);
D
Duroktar 已提交
362 363
	}

J
Joao Moreno 已提交
364
	@command('git.openChange')
365 366 367 368 369 370 371 372
	async openChange(arg?: Resource | Uri): Promise<void> {
		let resource: Resource | undefined = undefined;

		if (arg instanceof Resource) {
			resource = arg;
		} else if (arg instanceof Uri) {
			resource = this.getSCMResource(arg);
		} else {
J
Joao Moreno 已提交
373 374 375
			resource = this.getSCMResource();
		}

J
Joao Moreno 已提交
376 377
		if (!resource) {
			return;
J
Joao Moreno 已提交
378
		}
J
Joao Moreno 已提交
379

J
Joao Moreno 已提交
380
		return await this._openResource(resource);
J
Joao Moreno 已提交
381 382
	}

J
Joao Moreno 已提交
383
	@command('git.stage')
384
	async stage(...resourceStates: SourceControlResourceState[]): Promise<void> {
J
Joao Moreno 已提交
385
		if (resourceStates.length === 0 || !(resourceStates[0].resourceUri instanceof Uri)) {
386
			const resource = this.getSCMResource();
387 388 389 390 391 392 393 394

			if (!resource) {
				return;
			}

			resourceStates = [resource];
		}

395 396 397
		const resources = resourceStates
			.filter(s => s instanceof Resource && (s.resourceGroup instanceof WorkingTreeGroup || s.resourceGroup instanceof MergeGroup)) as Resource[];

398
		if (!resources.length) {
J
Joao Moreno 已提交
399 400
			return;
		}
J
Joao Moreno 已提交
401

402
		return await this.model.add(...resources);
J
Joao Moreno 已提交
403 404
	}

J
Joao Moreno 已提交
405
	@command('git.stageAll')
J
Joao Moreno 已提交
406
	async stageAll(): Promise<void> {
J
Joao Moreno 已提交
407 408 409
		return await this.model.add();
	}

410 411
	@command('git.stageSelectedRanges', false, true)
	async stageSelectedRanges(diffs: LineChange[]): Promise<void> {
J
Joao Moreno 已提交
412 413 414 415 416 417 418 419 420
		const textEditor = window.activeTextEditor;

		if (!textEditor) {
			return;
		}

		const modifiedDocument = textEditor.document;
		const modifiedUri = modifiedDocument.uri;

J
Joao Moreno 已提交
421
		if (modifiedUri.scheme !== 'file') {
J
Joao Moreno 已提交
422 423 424
			return;
		}

J
Joao Moreno 已提交
425
		const originalUri = toGitUri(modifiedUri, '~');
J
Joao Moreno 已提交
426
		const originalDocument = await workspace.openTextDocument(originalUri);
427 428 429 430
		const selectedLines = toLineRanges(textEditor.selections, modifiedDocument);
		const selectedDiffs = diffs
			.map(diff => selectedLines.reduce<LineChange | null>((result, range) => result || intersectDiffWithRange(modifiedDocument, diff, range), null))
			.filter(d => !!d) as LineChange[];
J
Joao Moreno 已提交
431 432 433 434 435

		if (!selectedDiffs.length) {
			return;
		}

436 437
		const result = applyLineChanges(originalDocument, modifiedDocument, selectedDiffs);

J
Joao Moreno 已提交
438
		await this.model.stage(modifiedUri, result);
J
Joao Moreno 已提交
439
	}
J
Joao Moreno 已提交
440

441 442
	@command('git.revertSelectedRanges', false, true)
	async revertSelectedRanges(diffs: LineChange[]): Promise<void> {
J
Joao Moreno 已提交
443 444 445 446 447 448 449 450 451 452 453 454 455
		const textEditor = window.activeTextEditor;

		if (!textEditor) {
			return;
		}

		const modifiedDocument = textEditor.document;
		const modifiedUri = modifiedDocument.uri;

		if (modifiedUri.scheme !== 'file') {
			return;
		}

J
Joao Moreno 已提交
456
		const originalUri = toGitUri(modifiedUri, '~');
J
Joao Moreno 已提交
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
		const originalDocument = await workspace.openTextDocument(originalUri);
		const selections = textEditor.selections;
		const selectedDiffs = diffs.filter(diff => {
			const modifiedRange = diff.modifiedEndLineNumber === 0
				? new Range(modifiedDocument.lineAt(diff.modifiedStartLineNumber - 1).range.end, modifiedDocument.lineAt(diff.modifiedStartLineNumber).range.start)
				: new Range(modifiedDocument.lineAt(diff.modifiedStartLineNumber - 1).range.start, modifiedDocument.lineAt(diff.modifiedEndLineNumber - 1).range.end);

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

		if (selectedDiffs.length === diffs.length) {
			return;
		}

		const basename = path.basename(modifiedUri.fsPath);
		const message = localize('confirm revert', "Are you sure you want to revert the selected changes in {0}?", basename);
		const yes = localize('revert', "Revert Changes");
		const pick = await window.showWarningMessage(message, { modal: true }, yes);

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

480
		const result = applyLineChanges(originalDocument, modifiedDocument, selectedDiffs);
J
Joao Moreno 已提交
481 482 483 484 485
		const edit = new WorkspaceEdit();
		edit.replace(modifiedUri, new Range(new Position(0, 0), modifiedDocument.lineAt(modifiedDocument.lineCount - 1).range.end), result);
		workspace.applyEdit(edit);
	}

J
Joao Moreno 已提交
486
	@command('git.unstage')
487
	async unstage(...resourceStates: SourceControlResourceState[]): Promise<void> {
J
Joao Moreno 已提交
488
		if (resourceStates.length === 0 || !(resourceStates[0].resourceUri instanceof Uri)) {
489
			const resource = this.getSCMResource();
490 491 492 493 494 495 496 497

			if (!resource) {
				return;
			}

			resourceStates = [resource];
		}

498 499 500
		const resources = resourceStates
			.filter(s => s instanceof Resource && s.resourceGroup instanceof IndexGroup) as Resource[];

501
		if (!resources.length) {
J
Joao Moreno 已提交
502 503 504
			return;
		}

505
		return await this.model.revertFiles(...resources);
J
Joao Moreno 已提交
506 507
	}

J
Joao Moreno 已提交
508
	@command('git.unstageAll')
J
Joao Moreno 已提交
509
	async unstageAll(): Promise<void> {
J
Joao Moreno 已提交
510
		return await this.model.revertFiles();
J
Joao Moreno 已提交
511
	}
J
Joao Moreno 已提交
512

513 514
	@command('git.unstageSelectedRanges', false, true)
	async unstageSelectedRanges(diffs: LineChange[]): Promise<void> {
J
Joao Moreno 已提交
515 516 517 518 519 520 521 522 523
		const textEditor = window.activeTextEditor;

		if (!textEditor) {
			return;
		}

		const modifiedDocument = textEditor.document;
		const modifiedUri = modifiedDocument.uri;

524 525 526 527 528 529 530
		if (modifiedUri.scheme !== 'git') {
			return;
		}

		const { ref } = fromGitUri(modifiedUri);

		if (ref !== '') {
J
Joao Moreno 已提交
531 532 533
			return;
		}

J
Joao Moreno 已提交
534
		const originalUri = toGitUri(modifiedUri, 'HEAD');
J
Joao Moreno 已提交
535
		const originalDocument = await workspace.openTextDocument(originalUri);
536 537 538 539
		const selectedLines = toLineRanges(textEditor.selections, modifiedDocument);
		const selectedDiffs = diffs
			.map(diff => selectedLines.reduce<LineChange | null>((result, range) => result || intersectDiffWithRange(modifiedDocument, diff, range), null))
			.filter(d => !!d) as LineChange[];
J
Joao Moreno 已提交
540 541 542 543 544

		if (!selectedDiffs.length) {
			return;
		}

545 546
		const invertedDiffs = selectedDiffs.map(invertLineChange);
		const result = applyLineChanges(modifiedDocument, originalDocument, invertedDiffs);
J
Joao Moreno 已提交
547 548 549 550

		await this.model.stage(modifiedUri, result);
	}

J
Joao Moreno 已提交
551
	@command('git.clean')
552
	async clean(...resourceStates: SourceControlResourceState[]): Promise<void> {
J
Joao Moreno 已提交
553
		if (resourceStates.length === 0 || !(resourceStates[0].resourceUri instanceof Uri)) {
554
			const resource = this.getSCMResource();
555 556 557 558 559 560 561 562

			if (!resource) {
				return;
			}

			resourceStates = [resource];
		}

563 564 565
		const resources = resourceStates
			.filter(s => s instanceof Resource && s.resourceGroup instanceof WorkingTreeGroup) as Resource[];

566
		if (!resources.length) {
J
Joao Moreno 已提交
567 568
			return;
		}
J
Joao Moreno 已提交
569

570
		const message = resources.length === 1
J
Joao Moreno 已提交
571
			? localize('confirm discard', "Are you sure you want to discard changes in {0}?", path.basename(resources[0].resourceUri.fsPath))
572 573
			: localize('confirm discard multiple', "Are you sure you want to discard changes in {0} files?", resources.length);

574
		const yes = localize('discard', "Discard Changes");
J
Joao Moreno 已提交
575
		const pick = await window.showWarningMessage(message, { modal: true }, yes);
J
Joao Moreno 已提交
576

J
Joao Moreno 已提交
577 578 579 580
		if (pick !== yes) {
			return;
		}

581
		await this.model.clean(...resources);
J
Joao Moreno 已提交
582
	}
J
Joao Moreno 已提交
583

J
Joao Moreno 已提交
584
	@command('git.cleanAll')
J
Joao Moreno 已提交
585
	async cleanAll(): Promise<void> {
586 587
		const message = localize('confirm discard all', "Are you sure you want to discard ALL changes? This is IRREVERSIBLE!");
		const yes = localize('discardAll', "Discard ALL Changes");
J
Joao Moreno 已提交
588
		const pick = await window.showWarningMessage(message, { modal: true }, yes);
J
Joao Moreno 已提交
589 590 591 592 593

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

J
Joao Moreno 已提交
594
		await this.model.clean(...this.model.workingTreeGroup.resources);
J
Joao Moreno 已提交
595 596
	}

J
Joao Moreno 已提交
597
	private async smartCommit(
598
		getCommitMessage: () => Promise<string | undefined>,
J
Joao Moreno 已提交
599 600
		opts?: CommitOptions
	): Promise<boolean> {
601 602 603
		const config = workspace.getConfiguration('git');
		const enableSmartCommit = config.get<boolean>('enableSmartCommit') === true;
		const noStagedChanges = this.model.indexGroup.resources.length === 0;
604
		const noUnstagedChanges = this.model.workingTreeGroup.resources.length === 0;
605 606

		// no changes, and the user has not configured to commit all in this case
607
		if (!noUnstagedChanges && noStagedChanges && !enableSmartCommit) {
608

J
Joao Moreno 已提交
609 610
			// prompt the user if we want to commit all or not
			const message = localize('no staged changes', "There are no staged changes to commit.\n\nWould you like to automatically stage all your changes and commit them directly?");
611 612 613 614 615
			const yes = localize('yes', "Yes");
			const always = localize('always', "Always");
			const pick = await window.showWarningMessage(message, { modal: true }, yes, always);

			if (pick === always) {
J
Joao Moreno 已提交
616 617 618
				config.update('enableSmartCommit', true, true);
			} else if (pick !== yes) {
				return false; // do not commit on cancel
619 620 621
			}
		}

J
Joao Moreno 已提交
622
		if (!opts) {
623
			opts = { all: noStagedChanges };
J
Joao Moreno 已提交
624 625 626 627
		}

		if (
			// no changes
628
			(noStagedChanges && noUnstagedChanges)
J
Joao Moreno 已提交
629
			// or no staged changes and not `all`
630
			|| (!opts.all && noStagedChanges)
J
Joao Moreno 已提交
631
		) {
J
Joao Moreno 已提交
632 633 634 635
			window.showInformationMessage(localize('no changes', "There are no changes to commit."));
			return false;
		}

J
Joao Moreno 已提交
636
		const message = await getCommitMessage();
J
Joao Moreno 已提交
637 638 639 640 641 642

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

J
Joao Moreno 已提交
643
		await this.model.commit(message, opts);
J
Joao Moreno 已提交
644 645 646 647

		return true;
	}

J
Joao Moreno 已提交
648
	private async commitWithAnyInput(opts?: CommitOptions): Promise<void> {
649
		const message = scm.inputBox.value;
J
Joao Moreno 已提交
650
		const getCommitMessage = async () => {
J
Joao Moreno 已提交
651 652 653 654 655 656
			if (message) {
				return message;
			}

			return await window.showInputBox({
				placeHolder: localize('commit message', "Commit message"),
J
Joao Moreno 已提交
657 658
				prompt: localize('provide commit message', "Please provide a commit message"),
				ignoreFocusOut: true
J
Joao Moreno 已提交
659
			});
J
Joao Moreno 已提交
660 661 662
		};

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

		if (message && didCommit) {
J
Joao Moreno 已提交
665
			scm.inputBox.value = await this.model.getCommitTemplate();
J
Joao Moreno 已提交
666
		}
J
Joao Moreno 已提交
667 668
	}

J
Joao Moreno 已提交
669
	@command('git.commit')
J
Joao Moreno 已提交
670 671 672 673
	async commit(): Promise<void> {
		await this.commitWithAnyInput();
	}

J
Joao Moreno 已提交
674
	@command('git.commitWithInput')
J
Joao Moreno 已提交
675
	async commitWithInput(): Promise<void> {
J
Joao Moreno 已提交
676 677 678 679
		if (!scm.inputBox.value) {
			return;
		}

J
Joao Moreno 已提交
680
		const didCommit = await this.smartCommit(async () => scm.inputBox.value);
J
Joao Moreno 已提交
681 682

		if (didCommit) {
J
Joao Moreno 已提交
683
			scm.inputBox.value = await this.model.getCommitTemplate();
J
Joao Moreno 已提交
684
		}
J
Joao Moreno 已提交
685 686
	}

J
Joao Moreno 已提交
687
	@command('git.commitStaged')
J
Joao Moreno 已提交
688
	async commitStaged(): Promise<void> {
J
Joao Moreno 已提交
689
		await this.commitWithAnyInput({ all: false });
J
Joao Moreno 已提交
690 691
	}

J
Joao Moreno 已提交
692
	@command('git.commitStagedSigned')
J
Joao Moreno 已提交
693
	async commitStagedSigned(): Promise<void> {
J
Joao Moreno 已提交
694
		await this.commitWithAnyInput({ all: false, signoff: true });
J
Joao Moreno 已提交
695 696
	}

J
Joao Moreno 已提交
697
	@command('git.commitAll')
J
Joao Moreno 已提交
698
	async commitAll(): Promise<void> {
J
Joao Moreno 已提交
699
		await this.commitWithAnyInput({ all: true });
J
Joao Moreno 已提交
700 701
	}

J
Joao Moreno 已提交
702
	@command('git.commitAllSigned')
J
Joao Moreno 已提交
703
	async commitAllSigned(): Promise<void> {
J
Joao Moreno 已提交
704
		await this.commitWithAnyInput({ all: true, signoff: true });
J
Joao Moreno 已提交
705 706
	}

J
Joao Moreno 已提交
707
	@command('git.undoCommit')
J
Joao Moreno 已提交
708
	async undoCommit(): Promise<void> {
J
Joao Moreno 已提交
709 710 711 712 713 714 715 716 717
		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 已提交
718 719
	}

J
Joao Moreno 已提交
720
	@command('git.checkout')
J
Joao Moreno 已提交
721 722 723 724 725
	async checkout(treeish: string): Promise<void> {
		if (typeof treeish === 'string') {
			return await this.model.checkout(treeish);
		}

J
Joao Moreno 已提交
726
		const config = workspace.getConfiguration('git');
J
Joao Moreno 已提交
727
		const checkoutType = config.get<string>('checkoutType') || 'all';
J
Joao Moreno 已提交
728 729 730
		const includeTags = checkoutType === 'all' || checkoutType === 'tags';
		const includeRemotes = checkoutType === 'all' || checkoutType === 'remote';

J
Joao Moreno 已提交
731 732
		const createBranch = new CreateBranchItem();

J
Joao Moreno 已提交
733 734 735 736 737 738 739 740 741
		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 已提交
742
		const picks = [createBranch, ...heads, ...tags, ...remoteHeads];
743
		const placeHolder = localize('select a ref to checkout', 'Select a ref to checkout');
J
Joao Moreno 已提交
744
		const choice = await window.showQuickPick(picks, { placeHolder });
J
Joao Moreno 已提交
745 746 747 748 749 750

		if (!choice) {
			return;
		}

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

J
Joao Moreno 已提交
753
	@command('git.branch')
J
Joao Moreno 已提交
754 755
	async branch(): Promise<void> {
		const result = await window.showInputBox({
J
Joao Moreno 已提交
756
			placeHolder: localize('branch name', "Branch name"),
J
Joao Moreno 已提交
757 758
			prompt: localize('provide branch name', "Please provide a branch name"),
			ignoreFocusOut: true
J
Joao Moreno 已提交
759
		});
J
Joao Moreno 已提交
760

J
Joao Moreno 已提交
761 762 763
		if (!result) {
			return;
		}
J
Joao Moreno 已提交
764

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

M
Maik Riechert 已提交
769
	@command('git.deleteBranch')
770 771 772 773 774 775 776 777
	async deleteBranch(name: string, force?: boolean): Promise<void> {
		let run: (force?: boolean) => Promise<void>;
		if (typeof name === 'string') {
			run = force => this.model.deleteBranch(name, force);
		} else {
			const currentHead = this.model.HEAD && this.model.HEAD.name;
			const heads = this.model.refs.filter(ref => ref.type === RefType.Head && ref.name !== currentHead)
				.map(ref => new BranchDeleteItem(ref));
M
Maik Riechert 已提交
778

M
Maik Riechert 已提交
779
			const placeHolder = localize('select branch to delete', 'Select a branch to delete');
780
			const choice = await window.showQuickPick<BranchDeleteItem>(heads, { placeHolder });
M
Maik Riechert 已提交
781

M
Maik Riechert 已提交
782
			if (!choice || !choice.branchName) {
783 784
				return;
			}
M
Maik Riechert 已提交
785
			name = choice.branchName;
786
			run = force => choice.run(this.model, force);
M
Maik Riechert 已提交
787 788
		}

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
		try {
			await run(force);
		} catch (err) {
			if (err.gitErrorCode !== GitErrorCodes.BranchNotFullyMerged) {
				throw err;
			}

			const message = localize('confirm force delete branch', "The branch '{0}' is not fully merged. Delete anyway?", name);
			const yes = localize('delete branch', "Delete Branch");
			const pick = await window.showWarningMessage(message, yes);

			if (pick === yes) {
				await run(true);
			}
		}
M
Maik Riechert 已提交
804 805
	}

806 807 808 809 810 811 812
	@command('git.merge')
	async merge(): Promise<void> {
		const config = workspace.getConfiguration('git');
		const checkoutType = config.get<string>('checkoutType') || 'all';
		const includeRemotes = checkoutType === 'all' || checkoutType === 'remote';

		const heads = this.model.refs.filter(ref => ref.type === RefType.Head)
J
Joao Moreno 已提交
813 814
			.filter(ref => ref.name || ref.commit)
			.map(ref => new MergeItem(ref as Branch));
815 816

		const remoteHeads = (includeRemotes ? this.model.refs.filter(ref => ref.type === RefType.RemoteHead) : [])
J
Joao Moreno 已提交
817 818
			.filter(ref => ref.name || ref.commit)
			.map(ref => new MergeItem(ref as Branch));
819 820

		const picks = [...heads, ...remoteHeads];
821 822
		const placeHolder = localize('select a branch to merge from', 'Select a branch to merge from');
		const choice = await window.showQuickPick<MergeItem>(picks, { placeHolder });
823 824 825 826 827

		if (!choice) {
			return;
		}

J
Joao Moreno 已提交
828 829 830 831 832 833 834 835 836 837
		try {
			await choice.run(this.model);
		} catch (err) {
			if (err.gitErrorCode !== GitErrorCodes.Conflict) {
				throw err;
			}

			const message = localize('merge conflicts', "There are merge conflicts. Resolve them before committing.");
			await window.showWarningMessage(message);
		}
838 839
	}

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
	@command('git.createTag')
	async createTag(): Promise<void> {
		const inputTagName = await window.showInputBox({
			placeHolder: localize('tag name', "Tag name"),
			prompt: localize('provide tag name', "Please provide a tag name"),
			ignoreFocusOut: true
		});

		if (!inputTagName) {
			return;
		}

		const inputMessage = await window.showInputBox({
			placeHolder: localize('tag message', "Message"),
			prompt: localize('provide tag message', "Please provide a message"),
			ignoreFocusOut: true
		});

		const name = inputTagName.replace(/^\.|\/\.|\.\.|~|\^|:|\/$|\.lock$|\.lock\/|\\|\*|\s|^\s*$|\.$/g, '-');
		const message = inputMessage || name;
		await this.model.tag(name, message);

		window.showInformationMessage(localize('tag creation success', "Successfully created tag."));
	}

J
Joao Moreno 已提交
865
	@command('git.pullFrom')
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
	async pullFrom(): Promise<void> {
		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;
		}

		const picks = remotes.map(r => ({ label: r.name, description: r.url }));
		const placeHolder = localize('pick remote pull repo', "Pick a remote to pull the branch from");
		const pick = await window.showQuickPick(picks, { placeHolder });

		if (!pick) {
			return;
		}

		const branchName = await window.showInputBox({
			placeHolder: localize('branch name', "Branch name"),
			prompt: localize('provide branch name', "Please provide a branch name"),
			ignoreFocusOut: true
		});

		if (!branchName) {
			return;
		}

		this.model.pull(false, pick.label, branchName);
	}

J
Joao Moreno 已提交
895
	@command('git.pull')
J
Joao Moreno 已提交
896
	async pull(): Promise<void> {
J
Joao Moreno 已提交
897 898 899 900 901 902 903 904
		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 已提交
905 906
	}

J
Joao Moreno 已提交
907
	@command('git.pullRebase')
J
Joao Moreno 已提交
908
	async pullRebase(): Promise<void> {
J
Joao Moreno 已提交
909 910 911 912 913 914 915
		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;
		}

J
Joao Moreno 已提交
916
		await this.model.pullWithRebase();
J
Joao Moreno 已提交
917 918
	}

J
Joao Moreno 已提交
919
	@command('git.push')
J
Joao Moreno 已提交
920
	async push(): Promise<void> {
J
Joao Moreno 已提交
921 922 923 924 925 926 927 928
		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 已提交
929 930
	}

931 932 933 934 935 936 937 938 939
	@command('git.pushWithTags')
	async pushWithTags(): Promise<void> {
		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;
		}

940
		await this.model.pushTags();
941 942 943 944

		window.showInformationMessage(localize('push with tags success', "Successfully pushed with tags."));
	}

J
Joao Moreno 已提交
945
	@command('git.pushTo')
J
Joao Moreno 已提交
946
	async pushTo(): Promise<void> {
J
Joao Moreno 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
		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;
		}

J
Joao Moreno 已提交
968
		this.model.pushTo(pick.label, branchName);
J
Joao Moreno 已提交
969 970
	}

J
Joao Moreno 已提交
971
	@command('git.sync')
J
Joao Moreno 已提交
972
	async sync(): Promise<void> {
J
Joao Moreno 已提交
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
		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 已提交
995 996 997
		await this.model.sync();
	}

J
Joao Moreno 已提交
998
	@command('git.publish')
J
Joao Moreno 已提交
999
	async publish(): Promise<void> {
J
Joao Moreno 已提交
1000 1001 1002 1003 1004 1005 1006
		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 已提交
1007 1008
		const branchName = this.model.HEAD && this.model.HEAD.name || '';
		const picks = this.model.remotes.map(r => r.name);
J
Joao Moreno 已提交
1009
		const placeHolder = localize('pick remote', "Pick a remote to publish the branch '{0}' to:", branchName);
J
Joao Moreno 已提交
1010 1011 1012 1013 1014 1015
		const choice = await window.showQuickPick(picks, { placeHolder });

		if (!choice) {
			return;
		}

J
Joao Moreno 已提交
1016
		await this.model.pushTo(choice, branchName, true);
J
Joao Moreno 已提交
1017 1018
	}

J
Joao Moreno 已提交
1019
	@command('git.showOutput')
J
Joao Moreno 已提交
1020 1021 1022 1023
	showOutput(): void {
		this.outputChannel.show();
	}

N
NKumar2 已提交
1024 1025
	@command('git.ignore')
	async ignore(...resourceStates: SourceControlResourceState[]): Promise<void> {
J
Joao Moreno 已提交
1026 1027
		if (resourceStates.length === 0 || !(resourceStates[0].resourceUri instanceof Uri)) {
			const uri = window.activeTextEditor && window.activeTextEditor.document.uri;
N
NKumar2 已提交
1028

J
Joao Moreno 已提交
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
			if (!uri) {
				return;
			}

			return await this.model.ignore([uri]);
		}

		const uris = resourceStates
			.filter(s => s instanceof Resource)
			.map(r => r.resourceUri);

		if (!uris.length) {
N
NKumar2 已提交
1041 1042 1043
			return;
		}

J
Joao Moreno 已提交
1044
		await this.model.ignore(uris);
N
NKumar2 已提交
1045 1046
	}

J
Joao Moreno 已提交
1047
	private createCommand(id: string, key: string, method: Function, skipModelCheck: boolean): (...args: any[]) => any {
1048
		const result = (...args) => {
J
Joao Moreno 已提交
1049
			if (!skipModelCheck && !this.model) {
J
Joao Moreno 已提交
1050 1051 1052 1053
				window.showInformationMessage(localize('disabled', "Git is either disabled or not supported in this workspace"));
				return;
			}

J
Joao Moreno 已提交
1054 1055
			this.telemetryReporter.sendTelemetryEvent('git.command', { command: id });

J
Joao Moreno 已提交
1056 1057 1058 1059 1060 1061
			const result = Promise.resolve(method.apply(this, args));

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

				switch (err.gitErrorCode) {
1062
					case GitErrorCodes.DirtyWorkTree:
J
Joao Moreno 已提交
1063 1064
						message = localize('clean repo', "Please clean your repository working tree before checkout.");
						break;
1065 1066 1067
					case GitErrorCodes.PushRejected:
						message = localize('cant push', "Can't push refs to remote. Run 'Pull' first to integrate your changes.");
						break;
J
Joao Moreno 已提交
1068
					default:
1069 1070 1071
						const hint = (err.stderr || err.message || String(err))
							.replace(/^error: /mi, '')
							.replace(/^> husky.*$/mi, '')
J
Joao Moreno 已提交
1072
							.split(/[\r\n]/)
1073 1074 1075 1076 1077 1078
							.filter(line => !!line)
						[0];

						message = hint
							? localize('git error details', "Git: {0}", hint)
							: localize('git error', "Git error");
J
Joao Moreno 已提交
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

						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();
				}
			});
		};
1097 1098 1099 1100 1101

		// patch this object, so people can call methods directly
		this[key] = result;

		return result;
J
Joao Moreno 已提交
1102 1103
	}

1104 1105
	private getSCMResource(uri?: Uri): Resource | undefined {
		uri = uri ? uri : window.activeTextEditor && window.activeTextEditor.document.uri;
J
Joao Moreno 已提交
1106 1107

		if (!uri) {
1108
			return undefined;
J
Joao Moreno 已提交
1109 1110 1111
		}

		if (uri.scheme === 'git') {
J
Joao Moreno 已提交
1112 1113
			const { path } = fromGitUri(uri);
			uri = Uri.file(path);
J
Joao Moreno 已提交
1114 1115 1116 1117 1118
		}

		if (uri.scheme === 'file') {
			const uriString = uri.toString();

J
Joao Moreno 已提交
1119 1120
			return this.model.workingTreeGroup.resources.filter(r => r.resourceUri.toString() === uriString)[0]
				|| this.model.indexGroup.resources.filter(r => r.resourceUri.toString() === uriString)[0];
J
Joao Moreno 已提交
1121 1122 1123
		}
	}

J
Joao Moreno 已提交
1124 1125 1126
	dispose(): void {
		this.disposables.forEach(d => d.dispose());
	}
J
Joao Moreno 已提交
1127
}