Newer
Older
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
#include "mercurialjobrunner.h"
#include "mercurialplugin.h"
#include "mercurialoutputwindow.h"
#include "constants.h"
#include "mercurialsettings.h"
#include <vcsbase/vcsbaseeditor.h>
#include <QtCore/QProcess>
#include <QtCore/QTime>
#include <QtCore/QString>
#include <QtCore/QSettings>
using namespace Mercurial::Internal;
using namespace Mercurial;
HgTask::HgTask(const QString &repositoryRoot, QStringList &arguments, bool emitRaw)
: m_repositoryRoot(repositoryRoot),
arguments(arguments),
emitRaw(emitRaw),
editor(0)
{
}
HgTask::HgTask(const QString &repositoryRoot, QStringList &arguments, VCSBase::VCSBaseEditor *editor)
: m_repositoryRoot(repositoryRoot),
arguments(arguments),
emitRaw(false),
editor(editor)
{
}
MercurialJobRunner::MercurialJobRunner()
: keepRunning(true)
{
plugin = MercurialPlugin::instance();
connect(this, SIGNAL(error(const QByteArray &)), plugin->outputPane(), SLOT(append(const QByteArray &)));
connect(this, SIGNAL(info(const QString &)), plugin->outputPane(), SLOT(append(const QString &)));
}
MercurialJobRunner::~MercurialJobRunner()
{
stop();
}
void MercurialJobRunner::stop()
{
mutex.lock();
keepRunning = false;
//Create a dummy task to break the cycle
QSharedPointer<HgTask> job(0);
jobs.enqueue(job);
waiter.wakeAll();
mutex.unlock();
wait();
}
void MercurialJobRunner::restart()
{
stop();
mutex.lock();
keepRunning = true;
mutex.unlock();
start();
}
void MercurialJobRunner::getSettings()
{
MercurialSettings *settings = MercurialPlugin::instance()->settings();
binary = settings->binary();
timeout = settings->timeout();
standardArguments = settings->standardArguments();
}
void MercurialJobRunner::enqueueJob(QSharedPointer<HgTask> &job)
{
mutex.lock();
jobs.enqueue(job);
waiter.wakeAll();
mutex.unlock();
}
void MercurialJobRunner::run()
{
getSettings();
forever {
mutex.lock();
while (jobs.count() == 0)
waiter.wait(&mutex);
if (!keepRunning) {
jobs.clear();
mutex.unlock();
return;
}
QSharedPointer<HgTask> job = jobs.dequeue();
mutex.unlock();
task(job);
}
}
void MercurialJobRunner::task(QSharedPointer<HgTask> &job)
{
HgTask *taskData = job.data();
if (taskData->shouldEmit())
//Call the job's signal so the Initator of the job can process the data
//Because the QSharedPointer that holds the HgTask will go out of scope and hence be deleted
//we have to block and wait until the signal is delivered
connect(this, SIGNAL(output(const QByteArray&)), taskData, SIGNAL(rawData(const QByteArray&)),
Qt::BlockingQueuedConnection);
else if (taskData->displayEditor())
//An editor has been created to display the data so send it there
connect(this, SIGNAL(output(const QByteArray&)), taskData->displayEditor(), SLOT(setPlainTextData(const QByteArray&)));
else
//Just output the data to the Mercurial output window
connect(this, SIGNAL(output(const QByteArray &)), plugin->outputPane(), SLOT(append(const QByteArray &)));
QString time = QTime::currentTime().toString(QLatin1String("HH:mm"));
QString starting = tr("%1 Calling: %2 %3\n").arg(time, "hg", taskData->args().join(" "));
//infom the user of what we are going to try and perform
emit info(starting);
if (Constants::debug)
qDebug() << Q_FUNC_INFO << "Repository root is " << taskData->repositoryRoot();
QProcess hgProcess;
hgProcess.setWorkingDirectory(taskData->repositoryRoot());
QStringList args = standardArguments;
args << taskData->args();
hgProcess.start(binary, args);
if (!hgProcess.waitForStarted()) {
QByteArray errorArray(Constants::ERRORSTARTING);
emit error(errorArray);
return;
}
hgProcess.closeWriteChannel();
if (!hgProcess.waitForFinished(timeout)) {
hgProcess.terminate();
QByteArray errorArray(Constants::TIMEDOUT);
emit error(errorArray);
return;
}
if ((hgProcess.exitStatus() == QProcess::NormalExit) && (hgProcess.exitCode() == 0)) {
QByteArray stdout = hgProcess.readAllStandardOutput();
/*
* sometimes success means output is actually on error channel (stderr)
* e.g. "hg revert" outputs "no changes needed to 'file'" on stderr if file has not changed
* from revision specified
*/
if (stdout == "")
stdout = hgProcess.readAllStandardError();
emit output(stdout);
} else {
QByteArray stderr = hgProcess.readAllStandardError();
emit error(stderr);
}
hgProcess.close();
//the signal connection is to last only for the duration of a job/task. next time a new
//output signal connection must be made
disconnect(this, SIGNAL(output(const QByteArray &)), 0, 0);
}