Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions extensions/git/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -478,7 +478,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)
);

const choices = [];
Expand Down
38 changes: 32 additions & 6 deletions extensions/git/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.' });
}
Expand All @@ -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'];

Copy link
Copy Markdown
Contributor

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 exec method. Clone should just switch to using stream instead.

Copy link
Copy Markdown
Contributor

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 objects and Compressing objects.

let prevInc = 0;

let result = Promise.all<any>([
new Promise<number>((c, e) => {
once(child, 'error', cpErrorHandler(e));
Expand All @@ -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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 byline to get lines.

const idx = s.indexOf('%');
const inc = parseInt(s.slice(idx - 3, idx));

if (progress) {
progress.report({
message: localize(cloneOutput.toLowerCase(), cloneOutput) + ': ' + inc + '%',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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]>;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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`);
Expand Down