forked from qt-creator/qt-creator
VcsCommand: Simplify ProgressParser
Get rid of abstract base ProgressParser and replace it with ProgressParser function. The only one former subclass GitProgressParser is now functor object. Pass future interface directly to the ProgressParser function, along with input text to be parsed. Change-Id: Icbcf0b6e55097f8b38eb8a32ceaa4414723116d0 Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -791,26 +791,19 @@ private:
|
||||
QStringList m_files;
|
||||
};
|
||||
|
||||
class GitProgressParser : public ProgressParser
|
||||
class GitProgressParser
|
||||
{
|
||||
public:
|
||||
static void attachToCommand(VcsCommand *command)
|
||||
{
|
||||
command->setProgressParser(new GitProgressParser);
|
||||
void operator()(QFutureInterface<void> &fi, const QString &inputText) const {
|
||||
const QRegularExpressionMatch match = m_progressExp.match(inputText);
|
||||
if (match.hasMatch()) {
|
||||
fi.setProgressRange(0, match.captured(2).toInt());
|
||||
fi.setProgressValue(match.captured(1).toInt());
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
GitProgressParser() : m_progressExp("\\((\\d+)/(\\d+)\\)") // e.g. Rebasing (7/42)
|
||||
{ }
|
||||
|
||||
void parseProgress(const QString &text) override
|
||||
{
|
||||
const QRegularExpressionMatch match = m_progressExp.match(text);
|
||||
if (match.hasMatch())
|
||||
setProgressAndMaximum(match.captured(1).toInt(), match.captured(2).toInt());
|
||||
}
|
||||
|
||||
const QRegularExpression m_progressExp;
|
||||
const QRegularExpression m_progressExp{"\\((\\d+)/(\\d+)\\)"}; // e.g. Rebasing (7/42)
|
||||
};
|
||||
|
||||
static inline QString msgRepositoryNotFound(const FilePath &dir)
|
||||
@@ -3404,7 +3397,7 @@ VcsCommand *GitClient::vcsExecAbortable(const FilePath &workingDirectory,
|
||||
command->addJob({vcsBinary(), arguments}, isRebase ? 0 : vcsTimeoutS());
|
||||
ConflictHandler::attachToCommand(command, workingDirectory, abortCommand);
|
||||
if (isRebase)
|
||||
GitProgressParser::attachToCommand(command);
|
||||
command->setProgressParser(GitProgressParser());
|
||||
command->start();
|
||||
|
||||
return command;
|
||||
|
@@ -13,16 +13,12 @@
|
||||
#include <utils/globalfilechangeblocker.h>
|
||||
#include <utils/qtcassert.h>
|
||||
#include <utils/qtcprocess.h>
|
||||
#include <utils/runextensions.h>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QFuture>
|
||||
#include <QFutureWatcher>
|
||||
#include <QMutex>
|
||||
#include <QTextCodec>
|
||||
#include <QThread>
|
||||
#include <QVariant>
|
||||
|
||||
#include <numeric>
|
||||
|
||||
/*!
|
||||
\fn void Utils::ProgressParser::parseProgress(const QString &text)
|
||||
@@ -67,8 +63,6 @@ public:
|
||||
m_futureInterface.setProgressRange(0, 1);
|
||||
}
|
||||
|
||||
~VcsCommandPrivate() { delete m_progressParser; }
|
||||
|
||||
Environment environment()
|
||||
{
|
||||
if (!(m_flags & RunFlags::ForceCLocale))
|
||||
@@ -98,7 +92,7 @@ public:
|
||||
const FilePath m_defaultWorkingDirectory;
|
||||
Environment m_environment;
|
||||
QTextCodec *m_codec = nullptr;
|
||||
ProgressParser *m_progressParser = nullptr;
|
||||
ProgressParser m_progressParser = {};
|
||||
QFutureWatcher<void> m_watcher;
|
||||
QList<Job> m_jobs;
|
||||
|
||||
@@ -143,8 +137,6 @@ void VcsCommandPrivate::setup()
|
||||
GlobalFileChangeBlocker::instance()->forceBlocked(true);
|
||||
});
|
||||
}
|
||||
if (m_progressParser)
|
||||
m_progressParser->setFuture(&m_futureInterface);
|
||||
}
|
||||
|
||||
void VcsCommandPrivate::cleanup()
|
||||
@@ -156,8 +148,6 @@ void VcsCommandPrivate::cleanup()
|
||||
GlobalFileChangeBlocker::instance()->forceBlocked(false);
|
||||
});
|
||||
}
|
||||
if (m_progressParser)
|
||||
m_progressParser->setFuture(nullptr);
|
||||
}
|
||||
|
||||
void VcsCommandPrivate::setupProcess(QtcProcess *process, const Job &job)
|
||||
@@ -186,7 +176,7 @@ void VcsCommandPrivate::installStdCallbacks(QtcProcess *process)
|
||||
|| !(m_flags & RunFlags::SuppressStdErr))) {
|
||||
process->setStdErrCallback([this](const QString &text) {
|
||||
if (m_progressParser)
|
||||
m_progressParser->parseProgress(text);
|
||||
m_progressParser(m_futureInterface, text);
|
||||
if (!(m_flags & RunFlags::SuppressStdErr))
|
||||
emit q->appendError(text);
|
||||
if (m_flags & RunFlags::ProgressiveOutput)
|
||||
@@ -198,7 +188,7 @@ void VcsCommandPrivate::installStdCallbacks(QtcProcess *process)
|
||||
|| m_flags & RunFlags::ShowStdOut) {
|
||||
process->setStdOutCallback([this](const QString &text) {
|
||||
if (m_progressParser)
|
||||
m_progressParser->parseProgress(text);
|
||||
m_progressParser(m_futureInterface, text);
|
||||
if (m_flags & RunFlags::ShowStdOut) {
|
||||
if (m_flags & RunFlags::SilentOutput)
|
||||
emit q->appendSilently(text);
|
||||
@@ -415,36 +405,11 @@ void VcsCommand::setCodec(QTextCodec *codec)
|
||||
}
|
||||
|
||||
//! Use \a parser to parse progress data from stdout. Command takes ownership of \a parser
|
||||
void VcsCommand::setProgressParser(ProgressParser *parser)
|
||||
void VcsCommand::setProgressParser(const ProgressParser &parser)
|
||||
{
|
||||
QTC_ASSERT(!d->m_progressParser, return);
|
||||
d->m_progressParser = parser;
|
||||
}
|
||||
|
||||
ProgressParser::ProgressParser() :
|
||||
m_futureMutex(new QMutex)
|
||||
{ }
|
||||
|
||||
ProgressParser::~ProgressParser()
|
||||
{
|
||||
delete m_futureMutex;
|
||||
}
|
||||
|
||||
void ProgressParser::setProgressAndMaximum(int value, int maximum)
|
||||
{
|
||||
QMutexLocker lock(m_futureMutex);
|
||||
if (!m_future)
|
||||
return;
|
||||
m_future->setProgressRange(0, maximum);
|
||||
m_future->setProgressValue(value);
|
||||
}
|
||||
|
||||
void ProgressParser::setFuture(QFutureInterface<void> *future)
|
||||
{
|
||||
QMutexLocker lock(m_futureMutex);
|
||||
m_future = future;
|
||||
}
|
||||
|
||||
CommandResult::CommandResult(const QtcProcess &process)
|
||||
: m_result(process.result())
|
||||
, m_exitCode(process.exitCode())
|
||||
|
@@ -12,7 +12,6 @@
|
||||
#include <QObject>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QMutex;
|
||||
class QVariant;
|
||||
template <typename T>
|
||||
class QFuture;
|
||||
@@ -31,23 +30,7 @@ namespace VcsBase {
|
||||
|
||||
namespace Internal { class VcsCommandPrivate; }
|
||||
|
||||
class VCSBASE_EXPORT ProgressParser
|
||||
{
|
||||
public:
|
||||
ProgressParser();
|
||||
virtual ~ProgressParser();
|
||||
|
||||
protected:
|
||||
virtual void parseProgress(const QString &text) = 0;
|
||||
void setProgressAndMaximum(int value, int maximum);
|
||||
|
||||
private:
|
||||
void setFuture(QFutureInterface<void> *future);
|
||||
|
||||
QFutureInterface<void> *m_future;
|
||||
QMutex *m_futureMutex = nullptr;
|
||||
friend class Internal::VcsCommandPrivate;
|
||||
};
|
||||
using ProgressParser = std::function<void(QFutureInterface<void> &, const QString &)>;
|
||||
|
||||
class VCSBASE_EXPORT CommandResult
|
||||
{
|
||||
@@ -96,7 +79,7 @@ public:
|
||||
|
||||
void setCodec(QTextCodec *codec);
|
||||
|
||||
void setProgressParser(ProgressParser *parser);
|
||||
void setProgressParser(const ProgressParser &parser);
|
||||
|
||||
static CommandResult runBlocking(const Utils::FilePath &workingDirectory,
|
||||
const Utils::Environment &environmentconst,
|
||||
|
Reference in New Issue
Block a user