-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitutils.js
259 lines (237 loc) · 7.81 KB
/
gitutils.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const prompt = require('electron-prompt');
const {exec} = require('child_process');
const path = require('path');
const {dialog, BrowserWindow, ipcMain} = require('electron');
// global children window options
const gitWindowOptions = {
width: 300,
minWidth: 300,
maxWidth: 800,
height: 325,
minHeight: 460,
maxHeight: 800,
title: 'GitBoard Selector',
alwaysOnTop: true,
webPreferences: {
nodeIntegration: true,
backgroundThrottling: false,
}
};
let pubMergeWindow = null;
let pubCheckoutWindow = null;
let pubPath = '';
const createCheckoutWindow = () => {
const checkoutWindow = new BrowserWindow(gitWindowOptions);
checkoutWindow.loadURL(path.join('file://', __dirname, 'git_checkout.html'));
checkoutWindow.setMenu(null);
checkoutWindow.setResizable(false);
if(process.platform === 'win32') {
let size = checkoutWindow.getSize();
checkoutWindow.setSize(size[0], parseInt(size[0] * 9 / 16));
}
return checkoutWindow;
};
const createMergeWindow = () => {
const mergeWindow = new BrowserWindow(gitWindowOptions);
mergeWindow.loadURL(path.join('file://', __dirname, 'git_merge.html'));
mergeWindow.setMenu(null);
mergeWindow.setResizable(false);
if(process.platform === 'win32') {
let size = mergeWindow.getSize();
mergeWindow.setSize(size[0], parseInt(size[0] * 9 / 16));
}
return mergeWindow;
};
ipcMain.on('use-branch', (evt, arg) => {
if (arg !== '' && arg !== null) {
const cmd = `cd ${pubPath} && git checkout ${arg}`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(pubCheckoutWindow, {
message: `Info: ${stderr}\n ${stdout}`
});
});
pubCheckoutWindow.hide();
}
});
ipcMain.on('merge-branch', (evt, arg) => {
if (arg !== '' && arg !== null) {
const cmd = `cd ${pubPath} && git merge ${arg}`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(pubMergeWindow, {
message: `Info: ${stderr}\n ${stdout}`
});
});
pubMergeWindow.hide();
}
});
ipcMain.on('delete-branch', (evt, arg) => {
if (arg !== '' && arg !== null) {
dialog.showMessageBox(pubCheckoutWindow, {
title: "Delete Branch",
message: `Are you sure you want to delete branch "${arg}"?`,
buttons: ["Yes", "No"],
}, (response) => {
if (response === 0) {
const cmd = `cd ${pubPath} && git branch -d ${arg}`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(pubCheckoutWindow, {
message: `Info: \n${stderr}\n ${stdout}`
});
});
pubCheckoutWindow.hide();
}
});
}
});
// create a new branch
ipcMain.on('new-branch', (evt, arg) => {
if (arg !== '' && arg !== null) {
prompt({
title: 'Branch name',
label: 'Enter new branch name: ',
type: 'input',
alwaysOnTop: true,
}).then((resp) => {
if (resp !== '' && resp !== null) {
resp = resp.replace(/ /g, '_');
const cmd = `cd ${pubPath} && git checkout -b ${resp}`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(pubCheckoutWindow, {
message: `Info: ${stderr}\n ${stdout}`,
});
});
pubCheckoutWindow.hide();
} else {
dialog.showMessageBox(pubCheckoutWindow, {
message: `Please enter a branch name first!`,
buttons: ['Ok'],
});
}
}).catch((err) => {
console.error(err);
});
}
});
ipcMain.on('get-branches', (evt, arg) => {
const cmd = `cd ${pubPath} && git branch`;
exec(cmd, (err, stdout, stderr) => {
if (!err && !stderr) {
evt.reply('branches-list', stdout);
}
});
});
module.exports.gitPull = (path, window, args) => {
pubPath = path;
const cmd = `cd ${path} && git pull`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(window, {
message: `Info: \n${stderr}\n ${stdout}`
});
});
};
module.exports.gitPush = (path, window, args) => {
pubPath = path;
dialog.showMessageBox(window, {
title: "Push Changes",
message: "Before pushing, ensure you have committed your changes!\nWould you like to continue?",
buttons: ["Yes", "No"],
}, (response) => {
if (response === 0) {
const cmd = `cd ${path} && git push`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(window, {
message: `Info: \n${stderr}\n ${stdout}`
});
});
}
});
};
module.exports.gitCheckout = (path, window, args) => {
pubPath = path;
pubCheckoutWindow = createCheckoutWindow();
};
module.exports.gitStatus = (path, window, args) => {
pubPath = path;
const cmd = `cd ${path} && git status`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(window, {
message: `Info: ${stderr}\n ${stdout}`
});
});
};
module.exports.gitLog = (path, window, args) => {
pubPath = path;
const cmd = `cd ${path} && git log`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(window, {
message: `Info: ${stderr}\n ${stdout}`
});
});
};
module.exports.gitDiff = (path, window, args) => {
pubPath = path;
const cmd = `cd ${path} && git diff`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(window, {
message: `Info: ${(stderr !== null && stderr !== '') ? stderr : 'No diff'}\n ${stdout}`
});
});
};
module.exports.gitMerge = (path, window, args) => {
pubPath = path;
pubMergeWindow = createMergeWindow();
};
module.exports.gitCommit = (path, window, args) => {
pubPath = path;
prompt({
title: 'Commit message',
label: 'Enter commit message: ',
type: 'input',
alwaysOnTop: true,
}).then((resp) => {
if (resp !== '' && resp !== null) {
const cmd = `cd ${path} && git add . && git commit -m "${resp}"`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(window, {
type: 'question',
message: 'Would you like to push these changes?',
buttons: [
'Push changes!', 'No, just commit.' //0, 1
]
}, (clicked) => {
switch (clicked) {
case 0:
exports.gitPush(path, window, null);
break;
case 1:
dialog.showMessageBox(window, {
message: `Info: ${stderr}\n ${stdout}`,
});
break;
default:
dialog.showMessageBox(window, {
message: `Info: ${stderr}\n ${stdout}`,
});
break;
}
});
});
} else {
dialog.showMessageBox(window, {
message: `Please enter a commit message first!`,
buttons: ['Ok'],
});
}
}).catch((err) => {
console.error(err);
});
};
module.exports.gitHelp = (path, window, args) => {
pubPath = path;
const cmd = `cd ${path} && git help`;
exec(cmd, (err, stdout, stderr) => {
dialog.showMessageBox(window, {
message: `Help: \n${stderr}\n ${stdout}`
});
});
};