-
Notifications
You must be signed in to change notification settings - Fork 7
/
SC_versioncontrol.cpp
472 lines (360 loc) · 11.8 KB
/
SC_versioncontrol.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/***************************************************************************
** **
** This file is part of SpineCreator, an easy to use GUI for **
** describing spiking neural network models. **
** Copyright (C) 2013-2014 Alex Cope, Paul Richmond, Seb James **
** **
** This program is free software: you can redistribute it and/or modify **
** it under the terms of the GNU General Public License as published by **
** the Free Software Foundation, either version 3 of the License, or **
** (at your option) any later version. **
** **
** This program is distributed in the hope that it will be useful, **
** but WITHOUT ANY WARRANTY; without even the implied warranty of **
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
** GNU General Public License for more details. **
** **
** You should have received a copy of the GNU General Public License **
** along with this program. If not, see http://www.gnu.org/licenses/. **
** **
****************************************************************************
** Author: Alex Cope **
** Website/Contact: http://bimpa.group.shef.ac.uk/ **
****************************************************************************/
#include "SC_versioncontrol.h"
#include "QProcess"
#include "QSettings"
#include "globalHeader.h"
#include "SC_commitdialog.h"
versionControl::versionControl(QObject *parent) :
QObject(parent)
{
// detect version control systems
this->detectVCSes();
this->version = NONE;
}
void versionControl::detectVCSes() {
// check for mercurial
// set up the environment for the spawned processes
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
//env.insert("PATH", qgetenv("PATH"));
// the path fetched by Qt on Mac may not include /usr/local/bin, so we need to check and add it
#ifdef Q_OS_MAC
QString path = qgetenv("PATH");
if (!path.contains("/usr/local/bin")) {
path = path + ":/usr/local/bin";
}
qDebug() << path;
env.insert("PATH", path);
// in addition the environment of spineCreator on Mac may not include /usr/local/bin, which is needed to launch hg in the first place
qputenv("PATH", path.toStdString().c_str());
#else
env.insert("PATH", qgetenv("PATH"));
#endif
// make the QProcess
QProcess * mercurial = new QProcess;
mercurial->setWorkingDirectory(qgetenv("HOME"));
mercurial->setProcessEnvironment(env);
#ifdef NEED_VERSIONCONTROL_FINISHED
connect(mercurial, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
#endif
connect(mercurial, SIGNAL(readyReadStandardOutput()), this, SLOT(standardOutput()));
connect(mercurial, SIGNAL(readyReadStandardError()), this, SLOT(standardError()));
mercurial->start("hg");
isMercurialThere = mercurial->waitForFinished(1000);
delete mercurial;
}
bool versionControl::haveMercurial() {
return this->isMercurialThere;
}
bool versionControl::isModelUnderMercurial() {
if (!this->isMercurialThere)
return false;
QSettings settings;
// get current model path from QSettings
QString path = settings.value("files/currentFileName", "No model").toString();
if (path == "No model") {
return false;
}
// strip the .proj from the end of the path, if has the .proj
if (path.contains(".proj")) {
int index = path.lastIndexOf(QDir::toNativeSeparators("/"));
path.chop(path.size()-index);
}
// set up the environment for the spawned processes
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PATH", qgetenv("PATH"));
// make the QProcess
QProcess * mercurial = new QProcess;
mercurial->setWorkingDirectory(QDir::toNativeSeparators(path));
mercurial->setProcessEnvironment(env);
#ifdef NEED_VERSIONCONTROL_FINISHED
connect(mercurial, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
#endif
connect(mercurial, SIGNAL(readyReadStandardOutput()), this, SLOT(standardOutput()));
connect(mercurial, SIGNAL(readyReadStandardError()), this, SLOT(standardError()));
mercurial->start("hg summary");
bool noerror = mercurial->waitForFinished(1000);
// tidy these up every so often...
stdOutText.clear();
stdErrText.clear();
if (noerror) {
// check output
if (mercurial->exitCode() == 0) {
return true;
} else {
return false;
}
}
return false;
}
// add a file to the repository
bool versionControl::addToMercurial(QString file) {
return runMercurial("add \"" + file + "\"");
}
// remove a file from the repository
bool versionControl::removeFromMercurial(QString file) {
return runMercurial("remove \"" + file + "\"");
}
// commit a new version
bool versionControl::commitMercurial(QString message) {
// different OS require different tricks for multi-line messages
#if defined Q_OS_LINUX
QString moo = "commit -m \"$(echo -e '" + message + "')\"";
qDebug() << moo;
return runMercurial("commit -m \"" + message + "\"");
#elif defined Q_OS_MAC
return runMercurial("commit -m \"$(echo -e '" + message + "')\"");
#elif defined Q_OS_WIN
return runMercurial("commit -m \"" + message + "\"");
#else
// Assume Unix-like
return runMercurial("commit -m \"$(echo -e '" + message + "')\"");
#endif
}
// remove a file from the repository
bool versionControl::updateMercurial() {
return runMercurial("update");
}
// remove a file from the repository
bool versionControl::revertMercurial() {
return runMercurial("revert");
}
// read the log
QString versionControl::showMercurialLog() {
runMercurial("log -v");
return stdOutText;
}
// fetch the status
QString versionControl::showMercurialStatus() {
runMercurial("status");
return stdOutText;
}
bool versionControl::runMercurial(QString options) {
if (!this->isMercurialThere)
return false;
QSettings settings;
// get current model path from QSettings
QString path = settings.value("files/currentFileName", "No model").toString();
if (path == "No model") {
return false;
}
// strip the .proj from the end of the path, if has the .proj
if (path.contains(".proj")) {
int index = path.lastIndexOf(QDir::toNativeSeparators("/"));
path.chop(path.size()-index);
}
// set up the environment for the spawned processes
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("PATH", qgetenv("PATH"));
// to be used as a username if one is not set
env.insert("EMAIL", QHostInfo::localHostName());
// make the QProcess
QProcess * mercurial = new QProcess;
mercurial->setWorkingDirectory(QDir::toNativeSeparators(path));
mercurial->setProcessEnvironment(env);
//connect(mercurial, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
connect(mercurial, SIGNAL(readyReadStandardOutput()), this, SLOT(standardOutput()));
connect(mercurial, SIGNAL(readyReadStandardError()), this, SLOT(standardError()));
// clear stdOut & stdErr texts
stdOutText.clear();
stdErrText.clear();
// launch
mercurial->start("hg " + options);
// wait until complete, or 3 s
bool noerror = mercurial->waitForFinished(3000);
if (noerror) {
// check output
if (mercurial->exitCode() == 0) {
delete mercurial;
return true;
} else {
delete mercurial;
return false;
}
}
delete mercurial;
return false;
}
bool versionControl::haveVersion() {
if (haveMercurial())
return true;
return false;
}
bool versionControl::isModelUnderVersion() {
if (this->version == NONE)
return false;
return true;
}
bool versionControl::addToVersion(QString file) {
switch (this->version) {
case NONE:
return false;
case MERCURIAL:
return addToMercurial(file);
case SVN:
case CVS:
return false;
}
return false;
}
bool versionControl::removeFromVersion(QString file) {
switch (this->version) {
case NONE:
return false;
case MERCURIAL:
return removeFromMercurial(file);
case SVN:
case CVS:
return false;
}
return false;
}
bool versionControl::commitVersion() {
QString message;
// obtain message
commitDialog * dialog = new commitDialog;
switch(dialog->exec()) {
case QDialog::Accepted:
message = dialog->getString();
break;
case QDialog::Rejected:
return false;
}
switch (this->version) {
case NONE:
return false;
case MERCURIAL:
return commitMercurial(message);
case SVN:
case CVS:
return false;
}
return false;
}
bool versionControl::updateVersion() {
switch (this->version) {
case NONE:
return false;
case MERCURIAL:
return updateMercurial();
case SVN:
case CVS:
return false;
}
return false;
}
bool versionControl::revertVersion() {
switch (this->version) {
case NONE:
return false;
case MERCURIAL:
return revertMercurial();
case SVN:
case CVS:
return false;
}
return false;
}
bool versionControl::showVersionStatus() {
QString status;
switch (this->version) {
case NONE:
return false;
case MERCURIAL:
status = showMercurialStatus();
break;
case SVN:
case CVS:
return false;
}
// show log
commitDialog * dialog = new commitDialog;
dialog->showStatus(status);
switch(dialog->exec()) {
case QDialog::Accepted:
return true;
case QDialog::Rejected:
return true;
}
return true;
}
bool versionControl::showVersionLog() {
QString log;
switch (this->version) {
case NONE:
return false;
case MERCURIAL:
log = showMercurialLog();
break;
case SVN:
case CVS:
return false;
}
// show log
commitDialog * dialog = new commitDialog;
dialog->showLog(log);
switch(dialog->exec()) {
case QDialog::Accepted:
return true;
case QDialog::Rejected:
return true;
}
return true;
}
void versionControl::setupVersion() {
if (isModelUnderMercurial()) {
this->version = MERCURIAL;
return;
}
this->version = NONE;
}
#ifdef NEED_VERSIONCONTROL_FINISHED
void versionControl::finished(int, QProcess::ExitStatus status)
{
// get status
/*if (status == QProcess::CrashExit) {
QMessageBox msgBox;
msgBox.setWindowTitle("Simulator Error");
msgBox.setText(stdErrText);
msgBox.exec();
return;
}
if (status == QProcess::NormalExit) {
QMessageBox msgBox;
msgBox.setWindowTitle("Simulator Complete");
msgBox.setText(stdOutText);
msgBox.exec();
return;
}*/
// collect logs
}
#endif
void versionControl::standardOutput() {
QByteArray data = ((QProcess *) sender())->readAllStandardOutput();
stdOutText = stdOutText + QString().fromUtf8(data);
}
void versionControl::standardError() {
QByteArray data = ((QProcess *) sender())->readAllStandardError();
stdErrText = stdErrText + QString().fromUtf8(data);
}