🎨 cleanup git show code

This commit is contained in:
Joao Moreno 2017-04-24 10:51:08 +02:00
parent 3598679114
commit 566fdd4879
3 changed files with 20 additions and 35 deletions

View file

@ -158,7 +158,7 @@ export interface IExecutionResult {
stderr: string;
}
export async function exec(child: cp.ChildProcess, encoding: string = 'utf8'): Promise<IExecutionResult> {
async function exec(child: cp.ChildProcess, options: any = {}): Promise<IExecutionResult> {
const disposables: IDisposable[] = [];
const once = (ee: NodeJS.EventEmitter, name: string, fn: Function) => {
@ -179,7 +179,7 @@ export async function exec(child: cp.ChildProcess, encoding: string = 'utf8'): P
new Promise<string>(c => {
const buffers: Buffer[] = [];
on(child.stdout, 'data', b => buffers.push(b));
once(child.stdout, 'close', () => c(iconv.decode(Buffer.concat(buffers), encoding)));
once(child.stdout, 'close', () => c(iconv.decode(Buffer.concat(buffers), options.encoding || 'utf8')));
}),
new Promise<string>(c => {
const buffers: Buffer[] = [];
@ -328,7 +328,7 @@ export class Git {
child.stdin.end(options.input, 'utf8');
}
const result = await exec(child, options.encoding);
const result = await exec(child, options);
if (result.exitCode) {
let gitErrorCode: string | undefined = void 0;
@ -513,14 +513,23 @@ export class Repository {
return result.stdout;
}
async buffer(object: string): Promise<string> {
async buffer(object: string, encoding: string = 'utf8'): Promise<string> {
const child = this.stream(['show', object]);
if (!child.stdout) {
return Promise.reject<string>('Can\'t open file from git');
}
return await this.doBuffer(object);
const { exitCode, stdout } = await exec(child, { encoding });
if (exitCode) {
return Promise.reject<string>(new GitError({
message: 'Could not show object.',
exitCode
}));
}
return stdout;
// TODO@joao
// return new Promise((c, e) => {
@ -539,20 +548,6 @@ export class Repository {
// });
}
private async doBuffer(object: string): Promise<string> {
const child = this.stream(['show', object]);
const { exitCode, stdout } = await exec(child);
if (exitCode) {
return Promise.reject<string>(new GitError({
message: 'Could not buffer object.',
exitCode
}));
}
return stdout;
}
async add(paths: string[]): Promise<void> {
const args = ['add', '-A', '--'];

View file

@ -29,7 +29,6 @@ async function init(context: ExtensionContext, disposables: Disposable[]): Promi
const outputChannel = window.createOutputChannel('Git');
disposables.push(outputChannel);
const configFiles = workspace.getConfiguration('files');
const config = workspace.getConfiguration('git');
const enabled = config.get<boolean>('enabled') === true;
const workspaceRootPath = workspace.rootPath;
@ -46,7 +45,7 @@ async function init(context: ExtensionContext, disposables: Disposable[]): Promi
return;
}
const model = new Model(git, workspaceRootPath, configFiles.get<string>('encoding'));
const model = new Model(git, workspaceRootPath);
outputChannel.appendLine(localize('using git', "Using git {0} from {1}", info.version, info.path));

View file

@ -6,7 +6,7 @@
'use strict';
import { Uri, Command, EventEmitter, Event, SourceControlResourceState, SourceControlResourceDecorations, Disposable, window, workspace } from 'vscode';
import { Git, Repository, Ref, Branch, Remote, PushOptions, Commit, GitErrorCodes, GitError } from './git';
import { Git, Repository, Ref, Branch, Remote, PushOptions, Commit, GitErrorCodes } from './git';
import { anyEvent, eventToPromise, filterEvent, mapEvent, EmptyDisposable, combinedDisposable, dispose } from './util';
import { memoize, throttle, debounce } from './decorators';
import { watch } from './watch';
@ -358,8 +358,7 @@ export class Model implements Disposable {
constructor(
private _git: Git,
private workspaceRootPath: string,
private encoding?: string
private workspaceRootPath: string
) {
const fsWatcher = workspace.createFileSystemWatcher('**');
this.onWorkspaceChange = anyEvent(fsWatcher.onDidChange, fsWatcher.onDidCreate, fsWatcher.onDidDelete);
@ -487,18 +486,10 @@ export class Model implements Disposable {
async show(ref: string, filePath: string): Promise<string> {
return await this.run(Operation.Show, async () => {
const relativePath = path.relative(this.repository.root, filePath).replace(/\\/g, '/');
const result = await this.repository.git.exec(this.repository.root, ['show', `${ref}:${relativePath}`], {
encoding: this.encoding
});
const configFiles = workspace.getConfiguration('files');
const encoding = configFiles.get<string>('encoding');
if (result.exitCode !== 0) {
throw new GitError({
message: localize('cantshow', "Could not show object"),
exitCode: result.exitCode
});
}
return result.stdout;
return await this.repository.buffer(`${ref}:${relativePath}`, encoding);
});
}