forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.ts
More file actions
76 lines (65 loc) · 1.88 KB
/
Copy pathgit.ts
File metadata and controls
76 lines (65 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as iconv from 'iconv-lite';
import { execFile } from 'child_process';
const gitInfo = {
path: null
};
export interface IGitResult {
/** The standard output from git. */
readonly stdout: string;
/** The standard error output from git. */
readonly stderr: string;
/** The exit code of the git process. */
readonly exitCode: number;
}
export interface IGitExecutionOptions {
readonly cwd?: string;
readonly env?: Object;
readonly encoding?: BufferEncoding;
readonly maxBuffer?: number;
readonly stdin?: string | Buffer;
readonly stdinEncoding?: string;
}
export function setGitPath(path: string) {
gitInfo.path = path;
}
export async function exec(args: any[], options: IGitExecutionOptions): Promise<IGitResult> {
const encoding = options.encoding || 'utf8';
const runOpts = {
...options,
encoding: encoding === 'utf8' ? 'utf8' : 'binary',
env: { ...(options.env || process.env) }
} as IGitExecutionOptions;
args.splice(0, 0, '-c', 'core.quotepath=false', '-c', 'color.ui=false');
let data: IGitResult;
try {
data = await run(gitInfo.path, args, encoding, runOpts);
} catch (ex) {
}
return data;
}
export function run(command: string, args: any[], encoding: string, options: IGitExecutionOptions = {}) {
const { stdin, stdinEncoding, ...opts } = { maxBuffer: 10 * 1024 * 1024, ...options } as IGitExecutionOptions;
return new Promise<IGitResult>((resolve, reject) => {
const proc = execFile(
command,
args,
opts,
(err: Error & { code?: string | number } | null, stdout, stderr) => {
let data = stdout;
if (!err) {
if (encoding !== 'utf8' && encoding !== 'binary') {
data = iconv.decode(Buffer.from(stdout, 'binary'), encoding);
}
}
resolve({
stdout: data,
stderr: stderr,
exitCode: err && err.code ? Number(err.code) : 0
});
}
);
if (stdin) {
proc.stdin.end(stdin, stdinEncoding || 'utf8');
}
});
}