-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgit.ts
More file actions
97 lines (79 loc) · 2.15 KB
/
git.ts
File metadata and controls
97 lines (79 loc) · 2.15 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { execSync, spawn, spawnSync } from 'child_process';
import { noLimits } from '../model/limits';
export function isRepo(): boolean {
try {
execSync('git rev-parse --is-inside-work-tree', { stdio: 'ignore' });
return true;
} catch (error) {
return false;
}
}
export function calcTreeHash(): string {
const result = spawnSync('git', ['rev-parse', 'HEAD^{tree}'], {
encoding: 'utf-8',
});
if (result.error) {
throw new Error('Creating Git Tree Hash failed: ' + result.error.message);
}
if (result.status !== 0) {
throw new Error(
'Creating Git Tree Hash failed with exit code: ' +
result.status +
'\n' +
result.stderr
);
}
return result.stdout.trim();
}
export function getCommitCount(): string {
const result = spawnSync('git', ['rev-list', '--count', 'HEAD'], {
encoding: 'utf-8',
});
if (result.error) {
throw new Error('Creating Git Tree Hash failed: ' + result.error.message);
}
if (result.status !== 0) {
throw new Error(
'Creating Git Tree Hash failed with exit code: ' +
result.status +
'\n' +
result.stderr
);
}
return result.stdout.trim();
}
export function getGitLog(limits = noLimits): Promise<string> {
return new Promise<string>((resolve, reject) => {
try {
const args = [
'log',
'--numstat',
'--pretty=format:"%an <%ae>,%ad%x09%H,%s"',
];
if (limits.limitCommits) {
args.push('-n ' + limits.limitCommits);
}
if (limits.limitMonths) {
args.push(`--since="${limits.limitMonths} months ago"`);
}
const subprocess = spawn('git', args);
let text = '';
let error = '';
subprocess.stdout.on('data', (data: string) => {
text += data;
});
subprocess.stderr.on('data', (data: string) => {
error += data;
});
subprocess.on('exit', (code: string) => {
if (code || error) {
reject(new Error('[Error running Git] ' + error));
} else {
resolve(text);
}
});
} catch (e) {
reject(new Error('Error running git: ' + e));
}
});
}