Route user to download missing git (#159505)

* Factor out captured variables that are not reused outside findGit callback

* Always register git protocol handler

* Notify the user when cloning without git installed

* Actually notify the user when cloning without git installed

Errors when invoking a command don't seem to get thrown in the extension host

* Add output channel logging

* Revert "Factor out captured variables that are not reused outside findGit callback"

This reverts commit df005bdda5.
This commit is contained in:
Joyce Er
2022-09-02 14:31:17 +00:00
committed by GitHub
parent 0797f094cd
commit d337885984
2 changed files with 30 additions and 8 deletions

View File

@@ -3,9 +3,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as nls from 'vscode-nls';
const localize = nls.loadMessageBundle();
import { UriHandler, Uri, window, Disposable, commands } from 'vscode';
import { dispose } from './util';
import * as querystring from 'querystring';
import { OutputChannelLogger } from './log';
const schemes = new Set(['file', 'git', 'http', 'https', 'ssh']);
@@ -13,32 +17,34 @@ export class GitProtocolHandler implements UriHandler {
private disposables: Disposable[] = [];
constructor() {
constructor(private readonly outputChannelLogger: OutputChannelLogger) {
this.disposables.push(window.registerUriHandler(this));
}
handleUri(uri: Uri): void {
this.outputChannelLogger.logInfo(`GitProtocolHandler.handleUri(${uri.toString()})`);
switch (uri.path) {
case '/clone': this.clone(uri);
}
}
private clone(uri: Uri): void {
private async clone(uri: Uri): Promise<void> {
const data = querystring.parse(uri.query);
const ref = data.ref;
if (!data.url) {
console.warn('Failed to open URI:', uri);
this.outputChannelLogger.logWarning('Failed to open URI:' + uri.toString());
return;
}
if (Array.isArray(data.url) && data.url.length === 0) {
console.warn('Failed to open URI:', uri);
this.outputChannelLogger.logWarning('Failed to open URI:' + uri.toString());
return;
}
if (ref !== undefined && typeof ref !== 'string') {
console.warn('Failed to open URI:', uri);
this.outputChannelLogger.logWarning('Failed to open URI:' + uri.toString());
return;
}
@@ -58,11 +64,26 @@ export class GitProtocolHandler implements UriHandler {
}
}
catch (ex) {
console.warn('Invalid URI:', uri);
this.outputChannelLogger.logWarning('Invalid URI:' + uri.toString());
return;
}
commands.executeCommand('git.clone', cloneUri.toString(true), undefined, { ref: ref });
if (!(await commands.getCommands(true)).includes('git.clone')) {
this.outputChannelLogger.logError('Could not complete git clone operation as git installation was not found.');
const errorMessage = localize('no git', 'Could not clone your repository as Git is not installed.');
const downloadGit = localize('download git', 'Download Git');
if (await window.showErrorMessage(errorMessage, downloadGit) === downloadGit) {
commands.executeCommand('vscode.open', Uri.parse('https://aka.ms/vscode-download-git'));
}
return;
} else {
const cloneTarget = cloneUri.toString(true);
this.outputChannelLogger.logInfo(`Executing git.clone for ${cloneTarget}`);
commands.executeCommand('git.clone', cloneTarget, undefined, { ref: ref });
}
}
dispose(): void {