annotate hgrunner.cpp @ 174:4dc802a4d5ae

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