This repository has been archived by the owner on Aug 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 656
/
Copy pathindex.ts
86 lines (70 loc) · 1.94 KB
/
index.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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {AbsoluteFilePath} from "@internal/path";
import {spawn} from "@internal/child-process";
export function extractFileList(out: string): string[] {
const lines = out.trim().split("\n");
const files: string[] = [];
for (const line of lines) {
const match = line.trim().match(/^(?:[AM]|\?\?)\s+(.*?)$/);
if (match != null) {
files.push(match[1]);
}
}
return files;
}
export class VCSClient {
constructor(root: AbsoluteFilePath) {
this.root = root;
}
public root: AbsoluteFilePath;
public getDefaultBranch(): Promise<string> {
throw new Error("unimplemented");
}
public getModifiedFiles(branch: string): Promise<string[]> {
throw new Error("unimplemented");
}
public getUncommittedFiles(): Promise<string[]> {
throw new Error("unimplemented");
}
}
class GitVCSClient extends VCSClient {
constructor(root: AbsoluteFilePath) {
super(root);
}
public async getDefaultBranch(): Promise<string> {
const exitCode = await spawn(
"git",
["show-ref", "--verify", "--quiet", "refs/heads/main"],
{cwd: this.root},
).wait();
return exitCode === 0 ? "main" : "master";
}
public async getUncommittedFiles(): Promise<string[]> {
const stdout = (await spawn("git", ["status", "--short"], {cwd: this.root}).waitSuccess()).getOutput(
true,
false,
);
return extractFileList(stdout);
}
public async getModifiedFiles(branch: string): Promise<string[]> {
const stdout = (await spawn(
"git",
["diff", "--name-status", branch],
{cwd: this.root},
).waitSuccess()).getOutput(true, false);
return extractFileList(stdout);
}
}
export async function getVCSClient(
root: AbsoluteFilePath,
): Promise<undefined | VCSClient> {
if (await root.append(".git").exists()) {
return new GitVCSClient(root);
}
return undefined;
}