From 50272f21f1d2b6c1df9ca84c5870d77614283bc2 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Fri, 5 Nov 2021 14:06:44 +0100 Subject: [PATCH] Use QtcProcess in ClearCaseSync Get rid of code repetition by making runProcess() a common method. Change-Id: If0b47fba4351119e7373caa250131891b9bb403d Reviewed-by: Orgad Shaneh --- src/plugins/clearcase/clearcasesync.cpp | 102 ++++++++++-------------- 1 file changed, 43 insertions(+), 59 deletions(-) diff --git a/src/plugins/clearcase/clearcasesync.cpp b/src/plugins/clearcase/clearcasesync.cpp index ba678c70217..fff514f5c1c 100644 --- a/src/plugins/clearcase/clearcasesync.cpp +++ b/src/plugins/clearcase/clearcasesync.cpp @@ -28,19 +28,49 @@ #include #include -#include #include #include + #include +#include #ifdef WITH_TESTS #include #include #endif +using namespace Utils; + namespace ClearCase { namespace Internal { +static void runProcess(QFutureInterface &future, + const ClearCaseSettings &settings, + const QStringList &args, + std::function processLine) +{ + const QString viewRoot = ClearCasePlugin::viewData().root; + QtcProcess process; + process.setWorkingDirectory(viewRoot); + process.setCommand({FilePath::fromString(settings.ccBinaryPath), args}); + process.start(); + if (!process.waitForStarted()) + return; + + int processed = 0; + QString buffer; + while (process.waitForReadyRead() && !future.isCanceled()) { + buffer += QString::fromLocal8Bit(process.readAllStandardOutput()); + while (const int index = buffer.indexOf('\n') != -1) { + const QString line = buffer.left(index + 1); + processLine(line, ++processed); + buffer = buffer.mid(index + 1); + } + } + if (!buffer.isEmpty()) + processLine(buffer, ++processed); +} + ClearCaseSync::ClearCaseSync(QSharedPointer statusMap) : m_statusMap(statusMap) { } @@ -132,7 +162,6 @@ void ClearCaseSync::syncSnapshotView(QFutureInterface &future, QStringList int totalFileCount = files.size(); const bool hot = (totalFileCount < 10); - int processed = 0; if (!hot) totalFileCount = settings.totalFiles.value(view, totalFileCount); @@ -159,40 +188,20 @@ void ClearCaseSync::syncSnapshotView(QFutureInterface &future, QStringList // adding 1 for initial sync in which total is not accurate, to prevent finishing // (we don't want it to become green) future.setProgressRange(0, totalFileCount + 1); - QProcess process; - process.setWorkingDirectory(viewRoot); - const QString program = settings.ccBinaryPath; - - process.start(program, args); - if (!process.waitForStarted()) - return; - QString buffer; - while (process.waitForReadyRead() && !future.isCanceled()) { - while (process.state() == QProcess::Running && - process.bytesAvailable() && !future.isCanceled()) - { - const QString line = QString::fromLocal8Bit(process.readLine().constData()); - buffer += line; - if (buffer.endsWith(QLatin1Char('\n')) || process.atEnd()) { - processCleartoolLsLine(viewRootDir, buffer); - buffer.clear(); - future.setProgressValue(qMin(totalFileCount, ++processed)); - } - } - } + int totalProcessed = 0; + runProcess(future, settings, args, [&](const QString &buffer, int processed) { + processCleartoolLsLine(viewRootDir, buffer); + future.setProgressValue(qMin(totalFileCount, processed)); + totalProcessed = processed; + }); if (!future.isCanceled()) { updateStatusForNotManagedFiles(files); future.setProgressValue(totalFileCount + 1); if (!hot) - updateTotalFilesCount(view, settings, processed); + updateTotalFilesCount(view, settings, totalProcessed); } - - if (process.state() == QProcess::Running) - process.kill(); - - process.waitForFinished(); } void ClearCaseSync::processCleartoolLscheckoutLine(const QString &buffer) @@ -210,37 +219,12 @@ void ClearCaseSync::syncDynamicView(QFutureInterface &future, // Always invalidate status for all files invalidateStatusAllFiles(); - QStringList args({"lscheckout", "-avobs", "-me", "-cview", "-s"}); + const QStringList args({"lscheckout", "-avobs", "-me", "-cview", "-s"}); - const QString viewRoot = ClearCasePlugin::viewData().root; - - QProcess process; - process.setWorkingDirectory(viewRoot); - - const QString program = settings.ccBinaryPath; - process.start(program, args); - if (!process.waitForStarted()) - return; - - QString buffer; - int processed = 0; - while (process.waitForReadyRead() && !future.isCanceled()) { - while (process.state() == QProcess::Running && - process.bytesAvailable() && !future.isCanceled()) { - const QString line = QString::fromLocal8Bit(process.readLine().constData()); - buffer += line; - if (buffer.endsWith(QLatin1Char('\n')) || process.atEnd()) { - processCleartoolLscheckoutLine(buffer); - buffer.clear(); - future.setProgressValue(++processed); - } - } - } - - if (process.state() == QProcess::Running) - process.kill(); - - process.waitForFinished(); + runProcess(future, settings, args, [&](const QString &buffer, int processed) { + processCleartoolLscheckoutLine(buffer); + future.setProgressValue(processed); + }); } void ClearCaseSync::run(QFutureInterface &future, QStringList &files)