/
repository.ts
222 lines (191 loc) · 6.25 KB
/
repository.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { spawnSync } from "child_process";
import { RepositoryType } from "../../models";
import { BasePath } from "../utils/base-path";
const TEN_MEGABYTES: number = 1024 * 10000;
function git(...args: string[]) {
return spawnSync("git", args, {
encoding: "utf-8",
windowsHide: true,
maxBuffer: TEN_MEGABYTES,
});
}
export const gitIsInstalled = git("--version").status === 0;
/**
* Stores data of a repository.
*/
export class Repository {
/**
* The root path of this repository.
*/
path: string;
/**
* The name of the branch this repository is on right now.
*/
branch: string;
/**
* A list of all files tracked by the repository.
*/
files: string[] = [];
/**
* The user/organization name of this repository on GitHub.
*/
user?: string;
/**
* The project name of this repository on GitHub.
*/
project?: string;
/**
* The hostname for this GitHub/Bitbucket/.etc project.
*
* Defaults to: `github.com` (for normal, public GitHub instance projects)
*
* Can be the hostname for an enterprise version of GitHub, e.g. `github.acme.com`
* (if found as a match in the list of git remotes).
*/
hostname = "github.com";
/**
* Whether this is a GitHub, Bitbucket, or other type of repository.
*/
type: RepositoryType = RepositoryType.GitHub;
private urlCache = new Map<string, string>();
/**
* Create a new Repository instance.
*
* @param path The root path of the repository.
*/
constructor(path: string, gitRevision: string, repoLinks: string[]) {
this.path = path;
this.branch = gitRevision || "master";
for (let i = 0, c = repoLinks.length; i < c; i++) {
let match =
/(github(?:\.[a-z]+)*\.[a-z]{2,})[:/]([^/]+)\/(.*)/.exec(
repoLinks[i]
);
// Github Enterprise
if (!match) {
match = /(\w+\.githubprivate.com)[:/]([^/]+)\/(.*)/.exec(
repoLinks[i]
);
}
// Github Enterprise
if (!match) {
match = /(\w+\.ghe.com)[:/]([^/]+)\/(.*)/.exec(
repoLinks[i]
);
}
// Github Enterprise
if (!match) {
match = /(\w+\.github.us)[:/]([^/]+)\/(.*)/.exec(
repoLinks[i]
);
}
if (!match) {
match = /(bitbucket.org)[:/]([^/]+)\/(.*)/.exec(repoLinks[i]);
}
if (!match) {
match = /(gitlab.com)[:/]([^/]+)\/(.*)/.exec(repoLinks[i]);
}
if (match) {
this.hostname = match[1];
this.user = match[2];
this.project = match[3];
if (this.project.endsWith(".git")) {
this.project = this.project.slice(0, -4);
}
break;
}
}
if (this.hostname.includes("bitbucket.org")) {
this.type = RepositoryType.Bitbucket;
} else if (this.hostname.includes("gitlab.com")) {
this.type = RepositoryType.GitLab;
} else {
this.type = RepositoryType.GitHub;
}
let out = git("-C", path, "ls-files");
if (out.status === 0) {
out.stdout.split("\n").forEach((file) => {
if (file !== "") {
this.files.push(BasePath.normalize(path + "/" + file));
}
});
}
if (!gitRevision) {
out = git("-C", path, "rev-parse", "--short", "HEAD");
if (out.status === 0) {
this.branch = out.stdout.replace("\n", "");
}
}
}
/**
* Check whether the given file is tracked by this repository.
*
* @param fileName The name of the file to test for.
* @returns TRUE when the file is part of the repository, otherwise FALSE.
*/
contains(fileName: string): boolean {
return this.files.includes(fileName);
}
/**
* Get the URL of the given file on GitHub or Bitbucket.
*
* @param fileName The file whose URL should be determined.
* @returns A URL pointing to the web preview of the given file or undefined.
*/
getURL(fileName: string): string | undefined {
if (this.urlCache.has(fileName)) {
return this.urlCache.get(fileName)!;
}
if (!this.user || !this.project || !this.contains(fileName)) {
return;
}
const url = [
`https://${this.hostname}`,
this.user,
this.project,
this.type === RepositoryType.GitLab ? "-" : undefined,
this.type === RepositoryType.Bitbucket ? "src" : "blob",
this.branch,
fileName.substring(this.path.length + 1),
]
.filter((s) => !!s)
.join("/");
this.urlCache.set(fileName, url);
return url;
}
getLineNumberAnchor(lineNumber: number): string {
switch (this.type) {
default:
case RepositoryType.GitHub:
case RepositoryType.GitLab:
return "L" + lineNumber;
case RepositoryType.Bitbucket:
return "lines-" + lineNumber;
}
}
/**
* Try to create a new repository instance.
*
* Checks whether the given path is the root of a valid repository and if so
* creates a new instance of {@link Repository}.
*
* @param path The potential repository root.
* @returns A new instance of {@link Repository} or undefined.
*/
static tryCreateRepository(
path: string,
gitRevision: string,
gitRemote: string
): Repository | undefined {
const out = git("-C", path, "rev-parse", "--show-toplevel");
const remotesOutput = git("-C", path, "remote", "get-url", gitRemote);
if (out.status !== 0 || remotesOutput.status !== 0) {
return;
}
return new Repository(
BasePath.normalize(out.stdout.replace("\n", "")),
gitRevision,
remotesOutput.stdout.split("\n")
);
}
}