Git: Use VcsCommand for Grep

* Logs the command in the Version Control pane
* Simplifies the client code

Change-Id: I398d57ab12ed6ba6bab1878934b929083f0bf6cb
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
Orgad Shaneh
2016-02-05 12:50:20 +02:00
committed by Orgad Shaneh
parent 6bf90bb793
commit 36d5bc1e98

View File

@@ -29,20 +29,21 @@
#include <coreplugin/vcsmanager.h> #include <coreplugin/vcsmanager.h>
#include <texteditor/findinfiles.h> #include <texteditor/findinfiles.h>
#include <vcsbase/vcscommand.h>
#include <vcsbase/vcsbaseconstants.h> #include <vcsbase/vcsbaseconstants.h>
#include <utils/filesearch.h> #include <utils/filesearch.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/runextensions.h> #include <utils/runextensions.h>
#include <utils/synchronousprocess.h>
#include <QCheckBox> #include <QCheckBox>
#include <QCoreApplication>
#include <QEventLoop>
#include <QFuture> #include <QFuture>
#include <QFutureWatcher> #include <QFutureWatcher>
#include <QScopedPointer>
#include <QSettings> #include <QSettings>
#include <QTextStream>
using namespace Utils; using namespace Utils;
@@ -50,6 +51,7 @@ namespace Git {
namespace Internal { namespace Internal {
using namespace Core; using namespace Core;
using VcsBase::VcsCommand;
namespace { namespace {
@@ -68,33 +70,33 @@ public:
m_directory = parameters.additionalParameters.toString(); m_directory = parameters.additionalParameters.toString();
} }
void processLine(const QByteArray &line, FileSearchResultList *resultList) const void processLine(const QString &line, FileSearchResultList *resultList) const
{ {
if (line.isEmpty()) if (line.isEmpty())
return; return;
static const char boldRed[] = "\x1b[1;31m"; static const QLatin1String boldRed("\x1b[1;31m");
static const char resetColor[] = "\x1b[m"; static const QLatin1String resetColor("\x1b[m");
FileSearchResult single; FileSearchResult single;
const int lineSeparator = line.indexOf('\0'); const int lineSeparator = line.indexOf(QChar::Null);
single.fileName = m_directory + QLatin1Char('/') single.fileName = m_directory + QLatin1Char('/')
+ QString::fromLocal8Bit(line.left(lineSeparator)); + line.left(lineSeparator);
const int textSeparator = line.indexOf('\0', lineSeparator + 1); const int textSeparator = line.indexOf(QChar::Null, lineSeparator + 1);
single.lineNumber = line.mid(lineSeparator + 1, textSeparator - lineSeparator - 1).toInt(); single.lineNumber = line.mid(lineSeparator + 1, textSeparator - lineSeparator - 1).toInt();
QByteArray text = line.mid(textSeparator + 1); QString text = line.mid(textSeparator + 1);
QVector<QPair<int, int>> matches; QVector<QPair<int, int>> matches;
for (;;) { for (;;) {
const int matchStart = text.indexOf(boldRed); const int matchStart = text.indexOf(boldRed);
if (matchStart == -1) if (matchStart == -1)
break; break;
const int matchTextStart = matchStart + int(sizeof(boldRed)) - 1; const int matchTextStart = matchStart + boldRed.size();
const int matchEnd = text.indexOf(resetColor, matchTextStart); const int matchEnd = text.indexOf(resetColor, matchTextStart);
QTC_ASSERT(matchEnd != -1, break); QTC_ASSERT(matchEnd != -1, break);
const int matchLength = matchEnd - matchTextStart; const int matchLength = matchEnd - matchTextStart;
matches.append(qMakePair(matchStart, matchLength)); matches.append(qMakePair(matchStart, matchLength));
text = text.left(matchStart) + text.mid(matchTextStart, matchLength) text = text.left(matchStart) + text.mid(matchTextStart, matchLength)
+ text.mid(matchEnd + int(sizeof(resetColor)) - 1); + text.mid(matchEnd + resetColor.size());
} }
single.matchingLine = QString::fromLocal8Bit(text); single.matchingLine = text;
foreach (auto match, matches) { foreach (auto match, matches) {
single.matchStart = match.first; single.matchStart = match.first;
single.matchLength = match.second; single.matchLength = match.second;
@@ -102,11 +104,13 @@ public:
} }
} }
void read() void read(const QString &text)
{ {
FileSearchResultList resultList; FileSearchResultList resultList;
while (m_process.canReadLine() && !m_fi.isCanceled()) QString t = text;
processLine(m_process.readLine().trimmed(), &resultList); QTextStream stream(&t);
while (!stream.atEnd() && !m_fi.isCanceled())
processLine(stream.readLine(), &resultList);
if (!resultList.isEmpty()) if (!resultList.isEmpty())
m_fi.reportResult(resultList); m_fi.reportResult(resultList);
} }
@@ -130,27 +134,18 @@ public:
arguments << QLatin1String("-F"); arguments << QLatin1String("-F");
arguments << m_parameters.text; arguments << m_parameters.text;
arguments << QLatin1String("--") << m_parameters.nameFilters; arguments << QLatin1String("--") << m_parameters.nameFilters;
QString args; GitClient *client = GitPlugin::instance()->client();
m_process.addArgs(&args, arguments); QScopedPointer<VcsCommand> command(client->createCommand(m_directory));
m_process.setWorkingDirectory(m_directory); command->addFlags(VcsCommand::SilentOutput);
m_process.setCommand(GitPlugin::instance()->client()->vcsBinary().toString(), args); command->setProgressiveOutput(true);
QFutureWatcher<FileSearchResultList> watcher; QFutureWatcher<FileSearchResultList> watcher;
watcher.setFuture(m_fi.future()); watcher.setFuture(m_fi.future());
connect(&watcher, &QFutureWatcher<FileSearchResultList>::canceled, connect(&watcher, &QFutureWatcher<FileSearchResultList>::canceled,
&m_process, &QtcProcess::kill); command.data(), &VcsCommand::cancel);
connect(&m_process, &QProcess::readyRead, connect(command.data(), &VcsCommand::stdOutText, this, &GitGrepRunner::read);
this, &GitGrepRunner::read); SynchronousProcessResponse resp = command->runCommand(client->vcsBinary(), arguments, 0);
m_process.start(); if (resp.result != SynchronousProcessResponse::Finished)
if (!m_process.waitForStarted()) m_fi.reportCanceled();
return;
QEventLoop eventLoop;
connect(&m_process, static_cast<void(QProcess::*)(int)>(&QProcess::finished),
this, [this, &eventLoop]() {
read();
eventLoop.quit();
});
eventLoop.exec();
m_fi.setProgressValue(1);
} }
static void run(QFutureInterface<FileSearchResultList> &fi, static void run(QFutureInterface<FileSearchResultList> &fi,
@@ -161,7 +156,6 @@ public:
} }
private: private:
QtcProcess m_process;
FutureInterfaceType m_fi; FutureInterfaceType m_fi;
QString m_directory; QString m_directory;
const TextEditor::FileFindParameters &m_parameters; const TextEditor::FileFindParameters &m_parameters;