-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.ts
121 lines (116 loc) · 3.39 KB
/
util.ts
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { execute, ExecuteError, type ExecuteOptions } from "./process.ts";
/**
* A helper function to throw error
*
* https://github.com/tc39/proposal-throw-expressions
*/
export function __throw(err: unknown): never {
throw err;
}
/**
* Returns a remote name that contains the given commitish.
*
* It returns undefined if there is no remote that contains the commitish.
* If there are multiple remotes that contain the commitish, it prefer to return "origin" or the first one.
*/
export async function getRemoteContains(
commitish: string,
options: ExecuteOptions = {},
): Promise<string | undefined> {
try {
const branches = (await execute(
["branch", "-r", "--contains", commitish, "--format=%(refname:short)"],
options,
)).trim().split("\n");
const remotes = (await execute(["remote"], options)).trim().split("\n");
const remoteContains = remotes.filter((remote) => {
return branches.some((branch) => branch.startsWith(`${remote}/`));
});
// Prefer "origin" if it exists
return remoteContains.includes("origin") ? "origin" : remoteContains.at(0);
} catch (err: unknown) {
if (err instanceof ExecuteError && err.code === 129) {
// error: malformed object name ...
return undefined;
}
throw err;
}
}
/**
* Returns a remote's fetch URL.
*
* It returns undefined if the remote does not exist.
* If the remote has multiple URLs, it returns the first one.
*/
export async function getRemoteFetchURL(
remote: string,
options: ExecuteOptions = {},
): Promise<URL | undefined> {
try {
const stdout = await execute(
["remote", "get-url", remote],
options,
);
// a remote always has at least one URL
const url = stdout.trim().split("\n").at(0)!;
return new URL(url.replace(/^git@([^:]+):(.*)\.git$/, "ssh://git@$1/$2"));
} catch (err: unknown) {
if (err instanceof ExecuteError && err.code === 2) {
// error: No such remote '...'
return undefined;
}
throw err;
}
}
/**
* Returns a commit's SHA-1.
*
* It returns undefined if the commitish does not exist.
*/
export async function getCommitSHA1(
commitish: string,
options: ExecuteOptions = {},
): Promise<string | undefined> {
try {
const stdout = await execute(
["rev-parse", commitish],
options,
);
return stdout.trim();
} catch (err: unknown) {
if (err instanceof ExecuteError && err.code === 128) {
// ...
// fatal: ambiguous argument '...': unknown revision or path not in the working tree.
// Use '--' to separate paths from revisions, like this:
// 'git <command> [<revision>...] -- [<file>...]'
return undefined;
}
throw err;
}
}
/**
* Returns a commit's abbrev ref
*
* It returns undefined if the commitish does not exist.
*/
export async function getCommitAbbrevRef(
commitish: string,
options: ExecuteOptions = {},
): Promise<string | undefined> {
try {
const stdout = await execute(
["rev-parse", "--abbrev-ref", commitish],
options,
);
return stdout.trim() || commitish;
} catch (err: unknown) {
if (err instanceof ExecuteError && err.code === 128) {
// ...
// fatal: ambiguous argument '...': unknown revision or path not in the working tree.
// Use '--' to separate paths from revisions, like this:
// 'git <command> [<revision>...] -- [<file>...]'
return undefined;
}
throw err;
}
}