/
git-ext.js
75 lines (61 loc) · 1.79 KB
/
git-ext.js
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
const path = require("path");
const vscode = require("vscode");
class GitExt {
constructor() {
const ext = vscode.extensions.getExtension("vscode.git");
this.gitApi = ext.isActive && ext.exports.getAPI(1);
}
get hasRepositories() {
return this.repositories.length > 0;
}
get repositories() {
return this.gitApi ? this.gitApi.repositories : [];
}
get rootPath() {
if (this.repositories.length > 0)
return this.selectedRepository.rootUri.fsPath;
return "";
}
get selectedFolderName() {
const segments = this.rootPath.split(path.sep);
if (segments.length > 0) {
return segments.pop();
}
return "";
}
get selectedRepository() {
if (this.repositories.length === 1) return this.repositories[0];
return this.repositories.find((repo) => repo.ui.selected);
}
updateSelectedInput(value) {
const valueIsFunction = typeof value === "function";
const repo = this.selectedRepository;
if (valueIsFunction) {
repo.inputBox.value = value(repo.inputBox.value);
} else {
repo.inputBox.value = value;
}
}
onDidChangeUiState(stateChangeCallback) {
const trackRepos = [];
for (let repo of this.repositories) {
if (!trackRepos.includes(repo.rootUri.path)) {
trackRepos.push(repo.rootUri.path);
repo.ui.onDidChange(stateChangeCallback);
}
}
this.gitApi.onDidOpenRepository(function (repo) {
if (!trackRepos.includes(repo.rootUri.path)) {
trackRepos.push(repo.rootUri.path);
repo.ui.onDidChange(stateChangeCallback);
}
});
this.gitApi.onDidCloseRepository(function (repo) {
const index = trackRepos.indexOf(repo.rootUri.path);
if (index > -1) {
trackRepos.splice(index, 1);
}
});
}
}
exports.GitExt = GitExt;