From 1f38d8e8dafe66de4f6c72236cc32992aa93d723 Mon Sep 17 00:00:00 2001 From: Joao Moreno Date: Mon, 28 Nov 2016 11:31:38 +0100 Subject: [PATCH] git: output channel --- extensions/git/src/git.ts | 27 +++++++++++---------- extensions/git/src/main.ts | 49 ++++++++++++++++++++++---------------- extensions/git/src/util.ts | 2 +- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/extensions/git/src/git.ts b/extensions/git/src/git.ts index c6b863351a8..7a572aa9adf 100644 --- a/extensions/git/src/git.ts +++ b/extensions/git/src/git.ts @@ -11,6 +11,7 @@ import * as cp from 'child_process'; import * as denodeify from 'denodeify'; import { IDisposable, toDisposable, dispose } from './util'; import * as _ from 'lodash'; +import { EventEmitter, Event } from 'vscode'; const readdir = denodeify(fs.readdir); @@ -252,12 +253,15 @@ function encodingExists(encoding) { export class Git { - gitPath: string; - version: string; - env: any; + private gitPath: string; + private version: string; + private env: any; private defaultEncoding: string; private outputListeners: { (output: string): void; }[]; + private _onOutput = new EventEmitter(); + get onOutput(): Event { return this._onOutput.event; } + constructor(options: IGitOptions) { this.gitPath = options.gitPath; this.version = options.version; @@ -276,11 +280,11 @@ export class Git { stream(cwd: string, args: string[], options: any = {}): cp.ChildProcess { options = _.assign({ cwd: cwd }, options || {}); - return this.spawn(args, options); + return this._spawn(args, options); } private _exec(args: string[], options: any = {}): Promise { - const child = this.spawn(args, options); + const child = this._spawn(args, options); if (options.input) { child.stdin.end(options.input, 'utf8'); @@ -304,6 +308,10 @@ export class Git { gitErrorCode = GitErrorCodes.CantAccessRemote; } + if (options.log !== false) { + this.log(result.stderr); + } + return Promise.reject(new GitError({ message: 'Failed to execute git', stdout: result.stdout, @@ -318,7 +326,7 @@ export class Git { }); } - spawn(args: string[], options: any = {}): cp.ChildProcess { + private _spawn(args: string[], options: any = {}): cp.ChildProcess { if (!this.gitPath) { throw new Error('git could not be found in the system.'); } @@ -340,12 +348,7 @@ export class Git { return cp.spawn(this.gitPath, args, options); } - onOutput(listener: (output: string) => void): () => void { - this.outputListeners.push(listener); - return () => this.outputListeners.splice(this.outputListeners.indexOf(listener), 1); - } - private log(output: string): void { - this.outputListeners.forEach(l => l(output)); + this._onOutput.fire(output); } } \ No newline at end of file diff --git a/extensions/git/src/main.ts b/extensions/git/src/main.ts index 5bdf56e8899..3da7898dc29 100644 --- a/extensions/git/src/main.ts +++ b/extensions/git/src/main.ts @@ -5,7 +5,7 @@ 'use strict'; -import { scm, ExtensionContext, workspace, Uri } from 'vscode'; +import { scm, ExtensionContext, workspace, Uri, window, Disposable } from 'vscode'; import * as path from 'path'; import { findGit, Git } from './git'; import { registerCommands } from './commands'; @@ -27,6 +27,23 @@ class GitSCMProvider { } } +class TextDocumentContentProvider { + + constructor(private git: Git, private rootPath: string) { } + + provideTextDocumentContent(uri: Uri) { + const relativePath = path.relative(this.rootPath, uri.fsPath); + + return this.git.exec(this.rootPath, ['show', `HEAD:${relativePath}`]).then(result => { + if (result.exitCode !== 0) { + return null; + } + + return result.stdout; + }); + } +} + export function activate(context: ExtensionContext): any { if (!workspace.rootPath) { return; @@ -36,28 +53,20 @@ export function activate(context: ExtensionContext): any { const pathHint = workspace.getConfiguration('git').get('path'); findGit(pathHint).then(info => { - log(`Using git ${info.version} from ${info.path}`); - const git = new Git({ gitPath: info.path, version: info.version }); - const provider = new GitSCMProvider(); - const providerDisposable = scm.registerSCMProvider('git', provider); - - const contentProvider = workspace.registerTextDocumentContentProvider('git-index', { - provideTextDocumentContent: uri => { - const relativePath = path.relative(rootPath, uri.fsPath); + const disposables: Disposable[] = []; - return git.exec(rootPath, ['show', `HEAD:${relativePath}`]).then(result => { - if (result.exitCode !== 0) { - return null; - } - - return result.stdout; - }); - } - }); + const outputChannel = window.createOutputChannel('git'); + outputChannel.appendLine(`Using git ${info.version} from ${info.path}`); + git.onOutput(str => outputChannel.append(str), null, disposables); - const commands = registerCommands(); + disposables.push( + registerCommands(), + scm.registerSCMProvider('git', new GitSCMProvider()), + workspace.registerTextDocumentContentProvider('git-index', new TextDocumentContentProvider(git, rootPath)), + outputChannel + ); - context.subscriptions.push(providerDisposable, contentProvider, commands); + context.subscriptions.push(...disposables); }); } \ No newline at end of file diff --git a/extensions/git/src/util.ts b/extensions/git/src/util.ts index dc98359c153..2cf2ec01029 100644 --- a/extensions/git/src/util.ts +++ b/extensions/git/src/util.ts @@ -34,4 +34,4 @@ export function filterEvent(event: Event, filter: (e: T) => boolean): Even export function anyEvent(...events: Event[]): Event { return (listener, thisArgs = null, disposables?) => combinedDisposable(events.map(event => event(i => listener.call(thisArgs, i), disposables))); -} +} \ No newline at end of file -- GitLab