annotate src/hgrunner.cpp @ 737:4f3a8aa8d384 tip

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