Commit 9a4394fa authored by Tim Jenssen's avatar Tim Jenssen Committed by Tobias Hunger

VcsManager: Simplify caching

Use value types in the cache and remove the code used to manage the
pointers.

Change-Id: I3babc69d571de161e161037da7cd731f70d0695d
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent 74c1ebdc
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include <vcsbase/vcsbaseconstants.h> #include <vcsbase/vcsbaseconstants.h>
#include <extensionsystem/pluginmanager.h> #include <extensionsystem/pluginmanager.h>
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/optional.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <QDir> #include <QDir>
...@@ -63,35 +64,29 @@ class VcsManagerPrivate ...@@ -63,35 +64,29 @@ class VcsManagerPrivate
public: public:
class VcsInfo { class VcsInfo {
public: public:
VcsInfo() = default;
VcsInfo(IVersionControl *vc, const QString &tl) : VcsInfo(IVersionControl *vc, const QString &tl) :
versionControl(vc), topLevel(tl) versionControl(vc), topLevel(tl)
{ } { }
VcsInfo(const VcsInfo &other) = default;
bool operator == (const VcsInfo &other) const bool operator == (const VcsInfo &other) const
{ {
return versionControl == other.versionControl && return versionControl == other.versionControl && topLevel == other.topLevel;
topLevel == other.topLevel;
} }
IVersionControl *versionControl; IVersionControl *versionControl = nullptr;
QString topLevel; QString topLevel;
}; };
~VcsManagerPrivate() Utils::optional<VcsInfo> findInCache(const QString &dir)
{ {
qDeleteAll(m_vcsInfoList); QTC_ASSERT(QDir(dir).isAbsolute(), return Utils::nullopt);
} QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return Utils::nullopt);
QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return Utils::nullopt);
VcsInfo *findInCache(const QString &dir)
{
QTC_ASSERT(QDir(dir).isAbsolute(), return nullptr);
QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return nullptr);
QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return nullptr);
const QMap<QString, VcsInfo *>::const_iterator it = m_cachedMatches.constFind(dir); const auto it = m_cachedMatches.constFind(dir);
if (it != m_cachedMatches.constEnd()) return it == m_cachedMatches.constEnd() ? Utils::nullopt : Utils::make_optional(it.value());
return it.value();
return nullptr;
} }
void clearCache() void clearCache()
...@@ -121,24 +116,10 @@ public: ...@@ -121,24 +116,10 @@ public:
|| topLevel == dir || topLevel.isEmpty(), return); || topLevel == dir || topLevel.isEmpty(), return);
QTC_ASSERT((topLevel.isEmpty() && !vc) || (!topLevel.isEmpty() && vc), return); QTC_ASSERT((topLevel.isEmpty() && !vc) || (!topLevel.isEmpty() && vc), return);
VcsInfo *newInfo = new VcsInfo(vc, topLevel);
bool createdNewInfo(true);
// Do we have a matching VcsInfo already?
foreach (VcsInfo *i, m_vcsInfoList) {
if (*i == *newInfo) {
delete newInfo;
newInfo = i;
createdNewInfo = false;
break;
}
}
if (createdNewInfo)
m_vcsInfoList.append(newInfo);
QString tmpDir = dir; QString tmpDir = dir;
const QChar slash = QLatin1Char('/'); const QChar slash = QLatin1Char('/');
while (tmpDir.count() >= topLevel.count() && !tmpDir.isEmpty()) { while (tmpDir.count() >= topLevel.count() && !tmpDir.isEmpty()) {
m_cachedMatches.insert(tmpDir, newInfo); m_cachedMatches.insert(tmpDir, VcsInfo(vc, topLevel));
// if no vc was found, this might mean we're inside a repo internal directory (.git) // if no vc was found, this might mean we're inside a repo internal directory (.git)
// Cache only input directory, not parents // Cache only input directory, not parents
if (!vc) if (!vc)
...@@ -151,8 +132,8 @@ public: ...@@ -151,8 +132,8 @@ public:
} }
} }
QMap<QString, VcsInfo *> m_cachedMatches; QList<IVersionControl *> m_versionControlList;
QList<VcsInfo *> m_vcsInfoList; QMap<QString, VcsInfo> m_cachedMatches;
IVersionControl *m_unconfiguredVcs = nullptr; IVersionControl *m_unconfiguredVcs = nullptr;
QStringList m_cachedAdditionalToolsPaths; QStringList m_cachedAdditionalToolsPaths;
...@@ -240,7 +221,7 @@ IVersionControl* VcsManager::findVersionControlForDirectory(const QString &input ...@@ -240,7 +221,7 @@ IVersionControl* VcsManager::findVersionControlForDirectory(const QString &input
if (directory[0].isLetter() && directory.indexOf(QLatin1Char(':') + QLatin1String(TEST_PREFIX)) == 1) if (directory[0].isLetter() && directory.indexOf(QLatin1Char(':') + QLatin1String(TEST_PREFIX)) == 1)
directory = directory.mid(2); directory = directory.mid(2);
#endif #endif
VcsManagerPrivate::VcsInfo *cachedData = d->findInCache(directory); auto cachedData = d->findInCache(directory);
if (cachedData) { if (cachedData) {
if (topLevelDirectory) if (topLevelDirectory)
*topLevelDirectory = cachedData->topLevel; *topLevelDirectory = cachedData->topLevel;
...@@ -341,9 +322,10 @@ QString VcsManager::findTopLevelForDirectory(const QString &directory) ...@@ -341,9 +322,10 @@ QString VcsManager::findTopLevelForDirectory(const QString &directory)
QStringList VcsManager::repositories(const IVersionControl *vc) QStringList VcsManager::repositories(const IVersionControl *vc)
{ {
QStringList result; QStringList result;
foreach (const VcsManagerPrivate::VcsInfo *vi, d->m_vcsInfoList) for (auto it = d->m_cachedMatches.constBegin(); it != d->m_cachedMatches.constEnd(); ++it) {
if (vi->versionControl == vc) if (it.value().versionControl == vc)
result.push_back(vi->topLevel); result.append(it.value().topLevel);
}
return result; return result;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment