Skip to content
Snippets Groups Projects
Commit d8a179c5 authored by Tobias Hunger's avatar Tobias Hunger
Browse files

VCS: Work better with nested VCS

Are more intelligent at guessing nested VCS. This should improve
git submodule support.

Task-number: QTCREATORBUG-4044
parent 9b121e3d
No related branches found
No related tags found
No related merge requests found
...@@ -53,20 +53,93 @@ enum { debug = 0 }; ...@@ -53,20 +53,93 @@ enum { debug = 0 };
namespace Core { namespace Core {
typedef QList<IVersionControl *> VersionControlList; typedef QList<IVersionControl *> VersionControlList;
typedef QMap<QString, IVersionControl *> VersionControlCache;
static inline VersionControlList allVersionControls() static inline VersionControlList allVersionControls()
{ {
return ExtensionSystem::PluginManager::instance()->getObjects<IVersionControl>(); return ExtensionSystem::PluginManager::instance()->getObjects<IVersionControl>();
} }
static const QChar SLASH('/');
// ---- VCSManagerPrivate: // ---- VCSManagerPrivate:
// Maintains a cache of top-level directory->version control. // Maintains a cache of top-level directory->version control.
class VcsManagerPrivate class VcsManagerPrivate
{ {
public: public:
VersionControlCache m_cachedMatches; class VcsInfo {
public:
VcsInfo(IVersionControl *vc, const QString &tl) :
versionControl(vc), topLevel(tl)
{ }
bool operator == (const VcsInfo &other) const
{
return versionControl == other.versionControl &&
topLevel == other.topLevel;
}
IVersionControl *versionControl;
QString topLevel;
};
~VcsManagerPrivate()
{
qDeleteAll(m_vcsInfoList);
}
VcsInfo *findInCache(const QString &directory)
{
const QMap<QString, VcsInfo *>::const_iterator it = m_cachedMatches.constFind(directory);
if (it != m_cachedMatches.constEnd())
return it.value();
return 0;
}
VcsInfo *findUpInCache(const QString &directory)
{
VcsInfo *result = 0;
// Split the path, trying to find the matching repository. We start from the reverse
// in order to detected nested repositories correctly (say, a git checkout under SVN).
for (int pos = directory.size() - 1; pos >= 0; pos = directory.lastIndexOf(SLASH, pos) - 1) {
const QString directoryPart = directory.left(pos);
result = findInCache(directoryPart);
if (result != 0)
break;
}
return result;
}
void cache(IVersionControl *vc, const QString topLevel, const QString directory)
{
Q_ASSERT(directory.startsWith(topLevel));
qDebug() << "New cache entries:" << vc->displayName() << topLevel << directory;
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 = directory;
while (tmpDir.count() >= topLevel.count()) {
m_cachedMatches.insert(tmpDir, newInfo);
int slashPos = tmpDir.lastIndexOf(SLASH);
tmpDir = slashPos >= 0 ? tmpDir.left(tmpDir.lastIndexOf(SLASH)) : QString();
}
}
QMap<QString, VcsInfo *> m_cachedMatches;
QList<VcsInfo *> m_vcsInfoList;
}; };
VcsManager::VcsManager(QObject *parent) : VcsManager::VcsManager(QObject *parent) :
...@@ -75,6 +148,8 @@ VcsManager::VcsManager(QObject *parent) : ...@@ -75,6 +148,8 @@ VcsManager::VcsManager(QObject *parent) :
{ {
} }
// ---- VCSManager:
VcsManager::~VcsManager() VcsManager::~VcsManager()
{ {
delete m_d; delete m_d;
...@@ -100,91 +175,55 @@ static bool longerThanPath(QPair<QString, IVersionControl *> &pair1, QPair<QStri ...@@ -100,91 +175,55 @@ static bool longerThanPath(QPair<QString, IVersionControl *> &pair1, QPair<QStri
IVersionControl* VcsManager::findVersionControlForDirectory(const QString &directory, IVersionControl* VcsManager::findVersionControlForDirectory(const QString &directory,
QString *topLevelDirectory) QString *topLevelDirectory)
{ {
typedef VersionControlCache::const_iterator VersionControlCacheConstIterator; if (directory.isEmpty())
return 0;
if (debug) {
qDebug(">findVersionControlForDirectory %s topLevelPtr %d",
qPrintable(directory), (topLevelDirectory != 0));
if (debug > 1) {
const VersionControlCacheConstIterator cend = m_d->m_cachedMatches.constEnd();
for (VersionControlCacheConstIterator it = m_d->m_cachedMatches.constBegin(); it != cend; ++it)
qDebug("Cache %s -> '%s'", qPrintable(it.key()), qPrintable(it.value()->displayName()));
}
}
QTC_ASSERT(!directory.isEmpty(), return 0);
const VersionControlCacheConstIterator cacheEnd = m_d->m_cachedMatches.constEnd(); qDebug() << "Searching Vcs for:" << directory;
if (topLevelDirectory) VcsManagerPrivate::VcsInfo * cachedData = m_d->findUpInCache(directory);
topLevelDirectory->clear(); if (cachedData) {
qDebug() << "Found in Cache:" << cachedData->versionControl->displayName() << cachedData->topLevel;
// First check if the directory has an entry, meaning it is a top level
const VersionControlCacheConstIterator fullPathIt = m_d->m_cachedMatches.constFind(directory);
if (fullPathIt != cacheEnd) {
if (topLevelDirectory) if (topLevelDirectory)
*topLevelDirectory = directory; *topLevelDirectory = cachedData->topLevel;
if (debug) return cachedData->versionControl;
qDebug("<findVersionControlForDirectory: full cache match for VCS '%s'", qPrintable(fullPathIt.value()->displayName()));
return fullPathIt.value();
}
// Split the path, trying to find the matching repository. We start from the reverse
// in order to detected nested repositories correctly (say, a git checkout under SVN).
// Note that detection of a nested version control will still fail if the
// above-located version control is detected and entered into the cache first.
// The nested one can then no longer be found due to the splitting of the paths.
int pos = directory.size() - 1;
const QChar slash = QLatin1Char('/');
while (true) {
const int index = directory.lastIndexOf(slash, pos);
if (index <= 0) // Stop at '/' or not found
break;
const QString directoryPart = directory.left(index);
const VersionControlCacheConstIterator it = m_d->m_cachedMatches.constFind(directoryPart);
if (it != cacheEnd) {
if (topLevelDirectory)
*topLevelDirectory = it.key();
if (debug)
qDebug("<findVersionControlForDirectory: cache match for VCS '%s', topLevel: %s",
qPrintable(it.value()->displayName()), qPrintable(it.key()));
return it.value();
}
pos = index - 1;
} }
// Nothing: ask the IVersionControls directly, insert the toplevel into the cache. // Nothing: ask the IVersionControls directly.
const VersionControlList versionControls = allVersionControls(); const VersionControlList versionControls = allVersionControls();
QList<QPair<QString, IVersionControl *> > allThatCanManage; QList<QPair<QString, IVersionControl *> > allThatCanManage;
foreach (IVersionControl * versionControl, versionControls) { foreach (IVersionControl * versionControl, versionControls) {
QString topLevel; QString topLevel = cachedData ? cachedData->topLevel : QString();
if (versionControl->managesDirectory(directory, &topLevel)) { if (versionControl->managesDirectory(directory, &topLevel))
if (debug)
qDebug("<findVersionControlForDirectory: %s manages %s",
qPrintable(versionControl->displayName()),
qPrintable(topLevel));
allThatCanManage.push_back(qMakePair(topLevel, versionControl)); allThatCanManage.push_back(qMakePair(topLevel, versionControl));
}
} }
// To properly find a nested repository (say, git checkout inside SVN), // To properly find a nested repository (say, git checkout inside SVN),
// we need to select the version control with the longest toplevel pathname. // we need to select the version control with the longest toplevel pathname.
qSort(allThatCanManage.begin(), allThatCanManage.end(), longerThanPath); qSort(allThatCanManage.begin(), allThatCanManage.end(), longerThanPath);
if (!allThatCanManage.isEmpty()) { if (allThatCanManage.isEmpty()) {
QString toplevel = allThatCanManage.first().first; m_d->cache(0, cachedData->topLevel, directory); // register that nothing was found!
IVersionControl *versionControl = allThatCanManage.first().second;
m_d->m_cachedMatches.insert(toplevel, versionControl); // report result;
if (topLevelDirectory) if (topLevelDirectory)
*topLevelDirectory = toplevel; topLevelDirectory->clear();
if (debug) return 0;
qDebug("<findVersionControlForDirectory: invocation of '%s' matches: %s",
qPrintable(versionControl->displayName()), qPrintable(toplevel));
return versionControl;
} }
if (debug)
qDebug("<findVersionControlForDirectory: No match for %s", qPrintable(directory)); // Register Vcs(s) with the cache
return 0; QString tmpDir = directory;
for (QList<QPair<QString, IVersionControl *> >::const_iterator i = allThatCanManage.constBegin();
i != allThatCanManage.constEnd(); ++i) {
m_d->cache(i->second, i->first, tmpDir);
tmpDir = i->first;
tmpDir = tmpDir.left(tmpDir.lastIndexOf(SLASH));
}
// return result
if (topLevelDirectory)
*topLevelDirectory = allThatCanManage.first().first;
return allThatCanManage.first().second;
} }
bool VcsManager::promptToDelete(const QString &fileName) bool VcsManager::promptToDelete(const QString &fileName)
...@@ -202,7 +241,7 @@ IVersionControl *VcsManager::checkout(const QString &versionControlType, ...@@ -202,7 +241,7 @@ IVersionControl *VcsManager::checkout(const QString &versionControlType,
if (versionControl->displayName() == versionControlType if (versionControl->displayName() == versionControlType
&& versionControl->supportsOperation(Core::IVersionControl::CheckoutOperation)) { && versionControl->supportsOperation(Core::IVersionControl::CheckoutOperation)) {
if (versionControl->vcsCheckout(directory, url)) { if (versionControl->vcsCheckout(directory, url)) {
m_d->m_cachedMatches.insert(directory, versionControl); m_d->cache(versionControl, directory, directory);
return versionControl; return versionControl;
} }
return 0; return 0;
...@@ -244,14 +283,4 @@ bool VcsManager::promptToDelete(IVersionControl *vc, const QString &fileName) ...@@ -244,14 +283,4 @@ bool VcsManager::promptToDelete(IVersionControl *vc, const QString &fileName)
return vc->vcsDelete(fileName); return vc->vcsDelete(fileName);
} }
CORE_EXPORT QDebug operator<<(QDebug in, const VcsManager &v)
{
QDebug nospace = in.nospace();
const VersionControlCache::const_iterator cend = v.m_d->m_cachedMatches.constEnd();
for (VersionControlCache::const_iterator it = v.m_d->m_cachedMatches.constBegin(); it != cend; ++it)
nospace << "Directory: " << it.key() << ' ' << it.value()->displayName() << '\n';
nospace << '\n';
return in;
}
} // namespace Core } // namespace Core
...@@ -85,8 +85,6 @@ public: ...@@ -85,8 +85,6 @@ public:
bool promptToDelete(const QString &fileName); bool promptToDelete(const QString &fileName);
bool promptToDelete(IVersionControl *versionControl, const QString &fileName); bool promptToDelete(IVersionControl *versionControl, const QString &fileName);
friend CORE_EXPORT QDebug operator<<(QDebug in, const VcsManager &);
signals: signals:
void repositoryChanged(const QString &repository); void repositoryChanged(const QString &repository);
...@@ -94,8 +92,6 @@ private: ...@@ -94,8 +92,6 @@ private:
VcsManagerPrivate *m_d; VcsManagerPrivate *m_d;
}; };
CORE_EXPORT QDebug operator<<(QDebug in, const VcsManager &);
} // namespace Core } // namespace Core
#endif // VCSMANAGER_H #endif // VCSMANAGER_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment