annotate hgrunner.cpp @ 123:3afa1ce339ec

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