This repository was archived by the owner on Jul 1, 2026. It is now read-only.
forked from benchmark-action/github-action-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.ts
More file actions
94 lines (80 loc) · 2.99 KB
/
Copy pathgit.ts
File metadata and controls
94 lines (80 loc) · 2.99 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
import { exec } from '@actions/exec';
import * as core from '@actions/core';
import * as github from '@actions/github';
interface ExecResult {
stdout: string;
stderr: string;
code: number | null;
}
async function capture(cmd: string, args: string[]): Promise<ExecResult> {
const res: ExecResult = {
stdout: '',
stderr: '',
code: null,
};
try {
const code = await exec(cmd, args, {
listeners: {
stdout(data) {
res.stdout += data.toString();
},
stderr(data) {
res.stderr += data.toString();
},
},
});
res.code = code;
return res;
} catch (err) {
const msg = `Command '${cmd}' failed with args '${args.join(' ')}': ${res.stderr}: ${err}`;
core.debug(`@actions/exec.exec() threw an error: ${msg}`);
throw new Error(msg);
}
}
export async function cmd(...args: string[]): Promise<string> {
core.debug(`Executing Git: ${args.join(' ')}`);
const userArgs = [
'-c',
'user.name=github-action-benchmark',
'-c',
'user.email=github@users.noreply.github.com',
'-c',
'http.https://github.com/.extraheader=', // This config is necessary to support actions/checkout@v2 (#9)
];
const res = await capture('git', userArgs.concat(args));
if (res.code !== 0) {
throw new Error(`Command 'git ${args.join(' ')}' failed: ${JSON.stringify(res)}`);
}
return res.stdout;
}
function getRemoteUrl(token: string): string {
const { repo, owner } = github.context.repo;
return `https://x-access-token:${token}@github.com/${owner}/${repo}.git`;
}
export async function push(token: string, branch: string, ...options: string[]): Promise<string> {
core.debug(`Executing 'git push' to branch '${branch}' with token and options '${options.join(' ')}'`);
const remote = getRemoteUrl(token);
let args = ['push', remote, `${branch}:${branch}`, '--no-verify'];
if (options.length > 0) {
args = args.concat(options);
}
return cmd(...args);
}
export async function pull(token: string | undefined, branch: string, ...options: string[]): Promise<string> {
core.debug(`Executing 'git pull' to branch '${branch}' with token and options '${options.join(' ')}'`);
const remote = token !== undefined ? getRemoteUrl(token) : 'origin';
let args = ['pull', remote, branch];
if (options.length > 0) {
args = args.concat(options);
}
return cmd(...args);
}
export async function fetch(token: string | undefined, branch: string, ...options: string[]): Promise<string> {
core.debug(`Executing 'git fetch' for branch '${branch}' with token and options '${options.join(' ')}'`);
const remote = token !== undefined ? getRemoteUrl(token) : 'origin';
let args = ['fetch', remote, `${branch}:${branch}`];
if (options.length > 0) {
args = args.concat(options);
}
return cmd(...args);
}