diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index 895a4640e68..8b4a222441b 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { Uri, commands, Disposable, window, workspace, QuickPickItem, OutputChannel, Range, WorkspaceEdit, Position, LineChange, SourceControlResourceState, TextDocumentShowOptions, ViewColumn, ProgressLocation, TextEditor, MessageOptions, WorkspaceFolder } from 'vscode'; +import { Uri, commands, Disposable, window, workspace, QuickPickItem, OutputChannel, Range, WorkspaceEdit, Position, LineChange, SourceControlResourceState, TextDocumentShowOptions, ViewColumn, ProgressLocation, TextEditor, MessageOptions, WorkspaceFolder, Progress } from 'vscode'; import { Git, CommitOptions, Stash, ForcePushMode } from './git'; import { Repository, Resource, ResourceGroupType } from './repository'; import { Model } from './model'; @@ -493,7 +493,7 @@ export class CommandCenter { const repositoryPath = await window.withProgress( opts, - (_, token) => this.git.clone(url!, parentPath, token) + (progress: Progress<{ message?: string, increment: number }>, token) => this.git.clone(url!, parentPath, progress, token) ); let message = localize('proposeopen', "Would you like to open the cloned repository?"); diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index 3dcf493277e..e70bd1e0c62 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -12,10 +12,13 @@ import { EventEmitter } from 'events'; import iconv = require('iconv-lite'); import * as filetype from 'file-type'; import { assign, groupBy, denodeify, IDisposable, toDisposable, dispose, mkdirp, readBytes, detectUnicodeEncoding, Encoding, onceEvent, splitInChunks, Limiter } from './util'; -import { CancellationToken } from 'vscode'; +import { CancellationToken, Progress } from 'vscode'; import { URI } from 'vscode-uri'; import { detectEncoding } from './encoding'; import { Ref, RefType, Branch, Remote, GitErrorCodes, LogOptions, Change, Status } from './api/git'; +import * as nls from 'vscode-nls'; + +const localize = nls.loadMessageBundle(); // https://github.com/microsoft/vscode/issues/65693 const MAX_CLI_LENGTH = 30000; @@ -163,9 +166,10 @@ export interface SpawnOptions extends cp.SpawnOptions { encoding?: string; log?: boolean; cancellationToken?: CancellationToken; + progress?: Progress<{ message?: string, increment: number }>; } -async function exec(child: cp.ChildProcess, cancellationToken?: CancellationToken): Promise> { +async function exec(child: cp.ChildProcess, cancellationToken?: CancellationToken, progress?: Progress<{ message?: string, increment: number }>): Promise> { if (!child.stdout || !child.stderr) { throw new GitError({ message: 'Failed to get stdout or stderr from git process.' }); } @@ -186,6 +190,9 @@ async function exec(child: cp.ChildProcess, cancellationToken?: CancellationToke disposables.push(toDisposable(() => ee.removeListener(name, fn))); }; + const cloneProgressOutput = ['Receiving objects', 'Resolving deltas']; + let prevInc = 0; + let result = Promise.all([ new Promise((c, e) => { once(child, 'error', cpErrorHandler(e)); @@ -198,7 +205,27 @@ async function exec(child: cp.ChildProcess, cancellationToken?: CancellationToke }), new Promise(c => { const buffers: Buffer[] = []; - on(child.stderr, 'data', (b: Buffer) => buffers.push(b)); + on(child.stderr, 'data', (b: Buffer) => { + buffers.push(b); + const s = b.toString(); + + // Check for git clone progress reporting + cloneProgressOutput.forEach(cloneOutput => { + if (s.startsWith(cloneOutput)) { + const idx = s.indexOf('%'); + const inc = parseInt(s.slice(idx - 3, idx)); + + if (progress) { + progress.report({ + message: localize(cloneOutput.toLowerCase(), cloneOutput) + ': ' + inc + '%', + increment: inc - prevInc + }); + + prevInc = inc; + } + } + }); + }); once(child.stderr, 'close', () => c(Buffer.concat(buffers).toString('utf8'))); }) ]) as Promise<[number, Buffer, string]>; @@ -341,7 +368,7 @@ export class Git { return; } - async clone(url: string, parentPath: string, cancellationToken?: CancellationToken): Promise { + async clone(url: string, parentPath: string, progress: Progress<{ message?: string, increment: number }>, cancellationToken?: CancellationToken): Promise { let baseFolderName = decodeURI(url).replace(/[\/]+$/, '').replace(/^.*[\/\\]/, '').replace(/\.git$/, '') || 'repository'; let folderName = baseFolderName; let folderPath = path.join(parentPath, folderName); @@ -355,7 +382,7 @@ export class Git { await mkdirp(parentPath); try { - await this.exec(parentPath, ['clone', url.includes(' ') ? encodeURI(url) : url, folderPath], { cancellationToken }); + await this.exec(parentPath, ['clone', url.includes(' ') ? encodeURI(url) : url, folderPath, '--progress'], { cancellationToken, progress }); } catch (err) { if (err.stderr) { err.stderr = err.stderr.replace(/^Cloning.+$/m, '').trim(); @@ -405,7 +432,7 @@ export class Git { child.stdin.end(options.input, 'utf8'); } - const bufferResult = await exec(child, options.cancellationToken); + const bufferResult = await exec(child, options.cancellationToken, options.progress); if (options.log !== false && bufferResult.stderr.length > 0) { this.log(`${bufferResult.stderr}\n`);