Files
qt-creator/src/plugins/coreplugin/vcsmanager.cpp

279 lines
8.9 KiB
C++
Raw Normal View History

/**************************************************************************
2008-12-02 12:01:29 +01:00
**
** This file is part of Qt Creator
**
2011-01-11 16:28:15 +01:00
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
2008-12-02 12:01:29 +01:00
**
2011-04-13 08:42:33 +02:00
** Contact: Nokia Corporation (info@qt.nokia.com)
2008-12-02 12:01:29 +01:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
2010-12-17 16:01:08 +01:00
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
2008-12-02 12:01:29 +01:00
**
**************************************************************************/
2008-12-02 15:08:31 +01:00
2008-12-02 12:01:29 +01:00
#include "vcsmanager.h"
#include "iversioncontrol.h"
#include "icore.h"
#include "filemanager.h"
2008-12-02 12:01:29 +01:00
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
2008-12-02 12:01:29 +01:00
#include <QtCore/QString>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QCoreApplication>
#include <QtCore/QFileInfo>
#include <QtGui/QMessageBox>
namespace Core {
typedef QList<IVersionControl *> VersionControlList;
static inline VersionControlList allVersionControls()
{
return ExtensionSystem::PluginManager::instance()->getObjects<IVersionControl>();
}
static const QChar SLASH('/');
// ---- VCSManagerPrivate:
// Maintains a cache of top-level directory->version control.
2010-12-07 17:34:43 +01:00
class VcsManagerPrivate
{
public:
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));
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;
2011-04-08 17:33:14 +02:00
while (tmpDir.count() >= topLevel.count() && tmpDir.count() > 0) {
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;
2008-12-02 12:01:29 +01:00
};
2010-12-07 17:34:43 +01:00
VcsManager::VcsManager(QObject *parent) :
QObject(parent),
2010-12-07 17:34:43 +01:00
m_d(new VcsManagerPrivate)
2008-12-02 12:01:29 +01:00
{
}
// ---- VCSManager:
2010-12-07 17:34:43 +01:00
VcsManager::~VcsManager()
2008-12-02 12:01:29 +01:00
{
delete m_d;
}
2010-12-07 17:34:43 +01:00
void VcsManager::extensionsInitialized()
{
// Change signal connections
FileManager *fileManager = ICore::instance()->fileManager();
foreach (IVersionControl *versionControl, allVersionControls()) {
connect(versionControl, SIGNAL(filesChanged(QStringList)),
fileManager, SIGNAL(filesChangedInternally(QStringList)));
connect(versionControl, SIGNAL(repositoryChanged(QString)),
this, SIGNAL(repositoryChanged(QString)));
}
}
static bool longerThanPath(QPair<QString, IVersionControl *> &pair1, QPair<QString, IVersionControl *> &pair2)
{
return pair1.first.size() > pair2.first.size();
}
2010-12-07 17:34:43 +01:00
IVersionControl* VcsManager::findVersionControlForDirectory(const QString &directory,
QString *topLevelDirectory)
2008-12-02 12:01:29 +01:00
{
if (directory.isEmpty())
return 0;
2011-04-08 17:33:14 +02:00
VcsManagerPrivate::VcsInfo *cachedData = m_d->findInCache(directory);
if (cachedData) {
if (topLevelDirectory)
*topLevelDirectory = cachedData->topLevel;
return cachedData->versionControl;
2008-12-02 12:01:29 +01:00
}
// Nothing: ask the IVersionControls directly.
const VersionControlList versionControls = allVersionControls();
QList<QPair<QString, IVersionControl *> > allThatCanManage;
2008-12-09 11:07:24 +01:00
foreach (IVersionControl * versionControl, versionControls) {
2011-04-08 17:33:14 +02:00
QString topLevel;
if (versionControl->managesDirectory(directory, &topLevel))
allThatCanManage.push_back(qMakePair(topLevel, versionControl));
2008-12-02 12:01:29 +01:00
}
// To properly find a nested repository (say, git checkout inside SVN),
// we need to select the version control with the longest toplevel pathname.
qSort(allThatCanManage.begin(), allThatCanManage.end(), longerThanPath);
if (allThatCanManage.isEmpty()) {
2011-04-08 17:33:14 +02:00
m_d->cache(0, QString(), directory); // register that nothing was found!
// report result;
if (topLevelDirectory)
topLevelDirectory->clear();
return 0;
}
// Register Vcs(s) with the cache
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;
2008-12-02 12:01:29 +01:00
}
2010-12-07 17:34:43 +01:00
bool VcsManager::promptToDelete(const QString &fileName)
2008-12-02 12:01:29 +01:00
{
if (IVersionControl *vc = findVersionControlForDirectory(QFileInfo(fileName).absolutePath()))
return promptToDelete(vc, fileName);
return true;
}
2010-12-07 17:34:43 +01:00
IVersionControl *VcsManager::checkout(const QString &versionControlType,
const QString &directory,
const QByteArray &url)
{
foreach (IVersionControl *versionControl, allVersionControls()) {
if (versionControl->displayName() == versionControlType
&& versionControl->supportsOperation(Core::IVersionControl::CheckoutOperation)) {
if (versionControl->vcsCheckout(directory, url)) {
m_d->cache(versionControl, directory, directory);
return versionControl;
}
return 0;
}
}
return 0;
}
2010-12-07 17:34:43 +01:00
bool VcsManager::findVersionControl(const QString &versionControlType)
{
foreach (IVersionControl * versionControl, allVersionControls()) {
if (versionControl->displayName() == versionControlType)
return true;
}
return false;
}
2010-12-07 17:34:43 +01:00
QString VcsManager::repositoryUrl(const QString &directory)
{
IVersionControl *vc = findVersionControlForDirectory(directory);
if (vc && vc->supportsOperation(Core::IVersionControl::GetRepositoryRootOperation))
return vc->vcsGetRepositoryURL(directory);
return QString();
}
2010-12-07 17:34:43 +01:00
bool VcsManager::promptToDelete(IVersionControl *vc, const QString &fileName)
{
QTC_ASSERT(vc, return true)
if (!vc->supportsOperation(IVersionControl::DeleteOperation))
return true;
2010-12-07 17:34:43 +01:00
const QString title = tr("Version Control");
const QString msg = tr("Would you like to remove this file from the version control system (%1)?\n"
"Note: This might remove the local file.").arg(vc->displayName());
2008-12-02 12:01:29 +01:00
const QMessageBox::StandardButton button =
QMessageBox::question(0, title, msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if (button != QMessageBox::Yes)
return true;
return vc->vcsDelete(fileName);
2008-12-02 12:01:29 +01:00
}
2008-12-02 15:08:31 +01:00
} // namespace Core