Added support for encodings for Git.

Fixes #21146

**Bug**
Git always uses utf8 encoding for retrieving file contents.

**Fix**
Pass the 'files.encoding' configuration property to the git-extension and use this encoding to decode git output.
This commit is contained in:
Vakhurin Sergey
2017-04-17 23:00:08 +03:00
parent 7300527f44
commit 74349805ab
5 changed files with 24 additions and 9 deletions

View File

@@ -9,6 +9,7 @@ import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';
import * as cp from 'child_process';
import iconv = require('iconv-lite');
import { assign, uniqBy, groupBy, denodeify, IDisposable, toDisposable, dispose, mkdirp } from './util';
import { EventEmitter, Event } from 'vscode';
import * as nls from 'vscode-nls';
@@ -159,7 +160,7 @@ export interface IExecutionResult {
stderr: string;
}
export async function exec(child: cp.ChildProcess): Promise<IExecutionResult> {
export async function exec(child: cp.ChildProcess, encoding: string = 'utf8'): Promise<IExecutionResult> {
const disposables: IDisposable[] = [];
const once = (ee: NodeJS.EventEmitter, name: string, fn: Function) => {
@@ -178,14 +179,14 @@ export async function exec(child: cp.ChildProcess): Promise<IExecutionResult> {
once(child, 'exit', c);
}),
new Promise<string>(c => {
const buffers: string[] = [];
const buffers: Buffer[] = [];
on(child.stdout, 'data', b => buffers.push(b));
once(child.stdout, 'close', () => c(buffers.join('')));
once(child.stdout, 'close', () => c(decode(Buffer.concat(buffers), encoding)));
}),
new Promise<string>(c => {
const buffers: string[] = [];
const buffers: Buffer[] = [];
on(child.stderr, 'data', b => buffers.push(b));
once(child.stderr, 'close', () => c(buffers.join('')));
once(child.stderr, 'close', () => c(decode(Buffer.concat(buffers), encoding)));
})
]);
@@ -194,6 +195,10 @@ export async function exec(child: cp.ChildProcess): Promise<IExecutionResult> {
return { exitCode, stdout, stderr };
}
function decode(buffer: NodeBuffer, encoding: string): string {
return iconv.decode(buffer, encoding);
}
export interface IGitErrorData {
error?: Error;
message?: string;
@@ -329,7 +334,7 @@ export class Git {
child.stdin.end(options.input, 'utf8');
}
const result = await exec(child);
const result = await exec(child, options.encoding);
if (result.exitCode) {
let gitErrorCode: string | undefined = void 0;