diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 00827778a0e..bc5a7362ad5 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -457,7 +457,7 @@ export class CommandCenter { ); } - async cloneRepository(url?: string, parentPath?: string, options: { recursive?: boolean } = {}): Promise { + async cloneRepository(url?: string, parentPath?: string, options: { recursive?: boolean; ref?: string } = {}): Promise { if (!url || typeof url !== 'string') { url = await pickRemoteSource({ providerLabel: provider => localize('clonefrom', "Clone from {0}", provider.name), @@ -519,6 +519,11 @@ export class CommandCenter { (progress, token) => this.git.clone(url!, { parentPath: parentPath!, progress, recursive: options.recursive }, token) ); + if (options.ref !== undefined) { + const repository = this.model.getRepository(Uri.file(repositoryPath)); + await repository?.checkout(options.ref); + } + const config = workspace.getConfiguration('git'); const openAfterClone = config.get<'always' | 'alwaysNewWindow' | 'whenNoFolderOpen' | 'prompt'>('openAfterClone'); @@ -596,8 +601,8 @@ export class CommandCenter { } @command('git.clone') - async clone(url?: string, parentPath?: string): Promise { - await this.cloneRepository(url, parentPath); + async clone(url?: string, parentPath?: string, options?: { ref?: string }): Promise { + await this.cloneRepository(url, parentPath, options); } @command('git.cloneRecursive') diff --git a/extensions/git/src/protocolHandler.ts b/extensions/git/src/protocolHandler.ts index 33decfe1846..33bc58f9099 100644 --- a/extensions/git/src/protocolHandler.ts +++ b/extensions/git/src/protocolHandler.ts @@ -25,6 +25,7 @@ export class GitProtocolHandler implements UriHandler { private clone(uri: Uri): void { const data = querystring.parse(uri.query); + const ref = data.ref; if (!data.url) { console.warn('Failed to open URI:', uri); @@ -36,6 +37,11 @@ export class GitProtocolHandler implements UriHandler { return; } + if (ref !== undefined && typeof ref !== 'string') { + console.warn('Failed to open URI:', uri); + return; + } + let cloneUri: Uri; try { let rawUri = Array.isArray(data.url) ? data.url[0] : data.url; @@ -56,7 +62,7 @@ export class GitProtocolHandler implements UriHandler { return; } - commands.executeCommand('git.clone', cloneUri.toString(true)); + commands.executeCommand('git.clone', cloneUri.toString(true), undefined, { ref: ref }); } dispose(): void {