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@113
|
37 #include <termios.h>
|
Chris@111
|
38 #include <fcntl.h>
|
Chris@80
|
39 #endif
|
Chris@76
|
40
|
jtkorhonen@0
|
41 HgRunner::HgRunner(QWidget * parent): QProgressBar(parent)
|
jtkorhonen@0
|
42 {
|
Chris@113
|
43 m_proc = 0;
|
Chris@84
|
44
|
jtkorhonen@0
|
45 setTextVisible(false);
|
jtkorhonen@0
|
46 setVisible(false);
|
Chris@84
|
47 m_isRunning = false;
|
jtkorhonen@0
|
48 }
|
jtkorhonen@0
|
49
|
jtkorhonen@0
|
50 HgRunner::~HgRunner()
|
jtkorhonen@0
|
51 {
|
Chris@111
|
52 closeTerminal();
|
Chris@84
|
53 delete m_proc;
|
jtkorhonen@0
|
54 }
|
jtkorhonen@0
|
55
|
Chris@109
|
56 void HgRunner::requestAction(HgAction action)
|
Chris@109
|
57 {
|
Chris@109
|
58 DEBUG << "requestAction " << action.action << endl;
|
Chris@109
|
59 bool pushIt = true;
|
Chris@109
|
60 if (m_queue.empty()) {
|
Chris@109
|
61 if (action == m_currentAction) {
|
Chris@109
|
62 // this request is identical to the thing we're executing
|
Chris@109
|
63 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
|
Chris@109
|
64 pushIt = false;
|
Chris@109
|
65 }
|
Chris@109
|
66 } else {
|
Chris@109
|
67 HgAction last = m_queue.back();
|
Chris@109
|
68 if (action == last) {
|
Chris@109
|
69 // this request is identical to the previous thing we
|
Chris@109
|
70 // queued which we haven't executed yet
|
Chris@109
|
71 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
|
Chris@109
|
72 pushIt = false;
|
Chris@109
|
73 }
|
Chris@109
|
74 }
|
Chris@109
|
75 if (pushIt) m_queue.push_back(action);
|
Chris@109
|
76 checkQueue();
|
Chris@109
|
77 }
|
Chris@109
|
78
|
Chris@62
|
79 QString HgRunner::getHgBinaryName()
|
Chris@62
|
80 {
|
Chris@62
|
81 QSettings settings;
|
Chris@77
|
82 QString hg = settings.value("hgbinary", "").toString();
|
Chris@77
|
83 if (hg == "") {
|
Chris@77
|
84 hg = findExecutable("hg");
|
Chris@77
|
85 }
|
Chris@77
|
86 if (hg != "hg") {
|
Chris@77
|
87 settings.setValue("hgbinary", hg);
|
Chris@77
|
88 }
|
Chris@62
|
89 return hg;
|
Chris@62
|
90 }
|
Chris@62
|
91
|
jtkorhonen@0
|
92 void HgRunner::started()
|
jtkorhonen@0
|
93 {
|
Chris@104
|
94 DEBUG << "started" << endl;
|
Chris@75
|
95 /*
|
Chris@104
|
96 m_proc->write("blah\n");
|
Chris@104
|
97 m_proc->write("blah\n");
|
Chris@104
|
98 m_proc -> closeWriteChannel();
|
Chris@75
|
99 */
|
jtkorhonen@0
|
100 }
|
jtkorhonen@0
|
101
|
Chris@84
|
102 void HgRunner::noteUsername(QString name)
|
Chris@75
|
103 {
|
Chris@84
|
104 m_userName = name;
|
Chris@75
|
105 }
|
Chris@75
|
106
|
Chris@84
|
107 void HgRunner::noteRealm(QString realm)
|
Chris@75
|
108 {
|
Chris@84
|
109 m_realm = realm;
|
Chris@84
|
110 }
|
Chris@84
|
111
|
Chris@84
|
112 void HgRunner::getUsername()
|
Chris@84
|
113 {
|
Chris@113
|
114 if (m_ptyFile) {
|
Chris@84
|
115 bool ok = false;
|
Chris@84
|
116 QString prompt = tr("User name:");
|
Chris@84
|
117 if (m_realm != "") {
|
Chris@84
|
118 prompt = tr("User name for \"%1\":").arg(m_realm);
|
Chris@84
|
119 }
|
Chris@84
|
120 QString pwd = QInputDialog::getText
|
Chris@84
|
121 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
122 tr("Enter user name"), prompt,
|
Chris@84
|
123 QLineEdit::Normal, QString(), &ok);
|
Chris@84
|
124 if (ok) {
|
Chris@113
|
125 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@113
|
126 m_ptyFile->flush();
|
Chris@84
|
127 return;
|
Chris@111
|
128 } else {
|
Chris@111
|
129 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
|
Chris@111
|
130 killCurrentCommand();
|
Chris@111
|
131 return;
|
Chris@84
|
132 }
|
Chris@84
|
133 }
|
Chris@84
|
134 // user cancelled or something went wrong
|
Chris@111
|
135 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
|
Chris@84
|
136 killCurrentCommand();
|
Chris@84
|
137 }
|
Chris@84
|
138
|
Chris@84
|
139 void HgRunner::getPassword()
|
Chris@84
|
140 {
|
Chris@113
|
141 if (m_ptyFile) {
|
Chris@84
|
142 bool ok = false;
|
Chris@84
|
143 QString prompt = tr("Password:");
|
Chris@84
|
144 if (m_userName != "") {
|
Chris@84
|
145 if (m_realm != "") {
|
Chris@84
|
146 prompt = tr("Password for \"%1\" at \"%2\":")
|
Chris@84
|
147 .arg(m_userName).arg(m_realm);
|
Chris@75
|
148 } else {
|
Chris@84
|
149 prompt = tr("Password for user \"%1\":")
|
Chris@84
|
150 .arg(m_userName);
|
Chris@75
|
151 }
|
Chris@75
|
152 }
|
Chris@84
|
153 QString pwd = QInputDialog::getText
|
Chris@84
|
154 (qobject_cast<QWidget *>(parent()),
|
Chris@84
|
155 tr("Enter password"), prompt,
|
Chris@84
|
156 QLineEdit::Password, QString(), &ok);
|
Chris@84
|
157 if (ok) {
|
Chris@113
|
158 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
|
Chris@113
|
159 m_ptyFile->flush();
|
Chris@84
|
160 return;
|
Chris@111
|
161 } else {
|
Chris@111
|
162 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
|
Chris@111
|
163 killCurrentCommand();
|
Chris@111
|
164 return;
|
Chris@84
|
165 }
|
Chris@75
|
166 }
|
Chris@84
|
167 // user cancelled or something went wrong
|
Chris@111
|
168 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
|
Chris@84
|
169 killCurrentCommand();
|
Chris@84
|
170 }
|
Chris@84
|
171
|
Chris@113
|
172 bool HgRunner::checkPrompts(QString chunk)
|
Chris@84
|
173 {
|
Chris@93
|
174 //DEBUG << "checkPrompts: " << chunk << endl;
|
Chris@84
|
175
|
Chris@84
|
176 QString text = chunk.trimmed();
|
Chris@84
|
177 QString lower = text.toLower();
|
Chris@84
|
178 if (lower.endsWith("password:")) {
|
Chris@84
|
179 getPassword();
|
Chris@113
|
180 return true;
|
Chris@84
|
181 }
|
Chris@84
|
182 if (lower.endsWith("user:")) {
|
Chris@84
|
183 getUsername();
|
Chris@113
|
184 return true;
|
Chris@84
|
185 }
|
Chris@84
|
186 QRegExp userRe("\\buser:\\s*([^\\s]+)");
|
Chris@84
|
187 if (userRe.indexIn(text) >= 0) {
|
Chris@84
|
188 noteUsername(userRe.cap(1));
|
Chris@84
|
189 }
|
Chris@84
|
190 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
|
Chris@84
|
191 if (realmRe.indexIn(text) >= 0) {
|
Chris@84
|
192 noteRealm(realmRe.cap(1));
|
Chris@84
|
193 }
|
Chris@113
|
194 return false;
|
Chris@84
|
195 }
|
Chris@84
|
196
|
Chris@110
|
197 void HgRunner::dataReadyStdout()
|
Chris@84
|
198 {
|
Chris@110
|
199 DEBUG << "dataReadyStdout" << endl;
|
Chris@110
|
200 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
|
Chris@113
|
201 if (!checkPrompts(chunk)) {
|
Chris@113
|
202 m_stdout += chunk;
|
Chris@113
|
203 }
|
Chris@110
|
204 }
|
Chris@110
|
205
|
Chris@110
|
206 void HgRunner::dataReadyStderr()
|
Chris@110
|
207 {
|
Chris@110
|
208 DEBUG << "dataReadyStderr" << endl;
|
Chris@110
|
209 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
|
Chris@113
|
210 DEBUG << chunk;
|
Chris@113
|
211 if (!checkPrompts(chunk)) {
|
Chris@113
|
212 m_stderr += chunk;
|
Chris@113
|
213 }
|
Chris@113
|
214 }
|
Chris@113
|
215
|
Chris@113
|
216 void HgRunner::dataReadyPty()
|
Chris@113
|
217 {
|
Chris@113
|
218 DEBUG << "dataReadyPty" << endl;
|
Chris@113
|
219 QString chunk = QString::fromUtf8(m_ptyFile->readAll());
|
Chris@113
|
220 DEBUG << "chunk of " << chunk.length() << " chars" << endl;
|
Chris@113
|
221 if (!checkPrompts(chunk)) {
|
Chris@113
|
222 m_stdout += chunk;
|
Chris@113
|
223 }
|
Chris@75
|
224 }
|
Chris@75
|
225
|
jtkorhonen@0
|
226 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
|
jtkorhonen@0
|
227 {
|
Chris@109
|
228 // Save the current action and reset m_currentAction before we
|
Chris@109
|
229 // emit a signal to mark the completion; otherwise we may be
|
Chris@109
|
230 // resetting the action after a slot has already tried to set it
|
Chris@109
|
231 // to something else to start a new action
|
Chris@109
|
232
|
Chris@109
|
233 HgAction completedAction = m_currentAction;
|
Chris@109
|
234
|
Chris@84
|
235 m_isRunning = false;
|
Chris@109
|
236 m_currentAction = HgAction();
|
Chris@84
|
237
|
Chris@84
|
238 closeProcInput();
|
Chris@113
|
239 delete m_proc;
|
Chris@113
|
240 m_proc = 0;
|
jtkorhonen@0
|
241
|
Chris@109
|
242 if (completedAction.action == ACT_NONE) {
|
Chris@109
|
243 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
|
Chris@62
|
244 } else {
|
Chris@113
|
245 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
|
Chris@113
|
246 DEBUG << "HgRunner::finished: Command completed successfully"
|
Chris@113
|
247 << endl;
|
Chris@113
|
248 emit commandCompleted(completedAction, m_stdout);
|
Chris@113
|
249 } else {
|
Chris@113
|
250 DEBUG << "HgRunner::finished: Command failed, exit code "
|
Chris@113
|
251 << procExitCode << ", exit status " << procExitStatus
|
Chris@113
|
252 << ", stderr follows" << endl;
|
Chris@113
|
253 DEBUG << m_stderr << endl;
|
Chris@113
|
254 emit commandFailed(completedAction, m_stderr);
|
Chris@113
|
255 }
|
jtkorhonen@0
|
256 }
|
Chris@109
|
257
|
Chris@109
|
258 checkQueue();
|
jtkorhonen@0
|
259 }
|
jtkorhonen@0
|
260
|
Chris@62
|
261 void HgRunner::killCurrentCommand()
|
jtkorhonen@0
|
262 {
|
Chris@109
|
263 if (m_isRunning) {
|
Chris@113
|
264 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
|
Chris@113
|
265 m_proc->kill();
|
jtkorhonen@0
|
266 }
|
jtkorhonen@0
|
267 }
|
jtkorhonen@0
|
268
|
Chris@109
|
269 void HgRunner::checkQueue()
|
Chris@62
|
270 {
|
Chris@109
|
271 if (m_isRunning) {
|
Chris@109
|
272 return;
|
Chris@109
|
273 }
|
Chris@109
|
274 if (m_queue.empty()) {
|
Chris@109
|
275 hide();
|
Chris@109
|
276 return;
|
Chris@109
|
277 }
|
Chris@109
|
278 HgAction toRun = m_queue.front();
|
Chris@109
|
279 m_queue.pop_front();
|
Chris@109
|
280 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
|
Chris@109
|
281 startCommand(toRun);
|
Chris@109
|
282 }
|
Chris@109
|
283
|
Chris@109
|
284 void HgRunner::startCommand(HgAction action)
|
Chris@109
|
285 {
|
Chris@109
|
286 QString executable = action.executable;
|
Chris@109
|
287 bool interactive = false;
|
Chris@109
|
288 QStringList params = action.params;
|
Chris@109
|
289
|
Chris@109
|
290 if (executable == "") {
|
Chris@109
|
291 // This is a Hg command
|
Chris@109
|
292 executable = getHgBinaryName();
|
Chris@104
|
293 #ifdef Q_OS_WIN32
|
Chris@109
|
294 // This at least means we won't block on the non-working password prompt
|
Chris@109
|
295 params.push_front("--noninteractive");
|
Chris@104
|
296 #else
|
Chris@109
|
297 // password prompt should work here
|
Chris@109
|
298 if (action.mayBeInteractive()) {
|
Chris@109
|
299 params.push_front("ui.interactive=true");
|
Chris@109
|
300 params.push_front("--config");
|
Chris@109
|
301 interactive = true;
|
Chris@109
|
302 } else {
|
Chris@109
|
303 params.push_front("--noninteractive");
|
Chris@109
|
304 }
|
Chris@107
|
305 }
|
Chris@104
|
306 #endif
|
jtkorhonen@0
|
307
|
Chris@84
|
308 m_isRunning = true;
|
jtkorhonen@0
|
309 setRange(0, 0);
|
Chris@109
|
310 show();
|
Chris@110
|
311 m_stdout.clear();
|
Chris@110
|
312 m_stderr.clear();
|
Chris@84
|
313 m_realm = "";
|
Chris@84
|
314 m_userName = "";
|
jtkorhonen@0
|
315
|
Chris@113
|
316 m_proc = new QProcess;
|
Chris@113
|
317
|
Chris@113
|
318 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
Chris@113
|
319 env.insert("LANG", "en_US.utf8");
|
Chris@113
|
320 env.insert("LC_ALL", "en_US.utf8");
|
Chris@113
|
321 env.insert("HGPLAIN", "1");
|
Chris@113
|
322 m_proc->setProcessEnvironment(env);
|
Chris@113
|
323
|
Chris@113
|
324 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
|
Chris@113
|
325 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
|
Chris@113
|
326 this, SLOT(finished(int, QProcess::ExitStatus)));
|
Chris@113
|
327 connect(m_proc, SIGNAL(readyReadStandardOutput()),
|
Chris@113
|
328 this, SLOT(dataReadyStdout()));
|
Chris@113
|
329 connect(m_proc, SIGNAL(readyReadStandardError()),
|
Chris@113
|
330 this, SLOT(dataReadyStderr()));
|
Chris@113
|
331
|
Chris@109
|
332 if (!action.workingDir.isEmpty()) {
|
Chris@109
|
333 m_proc->setWorkingDirectory(action.workingDir);
|
jtkorhonen@0
|
334 }
|
jtkorhonen@0
|
335
|
Chris@107
|
336 if (interactive) {
|
Chris@111
|
337 openTerminal();
|
Chris@111
|
338 if (m_ptySlaveFilename != "") {
|
Chris@113
|
339 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
|
Chris@107
|
340 m_proc->setStandardInputFile(m_ptySlaveFilename);
|
Chris@113
|
341 m_proc->setStandardOutputFile(m_ptySlaveFilename);
|
Chris@113
|
342 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
|
Chris@107
|
343 }
|
Chris@84
|
344 }
|
Chris@84
|
345
|
Chris@109
|
346 QString cmdline = executable;
|
Chris@57
|
347 foreach (QString param, params) cmdline += " " + param;
|
Chris@64
|
348 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
|
Chris@109
|
349 << action.workingDir << endl;
|
Chris@43
|
350
|
Chris@109
|
351 m_currentAction = action;
|
Chris@109
|
352
|
Chris@113
|
353 // fill these out with what we actually ran
|
Chris@113
|
354 m_currentAction.executable = executable;
|
Chris@113
|
355 m_currentAction.params = params;
|
Chris@113
|
356
|
Chris@109
|
357 DEBUG << "set current action to " << m_currentAction.action << endl;
|
Chris@109
|
358
|
Chris@109
|
359 m_proc->start(executable, params);
|
Chris@84
|
360 }
|
Chris@84
|
361
|
Chris@84
|
362 void HgRunner::closeProcInput()
|
Chris@84
|
363 {
|
Chris@84
|
364 DEBUG << "closeProcInput" << endl;
|
Chris@84
|
365
|
Chris@84
|
366 m_proc->closeWriteChannel();
|
Chris@111
|
367 }
|
Chris@111
|
368
|
Chris@111
|
369 void HgRunner::openTerminal()
|
Chris@111
|
370 {
|
Chris@111
|
371 #ifndef Q_OS_WIN32
|
Chris@111
|
372 if (m_ptySlaveFilename != "") return; // already open
|
Chris@111
|
373 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
|
Chris@111
|
374 int master = posix_openpt(O_RDWR | O_NOCTTY);
|
Chris@111
|
375 if (master < 0) {
|
Chris@111
|
376 DEBUG << "openpt failed" << endl;
|
Chris@111
|
377 perror("openpt failed");
|
Chris@111
|
378 return;
|
Chris@111
|
379 }
|
Chris@113
|
380 struct termios t;
|
Chris@113
|
381 if (tcgetattr(master, &t)) {
|
Chris@113
|
382 DEBUG << "tcgetattr failed" << endl;
|
Chris@113
|
383 perror("tcgetattr failed");
|
Chris@113
|
384 }
|
Chris@113
|
385 cfmakeraw(&t);
|
Chris@113
|
386 if (tcsetattr(master, TCSANOW, &t)) {
|
Chris@113
|
387 DEBUG << "tcsetattr failed" << endl;
|
Chris@113
|
388 perror("tcsetattr failed");
|
Chris@113
|
389 }
|
Chris@111
|
390 if (grantpt(master)) {
|
Chris@111
|
391 perror("grantpt failed");
|
Chris@111
|
392 }
|
Chris@111
|
393 if (unlockpt(master)) {
|
Chris@111
|
394 perror("unlockpt failed");
|
Chris@111
|
395 }
|
Chris@111
|
396 char *slave = ptsname(master);
|
Chris@111
|
397 if (!slave) {
|
Chris@111
|
398 perror("ptsname failed");
|
Chris@111
|
399 ::close(master);
|
Chris@111
|
400 return;
|
Chris@111
|
401 }
|
Chris@111
|
402 m_ptyMasterFd = master;
|
Chris@113
|
403 m_ptyFile = new QFile();
|
Chris@113
|
404 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
|
Chris@113
|
405 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
|
Chris@113
|
406 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
|
Chris@113
|
407 }
|
Chris@111
|
408 m_ptySlaveFilename = slave;
|
Chris@111
|
409 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
|
Chris@111
|
410 << m_ptySlaveFilename << endl;
|
Chris@111
|
411 #endif
|
Chris@111
|
412 }
|
Chris@111
|
413
|
Chris@111
|
414 void HgRunner::closeTerminal()
|
Chris@111
|
415 {
|
Chris@84
|
416 #ifndef Q_OS_WIN32
|
Chris@84
|
417 if (m_ptySlaveFilename != "") {
|
Chris@113
|
418 delete m_ptyFile;
|
Chris@113
|
419 m_ptyFile = 0;
|
Chris@84
|
420 ::close(m_ptyMasterFd);
|
Chris@84
|
421 m_ptySlaveFilename = "";
|
Chris@84
|
422 }
|
Chris@84
|
423 #endif
|
jtkorhonen@0
|
424 }
|