annotate src/hgrunner.cpp @ 561:1ff2a1bf0a40

Add a cancel button to the progress bar
author Chris Cannam
date Mon, 27 Feb 2012 17:25:50 +0000
parents 533519ebc0cb
children 39cac58b4f92
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@560 8 Copyright (c) 2012 Chris Cannam
Chris@560 9 Copyright (c) 2012 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@240 21 #include "settingsdialog.h"
Chris@50 22
Chris@62 23 #include <QSettings>
Chris@75 24 #include <QInputDialog>
Chris@444 25 #include <QDesktopServices>
Chris@188 26 #include <QTemporaryFile>
Chris@161 27 #include <QDir>
Chris@561 28 #include <QProgressBar>
Chris@561 29 #include <QToolButton>
Chris@561 30 #include <QGridLayout>
jtkorhonen@0 31
Chris@43 32 #include <iostream>
Chris@75 33 #include <errno.h>
Chris@75 34 #include <stdio.h>
Chris@111 35 #include <stdlib.h>
jtkorhonen@0 36
Chris@76 37 #ifndef Q_OS_WIN32
Chris@111 38 #include <unistd.h>
Chris@113 39 #include <termios.h>
Chris@111 40 #include <fcntl.h>
chris@479 41 #else
chris@479 42 #include <process.h>
Chris@80 43 #endif
Chris@76 44
Chris@561 45 HgRunner::HgRunner(QString myDirPath, QWidget *parent) :
Chris@561 46 QWidget(parent),
Chris@172 47 m_myDirPath(myDirPath)
jtkorhonen@0 48 {
Chris@561 49 QGridLayout *layout = new QGridLayout(this);
Chris@561 50 layout->setMargin(0);
Chris@561 51
Chris@561 52 m_progress = new QProgressBar;
Chris@561 53 layout->addWidget(m_progress, 0, 0);
Chris@561 54
Chris@561 55 m_cancel = new QToolButton;
Chris@561 56 m_cancel->setIcon(QIcon(":images/cancel-small.png"));
Chris@561 57 m_cancel->setToolButtonStyle(Qt::ToolButtonIconOnly);
Chris@561 58 m_cancel->setAutoRaise(true);
Chris@561 59 connect(m_cancel, SIGNAL(clicked()), this, SLOT(killCurrentActions()));
Chris@561 60 layout->addWidget(m_cancel, 0, 1);
Chris@561 61
Chris@113 62 m_proc = 0;
Chris@84 63
Chris@239 64 // Always unbundle the extension: even if it already exists (in
Chris@239 65 // case we're upgrading) and even if we're not going to use it (so
Chris@239 66 // that it's available in case someone wants to use it later,
Chris@239 67 // e.g. to fix a malfunctioning setup). But the path we actually
Chris@239 68 // prefer is the one in the settings first, if it exists; then the
Chris@239 69 // unbundled one; then anything in the path if for some reason
Chris@239 70 // unbundling failed
Chris@239 71 unbundleExtension();
Chris@239 72
Chris@561 73 m_progress->setTextVisible(false);
Chris@561 74 hide();
Chris@84 75 m_isRunning = false;
jtkorhonen@0 76 }
jtkorhonen@0 77
jtkorhonen@0 78 HgRunner::~HgRunner()
jtkorhonen@0 79 {
Chris@111 80 closeTerminal();
Chris@120 81 if (m_proc) {
Chris@120 82 m_proc->kill();
Chris@122 83 m_proc->deleteLater();
Chris@120 84 }
Chris@444 85 if (m_authFilePath != "") {
Chris@444 86 QFile(m_authFilePath).remove();
Chris@444 87 }
Chris@444 88 //!!! and remove any other misc auth file paths...
jtkorhonen@0 89 }
jtkorhonen@0 90
Chris@188 91 QString HgRunner::getUnbundledFileName()
Chris@188 92 {
Chris@240 93 return SettingsDialog::getUnbundledExtensionFileName();
Chris@188 94 }
Chris@188 95
Chris@180 96 QString HgRunner::unbundleExtension()
Chris@161 97 {
Chris@188 98 // Pull out the bundled Python file into a temporary file, and
Chris@188 99 // copy it to our known extension location, replacing the magic
Chris@188 100 // text NO_EASYHG_IMPORT_PATH with our installation location
Chris@188 101
Chris@161 102 QString bundled = ":easyhg.py";
Chris@188 103 QString unbundled = getUnbundledFileName();
Chris@188 104
Chris@188 105 QString target = QFileInfo(unbundled).path();
Chris@161 106 if (!QDir().mkpath(target)) {
Chris@161 107 DEBUG << "Failed to make unbundle path " << target << endl;
Chris@188 108 std::cerr << "Failed to make unbundle path " << target << std::endl;
Chris@180 109 return "";
Chris@161 110 }
Chris@188 111
Chris@161 112 QFile bf(bundled);
Chris@188 113 DEBUG << "unbundle: bundled file will be " << bundled << endl;
Chris@188 114 if (!bf.exists() || !bf.open(QIODevice::ReadOnly)) {
Chris@180 115 DEBUG << "Bundled extension is missing!" << endl;
Chris@180 116 return "";
Chris@180 117 }
Chris@188 118
Chris@188 119 QTemporaryFile tmpfile(QString("%1/easyhg.py.XXXXXX").arg(target));
Chris@188 120 tmpfile.setAutoRemove(false);
Chris@188 121 DEBUG << "unbundle: temp file will be " << tmpfile.fileName() << endl;
Chris@188 122 if (!tmpfile.open()) {
Chris@188 123 DEBUG << "Failed to open temporary file " << tmpfile.fileName() << endl;
Chris@188 124 std::cerr << "Failed to open temporary file " << tmpfile.fileName() << std::endl;
Chris@180 125 return "";
Chris@161 126 }
Chris@188 127
Chris@188 128 QString all = QString::fromUtf8(bf.readAll());
Chris@188 129 all.replace("NO_EASYHG_IMPORT_PATH", m_myDirPath);
Chris@188 130 tmpfile.write(all.toUtf8());
Chris@188 131 DEBUG << "unbundle: wrote " << all.length() << " characters" << endl;
Chris@188 132
Chris@188 133 tmpfile.close();
Chris@188 134
Chris@188 135 QFile ef(unbundled);
Chris@188 136 if (ef.exists()) {
Chris@188 137 DEBUG << "unbundle: removing old file " << unbundled << endl;
Chris@188 138 ef.remove();
Chris@188 139 }
Chris@188 140 DEBUG << "unbundle: renaming " << tmpfile.fileName() << " to " << unbundled << endl;
Chris@188 141 if (!tmpfile.rename(unbundled)) {
Chris@188 142 DEBUG << "Failed to move temporary file to target file " << unbundled << endl;
Chris@188 143 std::cerr << "Failed to move temporary file to target file " << unbundled << std::endl;
Chris@188 144 return "";
Chris@188 145 }
Chris@188 146
Chris@188 147 DEBUG << "Unbundled extension to " << unbundled << endl;
Chris@188 148 return unbundled;
Chris@161 149 }
Chris@161 150
Chris@109 151 void HgRunner::requestAction(HgAction action)
Chris@109 152 {
Chris@109 153 DEBUG << "requestAction " << action.action << endl;
Chris@109 154 bool pushIt = true;
Chris@109 155 if (m_queue.empty()) {
Chris@109 156 if (action == m_currentAction) {
Chris@109 157 // this request is identical to the thing we're executing
Chris@109 158 DEBUG << "requestAction: we're already handling this one, ignoring identical request" << endl;
Chris@109 159 pushIt = false;
Chris@109 160 }
Chris@109 161 } else {
Chris@109 162 HgAction last = m_queue.back();
Chris@109 163 if (action == last) {
Chris@109 164 // this request is identical to the previous thing we
Chris@109 165 // queued which we haven't executed yet
Chris@109 166 DEBUG << "requestAction: we're already queueing this one, ignoring identical request" << endl;
Chris@109 167 pushIt = false;
Chris@109 168 }
Chris@109 169 }
Chris@109 170 if (pushIt) m_queue.push_back(action);
Chris@109 171 checkQueue();
Chris@109 172 }
Chris@109 173
Chris@239 174 QString HgRunner::getHgBinaryName()
Chris@62 175 {
Chris@62 176 QSettings settings;
Chris@175 177 settings.beginGroup("Locations");
Chris@239 178 return settings.value("hgbinary", "").toString();
Chris@62 179 }
Chris@62 180
chris@406 181 QString HgRunner::getSshBinaryName()
chris@406 182 {
chris@406 183 QSettings settings;
chris@406 184 settings.beginGroup("Locations");
chris@406 185 return settings.value("sshbinary", "").toString();
chris@406 186 }
chris@406 187
Chris@239 188 QString HgRunner::getExtensionLocation()
Chris@239 189 {
Chris@239 190 QSettings settings;
Chris@239 191 settings.beginGroup("Locations");
Chris@239 192 QString extpath = settings.value("extensionpath", "").toString();
Chris@239 193 if (extpath != "" && QFile(extpath).exists()) return extpath;
Chris@239 194 return "";
Chris@239 195 }
Chris@239 196
jtkorhonen@0 197 void HgRunner::started()
jtkorhonen@0 198 {
Chris@104 199 DEBUG << "started" << endl;
Chris@75 200 /*
Chris@104 201 m_proc->write("blah\n");
Chris@104 202 m_proc->write("blah\n");
Chris@104 203 m_proc -> closeWriteChannel();
Chris@75 204 */
jtkorhonen@0 205 }
jtkorhonen@0 206
Chris@84 207 void HgRunner::noteUsername(QString name)
Chris@75 208 {
Chris@84 209 m_userName = name;
Chris@75 210 }
Chris@75 211
Chris@84 212 void HgRunner::noteRealm(QString realm)
Chris@75 213 {
Chris@84 214 m_realm = realm;
Chris@84 215 }
Chris@84 216
Chris@84 217 void HgRunner::getUsername()
Chris@84 218 {
Chris@113 219 if (m_ptyFile) {
Chris@84 220 bool ok = false;
Chris@84 221 QString prompt = tr("User name:");
Chris@84 222 if (m_realm != "") {
Chris@84 223 prompt = tr("User name for \"%1\":").arg(m_realm);
Chris@84 224 }
Chris@84 225 QString pwd = QInputDialog::getText
Chris@84 226 (qobject_cast<QWidget *>(parent()),
Chris@84 227 tr("Enter user name"), prompt,
Chris@84 228 QLineEdit::Normal, QString(), &ok);
Chris@84 229 if (ok) {
Chris@113 230 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
Chris@113 231 m_ptyFile->flush();
Chris@84 232 return;
Chris@111 233 } else {
Chris@111 234 DEBUG << "HgRunner::getUsername: user cancelled" << endl;
Chris@111 235 killCurrentCommand();
Chris@111 236 return;
Chris@84 237 }
Chris@84 238 }
Chris@84 239 // user cancelled or something went wrong
Chris@111 240 DEBUG << "HgRunner::getUsername: something went wrong" << endl;
Chris@84 241 killCurrentCommand();
Chris@84 242 }
Chris@84 243
Chris@84 244 void HgRunner::getPassword()
Chris@84 245 {
Chris@113 246 if (m_ptyFile) {
Chris@84 247 bool ok = false;
Chris@84 248 QString prompt = tr("Password:");
Chris@84 249 if (m_userName != "") {
Chris@84 250 if (m_realm != "") {
Chris@84 251 prompt = tr("Password for \"%1\" at \"%2\":")
Chris@84 252 .arg(m_userName).arg(m_realm);
Chris@75 253 } else {
Chris@84 254 prompt = tr("Password for user \"%1\":")
Chris@84 255 .arg(m_userName);
Chris@75 256 }
Chris@75 257 }
Chris@84 258 QString pwd = QInputDialog::getText
Chris@84 259 (qobject_cast<QWidget *>(parent()),
Chris@84 260 tr("Enter password"), prompt,
Chris@84 261 QLineEdit::Password, QString(), &ok);
Chris@84 262 if (ok) {
Chris@113 263 m_ptyFile->write(QString("%1\n").arg(pwd).toUtf8());
Chris@113 264 m_ptyFile->flush();
Chris@84 265 return;
Chris@111 266 } else {
Chris@111 267 DEBUG << "HgRunner::getPassword: user cancelled" << endl;
Chris@111 268 killCurrentCommand();
Chris@111 269 return;
Chris@84 270 }
Chris@75 271 }
Chris@84 272 // user cancelled or something went wrong
Chris@111 273 DEBUG << "HgRunner::getPassword: something went wrong" << endl;
Chris@84 274 killCurrentCommand();
Chris@84 275 }
Chris@84 276
Chris@113 277 bool HgRunner::checkPrompts(QString chunk)
Chris@84 278 {
Chris@93 279 //DEBUG << "checkPrompts: " << chunk << endl;
Chris@84 280
Chris@128 281 if (!m_currentAction.mayBeInteractive()) return false;
Chris@128 282
Chris@84 283 QString text = chunk.trimmed();
Chris@84 284 QString lower = text.toLower();
Chris@84 285 if (lower.endsWith("password:")) {
Chris@84 286 getPassword();
Chris@113 287 return true;
Chris@84 288 }
Chris@128 289 if (lower.endsWith("user:") || lower.endsWith("username:")) {
Chris@84 290 getUsername();
Chris@113 291 return true;
Chris@84 292 }
Chris@128 293 QRegExp userRe("\\buser(name)?:\\s*([^\\s]+)");
Chris@84 294 if (userRe.indexIn(text) >= 0) {
Chris@128 295 noteUsername(userRe.cap(2));
Chris@84 296 }
Chris@84 297 QRegExp realmRe("\\brealmr:\\s*([^\\s]+)");
Chris@84 298 if (realmRe.indexIn(text) >= 0) {
Chris@84 299 noteRealm(realmRe.cap(1));
Chris@84 300 }
Chris@113 301 return false;
Chris@84 302 }
Chris@84 303
Chris@110 304 void HgRunner::dataReadyStdout()
Chris@84 305 {
Chris@110 306 DEBUG << "dataReadyStdout" << endl;
Chris@408 307 if (!m_proc) return;
Chris@110 308 QString chunk = QString::fromUtf8(m_proc->readAllStandardOutput());
Chris@113 309 if (!checkPrompts(chunk)) {
Chris@113 310 m_stdout += chunk;
Chris@113 311 }
Chris@110 312 }
Chris@110 313
Chris@110 314 void HgRunner::dataReadyStderr()
Chris@110 315 {
Chris@110 316 DEBUG << "dataReadyStderr" << endl;
Chris@408 317 if (!m_proc) return;
Chris@110 318 QString chunk = QString::fromUtf8(m_proc->readAllStandardError());
Chris@113 319 DEBUG << chunk;
Chris@113 320 if (!checkPrompts(chunk)) {
Chris@113 321 m_stderr += chunk;
Chris@113 322 }
Chris@113 323 }
Chris@113 324
Chris@113 325 void HgRunner::dataReadyPty()
Chris@113 326 {
Chris@113 327 DEBUG << "dataReadyPty" << endl;
Chris@113 328 QString chunk = QString::fromUtf8(m_ptyFile->readAll());
Chris@113 329 DEBUG << "chunk of " << chunk.length() << " chars" << endl;
Chris@113 330 if (!checkPrompts(chunk)) {
Chris@113 331 m_stdout += chunk;
Chris@113 332 }
Chris@75 333 }
Chris@75 334
Chris@189 335 void HgRunner::error(QProcess::ProcessError)
Chris@189 336 {
Chris@189 337 finished(-1, QProcess::CrashExit);
Chris@189 338 }
Chris@189 339
jtkorhonen@0 340 void HgRunner::finished(int procExitCode, QProcess::ExitStatus procExitStatus)
jtkorhonen@0 341 {
chris@385 342 if (!m_proc) return;
chris@385 343
chris@385 344 // Save the current action and reset m_currentAction before we
Chris@109 345 // emit a signal to mark the completion; otherwise we may be
Chris@109 346 // resetting the action after a slot has already tried to set it
Chris@109 347 // to something else to start a new action
Chris@109 348
Chris@109 349 HgAction completedAction = m_currentAction;
Chris@109 350
Chris@84 351 m_isRunning = false;
Chris@109 352 m_currentAction = HgAction();
Chris@84 353
Chris@195 354 //closeProcInput();
Chris@189 355 m_proc->deleteLater();
Chris@113 356 m_proc = 0;
jtkorhonen@0 357
Chris@109 358 if (completedAction.action == ACT_NONE) {
Chris@109 359 DEBUG << "HgRunner::finished: WARNING: completed action is ACT_NONE" << endl;
Chris@62 360 } else {
Chris@113 361 if (procExitCode == 0 && procExitStatus == QProcess::NormalExit) {
Chris@113 362 DEBUG << "HgRunner::finished: Command completed successfully"
Chris@113 363 << endl;
Chris@124 364 // DEBUG << "stdout is " << m_stdout << endl;
Chris@113 365 emit commandCompleted(completedAction, m_stdout);
Chris@113 366 } else {
Chris@113 367 DEBUG << "HgRunner::finished: Command failed, exit code "
Chris@113 368 << procExitCode << ", exit status " << procExitStatus
Chris@113 369 << ", stderr follows" << endl;
Chris@113 370 DEBUG << m_stderr << endl;
Chris@537 371 emit commandFailed(completedAction, m_stderr, m_stdout);
Chris@113 372 }
jtkorhonen@0 373 }
Chris@109 374
Chris@109 375 checkQueue();
jtkorhonen@0 376 }
jtkorhonen@0 377
Chris@182 378 void HgRunner::killCurrentActions()
Chris@182 379 {
Chris@182 380 m_queue.clear();
Chris@182 381 killCurrentCommand();
Chris@182 382 }
Chris@182 383
Chris@62 384 void HgRunner::killCurrentCommand()
jtkorhonen@0 385 {
Chris@109 386 if (m_isRunning) {
Chris@113 387 m_currentAction.action = ACT_NONE; // so that we don't bother to notify
chris@385 388 if (m_proc) m_proc->kill();
jtkorhonen@0 389 }
jtkorhonen@0 390 }
jtkorhonen@0 391
Chris@109 392 void HgRunner::checkQueue()
Chris@62 393 {
Chris@109 394 if (m_isRunning) {
Chris@109 395 return;
Chris@109 396 }
Chris@109 397 if (m_queue.empty()) {
Chris@109 398 hide();
Chris@109 399 return;
Chris@109 400 }
Chris@109 401 HgAction toRun = m_queue.front();
Chris@109 402 m_queue.pop_front();
Chris@109 403 DEBUG << "checkQueue: have action: running " << toRun.action << endl;
Chris@109 404 startCommand(toRun);
Chris@109 405 }
Chris@109 406
Chris@455 407 void HgRunner::pruneOldAuthFiles()
Chris@455 408 {
Chris@455 409 QString path = QDesktopServices::storageLocation
Chris@455 410 (QDesktopServices::CacheLocation);
Chris@455 411 QDir d(path);
Chris@455 412 if (!d.exists()) return;
Chris@455 413 QStringList filters;
Chris@455 414 filters << "easyhg.*.dat";
Chris@455 415 QStringList fl = d.entryList(filters);
Chris@455 416 foreach (QString f, fl) {
Chris@455 417 QStringList parts = f.split('.');
Chris@455 418 if (parts.size() > 1) {
Chris@455 419 int pid = parts[1].toInt();
Chris@455 420 DEBUG << "Checking pid " << pid << " for cache file " << f << endl;
Chris@455 421
Chris@455 422 ProcessStatus ps = GetProcessStatus(pid);
Chris@455 423 if (ps == ProcessNotRunning) {
Chris@455 424 DEBUG << "Removing stale cache file " << f << endl;
Chris@455 425 QDir(d).remove(f);
Chris@455 426 }
Chris@455 427 }
Chris@455 428 }
Chris@455 429 }
Chris@455 430
Chris@455 431 QString HgRunner::getAuthFilePath()
Chris@455 432 {
Chris@455 433 if (m_authFilePath == "") {
Chris@455 434
Chris@455 435 pruneOldAuthFiles();
Chris@455 436
Chris@455 437 QByteArray fileExt = randomKey();
Chris@455 438 if (fileExt == QByteArray()) {
Chris@455 439 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth file ext" << endl;
Chris@455 440 return "";
Chris@455 441 }
Chris@455 442 QString fileExt16 = QString::fromLocal8Bit(fileExt.toBase64()).left(16)
Chris@455 443 .replace('+', '-').replace('/', '_');
Chris@455 444 QString path = QDesktopServices::storageLocation
Chris@455 445 (QDesktopServices::CacheLocation);
Chris@455 446 QDir().mkpath(path);
Chris@455 447 if (path == "") {
Chris@455 448 DEBUG << "HgRunner::addExtensionOptions: Failed to get cache location" << endl;
Chris@455 449 return "";
Chris@455 450 }
Chris@455 451
Chris@455 452 m_authFilePath = QString("%1/easyhg.%2.%3.dat").arg(path)
Chris@455 453 .arg(getpid()).arg(fileExt16);
Chris@455 454 }
Chris@455 455
Chris@455 456 return m_authFilePath;
Chris@455 457 }
Chris@455 458
Chris@455 459 QString HgRunner::getAuthKey()
Chris@455 460 {
Chris@455 461 if (m_authKey == "") {
Chris@455 462 QByteArray key = randomKey();
Chris@455 463 if (key == QByteArray()) {
Chris@455 464 DEBUG << "HgRunner::addExtensionOptions: Failed to get proper auth key" << endl;
Chris@455 465 return "";
Chris@455 466 }
Chris@455 467 QString key16 = QString::fromLocal8Bit(key.toBase64()).left(16);
Chris@455 468 m_authKey = key16;
Chris@455 469 }
Chris@455 470
Chris@455 471 return m_authKey;
Chris@455 472 }
Chris@455 473
Chris@444 474 QStringList HgRunner::addExtensionOptions(QStringList params)
Chris@444 475 {
Chris@444 476 QString extpath = getExtensionLocation();
Chris@444 477 if (extpath == "") {
Chris@444 478 DEBUG << "HgRunner::addExtensionOptions: Failed to get extension location" << endl;
Chris@444 479 return params;
Chris@444 480 }
Chris@444 481
Chris@455 482 QString afp = getAuthFilePath();
Chris@455 483 QString afk = getAuthKey();
Chris@444 484
Chris@455 485 if (afp != "" && afk != "") {
Chris@455 486 params.push_front(QString("easyhg.authkey=%1").arg(m_authKey));
Chris@455 487 params.push_front("--config");
Chris@455 488 params.push_front(QString("easyhg.authfile=%1").arg(m_authFilePath));
Chris@455 489 params.push_front("--config");
Chris@444 490 }
Chris@444 491
Chris@444 492 // Looks like this one must be without quotes, even though the SSH
Chris@444 493 // one above only works on Windows if it has quotes (at least where
Chris@444 494 // there is a space in the path). Odd
Chris@444 495 params.push_front(QString("extensions.easyhg=%1").arg(extpath));
Chris@444 496 params.push_front("--config");
Chris@444 497
Chris@444 498 return params;
Chris@444 499 }
Chris@444 500
Chris@109 501 void HgRunner::startCommand(HgAction action)
Chris@109 502 {
Chris@109 503 QString executable = action.executable;
Chris@109 504 bool interactive = false;
Chris@109 505 QStringList params = action.params;
Chris@109 506
Chris@340 507 if (action.workingDir.isEmpty()) {
Chris@340 508 // We require a working directory, never just operate in pwd
Chris@537 509 emit commandFailed(action, "EasyMercurial: No working directory supplied, will not run Mercurial command without one", "");
Chris@340 510 return;
Chris@340 511 }
Chris@340 512
Chris@109 513 if (executable == "") {
Chris@109 514 // This is a Hg command
Chris@239 515 executable = getHgBinaryName();
chris@406 516 if (executable == "") executable = "hg";
chris@406 517
chris@406 518 QString ssh = getSshBinaryName();
chris@406 519 if (ssh != "") {
chris@406 520 params.push_front(QString("ui.ssh=\"%1\"").arg(ssh));
chris@406 521 params.push_front("--config");
chris@406 522 }
Chris@161 523
Chris@161 524 if (action.mayBeInteractive()) {
Chris@161 525 params.push_front("ui.interactive=true");
Chris@161 526 params.push_front("--config");
Chris@484 527 QSettings settings;
Chris@176 528 if (settings.value("useextension", true).toBool()) {
Chris@444 529 params = addExtensionOptions(params);
Chris@176 530 }
Chris@161 531 interactive = true;
Chris@161 532 }
Chris@161 533
Chris@161 534 //!!! want an option to use the mercurial_keyring extension as well
Chris@107 535 }
jtkorhonen@0 536
Chris@84 537 m_isRunning = true;
Chris@561 538 m_progress->setRange(0, 0);
Chris@115 539 if (!action.shouldBeFast()) show();
Chris@110 540 m_stdout.clear();
Chris@110 541 m_stderr.clear();
Chris@84 542 m_realm = "";
Chris@84 543 m_userName = "";
jtkorhonen@0 544
Chris@113 545 m_proc = new QProcess;
Chris@113 546
Chris@177 547 QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
Chris@177 548
Chris@177 549 #ifdef Q_OS_WIN32
Chris@257 550 // On Win32 we like to bundle Hg and other executables with EasyHg
Chris@177 551 if (m_myDirPath != "") {
Chris@177 552 env.insert("PATH", m_myDirPath + ";" + env.value("PATH"));
Chris@172 553 }
Chris@172 554 #endif
Chris@172 555
Chris@257 556 #ifdef Q_OS_MAC
Chris@490 557 if (QSettings().value("python32", false).toBool()) {
Chris@261 558 env.insert("VERSIONER_PYTHON_PREFER_32_BIT", "1");
Chris@257 559 }
Chris@257 560 #endif
Chris@257 561
Chris@113 562 env.insert("LANG", "en_US.utf8");
Chris@113 563 env.insert("LC_ALL", "en_US.utf8");
Chris@408 564 env.insert("HGENCODING", "utf8");
Chris@113 565 env.insert("HGPLAIN", "1");
Chris@113 566 m_proc->setProcessEnvironment(env);
Chris@113 567
Chris@113 568 connect(m_proc, SIGNAL(started()), this, SLOT(started()));
Chris@189 569 connect(m_proc, SIGNAL(error(QProcess::ProcessError)),
Chris@189 570 this, SLOT(error(QProcess::ProcessError)));
Chris@113 571 connect(m_proc, SIGNAL(finished(int, QProcess::ExitStatus)),
Chris@113 572 this, SLOT(finished(int, QProcess::ExitStatus)));
Chris@113 573 connect(m_proc, SIGNAL(readyReadStandardOutput()),
Chris@113 574 this, SLOT(dataReadyStdout()));
Chris@113 575 connect(m_proc, SIGNAL(readyReadStandardError()),
Chris@113 576 this, SLOT(dataReadyStderr()));
Chris@113 577
Chris@340 578 m_proc->setWorkingDirectory(action.workingDir);
jtkorhonen@0 579
Chris@107 580 if (interactive) {
Chris@111 581 openTerminal();
Chris@111 582 if (m_ptySlaveFilename != "") {
Chris@113 583 DEBUG << "HgRunner: connecting to pseudoterminal" << endl;
Chris@107 584 m_proc->setStandardInputFile(m_ptySlaveFilename);
Chris@114 585 // m_proc->setStandardOutputFile(m_ptySlaveFilename);
Chris@113 586 // m_proc->setStandardErrorFile(m_ptySlaveFilename);
Chris@107 587 }
Chris@84 588 }
Chris@84 589
Chris@109 590 QString cmdline = executable;
Chris@57 591 foreach (QString param, params) cmdline += " " + param;
Chris@64 592 DEBUG << "HgRunner: starting: " << cmdline << " with cwd "
Chris@109 593 << action.workingDir << endl;
Chris@43 594
Chris@109 595 m_currentAction = action;
Chris@109 596
Chris@113 597 // fill these out with what we actually ran
Chris@113 598 m_currentAction.executable = executable;
Chris@113 599 m_currentAction.params = params;
Chris@113 600
Chris@109 601 DEBUG << "set current action to " << m_currentAction.action << endl;
Chris@109 602
Chris@238 603 emit commandStarting(action);
Chris@238 604
Chris@109 605 m_proc->start(executable, params);
Chris@84 606 }
Chris@84 607
Chris@84 608 void HgRunner::closeProcInput()
Chris@84 609 {
Chris@84 610 DEBUG << "closeProcInput" << endl;
Chris@84 611
chris@385 612 if (m_proc) m_proc->closeWriteChannel();
Chris@111 613 }
Chris@111 614
Chris@111 615 void HgRunner::openTerminal()
Chris@111 616 {
Chris@111 617 #ifndef Q_OS_WIN32
Chris@111 618 if (m_ptySlaveFilename != "") return; // already open
Chris@111 619 DEBUG << "HgRunner::openTerminal: trying to open new pty" << endl;
Chris@111 620 int master = posix_openpt(O_RDWR | O_NOCTTY);
Chris@111 621 if (master < 0) {
Chris@111 622 DEBUG << "openpt failed" << endl;
Chris@111 623 perror("openpt failed");
Chris@111 624 return;
Chris@111 625 }
Chris@113 626 struct termios t;
Chris@113 627 if (tcgetattr(master, &t)) {
Chris@113 628 DEBUG << "tcgetattr failed" << endl;
Chris@113 629 perror("tcgetattr failed");
Chris@113 630 }
Chris@113 631 cfmakeraw(&t);
Chris@113 632 if (tcsetattr(master, TCSANOW, &t)) {
Chris@113 633 DEBUG << "tcsetattr failed" << endl;
Chris@113 634 perror("tcsetattr failed");
Chris@113 635 }
Chris@111 636 if (grantpt(master)) {
Chris@111 637 perror("grantpt failed");
Chris@111 638 }
Chris@111 639 if (unlockpt(master)) {
Chris@111 640 perror("unlockpt failed");
Chris@111 641 }
Chris@111 642 char *slave = ptsname(master);
Chris@111 643 if (!slave) {
Chris@111 644 perror("ptsname failed");
Chris@111 645 ::close(master);
Chris@111 646 return;
Chris@111 647 }
Chris@111 648 m_ptyMasterFd = master;
Chris@113 649 m_ptyFile = new QFile();
Chris@113 650 connect(m_ptyFile, SIGNAL(readyRead()), this, SLOT(dataReadyPty()));
Chris@113 651 if (!m_ptyFile->open(m_ptyMasterFd, QFile::ReadWrite)) {
Chris@113 652 DEBUG << "HgRunner::openTerminal: Failed to open QFile on master fd" << endl;
Chris@113 653 }
Chris@111 654 m_ptySlaveFilename = slave;
Chris@111 655 DEBUG << "HgRunner::openTerminal: succeeded, slave is "
Chris@111 656 << m_ptySlaveFilename << endl;
Chris@111 657 #endif
Chris@111 658 }
Chris@111 659
Chris@111 660 void HgRunner::closeTerminal()
Chris@111 661 {
Chris@84 662 #ifndef Q_OS_WIN32
Chris@84 663 if (m_ptySlaveFilename != "") {
Chris@113 664 delete m_ptyFile;
Chris@113 665 m_ptyFile = 0;
Chris@84 666 ::close(m_ptyMasterFd);
Chris@84 667 m_ptySlaveFilename = "";
Chris@84 668 }
Chris@84 669 #endif
jtkorhonen@0 670 }