-
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathgitUtils.ts
More file actions
63 lines (55 loc) · 1.53 KB
/
gitUtils.ts
File metadata and controls
63 lines (55 loc) · 1.53 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
import { exec, getExecOutput } from "@actions/exec";
export const setupUser = async () => {
await exec("git", [
"config",
"user.name",
`"github-actions[bot]"`,
]);
await exec("git", [
"config",
"user.email",
`"41898282+github-actions[bot]@users.noreply.github.com"`,
]);
};
export const pullBranch = async (branch: string) => {
await exec("git", ["pull", "origin", branch]);
};
export const push = async (
branch: string,
{ force }: { force?: boolean } = {}
) => {
await exec(
"git",
["push", "origin", `HEAD:${branch}`, force && "--force"].filter<string>(
Boolean as any
)
);
};
export const pushTags = async () => {
await exec("git", ["push", "origin", "--tags"]);
};
export const switchToMaybeExistingBranch = async (branch: string) => {
let { stderr } = await getExecOutput("git", ["checkout", branch], {
ignoreReturnCode: true,
});
let isCreatingBranch = !stderr
.toString()
.includes(`Switched to a new branch '${branch}'`);
if (isCreatingBranch) {
await exec("git", ["checkout", "-b", branch]);
}
};
export const reset = async (
pathSpec: string,
mode: "hard" | "soft" | "mixed" = "hard"
) => {
await exec("git", ["reset", `--${mode}`, pathSpec]);
};
export const commitAll = async (message: string) => {
await exec("git", ["add", "."]);
await exec("git", ["commit", "-m", message]);
};
export const checkIfClean = async (): Promise<boolean> => {
const { stdout } = await getExecOutput("git", ["status", "--porcelain"]);
return !stdout.length;
};