commands.ts 22.6 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, computeDiff, Range, WorkspaceEdit, Position } from 'vscode';
J
Joao Moreno 已提交
9
import { Ref, RefType, Git } 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
import * as os from 'os';
14
import { uniqueFilter } from './util';
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;
	}
}

J
Joao Moreno 已提交
63
const Commands: { commandId: string; key: string; method: Function; skipModelCheck: boolean; }[] = [];
J
Joao Moreno 已提交
64

J
Joao Moreno 已提交
65
function command(commandId: string, skipModelCheck = false): Function {
J
Joao Moreno 已提交
66
	return (target: any, key: string, descriptor: any) => {
J
Joao Moreno 已提交
67 68 69 70
		if (!(typeof descriptor.value === 'function')) {
			throw new Error('not supported');
		}

J
Joao Moreno 已提交
71
		Commands.push({ commandId, key, method: descriptor.value, skipModelCheck });
J
Joao Moreno 已提交
72 73
	};
}
J
Joao Moreno 已提交
74

J
Joao Moreno 已提交
75
export class CommandCenter {
J
Joao Moreno 已提交
76

J
Joao Moreno 已提交
77
	private model: Model;
J
Joao Moreno 已提交
78
	private disposables: Disposable[];
J
Joao Moreno 已提交
79

J
Joao Moreno 已提交
80
	constructor(
J
Joao Moreno 已提交
81
		private git: Git,
J
Joao Moreno 已提交
82
		model: Model | undefined,
J
Joao Moreno 已提交
83 84
		private outputChannel: OutputChannel,
		private telemetryReporter: TelemetryReporter
J
Joao Moreno 已提交
85
	) {
J
Joao Moreno 已提交
86 87 88 89
		if (model) {
			this.model = model;
		}

J
Joao Moreno 已提交
90
		this.disposables = Commands
J
Joao Moreno 已提交
91
			.map(({ commandId, key, method, skipModelCheck }) => commands.registerCommand(commandId, this.createCommand(commandId, key, method, skipModelCheck)));
J
Joao Moreno 已提交
92 93
	}

J
Joao Moreno 已提交
94
	@command('git.refresh')
J
Joao Moreno 已提交
95
	async refresh(): Promise<void> {
J
Joao Moreno 已提交
96
		await this.model.status();
J
Joao Moreno 已提交
97
	}
J
Joao Moreno 已提交
98

J
Joao Moreno 已提交
99 100 101 102 103
	async open(resource: Resource): Promise<void> {
		const left = this.getLeftResource(resource);
		const right = this.getRightResource(resource);
		const title = this.getTitle(resource);

J
Joao Moreno 已提交
104 105 106 107 108
		if (!right) {
			// TODO
			console.error('oh no');
			return;
		}
J
Joao Moreno 已提交
109

J
Joao Moreno 已提交
110
		if (!left) {
J
Joao Moreno 已提交
111
			return await commands.executeCommand<void>('vscode.open', right);
J
Joao Moreno 已提交
112 113
		}

J
Joao Moreno 已提交
114
		return await commands.executeCommand<void>('vscode.diff', left, right, title);
J
Joao Moreno 已提交
115 116 117 118 119 120
	}

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

			case Status.MODIFIED:
124
				return resource.uri.with({ scheme: 'git', query: '~' });
J
Joao Moreno 已提交
125
		}
J
Joao Moreno 已提交
126
	}
J
Joao Moreno 已提交
127

J
Joao Moreno 已提交
128 129 130 131 132
	private getRightResource(resource: Resource): Uri | undefined {
		switch (resource.type) {
			case Status.INDEX_MODIFIED:
			case Status.INDEX_ADDED:
			case Status.INDEX_COPIED:
J
Joao Moreno 已提交
133 134
				return resource.uri.with({ scheme: 'git' });

J
Joao Moreno 已提交
135 136 137 138 139 140 141 142 143 144
			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 已提交
145 146 147 148 149 150 151 152 153
				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 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
			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 已提交
174
	private async _clone(): Promise<boolean> {
J
Joao Moreno 已提交
175
		const url = await window.showInputBox({
J
Joao Moreno 已提交
176 177
			prompt: localize('repourl', "Repository URL"),
			ignoreFocusOut: true
J
Joao Moreno 已提交
178 179 180
		});

		if (!url) {
J
Joao Moreno 已提交
181
			throw new Error('no_URL');
J
Joao Moreno 已提交
182 183 184 185
		}

		const parentPath = await window.showInputBox({
			prompt: localize('parent', "Parent Directory"),
J
Joao Moreno 已提交
186 187
			value: os.homedir(),
			ignoreFocusOut: true
J
Joao Moreno 已提交
188 189 190
		});

		if (!parentPath) {
J
Joao Moreno 已提交
191
			throw new Error('no_directory');
J
Joao Moreno 已提交
192 193
		}

J
Joao Moreno 已提交
194
		const clonePromise = this.git.clone(url, parentPath);
J
Joao Moreno 已提交
195
		window.setStatusBarMessage(localize('cloning', "Cloning git repository..."), clonePromise);
J
Joao Moreno 已提交
196
		let repositoryPath: string;
J
Joao Moreno 已提交
197

198
		try {
J
Joao Moreno 已提交
199
			repositoryPath = await clonePromise;
200 201
		} catch (err) {
			if (/already exists and is not an empty directory/.test(err && err.stderr || '')) {
J
Joao Moreno 已提交
202
				throw new Error('directory_not_empty');
203
			}
J
Joao Moreno 已提交
204

205
			throw err;
J
Joao Moreno 已提交
206
		}
J
Joao Moreno 已提交
207 208 209 210 211 212 213 214 215 216 217 218

		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;

		if (openFolder) {
			commands.executeCommand('vscode.openFolder', Uri.file(repositoryPath));
		}

		return openFolder;
	}

J
Joao Moreno 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
	/**
	 * Attempts to clone a git repository. Throws descriptive errors
	 * for usual error cases. Returns whether the user chose to open
	 * the resulting folder or otherwise.
	 *
	 * This only exists for the walkthrough contribution to have good
	 * telemetry.
	 *
	 * TODO@Christof: when all the telemetry questions are answered,
	 * please clean this up into a single clone method.
	 */
	@command('git.clone', true)
	async clone(): Promise<boolean> {
		return await this._clone();
	}

	@command('git.cloneSilent', true)
	async cloneSilent(): Promise<void> {
J
Joao Moreno 已提交
237 238 239 240 241
		try {
			await this._clone();
		} catch (err) {
			// noop
		}
J
Joao Moreno 已提交
242 243
	}

J
Joao Moreno 已提交
244 245 246 247 248
	@command('git.init')
	async init(): Promise<void> {
		await this.model.init();
	}

J
Joao Moreno 已提交
249
	@command('git.openFile')
J
Joao Moreno 已提交
250
	async openFile(uri?: Uri): Promise<void> {
251 252 253 254
		if (uri && uri.scheme === 'file') {
			return await commands.executeCommand<void>('vscode.open', uri);
		}

J
Joao Moreno 已提交
255
		const resource = this.resolveSCMResource(uri);
J
Joao Moreno 已提交
256

J
Joao Moreno 已提交
257 258
		if (!resource) {
			return;
J
Joao Moreno 已提交
259 260
		}

J
Joao Moreno 已提交
261
		return await commands.executeCommand<void>('vscode.open', resource.uri);
J
Joao Moreno 已提交
262 263 264
	}

	@command('git.openChange')
J
Joao Moreno 已提交
265 266
	async openChange(uri?: Uri): Promise<void> {
		const resource = this.resolveSCMResource(uri);
J
Joao Moreno 已提交
267

J
Joao Moreno 已提交
268 269
		if (!resource) {
			return;
J
Joao Moreno 已提交
270 271
		}

J
Joao Moreno 已提交
272
		return await this.open(resource);
J
Joao Moreno 已提交
273 274
	}

J
Joao Moreno 已提交
275
	@command('git.stage')
276 277
	async stage(...uris: Uri[]): Promise<void> {
		const resources = this.toSCMResources(uris);
J
Joao Moreno 已提交
278

279
		if (!resources.length) {
J
Joao Moreno 已提交
280 281
			return;
		}
J
Joao Moreno 已提交
282

283
		return await this.model.add(...resources);
J
Joao Moreno 已提交
284 285
	}

J
Joao Moreno 已提交
286
	@command('git.stageAll')
J
Joao Moreno 已提交
287
	async stageAll(): Promise<void> {
J
Joao Moreno 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301
		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;

J
Joao Moreno 已提交
302
		if (modifiedUri.scheme !== 'file') {
J
Joao Moreno 已提交
303 304 305
			return;
		}

J
Joao Moreno 已提交
306
		const originalUri = modifiedUri.with({ scheme: 'git', query: '~' });
J
Joao Moreno 已提交
307 308 309 310 311
		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
312
				? new Range(modifiedDocument.lineAt(diff.modifiedStartLineNumber - 1).range.end, modifiedDocument.lineAt(diff.modifiedStartLineNumber).range.start)
J
Joao Moreno 已提交
313 314 315 316 317 318 319 320 321 322 323
				: 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 已提交
324
	}
J
Joao Moreno 已提交
325

J
Joao Moreno 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
	@command('git.revertSelectedRanges')
	async revertSelectedRanges(): Promise<void> {
		const textEditor = window.activeTextEditor;

		if (!textEditor) {
			return;
		}

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

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

		const originalUri = modifiedUri.with({ scheme: 'git', query: '~' });
		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(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;
		}

		const result = staging.applyChanges(originalDocument, modifiedDocument, selectedDiffs);
		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 已提交
372
	@command('git.unstage')
373 374
	async unstage(...uris: Uri[]): Promise<void> {
		const resources = this.toSCMResources(uris);
J
Joao Moreno 已提交
375

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

380
		return await this.model.revertFiles(...resources);
J
Joao Moreno 已提交
381 382
	}

J
Joao Moreno 已提交
383
	@command('git.unstageAll')
J
Joao Moreno 已提交
384
	async unstageAll(): Promise<void> {
J
Joao Moreno 已提交
385
		return await this.model.revertFiles();
J
Joao Moreno 已提交
386
	}
J
Joao Moreno 已提交
387

J
Joao Moreno 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	@command('git.unstageSelectedRanges')
	async unstageSelectedRanges(): Promise<void> {
		const textEditor = window.activeTextEditor;

		if (!textEditor) {
			return;
		}

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

		if (modifiedUri.scheme !== 'git' || modifiedUri.query !== '') {
			return;
		}

		const originalUri = modifiedUri.with({ scheme: 'git', query: 'HEAD' });
		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 invertedDiffs = selectedDiffs.map(c => ({
			modifiedStartLineNumber: c.originalStartLineNumber,
			modifiedEndLineNumber: c.originalEndLineNumber,
			originalStartLineNumber: c.modifiedStartLineNumber,
			originalEndLineNumber: c.modifiedEndLineNumber
		}));

		const result = staging.applyChanges(modifiedDocument, originalDocument, invertedDiffs);
		await this.model.stage(modifiedUri, result);
	}

J
Joao Moreno 已提交
430
	@command('git.clean')
431 432
	async clean(...uris: Uri[]): Promise<void> {
		const resources = this.toSCMResources(uris);
J
Joao Moreno 已提交
433

434
		if (!resources.length) {
J
Joao Moreno 已提交
435 436
			return;
		}
J
Joao Moreno 已提交
437

438 439 440 441
		const message = resources.length === 1
			? localize('confirm discard', "Are you sure you want to discard changes in {0}?", path.basename(resources[0].uri.fsPath))
			: localize('confirm discard multiple', "Are you sure you want to discard changes in {0} files?", resources.length);

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

J
Joao Moreno 已提交
445 446 447 448
		if (pick !== yes) {
			return;
		}

449
		await this.model.clean(...resources);
J
Joao Moreno 已提交
450
	}
J
Joao Moreno 已提交
451

J
Joao Moreno 已提交
452
	@command('git.cleanAll')
J
Joao Moreno 已提交
453
	async cleanAll(): Promise<void> {
454 455
		const message = localize('confirm discard all', "Are you sure you want to discard ALL changes?");
		const yes = localize('discard', "Discard Changes");
J
Joao Moreno 已提交
456
		const pick = await window.showWarningMessage(message, { modal: true }, yes);
J
Joao Moreno 已提交
457 458 459 460 461

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

J
Joao Moreno 已提交
462
		await this.model.clean(...this.model.workingTreeGroup.resources);
J
Joao Moreno 已提交
463 464
	}

J
Joao Moreno 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477 478
	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 已提交
479 480 481 482
			window.showInformationMessage(localize('no changes', "There are no changes to commit."));
			return false;
		}

J
Joao Moreno 已提交
483
		const message = await getCommitMessage();
J
Joao Moreno 已提交
484 485 486 487 488 489

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

J
Joao Moreno 已提交
490
		await this.model.commit(message, opts);
J
Joao Moreno 已提交
491 492 493 494

		return true;
	}

J
Joao Moreno 已提交
495
	private async commitWithAnyInput(opts?: CommitOptions): Promise<void> {
496
		const message = scm.inputBox.value;
J
Joao Moreno 已提交
497
		const getCommitMessage = async () => {
J
Joao Moreno 已提交
498 499 500 501 502 503
			if (message) {
				return message;
			}

			return await window.showInputBox({
				placeHolder: localize('commit message', "Commit message"),
J
Joao Moreno 已提交
504 505
				prompt: localize('provide commit message', "Please provide a commit message"),
				ignoreFocusOut: true
J
Joao Moreno 已提交
506
			});
J
Joao Moreno 已提交
507 508 509
		};

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

		if (message && didCommit) {
J
Joao Moreno 已提交
512
			scm.inputBox.value = await this.model.getCommitTemplate();
J
Joao Moreno 已提交
513
		}
J
Joao Moreno 已提交
514 515
	}

J
Joao Moreno 已提交
516
	@command('git.commit')
J
Joao Moreno 已提交
517 518 519 520
	async commit(): Promise<void> {
		await this.commitWithAnyInput();
	}

J
Joao Moreno 已提交
521
	@command('git.commitWithInput')
J
Joao Moreno 已提交
522
	async commitWithInput(): Promise<void> {
J
Joao Moreno 已提交
523
		const didCommit = await this.smartCommit(async () => scm.inputBox.value);
J
Joao Moreno 已提交
524 525

		if (didCommit) {
J
Joao Moreno 已提交
526
			scm.inputBox.value = await this.model.getCommitTemplate();
J
Joao Moreno 已提交
527
		}
J
Joao Moreno 已提交
528 529
	}

J
Joao Moreno 已提交
530
	@command('git.commitStaged')
J
Joao Moreno 已提交
531
	async commitStaged(): Promise<void> {
J
Joao Moreno 已提交
532
		await this.commitWithAnyInput({ all: false });
J
Joao Moreno 已提交
533 534
	}

J
Joao Moreno 已提交
535
	@command('git.commitStagedSigned')
J
Joao Moreno 已提交
536
	async commitStagedSigned(): Promise<void> {
J
Joao Moreno 已提交
537
		await this.commitWithAnyInput({ all: false, signoff: true });
J
Joao Moreno 已提交
538 539
	}

J
Joao Moreno 已提交
540
	@command('git.commitAll')
J
Joao Moreno 已提交
541
	async commitAll(): Promise<void> {
J
Joao Moreno 已提交
542
		await this.commitWithAnyInput({ all: true });
J
Joao Moreno 已提交
543 544
	}

J
Joao Moreno 已提交
545
	@command('git.commitAllSigned')
J
Joao Moreno 已提交
546
	async commitAllSigned(): Promise<void> {
J
Joao Moreno 已提交
547
		await this.commitWithAnyInput({ all: true, signoff: true });
J
Joao Moreno 已提交
548 549
	}

J
Joao Moreno 已提交
550
	@command('git.undoCommit')
J
Joao Moreno 已提交
551
	async undoCommit(): Promise<void> {
J
Joao Moreno 已提交
552 553 554 555 556 557 558 559 560
		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 已提交
561 562
	}

J
Joao Moreno 已提交
563
	@command('git.checkout')
J
Joao Moreno 已提交
564 565
	async checkout(): Promise<void> {
		const config = workspace.getConfiguration('git');
J
Joao Moreno 已提交
566
		const checkoutType = config.get<string>('checkoutType') || 'all';
J
Joao Moreno 已提交
567 568 569 570 571 572 573 574 575 576 577 578
		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 已提交
579 580 581
		const picks = [...heads, ...tags, ...remoteHeads];
		const placeHolder = 'Select a ref to checkout';
		const choice = await window.showQuickPick<CheckoutItem>(picks, { placeHolder });
J
Joao Moreno 已提交
582 583 584 585 586 587

		if (!choice) {
			return;
		}

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

J
Joao Moreno 已提交
590
	@command('git.branch')
J
Joao Moreno 已提交
591 592
	async branch(): Promise<void> {
		const result = await window.showInputBox({
J
Joao Moreno 已提交
593
			placeHolder: localize('branch name', "Branch name"),
J
Joao Moreno 已提交
594 595
			prompt: localize('provide branch name', "Please provide a branch name"),
			ignoreFocusOut: true
J
Joao Moreno 已提交
596
		});
J
Joao Moreno 已提交
597

J
Joao Moreno 已提交
598 599 600
		if (!result) {
			return;
		}
J
Joao Moreno 已提交
601

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

J
Joao Moreno 已提交
606
	@command('git.pull')
J
Joao Moreno 已提交
607
	async pull(): Promise<void> {
J
Joao Moreno 已提交
608 609 610 611 612 613 614 615
		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 已提交
616 617
	}

J
Joao Moreno 已提交
618
	@command('git.pullRebase')
J
Joao Moreno 已提交
619
	async pullRebase(): Promise<void> {
J
Joao Moreno 已提交
620 621 622 623 624 625 626 627
		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 已提交
628 629
	}

J
Joao Moreno 已提交
630
	@command('git.push')
J
Joao Moreno 已提交
631
	async push(): Promise<void> {
J
Joao Moreno 已提交
632 633 634 635 636 637 638 639
		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 已提交
640 641
	}

J
Joao Moreno 已提交
642
	@command('git.pushTo')
J
Joao Moreno 已提交
643
	async pushTo(): Promise<void> {
J
Joao Moreno 已提交
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
		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 已提交
666 667
	}

J
Joao Moreno 已提交
668
	@command('git.sync')
J
Joao Moreno 已提交
669
	async sync(): Promise<void> {
J
Joao Moreno 已提交
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
		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 已提交
692 693 694
		await this.model.sync();
	}

J
Joao Moreno 已提交
695
	@command('git.publish')
J
Joao Moreno 已提交
696
	async publish(): Promise<void> {
J
Joao Moreno 已提交
697 698 699 700 701 702 703
		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 已提交
704 705
		const branchName = this.model.HEAD && this.model.HEAD.name || '';
		const picks = this.model.remotes.map(r => r.name);
J
Joao Moreno 已提交
706
		const placeHolder = localize('pick remote', "Pick a remote to publish the branch '{0}' to:", branchName);
J
Joao Moreno 已提交
707 708 709 710 711 712 713 714 715
		const choice = await window.showQuickPick(picks, { placeHolder });

		if (!choice) {
			return;
		}

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

J
Joao Moreno 已提交
716
	@command('git.showOutput')
J
Joao Moreno 已提交
717 718 719 720
	showOutput(): void {
		this.outputChannel.show();
	}

J
Joao Moreno 已提交
721
	private createCommand(id: string, key: string, method: Function, skipModelCheck: boolean): (...args: any[]) => any {
722
		const result = (...args) => {
J
Joao Moreno 已提交
723
			if (!skipModelCheck && !this.model) {
J
Joao Moreno 已提交
724 725 726 727
				window.showInformationMessage(localize('disabled', "Git is either disabled or not supported in this workspace"));
				return;
			}

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

J
Joao Moreno 已提交
730 731 732 733 734 735 736 737 738 739
			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:
740 741 742
						const hint = (err.stderr || err.message || String(err))
							.replace(/^error: /mi, '')
							.replace(/^> husky.*$/mi, '')
J
Joao Moreno 已提交
743
							.split(/[\r\n]/)
744 745 746 747 748 749
							.filter(line => !!line)
						[0];

						message = hint
							? localize('git error details', "Git: {0}", hint)
							: localize('git error', "Git error");
J
Joao Moreno 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767

						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();
				}
			});
		};
768 769 770 771 772

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

		return result;
J
Joao Moreno 已提交
773 774
	}

J
Joao Moreno 已提交
775 776 777 778
	private resolveSCMResource(uri?: Uri): Resource | undefined {
		uri = uri || window.activeTextEditor && window.activeTextEditor.document.uri;

		if (!uri) {
779
			return undefined;
J
Joao Moreno 已提交
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
		}

		if (uri.scheme === 'scm' && uri.authority === 'git') {
			const resource = scm.getResourceFromURI(uri);
			return resource instanceof Resource ? resource : undefined;
		}

		if (uri.scheme === 'git') {
			uri = uri.with({ scheme: 'file' });
		}

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

			return this.model.workingTreeGroup.resources.filter(r => r.uri.toString() === uriString)[0]
				|| this.model.indexGroup.resources.filter(r => r.uri.toString() === uriString)[0];
		}
	}

799 800 801 802 803 804
	private toSCMResources(uris: Uri[]): Resource[] {
		return uris.filter(uniqueFilter(uri => uri.toString()))
			.map(uri => this.resolveSCMResource(uri))
			.filter(r => !!r) as Resource[];
	}

J
Joao Moreno 已提交
805 806 807
	dispose(): void {
		this.disposables.forEach(d => d.dispose());
	}
J
Joao Moreno 已提交
808
}