Commit 6a72edb2 authored by cerf's avatar cerf Committed by Tobias Hunger
Browse files

vcsbase: throw away VCSJobRunner, replaced by GitCommand



VCSBase::Command was created from GitCommand, it replaces VCSJobRunner.
VCSBase::Command is simpler to use (in VCSBaseClient) and commands
can show up in the progress manager.
From now on, the git plugin can easily rely on vcsbase.

Change-Id: I40593f354249cd94a4c79e90f9bf1362ff3bf5ec
Merge-request: 394
Reviewed-by: default avatarTobias Hunger <tobias.hunger@nokia.com>
Reviewed-on: http://codereview.qt-project.org/6078
parent d74a866b
...@@ -163,7 +163,6 @@ bool BazaarPlugin::initialize(const QStringList &arguments, QString *errorMessag ...@@ -163,7 +163,6 @@ bool BazaarPlugin::initialize(const QStringList &arguments, QString *errorMessag
addAutoReleasedObject(m_optionsPage); addAutoReleasedObject(m_optionsPage);
m_bazaarSettings.readSettings(m_core->settings()); m_bazaarSettings.readSettings(m_core->settings());
connect(m_optionsPage, SIGNAL(settingsChanged()), m_client, SLOT(handleSettingsChanged()));
connect(m_client, SIGNAL(changed(QVariant)), versionControl(), SLOT(changed(QVariant))); connect(m_client, SIGNAL(changed(QVariant)), versionControl(), SLOT(changed(QVariant)));
static const char *describeSlot = SLOT(view(QString,QString)); static const char *describeSlot = SLOT(view(QString,QString));
......
...@@ -33,11 +33,11 @@ ...@@ -33,11 +33,11 @@
#include "mercurialclient.h" #include "mercurialclient.h"
#include "constants.h" #include "constants.h"
#include <vcsbase/command.h>
#include <vcsbase/vcsbaseoutputwindow.h> #include <vcsbase/vcsbaseoutputwindow.h>
#include <vcsbase/vcsbaseplugin.h> #include <vcsbase/vcsbaseplugin.h>
#include <vcsbase/vcsbaseeditor.h> #include <vcsbase/vcsbaseeditor.h>
#include <vcsbase/vcsbaseeditorparameterwidget.h> #include <vcsbase/vcsbaseeditorparameterwidget.h>
#include <vcsbase/vcsjobrunner.h>
#include <utils/synchronousprocess.h> #include <utils/synchronousprocess.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
...@@ -277,12 +277,10 @@ void MercurialClient::incoming(const QString &repositoryRoot, const QString &rep ...@@ -277,12 +277,10 @@ void MercurialClient::incoming(const QString &repositoryRoot, const QString &rep
VCSBase::VCSBaseEditorWidget *editor = createVCSEditor(kind, title, repositoryRoot, VCSBase::VCSBaseEditorWidget *editor = createVCSEditor(kind, title, repositoryRoot,
true, "incoming", id); true, "incoming", id);
VCSBase::Command *cmd = createCommand(repository, editor);
QSharedPointer<VCSBase::VCSJob> job(new VCSBase::VCSJob(repositoryRoot, args, editor));
// Suppress SSH prompting.
if (!repository.isEmpty() && VCSBase::VCSBasePlugin::isSshPromptConfigured()) if (!repository.isEmpty() && VCSBase::VCSBasePlugin::isSshPromptConfigured())
job->setUnixTerminalDisabled(true); cmd->setUnixTerminalDisabled(true);
enqueueJob(job); enqueueJob(cmd, args);
} }
void MercurialClient::outgoing(const QString &repositoryRoot) void MercurialClient::outgoing(const QString &repositoryRoot)
...@@ -297,10 +295,9 @@ void MercurialClient::outgoing(const QString &repositoryRoot) ...@@ -297,10 +295,9 @@ void MercurialClient::outgoing(const QString &repositoryRoot)
VCSBase::VCSBaseEditorWidget *editor = createVCSEditor(kind, title, repositoryRoot, true, VCSBase::VCSBaseEditorWidget *editor = createVCSEditor(kind, title, repositoryRoot, true,
"outgoing", repositoryRoot); "outgoing", repositoryRoot);
QSharedPointer<VCSBase::VCSJob> job(new VCSBase::VCSJob(repositoryRoot, args, editor)); VCSBase::Command *cmd = createCommand(repositoryRoot, editor);
// Suppress SSH prompting cmd->setUnixTerminalDisabled(VCSBase::VCSBasePlugin::isSshPromptConfigured());
job->setUnixTerminalDisabled(VCSBase::VCSBasePlugin::isSshPromptConfigured()); enqueueJob(cmd, args);
enqueueJob(job);
} }
void MercurialClient::annotate(const QString &workingDir, const QString &file, void MercurialClient::annotate(const QString &workingDir, const QString &file,
......
...@@ -166,8 +166,6 @@ bool MercurialPlugin::initialize(const QStringList & /* arguments */, QString * ...@@ -166,8 +166,6 @@ bool MercurialPlugin::initialize(const QStringList & /* arguments */, QString *
addAutoReleasedObject(optionsPage); addAutoReleasedObject(optionsPage);
mercurialSettings.readSettings(core->settings()); mercurialSettings.readSettings(core->settings());
connect(optionsPage, SIGNAL(settingsChanged()), m_client, SLOT(handleSettingsChanged()));
connect(m_client, SIGNAL(changed(QVariant)), versionControl(), SLOT(changed(QVariant))); connect(m_client, SIGNAL(changed(QVariant)), versionControl(), SLOT(changed(QVariant)));
static const char *describeSlot = SLOT(view(QString,QString)); static const char *describeSlot = SLOT(view(QString,QString));
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Brian McGillion & Hugues Delorme
**
** Contact: Nokia Corporation (info@qt.nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at info@qt.nokia.com.
**
**************************************************************************/
#include "command.h"
#include "vcsbaseconstants.h"
#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <extensionsystem/pluginmanager.h>
#include <utils/synchronousprocess.h>
#include <QtCore/QDebug>
#include <QtCore/QProcess>
#include <QtCore/QFuture>
#include <QtCore/QtConcurrentRun>
#include <QtCore/QFileInfo>
#include <QtCore/QCoreApplication>
Q_DECLARE_METATYPE(QVariant)
namespace VCSBase {
static QString msgTermination(int exitCode, const QString &binaryPath, const QStringList &args)
{
QString cmd = QFileInfo(binaryPath).baseName();
if (!args.empty()) {
cmd += QLatin1Char(' ');
cmd += args.front();
}
return exitCode ?
QCoreApplication::translate("VcsCommand", "\n'%1' failed (exit code %2).\n").arg(cmd).arg(exitCode) :
QCoreApplication::translate("VcsCommand", "\n'%1' completed (exit code %2).\n").arg(cmd).arg(exitCode);
}
class CommandPrivate
{
public:
struct Job {
explicit Job(const QStringList &a, int t);
QStringList arguments;
int timeout;
};
CommandPrivate(const QString &binary,
const QString &workingDirectory,
const QProcessEnvironment &environment);
const QString m_binaryPath;
const QString m_workingDirectory;
const QProcessEnvironment m_environment;
QVariant m_cookie;
bool m_unixTerminalDisabled;
int m_defaultTimeout;
QList<Job> m_jobs;
Command::TerminationReportMode m_reportTerminationMode;
bool m_lastExecSuccess;
int m_lastExecExitCode;
};
CommandPrivate::CommandPrivate(const QString &binary,
const QString &workingDirectory,
const QProcessEnvironment &environment) :
m_binaryPath(binary),
m_workingDirectory(workingDirectory),
m_environment(environment),
m_unixTerminalDisabled(false),
m_defaultTimeout(10),
m_reportTerminationMode(Command::NoReport),
m_lastExecSuccess(false),
m_lastExecExitCode(-1)
{
}
CommandPrivate::Job::Job(const QStringList &a, int t) :
arguments(a),
timeout(t)
{
// Finished cookie is emitted via queued slot, needs metatype
static const int qvMetaId = qRegisterMetaType<QVariant>();
Q_UNUSED(qvMetaId)
}
Command::Command(const QString &binary,
const QString &workingDirectory,
const QProcessEnvironment &environment) :
d(new CommandPrivate(binary, workingDirectory, environment))
{
}
Command::~Command()
{
delete d;
}
const QString &Command::binaryPath() const
{
return d->m_binaryPath;
}
const QString &Command::workingDirectory() const
{
return d->m_workingDirectory;
}
const QProcessEnvironment &Command::processEnvironment() const
{
return d->m_environment;
}
Command::TerminationReportMode Command::reportTerminationMode() const
{
return d->m_reportTerminationMode;
}
void Command::setTerminationReportMode(TerminationReportMode m)
{
d->m_reportTerminationMode = m;
}
int Command::defaultTimeout() const
{
return d->m_defaultTimeout;
}
void Command::setDefaultTimeout(int timeout)
{
d->m_defaultTimeout = timeout;
}
bool Command::unixTerminalDisabled() const
{
return d->m_unixTerminalDisabled;
}
void Command::setUnixTerminalDisabled(bool e)
{
d->m_unixTerminalDisabled = e;
}
void Command::addJob(const QStringList &arguments)
{
addJob(arguments, defaultTimeout());
}
void Command::addJob(const QStringList &arguments, int timeout)
{
d->m_jobs.push_back(CommandPrivate::Job(arguments, timeout));
}
void Command::execute()
{
if (Constants::Internal::debug)
qDebug() << "Command::execute" << d->m_workingDirectory << d->m_jobs.size();
d->m_lastExecSuccess = false;
d->m_lastExecExitCode = -1;
if (d->m_jobs.empty())
return;
// For some reason QtConcurrent::run() only works on this
QFuture<void> task = QtConcurrent::run(this, &Command::run);
QString binary = QFileInfo(d->m_binaryPath).baseName();
if (!binary.isEmpty())
binary = binary.replace(0, 1, binary[0].toUpper()); // Upper the first letter
const QString taskName = binary + QLatin1Char(' ') + d->m_jobs.front().arguments.at(0);
Core::ICore::instance()->progressManager()->addTask(task, taskName, binary + QLatin1String(".action"));
}
bool Command::lastExecutionSuccess() const
{
return d->m_lastExecSuccess;
}
int Command::lastExecutionExitCode() const
{
return d->m_lastExecExitCode;
}
QString Command::msgTimeout(int seconds)
{
return tr("Error: VCS timed out after %1s.").arg(seconds);
}
void Command::run()
{
if (Constants::Internal::debug)
qDebug() << "Command::run" << workingDirectory() << d->m_jobs.size()
<< "terminal_disabled" << unixTerminalDisabled();
// Check that the binary path is not empty
if (binaryPath().trimmed().isEmpty()) {
emit errorText(tr("Unable to start process, binary is empty"));
return;
}
const unsigned processFlags = unixTerminalDisabled() ?
unsigned(Utils::SynchronousProcess::UnixTerminalDisabled) :
unsigned(0);
const QSharedPointer<QProcess> process = Utils::SynchronousProcess::createProcess(processFlags);
if (!workingDirectory().isEmpty())
process->setWorkingDirectory(workingDirectory());
process->setProcessEnvironment(processEnvironment());
QByteArray stdOut;
QByteArray stdErr;
QString error;
const int count = d->m_jobs.size();
int exitCode = -1;
bool ok = true;
for (int j = 0; j < count; j++) {
if (Constants::Internal::debug)
qDebug() << "Command::run" << j << '/' << count << d->m_jobs.at(j).arguments;
process->start(binaryPath(), d->m_jobs.at(j).arguments);
if (!process->waitForStarted()) {
ok = false;
error += QString::fromLatin1("Error: \"%1\" could not be started: %2")
.arg(binaryPath(), process->errorString());
break;
}
process->closeWriteChannel();
const int timeOutSeconds = d->m_jobs.at(j).timeout;
if (!Utils::SynchronousProcess::readDataFromProcess(*process, timeOutSeconds * 1000,
&stdOut, &stdErr, false)) {
Utils::SynchronousProcess::stopProcess(*process);
ok = false;
error += msgTimeout(timeOutSeconds);
break;
}
error += QString::fromLocal8Bit(stdErr);
exitCode = process->exitCode();
switch (reportTerminationMode()) {
case NoReport:
break;
case ReportStdout:
stdOut += msgTermination(exitCode, binaryPath(), d->m_jobs.at(j).arguments).toUtf8();
break;
case ReportStderr:
error += msgTermination(exitCode, binaryPath(), d->m_jobs.at(j).arguments);
break;
}
}
// Special hack: Always produce output for diff
if (ok && stdOut.isEmpty() && d->m_jobs.front().arguments.at(0) == QLatin1String("diff")) {
stdOut += "No difference to HEAD";
} else {
// @TODO: Remove, see below
if (ok && d->m_jobs.front().arguments.at(0) == QLatin1String("status"))
removeColorCodes(&stdOut);
}
d->m_lastExecSuccess = ok;
d->m_lastExecExitCode = exitCode;
if (ok && !stdOut.isEmpty())
emit outputData(stdOut);
if (!error.isEmpty())
emit errorText(error);
emit finished(ok, exitCode, cookie());
if (ok)
emit success(cookie());
// As it is used asynchronously, we need to delete ourselves
this->deleteLater();
}
// Clean output from carriage return and ANSI color codes.
// @TODO: Remove once all relevant commands support "--no-color",
//("status" is missing it as of git 1.6.2)
void Command::removeColorCodes(QByteArray *data)
{
// Remove ansi color codes that look like "ESC[<stuff>m"
const QByteArray ansiColorEscape("\033[");
int escapePos = 0;
while (true) {
const int nextEscapePos = data->indexOf(ansiColorEscape, escapePos);
if (nextEscapePos == -1)
break;
const int endEscapePos = data->indexOf('m', nextEscapePos + ansiColorEscape.size());
if (endEscapePos != -1) {
data->remove(nextEscapePos, endEscapePos - nextEscapePos + 1);
escapePos = nextEscapePos;
} else {
escapePos = nextEscapePos + ansiColorEscape.size();
}
}
}
const QVariant &Command::cookie() const
{
return d->m_cookie;
}
void Command::setCookie(const QVariant &cookie)
{
d->m_cookie = cookie;
}
} // namespace VCSBase
...@@ -30,105 +30,76 @@ ...@@ -30,105 +30,76 @@
** **
**************************************************************************/ **************************************************************************/
#ifndef VCSJOBRUNNER_H #ifndef VCSBASE_COMMAND_H
#define VCSJOBRUNNER_H #define VCSBASE_COMMAND_H
#include "vcsbase_global.h" #include "vcsbase_global.h"
#include <QtCore/QThread> #include <QtCore/QObject>
#include <QtCore/QStringList> #include <QtCore/QStringList>
#include <QtCore/QString>
#include <QtCore/QPointer>
#include <QtCore/QSharedPointer>
#include <QtCore/QVariant> #include <QtCore/QVariant>
#include <QtCore/QProcessEnvironment>
QT_BEGIN_NAMESPACE
class QProcess;
QT_END_NAMESPACE
namespace VCSBase { namespace VCSBase {
class VCSBaseEditorWidget;
class VCSJobRunnerPrivate;
class VCSBASE_EXPORT VCSJob : public QObject class VCSBASE_EXPORT Command : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
enum DataEmitMode { // Where to report command termination with exit code if desired
NoDataEmitMode, enum TerminationReportMode { NoReport,
RawDataEmitMode, ReportStdout, // This assumes UTF8
EditorDataEmitMode ReportStderr };
};
Command(const QString &binary,
VCSJob(const QString &workingDir, const QStringList &args, const QString &workingDirectory,
DataEmitMode emitMode = NoDataEmitMode); const QProcessEnvironment &environment);
VCSJob(const QString &workingDir, const QStringList &args, ~Command();
VCSBase::VCSBaseEditorWidget *editor);
void addJob(const QStringList &arguments);
DataEmitMode dataEmitMode() const; void addJob(const QStringList &arguments, int timeout);
VCSBase::VCSBaseEditorWidget *displayEditor() const; void execute();
QStringList arguments() const; bool lastExecutionSuccess() const;
QString workingDirectory() const; int lastExecutionExitCode() const;
const QVariant &cookie() const;
// Clean output from carriage return and ANSI color codes
// Workaround until all relevant commands support "--no-color"
static void removeColorCodes(QByteArray *data);
const QString &binaryPath() const;
const QString &workingDirectory() const;
const QProcessEnvironment &processEnvironment() const;
// Report command termination with exit code
TerminationReportMode reportTerminationMode() const;
void setTerminationReportMode(TerminationReportMode m);
int defaultTimeout() const;
void setDefaultTimeout(int timeout);
// Disable Terminal on UNIX (see VCS SSH handling)
bool unixTerminalDisabled() const; bool unixTerminalDisabled() const;
void setUnixTerminalDisabled(bool);
void setDisplayEditor(VCSBase::VCSBaseEditorWidget *editor); static QString msgTimeout(int seconds);
void setCookie(const QVariant &cookie);
// Disable terminal to suppress SSH prompting
void setUnixTerminalDisabled(bool v);
signals: const QVariant &cookie() const;
void succeeded(const QVariant &cookie); // Use a queued connection void setCookie(const QVariant &cookie);
void rawData(const QByteArray &data);
private: private:
friend class VCSJobRunner;
const QString m_workingDir;
const QStringList m_arguments;
bool m_emitRaw;
QVariant m_cookie;
QPointer<VCSBase::VCSBaseEditorWidget> m_editor; // User might close it
bool m_unixTerminalDisabled;
};
class VCSBASE_EXPORT VCSJobRunner : public QThread
{
Q_OBJECT
public:
VCSJobRunner();
~VCSJobRunner();
void enqueueJob(const QSharedPointer<VCSJob> &job);
void restart();
static QString msgStartFailed(const QString &binary, const QString &why);
static QString msgTimeout(const QString &binary, int timeoutSeconds);
// Set environment for a VCS process to run in locale "C"
static void setProcessEnvironment(QProcess *p);
const QString &binary() const;
void setBinary(const QString &bin);
int timeoutMs() const;
void setTimeoutMs(int msec);
protected:
void run(); void run();
signals: signals:
void commandStarted(const QString &notice); void outputData(const QByteArray &);
void error(const QString &error); void errorText(const QString &);
void output(const QByteArray &output); void finished(bool ok, int exitCode, const QVariant &cookie);
void success(const QVariant &cookie);
private: private:
void task(const QSharedPointer<VCSJob> &job); class CommandPrivate *d;
void stop();
VCSJobRunnerPrivate *d;
}; };
} //namespace VCSBase } //namespace VCSBase
#endif // VCSJOBRUNNER_H #endif // VCSBASE_COMMAND_H
...@@ -29,7 +29,7 @@ HEADERS += vcsbase_global.h \ ...@@ -29,7 +29,7 @@ HEADERS += vcsbase_global.h \
vcsbaseoutputwindow.h \ vcsbaseoutputwindow.h \
cleandialog.h \ cleandialog.h \
vcsbaseoptionspage.h \ vcsbaseoptionspage.h \
vcsjobrunner.h \ command.h \
vcsbaseclient.h \ vcsbaseclient.h \
vcsbaseclientsettings.h \ vcsbaseclientsettings.h \