-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathgit.ts
118 lines (106 loc) · 4.42 KB
/
git.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
import { ExtensionContext, WorkspaceFolder, commands, window } from "vscode";
import { Tools } from "../Tools";
import { getBranchLibraryName } from "./env";
import { instance } from "../../instantiate";
import { ConnectionConfiguration, GlobalConfiguration } from "../Configuration";
import IBMi from "../IBMi";
import IBMiContent from "../IBMiContent";
const lastBranch: { [workspaceUri: string]: string } = {};
export function getGitBranch(workspaceFolder: WorkspaceFolder) {
const gitApi = Tools.getGitAPI();
if (gitApi) {
const repo = gitApi.getRepository(workspaceFolder.uri);
if (repo) {
return repo.state.HEAD?.name;
}
}
}
export function setupGitEventHandler(context: ExtensionContext) {
const gitApi = Tools.getGitAPI();
if (gitApi) {
gitApi.onDidOpenRepository((repo) => {
const workspaceUri = repo.rootUri.toString();
const changeEvent = repo.state.onDidChange((_e) => {
if (GlobalConfiguration.get(`createLibraryOnBranchChange`)) {
if (repo) {
const head = repo.state.HEAD;
const connection = instance.getConnection();
if (head && head.name) {
const currentBranch = head.name;
if (currentBranch && currentBranch !== lastBranch[workspaceUri]) {
if (connection) {
const content = instance.getContent()!;
const config = instance.getConfig()!;
if (currentBranch.includes(`/`)) {
setupBranchLibrary(currentBranch, content, connection, config);
}
}
}
lastBranch[workspaceUri] = currentBranch;
}
}
}
});
context.subscriptions.push(changeEvent);
});
}
}
function setupBranchLibrary(currentBranch: string, content: IBMiContent, connection: IBMi, config: ConnectionConfiguration.Parameters) {
const filters = config.objectFilters;
const newBranchLib = getBranchLibraryName(currentBranch);
content.checkObject({ library: `QSYS`, name: newBranchLib, type: `*LIB` }).then(exists => {
if (exists) {
if (!filters.some(filter => filter.library.toUpperCase() === newBranchLib)) {
window.showInformationMessage(`The branch library ${newBranchLib} exists for this branch. Do you want to create a filter?`, `Yes`, `No`).then(answer => {
if (answer === `Yes`) {
filters.push({
name: currentBranch,
filterType: `simple`,
library: newBranchLib,
object: `*ALL`,
types: [`*ALL`],
member: `*`,
memberType: `*`,
protected: false
});
config.objectFilters = filters;
ConnectionConfiguration.update(config).then(() => {
commands.executeCommand(`code-for-ibmi.refreshObjectBrowser`);
});
}
});
}
} else {
window.showInformationMessage(`Would you like to create a new library ${newBranchLib} for this branch?`, `Yes`, `No`).then(answer => {
if (answer === `Yes`) {
const escapedText = currentBranch.replace(/'/g, `''`);
connection.runCommand({ command: `CRTLIB LIB(${newBranchLib}) TEXT('${escapedText}') TYPE(*TEST)`, noLibList: true })
.then((createResult) => {
if (createResult && createResult.code === 0) {
window.showInformationMessage(`Library ${newBranchLib} created. Use '&BRANCHLIB' as a reference to it.`, `Create filter`).then(answer => {
if (answer === `Create filter`) {
filters.push({
name: currentBranch,
filterType: `simple`,
library: newBranchLib,
object: `*ALL`,
types: [`*ALL`],
member: `*`,
memberType: `*`,
protected: false
});
config.objectFilters = filters;
ConnectionConfiguration.update(config).then(() => {
commands.executeCommand(`code-for-ibmi.refreshObjectBrowser`);
});
}
});
} else {
window.showErrorMessage(`Error creating library ${newBranchLib}: ${createResult ? createResult.stderr : `Unknown error`}`);
}
});
}
});
}
});
}