Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/core/git.ts
3557 views
1
/*
2
* git.ts
3
*
4
* Copyright (C) 2020-2022 Posit Software, PBC
5
*/
6
7
import { which } from "./path.ts";
8
import { execProcess } from "./process.ts";
9
import SemVer from "semver/mod.ts";
10
11
export async function gitCmds(dir: string, argsArray: Array<string[]>) {
12
for (const args of argsArray) {
13
if (
14
!(await execProcess({
15
cmd: "git",
16
args,
17
cwd: dir,
18
})).success
19
) {
20
throw new Error();
21
}
22
}
23
}
24
25
export async function gitVersion(): Promise<SemVer> {
26
const result = await execProcess(
27
{
28
cmd: "git",
29
args: ["--version"],
30
stdout: "piped",
31
},
32
);
33
if (!result.success) {
34
throw new Error(
35
"Unable to determine git version. Please check that git is installed and available on your PATH.",
36
);
37
}
38
const match = result.stdout?.match(/git version (\d+\.\d+\.\d+)/);
39
if (match) {
40
return new SemVer(match[1]);
41
} else {
42
throw new Error(
43
`Unable to determine git version from string ${result.stdout}`,
44
);
45
}
46
}
47
48
export async function lsFiles(
49
cwd?: string,
50
args?: string[],
51
): Promise<string[] | undefined> {
52
if (await which("git")) {
53
const result = await execProcess({
54
cmd: "git",
55
args: ["ls-files", ...(args || [])],
56
cwd,
57
stdout: "piped",
58
stderr: "piped",
59
});
60
61
if (result.success) {
62
return result.stdout?.split("\n").filter((file) => {
63
return file.length > 0;
64
});
65
}
66
}
67
68
return Promise.resolve(undefined);
69
}
70
71
export async function gitBranchExists(
72
branch: string,
73
cwd?: string,
74
): Promise<boolean | undefined> {
75
if (await which("git")) {
76
const result = await execProcess({
77
cmd: "git",
78
args: ["show-ref", "--verify", "--quiet", `refs/heads/${branch}`],
79
cwd,
80
stdout: "piped",
81
stderr: "piped",
82
});
83
84
return result.code === 0;
85
}
86
87
return Promise.resolve(undefined);
88
}
89
90