forked from mdn/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.ts
More file actions
66 lines (59 loc) · 1.73 KB
/
git.ts
File metadata and controls
66 lines (59 loc) · 1.73 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
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import child_process from 'node:child_process';
type Fields = {
value: string;
headPath: string;
basePath: string;
};
/**
* Get the git merge base
*
* @param {string} x The first git reference
* @param {string} y The second git reference
* @returns {string} The output from the `git merge-base` command
*/
const getMergeBase = (x: string, y = 'HEAD'): string =>
child_process
.execSync(`git merge-base ${x} ${y}`, { encoding: 'utf-8' })
.trim();
/**
* Parse fields from a git diff status output
*
* @param {string[]} fields The fields to parse
* @returns {Fields} The parsed fields
*/
const parseFields = (fields: string[]): Fields => ({
value: fields[0],
headPath: fields[2] || fields[1],
basePath: fields[1],
});
/**
* Get git diff statuses between two refs
*
* @param {string} base The first git ref
* @param {string} head The second git refs
* @returns {Fields[]} The diff statuses
*/
const getGitDiffStatuses = (base: string, head: string): Fields[] =>
child_process
.execSync(`git diff --name-status ${base} ${head}`, { encoding: 'utf-8' })
.trim()
.split('\n')
.map((line) => line.split('\t'))
.map(parseFields);
/**
* Get file contents from a specific commit and file path
*
* @param {string} commit The commit hash to get contents from
* @param {string} path The file path to get contents from
* @returns {string} The file contents
*/
const getFileContent = (commit: string, path: string): string =>
child_process
.execSync(`git show ${commit}:${path}`, {
encoding: 'utf-8',
stdio: 'pipe',
})
.trim();
export { getMergeBase, getGitDiffStatuses, getFileContent };