Chris@57
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@57
|
2
|
Chris@57
|
3 /*
|
Chris@57
|
4 EasyMercurial
|
Chris@57
|
5
|
Chris@57
|
6 Based on HgExplorer by Jari Korhonen
|
Chris@57
|
7 Copyright (c) 2010 Jari Korhonen
|
Chris@57
|
8 Copyright (c) 2010 Chris Cannam
|
Chris@57
|
9 Copyright (c) 2010 Queen Mary, University of London
|
Chris@57
|
10
|
Chris@57
|
11 This program is free software; you can redistribute it and/or
|
Chris@57
|
12 modify it under the terms of the GNU General Public License as
|
Chris@57
|
13 published by the Free Software Foundation; either version 2 of the
|
Chris@57
|
14 License, or (at your option) any later version. See the file
|
Chris@57
|
15 COPYING included with this distribution for more information.
|
Chris@57
|
16 */
|
jtkorhonen@0
|
17
|
jtkorhonen@0
|
18 #include "hgrunner.h"
|
Chris@62
|
19 #include "common.h"
|
Chris@57
|
20 #include "debug.h"
|
Chris@50
|
21
|
Chris@50
|
22 #include <QPushButton>
|
Chris@50
|
23 #include <QListWidget>
|
Chris@50
|
24 #include <QDialog>
|
Chris@50
|
25 #include <QLabel>
|
Chris@50
|
26 #include <QVBoxLayout>
|
Chris@62
|
27 #include <QSettings>
|
Chris@75
|
28 #include <QInputDialog>
|
jtkorhonen@0
|
29
|
Chris@43
|
30 #include <iostream>
|
Chris@75
|
31 #include <errno.h>
|
Chris@75
|
32 #include <stdio.h>
|
Chris@111
|
33 #include <stdlib.h>
|
jtkorhonen@0
|
34
|
Chris@76
|
35 #ifndef Q_OS_WIN32
|
Chris@111
|
36 #include <unistd.h>
|
Chris@111
|
37 #include <fcntl.h>
|
Chris@80
|
38 #endif
|
Chris@76
|
39
|
jtkorhonen@0
|
40 HgRunner::HgRunner(QWidget * parent): QProgressBar(parent)
|
jtkorhonen@0
|
41 {
|
Chris@84
|
42 m_proc = new QProcess(this);
|
jtkorhonen@0
|
43
|
cannam@45
|
44 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
cannam@45
|
45 env.insert("LANG", "en_US.utf8");
|
cannam@45
|
46 env.insert("LC_ALL", "en_US.utf8");
|
Chris@104
|
47 env.insert("HGPLAIN", "1");
|
Chris@84
|
48 m_proc->setProcessEnvironment(env);
|
Chris@84
|
49
|
jtkorhonen@0
|
50 setTextVisible(false);
|
jtkorhonen@0
|
51 setVisible(false);
|
Chris@84
|
52 m_isRunning = false;
|
jtkorhonen@0
|
53
|
Chris@84
|
54 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
|
Chris@84
|
55 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
|
Chris@62
|
56 this, SLOT(finished(int, QProcess::ExitStatus)));
|
Chris@110
|
57 connect(m_proc, SIGNAL(readyReadStandardOutput()),
|
Chris@110
|
58 this, SLOT(dataReadyStdout()));
|
Chris@110
|
59 connect(m_proc, SIGNAL(readyReadStandardError()),
|
Chris@110
|
60 this, SLOT(dataReadyStderr()));
|
jtkorhonen@0
|
61 }
|
jtkorhonen@0
|
62
|
jtkorhonen@0
|
63 HgRunner::~HgRunner()
|
jtkorhonen@0
|
64 {
|
Chris@111
|
65 closeTerminal();
|
Chris@84
|
66 delete m_proc;
|
jtkorhonen@0
|
67 }
|
jtkorhonen@0
|
68
|
Chris@109
|
69 void HgRunner::requestAction(HgAction action)
|
Chris@109
|
70 {
|
Chris@109
|
71 DEBUG << "requestAction " << action.action << endl;
|
Chris@109
|
72 bool pushIt = true;
|
Chris@109
|
73 if (m_queue.empty()) {
|
Chris@109
|
74 if (action == m_currentAction) {
|
Chris@109
|
75 // this request is identical to the thing we're executing
|
Chris@109
|
76 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
|
Chris@109
|
77 pushIt = false;
|
Chris@109
|
78 }
|
Chris@109
|
79 } else {
|
Chris@109
|
80 HgAction last = m_queue.back();
|
Chris@109
|
81 if (action == last) {
|
Chris@109
|
82 // this request is identical to the previous thing we
|
Chris@109
|
83 // queued which we haven't executed yet
|
Chris@109
|
84 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
|
Chris@109
|
85 pushIt = false;
|
Chris@109
|
86 }
|
Chris@109
|
87 }
|
Chris@109
|
88 if (pushIt) m_queue.push_back(action);
|
Chris@109
|
89 checkQueue();
|
Chris@109
|
90 }
|
Chris@109
|
91
|
Chris@62
|
92 QString HgRunner::getHgBinaryName()
|
Chris@62
|
93 {
|
Chris@62
|
94 QSettings settings;
|
Chris@77
|
95 QString hg = settings.value("hgbinary", "").toString();
|
Chris@77
|
96 if (hg == "") {
|
Chris@77
|
97 hg = findExecutable("hg");
|
Chris@77
|
98 }
|
Chris@77
|
99 if (hg != "hg") {
|
Chris@77
|
100 settings.setValue("hgbinary", hg);
|
Chris@77
|
101 }
|
Chris@62
|
102 return hg;
|
Chris@62
|
103 }
|
Chris@62
|
104
|
jtkorhonen@0
|
105 void HgRunner::started()
|
jtkorhonen@0
|
106 {
|
Chris@104
|
107 DEBUG << "started" << endl;
|
Chris@75
|
108 /*
|
Chris@104
|
109 m_proc->write("blah\n");
|
Chris@104
|
110 m_proc->write("blah\n");
|
Chris@104
|
111 m_proc -> closeWriteChannel();
|
Chris@75
|
112 */
|
jtkorhonen@0
|
113 }
|
jtkorhonen@0
|
114
|
Chris@84
|
115 void HgRunner::noteUsername(QString name)
|
Chris@75
|
116 {
|
Chris@84
|
117 m_userName = name;
|
Chris@75
|
118 }
|
Chris@75
|
119
|
Chris@84
|
120 void HgRunner::noteRealm(QString realm)
|
Chris@75
|
121 {
|
Chris@84
|
122 m_realm = realm;
|
Chris@84
|
123 }
|
Chris@84
|
124
|
Chris@84
|
125 void HgRunner::getUsername()
|
Chris@84
|
126 {
|
Chris@84
|
127 if (m_procInput) {
|
Chris@84
|
128 bool ok = false;
|
Chris@84
|
129 QString prompt = tr("User name:");
|
Chris@84
|
130 if (m_realm != "") {
|
Chris@84
|
131 prompt = tr("User name for \"%1\":").arg(m_realm);
|
Chris@84
|
132 }
|
Chris@84
|
133 QString pwd = QInputDialog::getText
|
Chris@84
|
134 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
135 tr("Enter user name"), prompt,
|
Chris@84
|
136 QLineEdit::Normal, QString(), &ok);
|
Chris@84
|
137 if (ok) {
|
Chris@84
|
138 m_procInput->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@84
|
139 m_procInput->flush();
|
Chris@84
|
140 return;
|
Chris@111
|
141 } else {
|
Chris@111
|
142 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
|
Chris@111
|
143 killCurrentCommand();
|
Chris@111
|
144 return;
|
Chris@84
|
145 }
|
Chris@84
|
146 }
|
Chris@84
|
147 // user cancelled or something went wrong
|
Chris@111
|
148 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
|
Chris@84
|
149 killCurrentCommand();
|
Chris@84
|
150 }
|
Chris@84
|
151
|
Chris@84
|
152 void HgRunner::getPassword()
|
Chris@84
|
153 {
|
Chris@84
|
154 if (m_procInput) {
|
Chris@84
|
155 bool ok = false;
|
Chris@84
|
156 QString prompt = tr("Password:");
|
Chris@84
|
157 if (m_userName != "") {
|
Chris@84
|
158 if (m_realm != "") {
|
Chris@84
|
159 prompt = tr("Password for \"%1\" at \"%2\":")
|
Chris@84
|
160 .arg(m_userName).arg(m_realm);
|
Chris@75
|
161 } else {
|
Chris@84
|
162 prompt = tr("Password for user \"%1\":")
|
Chris@84
|
163 .arg(m_userName);
|
Chris@75
|
164 }
|
Chris@75
|
165 }
|
Chris@84
|
166 QString pwd = QInputDialog::getText
|
Chris@84
|
167 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
168 tr("Enter password"), prompt,
|
Chris@84
|
169 QLineEdit::Password, QString(), &ok);
|
Chris@84
|
170 if (ok) {
|
Chris@84
|
171 m_procInput->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@84
|
172 m_procInput->flush();
|
Chris@84
|
173 return;
|
Chris@111
|
174 } else {
|
Chris@111
|
175 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
|
Chris@111
|
176 killCurrentCommand();
|
Chris@111
|
177 return;
|
Chris@84
|
178 }
|
Chris@75
|
179 }
|
Chris@84
|
180 // user cancelled or something went wrong
|
Chris@111
|
181 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
|
Chris@84
|
182 killCurrentCommand();
|
Chris@84
|
183 }
|
Chris@84
|
184
|
Chris@84
|
185 void HgRunner::checkPrompts(QString chunk)
|
Chris@84
|
186 {
|
Chris@93
|
187 //DEBUG << "checkPrompts: " << chunk << endl;
|
Chris@84
|
188
|
Chris@84
|
189 QString text = chunk.trimmed();
|
Chris@84
|
190 QString lower = text.toLower();
|
Chris@84
|
191 if (lower.endsWith("password:")) {
|
Chris@84
|
192 getPassword();
|
Chris@84
|
193 return;
|
Chris@84
|
194 }
|
Chris@84
|
195 if (lower.endsWith("user:")) {
|
Chris@84
|
196 getUsername();
|
Chris@84
|
197 return;
|
Chris@84
|
198 }
|
Chris@84
|
199 QRegExp userRe("\\buser:\\s*([^\\s]+)");
|
Chris@84
|
200 if (userRe.indexIn(text) >= 0) {
|
Chris@84
|
201 noteUsername(userRe.cap(1));
|
Chris@84
|
202 }
|
Chris@84
|
203 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
|
Chris@84
|
204 if (realmRe.indexIn(text) >= 0) {
|
Chris@84
|
205 noteRealm(realmRe.cap(1));
|
Chris@84
|
206 }
|
Chris@84
|
207 }
|
Chris@84
|
208
|
Chris@110
|
209 void HgRunner::dataReadyStdout()
|
Chris@84
|
210 {
|
Chris@110
|
211 DEBUG << "dataReadyStdout" << endl;
|
Chris@110
|
212 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
|
Chris@110
|
213 m_stdout += chunk;
|
Chris@110
|
214 checkPrompts(chunk);
|
Chris@110
|
215 }
|
Chris@110
|
216
|
Chris@110
|
217 void HgRunner::dataReadyStderr()
|
Chris@110
|
218 {
|
Chris@110
|
219 DEBUG << "dataReadyStderr" << endl;
|
Chris@110
|
220 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
|
Chris@110
|
221 m_stderr += chunk;
|
Chris@84
|
222 checkPrompts(chunk);
|
Chris@75
|
223 }
|
Chris@75
|
224
|
jtkorhonen@0
|
225 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
226 {
|
Chris@109
|
227 // Save the current action and reset m_currentAction before we
|
Chris@109
|
228 // emit a signal to mark the completion; otherwise we may be
|
Chris@109
|
229 // resetting the action after a slot has already tried to set it
|
Chris@109
|
230 // to something else to start a new action
|
Chris@109
|
231
|
Chris@109
|
232 HgAction completedAction = m_currentAction;
|
Chris@109
|
233
|
Chris@84
|
234 m_isRunning = false;
|
Chris@109
|
235 m_currentAction = HgAction();
|
Chris@84
|
236
|
Chris@84
|
237 closeProcInput();
|
jtkorhonen@0
|
238
|
Chris@109
|
239 if (completedAction.action == ACT_NONE) {
|
Chris@109
|
240 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
|
Chris@109
|
241 }
|
Chris@109
|
242
|
Chris@74
|
243 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
|
Chris@84
|
244 DEBUG << "HgRunner::finished: Command completed successfully" << endl;
|
Chris@110
|
245 emit commandCompleted(completedAction, m_stdout);
|
Chris@62
|
246 } else {
|
Chris@111
|
247 DEBUG << "HgRunner::finished: Command failed, stderr follows" << endl;
|
Chris@111
|
248 DEBUG << m_stderr << endl;
|
Chris@110
|
249 emit commandFailed(completedAction, m_stderr);
|
jtkorhonen@0
|
250 }
|
Chris@109
|
251
|
Chris@109
|
252 checkQueue();
|
jtkorhonen@0
|
253 }
|
jtkorhonen@0
|
254
|
Chris@62
|
255 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
256 {
|
Chris@109
|
257 if (m_isRunning) {
|
Chris@84
|
258 m_proc -> kill();
|
jtkorhonen@0
|
259 }
|
jtkorhonen@0
|
260 }
|
jtkorhonen@0
|
261
|
Chris@109
|
262 void HgRunner::checkQueue()
|
Chris@62
|
263 {
|
Chris@109
|
264 if (m_isRunning) {
|
Chris@109
|
265 return;
|
Chris@109
|
266 }
|
Chris@109
|
267 if (m_queue.empty()) {
|
Chris@109
|
268 hide();
|
Chris@109
|
269 return;
|
Chris@109
|
270 }
|
Chris@109
|
271 HgAction toRun = m_queue.front();
|
Chris@109
|
272 m_queue.pop_front();
|
Chris@109
|
273 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
|
Chris@109
|
274 startCommand(toRun);
|
Chris@109
|
275 }
|
Chris@109
|
276
|
Chris@109
|
277 void HgRunner::startCommand(HgAction action)
|
Chris@109
|
278 {
|
Chris@109
|
279 QString executable = action.executable;
|
Chris@109
|
280 bool interactive = false;
|
Chris@109
|
281 QStringList params = action.params;
|
Chris@109
|
282
|
Chris@109
|
283 if (executable == "") {
|
Chris@109
|
284 // This is a Hg command
|
Chris@109
|
285 executable = getHgBinaryName();
|
Chris@104
|
286 #ifdef Q_OS_WIN32
|
Chris@109
|
287 // This at least means we won't block on the non-working password prompt
|
Chris@109
|
288 params.push_front("--noninteractive");
|
Chris@104
|
289 #else
|
Chris@109
|
290 // password prompt should work here
|
Chris@109
|
291 if (action.mayBeInteractive()) {
|
Chris@109
|
292 params.push_front("ui.interactive=true");
|
Chris@109
|
293 params.push_front("--config");
|
Chris@109
|
294 interactive = true;
|
Chris@109
|
295 } else {
|
Chris@109
|
296 params.push_front("--noninteractive");
|
Chris@109
|
297 }
|
Chris@107
|
298 }
|
Chris@104
|
299 #endif
|
jtkorhonen@0
|
300
|
Chris@84
|
301 m_isRunning = true;
|
jtkorhonen@0
|
302 setRange(0, 0);
|
Chris@109
|
303 show();
|
Chris@110
|
304 m_stdout.clear();
|
Chris@110
|
305 m_stderr.clear();
|
Chris@84
|
306 m_realm = "";
|
Chris@84
|
307 m_userName = "";
|
jtkorhonen@0
|
308
|
Chris@109
|
309 if (!action.workingDir.isEmpty()) {
|
Chris@109
|
310 m_proc->setWorkingDirectory(action.workingDir);
|
jtkorhonen@0
|
311 }
|
jtkorhonen@0
|
312
|
Chris@107
|
313 if (interactive) {
|
Chris@111
|
314 openTerminal();
|
Chris@111
|
315 if (m_ptySlaveFilename != "") {
|
Chris@107
|
316 m_proc->setStandardInputFile(m_ptySlaveFilename);
|
Chris@107
|
317 }
|
Chris@84
|
318 }
|
Chris@84
|
319
|
Chris@109
|
320 QString cmdline = executable;
|
Chris@57
|
321 foreach (QString param, params) cmdline += " " + param;
|
Chris@64
|
322 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
|
Chris@109
|
323 << action.workingDir << endl;
|
Chris@43
|
324
|
Chris@109
|
325 m_currentAction = action;
|
Chris@109
|
326
|
Chris@109
|
327 DEBUG << "set current action to " << m_currentAction.action << endl;
|
Chris@109
|
328
|
Chris@109
|
329 m_proc->start(executable, params);
|
Chris@84
|
330 }
|
Chris@84
|
331
|
Chris@84
|
332 void HgRunner::closeProcInput()
|
Chris@84
|
333 {
|
Chris@84
|
334 DEBUG << "closeProcInput" << endl;
|
Chris@84
|
335
|
Chris@84
|
336 m_proc->closeWriteChannel();
|
Chris@111
|
337 }
|
Chris@111
|
338
|
Chris@111
|
339 void HgRunner::openTerminal()
|
Chris@111
|
340 {
|
Chris@111
|
341 #ifndef Q_OS_WIN32
|
Chris@111
|
342 if (m_ptySlaveFilename != "") return; // already open
|
Chris@111
|
343 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
|
Chris@111
|
344 int master = posix_openpt(O_RDWR | O_NOCTTY);
|
Chris@111
|
345 if (master < 0) {
|
Chris@111
|
346 DEBUG << "openpt failed" << endl;
|
Chris@111
|
347 perror("openpt failed");
|
Chris@111
|
348 return;
|
Chris@111
|
349 }
|
Chris@111
|
350 if (grantpt(master)) {
|
Chris@111
|
351 perror("grantpt failed");
|
Chris@111
|
352 }
|
Chris@111
|
353 if (unlockpt(master)) {
|
Chris@111
|
354 perror("unlockpt failed");
|
Chris@111
|
355 }
|
Chris@111
|
356 char *slave = ptsname(master);
|
Chris@111
|
357 if (!slave) {
|
Chris@111
|
358 perror("ptsname failed");
|
Chris@111
|
359 ::close(master);
|
Chris@111
|
360 return;
|
Chris@111
|
361 }
|
Chris@111
|
362 m_ptyMasterFd = master;
|
Chris@111
|
363 m_procInput = new QFile();
|
Chris@111
|
364 m_procInput->open(m_ptyMasterFd, QFile::WriteOnly);
|
Chris@111
|
365 m_ptySlaveFilename = slave;
|
Chris@111
|
366 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
|
Chris@111
|
367 << m_ptySlaveFilename << endl;
|
Chris@111
|
368 #endif
|
Chris@111
|
369 }
|
Chris@111
|
370
|
Chris@111
|
371 void HgRunner::closeTerminal()
|
Chris@111
|
372 {
|
Chris@84
|
373 #ifndef Q_OS_WIN32
|
Chris@84
|
374 if (m_ptySlaveFilename != "") {
|
Chris@111
|
375 delete m_procInput;
|
Chris@111
|
376 m_procInput = 0;
|
Chris@84
|
377 ::close(m_ptyMasterFd);
|
Chris@84
|
378 m_ptySlaveFilename = "";
|
Chris@84
|
379 }
|
Chris@84
|
380 #endif
|
jtkorhonen@0
|
381 }
|