-
Notifications
You must be signed in to change notification settings - Fork 42k
Show git clone progress bar and percentage complete #71341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,10 +12,12 @@ 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 } from './util'; | ||
| import { CancellationToken, Uri } from 'vscode'; | ||
| import { CancellationToken, Uri, Progress } from 'vscode'; | ||
| import * as nls from 'vscode-nls'; | ||
| import { detectEncoding } from './encoding'; | ||
| import { Ref, RefType, Branch, Remote, GitErrorCodes, LogOptions, Change, Status } from './api/git'; | ||
|
|
||
| const localize = nls.loadMessageBundle(); | ||
| const readfile = denodeify<string, string | null, string>(fs.readFile); | ||
|
|
||
| export interface IGit { | ||
|
|
@@ -159,9 +161,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<IExecutionResult<Buffer>> { | ||
| async function exec(child: cp.ChildProcess, cancellationToken?: CancellationToken, progress?: Progress<{ message?: string, increment: number }>): Promise<IExecutionResult<Buffer>> { | ||
| if (!child.stdout || !child.stderr) { | ||
| throw new GitError({ message: 'Failed to get stdout or stderr from git process.' }); | ||
| } | ||
|
|
@@ -182,6 +185,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<any>([ | ||
| new Promise<number>((c, e) => { | ||
| once(child, 'error', cpErrorHandler(e)); | ||
|
|
@@ -194,7 +200,27 @@ async function exec(child: cp.ChildProcess, cancellationToken?: CancellationToke | |
| }), | ||
| new Promise<string>(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)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not guaranteed that output comes line by line. So you should use something like |
||
| const idx = s.indexOf('%'); | ||
| const inc = parseInt(s.slice(idx - 3, idx)); | ||
|
|
||
| if (progress) { | ||
| progress.report({ | ||
| message: localize(cloneOutput.toLowerCase(), cloneOutput) + ': ' + inc + '%', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Localize can only be used with literal strings, there's no way this would ever work. |
||
| increment: inc - prevInc | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This progress is incorrect. It goes twice from 0 to 100, which causes the progress bar to reset halfway. Since we know there are two phases, we should just divide everything by 2. |
||
| }); | ||
|
|
||
| prevInc = inc; | ||
| } | ||
| } | ||
| }); | ||
| }); | ||
| once(child.stderr, 'close', () => c(Buffer.concat(buffers).toString('utf8'))); | ||
| }) | ||
| ]) as Promise<[number, Buffer, string]>; | ||
|
|
@@ -335,7 +361,7 @@ export class Git { | |
| return; | ||
| } | ||
|
|
||
| async clone(url: string, parentPath: string, cancellationToken?: CancellationToken): Promise<string> { | ||
| async clone(url: string, parentPath: string, progress: Progress<{ message?: string, increment: number }>, cancellationToken?: CancellationToken): Promise<string> { | ||
| let baseFolderName = decodeURI(url).replace(/^.*\//, '').replace(/\.git$/, '') || 'repository'; | ||
| let folderName = baseFolderName; | ||
| let folderPath = path.join(parentPath, folderName); | ||
|
|
@@ -349,7 +375,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(); | ||
|
|
@@ -388,7 +414,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`); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This clone-specific work should not be done in the generic
execmethod. Clone should just switch to usingstreaminstead.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, two more "events" are missing:
Counting objectsandCompressing objects.