annotate src/hgrunner.cpp @ 691:5b3bcb2d0943

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