VCS: Use runVcs for Command

* Removes duplication
* Supports all flags accepted by runVcs
* Supports output while running

Change-Id: Id067791760753184cd2516c23634564880ca5db0
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
Orgad Shaneh
2013-08-13 08:37:27 +03:00
committed by Orgad Shaneh
parent cc2610aa71
commit f9c4889a7f
5 changed files with 39 additions and 94 deletions

View File

@@ -689,7 +689,7 @@ public:
m_command(command) m_command(command)
{ {
if (parentCommand) { if (parentCommand) {
parentCommand->setExpectChanges(true); parentCommand->addFlags(VcsBasePlugin::ExpectRepoChanges);
connect(parentCommand, SIGNAL(output(QString)), this, SLOT(readStdOut(QString))); connect(parentCommand, SIGNAL(output(QString)), this, SLOT(readStdOut(QString)));
connect(parentCommand, SIGNAL(errorText(QString)), this, SLOT(readStdErr(QString))); connect(parentCommand, SIGNAL(errorText(QString)), this, SLOT(readStdErr(QString)));
} }
@@ -1246,16 +1246,6 @@ void GitClient::slotBlameRevisionRequested(const QString &source, QString change
blame(fi.absolutePath(), QStringList(), fi.fileName(), change, lineNumber); blame(fi.absolutePath(), QStringList(), fi.fileName(), change, lineNumber);
} }
void GitClient::appendOutput(const QString &text) const
{
outputWindow()->append(text);
}
void GitClient::appendOutputSilently(const QString &text) const
{
outputWindow()->appendSilently(text);
}
QTextCodec *GitClient::getSourceCodec(const QString &file) const QTextCodec *GitClient::getSourceCodec(const QString &file) const
{ {
if (QFileInfo(file).isFile()) if (QFileInfo(file).isFile())
@@ -2233,15 +2223,13 @@ VcsBase::Command *GitClient::createCommand(const QString &workingDirectory,
if (editor) if (editor)
connect(command, SIGNAL(finished(bool,int,QVariant)), editor, SLOT(commandFinishedGotoLine(bool,int,QVariant))); connect(command, SIGNAL(finished(bool,int,QVariant)), editor, SLOT(commandFinishedGotoLine(bool,int,QVariant)));
if (useOutputToWindow) { if (useOutputToWindow) {
command->addFlags(VcsBasePlugin::ShowStdOutInLogWindow);
if (editor) // assume that the commands output is the important thing if (editor) // assume that the commands output is the important thing
connect(command, SIGNAL(output(QString)), this, SLOT(appendOutputSilently(QString))); command->addFlags(VcsBasePlugin::SilentOutput);
else
connect(command, SIGNAL(output(QString)), this, SLOT(appendOutput(QString)));
} else if (editor) { } else if (editor) {
connect(command, SIGNAL(output(QString)), editor, SLOT(setPlainTextFiltered(QString))); connect(command, SIGNAL(output(QString)), editor, SLOT(setPlainTextFiltered(QString)));
} }
connect(command, SIGNAL(errorText(QString)), outputWindow(), SLOT(appendError(QString)));
return command; return command;
} }
@@ -2257,7 +2245,8 @@ VcsBase::Command *GitClient::executeGit(const QString &workingDirectory,
VcsBase::Command *command = createCommand(workingDirectory, editor, useOutputToWindow, editorLineNumber); VcsBase::Command *command = createCommand(workingDirectory, editor, useOutputToWindow, editorLineNumber);
command->addJob(arguments, settings()->intValue(GitSettings::timeoutKey)); command->addJob(arguments, settings()->intValue(GitSettings::timeoutKey));
command->setUnixTerminalDisabled(false); command->setUnixTerminalDisabled(false);
command->setExpectChanges(expectChanges); if (expectChanges)
command->addFlags(VcsBasePlugin::ExpectRepoChanges);
command->execute(); command->execute();
return command; return command;
} }

View File

@@ -328,8 +328,6 @@ public slots:
private slots: private slots:
void slotBlameRevisionRequested(const QString &source, QString change, int lineNumber); void slotBlameRevisionRequested(const QString &source, QString change, int lineNumber);
void appendOutput(const QString &text) const;
void appendOutputSilently(const QString &text) const;
void finishSubmoduleUpdate(); void finishSubmoduleUpdate();
void fetchFinished(const QVariant &cookie); void fetchFinished(const QVariant &cookie);

View File

@@ -28,6 +28,7 @@
****************************************************************************/ ****************************************************************************/
#include "command.h" #include "command.h"
#include "vcsbaseplugin.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h> #include <coreplugin/progressmanager/progressmanager.h>
@@ -70,8 +71,9 @@ public:
QVariant m_cookie; QVariant m_cookie;
bool m_unixTerminalDisabled; bool m_unixTerminalDisabled;
int m_defaultTimeout; int m_defaultTimeout;
bool m_expectChanges; unsigned m_flags;
QTextCodec *m_codec; QTextCodec *m_codec;
const QString m_sshPasswordPrompt;
QList<Job> m_jobs; QList<Job> m_jobs;
@@ -87,8 +89,9 @@ CommandPrivate::CommandPrivate(const QString &binary,
m_environment(environment), m_environment(environment),
m_unixTerminalDisabled(false), m_unixTerminalDisabled(false),
m_defaultTimeout(10), m_defaultTimeout(10),
m_expectChanges(false), m_flags(0),
m_codec(0), m_codec(0),
m_sshPasswordPrompt(VcsBasePlugin::sshPrompt()),
m_lastExecSuccess(false), m_lastExecSuccess(false),
m_lastExecExitCode(-1) m_lastExecExitCode(-1)
{ {
@@ -152,14 +155,14 @@ void Command::setUnixTerminalDisabled(bool e)
d->m_unixTerminalDisabled = e; d->m_unixTerminalDisabled = e;
} }
bool Command::expectChanges() const unsigned Command::flags() const
{ {
return d->m_expectChanges; return d->m_flags;
} }
void Command::setExpectChanges(bool e) void Command::addFlags(unsigned f)
{ {
d->m_expectChanges = e; d->m_flags |= f;
} }
void Command::addJob(const QStringList &arguments) void Command::addJob(const QStringList &arguments)
@@ -200,11 +203,6 @@ int Command::lastExecutionExitCode() const
return d->m_lastExecExitCode; return d->m_lastExecExitCode;
} }
QString Command::msgTimeout(int seconds)
{
return tr("Error: VCS timed out after %1s.").arg(seconds);
}
void Command::run() void Command::run()
{ {
// Check that the binary path is not empty // Check that the binary path is not empty
@@ -213,64 +211,35 @@ void Command::run()
return; return;
} }
const unsigned processFlags = unixTerminalDisabled() ? QString stdOut;
unsigned(Utils::SynchronousProcess::UnixTerminalDisabled) : QString stdErr;
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(); const int count = d->m_jobs.size();
int exitCode = -1; d->m_lastExecExitCode = -1;
bool ok = true; d->m_lastExecSuccess = true;
for (int j = 0; j < count; j++) { for (int j = 0; j < count; j++) {
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; const int timeOutSeconds = d->m_jobs.at(j).timeout;
if (!Utils::SynchronousProcess::readDataFromProcess(*process, Utils::SynchronousProcessResponse resp =
VcsBasePlugin::runVcs(d->m_workingDirectory, d->m_binaryPath,
d->m_jobs.at(j).arguments,
timeOutSeconds >= 0 ? timeOutSeconds * 1000 : -1, timeOutSeconds >= 0 ? timeOutSeconds * 1000 : -1,
&stdOut, &stdErr, false)) { d->m_environment, d->m_sshPasswordPrompt,
Utils::SynchronousProcess::stopProcess(*process); d->m_flags, d->m_codec);
ok = false; stdOut += resp.stdOut;
error += msgTimeout(timeOutSeconds); stdErr += resp.stdErr;
d->m_lastExecExitCode = resp.exitCode;
d->m_lastExecSuccess = resp.result == Utils::SynchronousProcessResponse::Finished;
if (!d->m_lastExecSuccess)
break; break;
} }
error += QString::fromLocal8Bit(stdErr); emit output(stdOut);
exitCode = process->exitCode(); if (!stdErr.isEmpty())
} emit errorText(stdErr);
d->m_lastExecSuccess = ok; emit finished(d->m_lastExecSuccess, d->m_lastExecExitCode, cookie());
d->m_lastExecExitCode = exitCode; if (d->m_lastExecSuccess)
if (ok) {
emit output(Utils::SynchronousProcess::normalizeNewlines(
d->m_codec ? d->m_codec->toUnicode(stdOut)
: QString::fromLocal8Bit(stdOut.constData(), stdOut.size())));
}
if (!error.isEmpty())
emit errorText(error);
emit finished(ok, exitCode, cookie());
if (ok) {
emit success(cookie()); emit success(cookie());
if (d->m_expectChanges)
Core::ICore::vcsManager()->emitRepositoryChanged(d->m_workingDirectory);
}
// As it is used asynchronously, we need to delete ourselves // As it is used asynchronously, we need to delete ourselves
this->deleteLater(); this->deleteLater();

View File

@@ -69,10 +69,8 @@ public:
bool unixTerminalDisabled() const; bool unixTerminalDisabled() const;
void setUnixTerminalDisabled(bool); void setUnixTerminalDisabled(bool);
bool expectChanges() const; unsigned flags() const;
void setExpectChanges(bool); void addFlags(unsigned f);
static QString msgTimeout(int seconds);
const QVariant &cookie() const; const QVariant &cookie() const;
void setCookie(const QVariant &cookie); void setCookie(const QVariant &cookie);

View File

@@ -596,27 +596,18 @@ Command *VcsBaseClient::createCommand(const QString &workingDirectory,
if (editor) if (editor)
d->bindCommandToEditor(cmd, editor); d->bindCommandToEditor(cmd, editor);
if (mode == VcsWindowOutputBind) { if (mode == VcsWindowOutputBind) {
if (editor) { // assume that the commands output is the important thing cmd->addFlags(VcsBasePlugin::ShowStdOutInLogWindow);
connect(cmd, SIGNAL(output(QString)), if (editor) // assume that the commands output is the important thing
::vcsOutputWindow(), SLOT(appendSilently(QString))); cmd->addFlags(VcsBasePlugin::SilentOutput);
} else {
connect(cmd, SIGNAL(output(QString)),
::vcsOutputWindow(), SLOT(append(QString)));
}
} else if (editor) { } else if (editor) {
connect(cmd, SIGNAL(output(QString)), editor, SLOT(setPlainText(QString))); connect(cmd, SIGNAL(output(QString)), editor, SLOT(setPlainText(QString)));
} }
if (::vcsOutputWindow())
connect(cmd, SIGNAL(errorText(QString)),
::vcsOutputWindow(), SLOT(appendError(QString)));
return cmd; return cmd;
} }
void VcsBaseClient::enqueueJob(Command *cmd, const QStringList &args) void VcsBaseClient::enqueueJob(Command *cmd, const QStringList &args)
{ {
const QString binary = QFileInfo(d->m_clientSettings->binaryPath()).baseName();
::vcsOutputWindow()->appendCommand(cmd->workingDirectory(), binary, args);
cmd->addJob(args); cmd->addJob(args);
cmd->execute(); cmd->execute();
} }