2009-02-25 09:15:00 +01:00
|
|
|
/**************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2011-11-02 15:59:12 +01:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
** GNU Lesser General Public License Usage
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
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.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
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
|
2011-11-02 15:59:12 +01:00
|
|
|
** Nokia at qt-info@nokia.com.
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +01:00
|
|
|
**************************************************************************/
|
2008-12-02 15:08:31 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "vcsmanager.h"
|
|
|
|
|
#include "iversioncontrol.h"
|
2009-12-21 11:08:20 +01:00
|
|
|
#include "icore.h"
|
2012-02-14 16:43:51 +01:00
|
|
|
#include "documentmanager.h"
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2010-01-11 15:22:17 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QList>
|
|
|
|
|
#include <QMap>
|
|
|
|
|
#include <QCoreApplication>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QMessageBox>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
|
2008-12-03 15:04:51 +01:00
|
|
|
typedef QList<IVersionControl *> VersionControlList;
|
|
|
|
|
|
|
|
|
|
static inline VersionControlList allVersionControls()
|
|
|
|
|
{
|
|
|
|
|
return ExtensionSystem::PluginManager::instance()->getObjects<IVersionControl>();
|
|
|
|
|
}
|
|
|
|
|
|
2009-12-08 14:26:41 +01:00
|
|
|
// ---- VCSManagerPrivate:
|
|
|
|
|
// Maintains a cache of top-level directory->version control.
|
|
|
|
|
|
2010-12-07 17:34:43 +01:00
|
|
|
class VcsManagerPrivate
|
|
|
|
|
{
|
|
|
|
|
public:
|
2011-04-08 13:44:48 +02:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-13 15:05:50 +00:00
|
|
|
VcsInfo *findInCache(const QString &dir)
|
2011-04-08 13:44:48 +02:00
|
|
|
{
|
2012-01-12 11:32:45 +01:00
|
|
|
QTC_ASSERT(QDir(dir).isAbsolute(), return 0);
|
|
|
|
|
QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return 0);
|
|
|
|
|
QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return 0);
|
2011-09-13 15:05:50 +00:00
|
|
|
|
|
|
|
|
const QMap<QString, VcsInfo *>::const_iterator it = m_cachedMatches.constFind(dir);
|
2011-04-08 13:44:48 +02:00
|
|
|
if (it != m_cachedMatches.constEnd())
|
|
|
|
|
return it.value();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VcsInfo *findUpInCache(const QString &directory)
|
|
|
|
|
{
|
|
|
|
|
VcsInfo *result = 0;
|
2011-12-22 14:44:14 +01:00
|
|
|
const QChar slash = QLatin1Char('/');
|
2011-04-08 13:44:48 +02:00
|
|
|
// 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).
|
2011-12-22 14:44:14 +01:00
|
|
|
for (int pos = directory.size() - 1; pos >= 0; pos = directory.lastIndexOf(slash, pos) - 1) {
|
2011-04-08 13:44:48 +02:00
|
|
|
const QString directoryPart = directory.left(pos);
|
|
|
|
|
result = findInCache(directoryPart);
|
|
|
|
|
if (result != 0)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-12 14:27:40 +01:00
|
|
|
void resetCache(const QString &dir)
|
2011-09-13 15:05:50 +00:00
|
|
|
{
|
2012-01-12 14:27:40 +01:00
|
|
|
QTC_ASSERT(QDir(dir).isAbsolute(), return);
|
|
|
|
|
QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return);
|
|
|
|
|
QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return);
|
|
|
|
|
|
2011-09-13 15:05:50 +00:00
|
|
|
const QString dirSlash = dir + QLatin1Char('/');
|
|
|
|
|
foreach (const QString &key, m_cachedMatches.keys()) {
|
|
|
|
|
if (key == dir || key.startsWith(dirSlash))
|
|
|
|
|
m_cachedMatches.remove(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-12 14:27:40 +01:00
|
|
|
void cache(IVersionControl *vc, const QString &topLevel, const QString &dir)
|
2011-04-08 13:44:48 +02:00
|
|
|
{
|
2012-01-12 14:27:40 +01:00
|
|
|
QTC_ASSERT(QDir(dir).isAbsolute(), return);
|
|
|
|
|
QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return);
|
|
|
|
|
QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return);
|
2012-01-13 14:26:26 +01:00
|
|
|
QTC_ASSERT(dir.startsWith(topLevel + QLatin1Char('/'))
|
|
|
|
|
|| topLevel == dir || topLevel.isEmpty(), return);
|
|
|
|
|
QTC_ASSERT((topLevel.isEmpty() && !vc) || (!topLevel.isEmpty() && vc), return);
|
2012-01-12 14:27:40 +01:00
|
|
|
|
2011-04-08 13:44:48 +02:00
|
|
|
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);
|
|
|
|
|
|
2011-09-13 15:05:50 +00:00
|
|
|
QString tmpDir = dir;
|
2011-12-22 14:44:14 +01:00
|
|
|
const QChar slash = QLatin1Char('/');
|
2011-04-08 17:33:14 +02:00
|
|
|
while (tmpDir.count() >= topLevel.count() && tmpDir.count() > 0) {
|
2011-04-08 13:44:48 +02:00
|
|
|
m_cachedMatches.insert(tmpDir, newInfo);
|
2011-12-22 14:44:14 +01:00
|
|
|
const int slashPos = tmpDir.lastIndexOf(slash);
|
|
|
|
|
if (slashPos >= 0) {
|
|
|
|
|
tmpDir.truncate(slashPos);
|
|
|
|
|
} else {
|
|
|
|
|
tmpDir.clear();
|
|
|
|
|
}
|
2011-04-08 13:44:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) :
|
2009-11-11 14:32:54 +01:00
|
|
|
QObject(parent),
|
2011-09-07 14:26:11 +02:00
|
|
|
d(new VcsManagerPrivate)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-08 13:44:48 +02:00
|
|
|
// ---- VCSManager:
|
|
|
|
|
|
2010-12-07 17:34:43 +01:00
|
|
|
VcsManager::~VcsManager()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-09-07 14:26:11 +02:00
|
|
|
delete d;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2010-12-07 17:34:43 +01:00
|
|
|
void VcsManager::extensionsInitialized()
|
2009-11-11 14:32:54 +01:00
|
|
|
{
|
|
|
|
|
// Change signal connections
|
|
|
|
|
foreach (IVersionControl *versionControl, allVersionControls()) {
|
|
|
|
|
connect(versionControl, SIGNAL(filesChanged(QStringList)),
|
2012-02-14 16:43:51 +01:00
|
|
|
DocumentManager::instance(), SIGNAL(filesChangedInternally(QStringList)));
|
2009-11-11 14:32:54 +01:00
|
|
|
connect(versionControl, SIGNAL(repositoryChanged(QString)),
|
|
|
|
|
this, SIGNAL(repositoryChanged(QString)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-07 12:26:42 +01:00
|
|
|
static bool longerThanPath(QPair<QString, IVersionControl *> &pair1, QPair<QString, IVersionControl *> &pair2)
|
|
|
|
|
{
|
|
|
|
|
return pair1.first.size() > pair2.first.size();
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-13 15:05:50 +00:00
|
|
|
void VcsManager::resetVersionControlForDirectory(const QString &inputDirectory)
|
|
|
|
|
{
|
|
|
|
|
if (inputDirectory.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QString directory = QDir(inputDirectory).absolutePath();
|
|
|
|
|
|
|
|
|
|
d->resetCache(directory);
|
|
|
|
|
emit repositoryChanged(directory);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-04 10:31:35 +02:00
|
|
|
IVersionControl* VcsManager::findVersionControlForDirectory(const QString &inputDirectory,
|
2009-12-08 14:26:41 +01:00
|
|
|
QString *topLevelDirectory)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2011-12-22 14:44:14 +01:00
|
|
|
typedef QPair<QString, IVersionControl *> StringVersionControlPair;
|
|
|
|
|
typedef QList<StringVersionControlPair> StringVersionControlPairs;
|
2011-05-04 10:31:35 +02:00
|
|
|
if (inputDirectory.isEmpty())
|
2011-04-08 13:44:48 +02:00
|
|
|
return 0;
|
2010-05-20 16:24:39 +02:00
|
|
|
|
2011-05-04 10:31:35 +02:00
|
|
|
// Make sure we a clean absolute path:
|
|
|
|
|
const QString directory = QDir(inputDirectory).absolutePath();
|
|
|
|
|
|
2011-09-07 14:26:11 +02:00
|
|
|
VcsManagerPrivate::VcsInfo *cachedData = d->findInCache(directory);
|
2011-04-08 13:44:48 +02:00
|
|
|
if (cachedData) {
|
2009-12-08 14:26:41 +01:00
|
|
|
if (topLevelDirectory)
|
2011-04-08 13:44:48 +02:00
|
|
|
*topLevelDirectory = cachedData->topLevel;
|
|
|
|
|
return cachedData->versionControl;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-04-08 13:44:48 +02:00
|
|
|
// Nothing: ask the IVersionControls directly.
|
2008-12-03 15:04:51 +01:00
|
|
|
const VersionControlList versionControls = allVersionControls();
|
2011-12-22 14:44:14 +01:00
|
|
|
StringVersionControlPairs allThatCanManage;
|
2010-12-07 12:26:42 +01:00
|
|
|
|
2008-12-09 11:07:24 +01:00
|
|
|
foreach (IVersionControl * versionControl, versionControls) {
|
2011-04-08 17:33:14 +02:00
|
|
|
QString topLevel;
|
2011-04-08 13:44:48 +02:00
|
|
|
if (versionControl->managesDirectory(directory, &topLevel))
|
2011-12-22 14:44:14 +01:00
|
|
|
allThatCanManage.push_back(StringVersionControlPair(topLevel, versionControl));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
2010-12-07 12:26:42 +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);
|
|
|
|
|
|
2011-04-08 13:44:48 +02:00
|
|
|
if (allThatCanManage.isEmpty()) {
|
2011-09-07 14:26:11 +02:00
|
|
|
d->cache(0, QString(), directory); // register that nothing was found!
|
2011-04-08 13:44:48 +02:00
|
|
|
|
|
|
|
|
// report result;
|
2010-12-07 12:26:42 +01:00
|
|
|
if (topLevelDirectory)
|
2011-04-08 13:44:48 +02:00
|
|
|
topLevelDirectory->clear();
|
|
|
|
|
return 0;
|
2010-12-07 12:26:42 +01:00
|
|
|
}
|
2011-04-08 13:44:48 +02:00
|
|
|
|
|
|
|
|
// Register Vcs(s) with the cache
|
|
|
|
|
QString tmpDir = directory;
|
2011-12-22 14:44:14 +01:00
|
|
|
const QChar slash = QLatin1Char('/');
|
|
|
|
|
const StringVersionControlPairs::const_iterator cend = allThatCanManage.constEnd();
|
|
|
|
|
for (StringVersionControlPairs::const_iterator i = allThatCanManage.constBegin(); i != cend; ++i) {
|
2011-09-07 14:26:11 +02:00
|
|
|
d->cache(i->second, i->first, tmpDir);
|
2011-04-08 13:44:48 +02:00
|
|
|
tmpDir = i->first;
|
2011-12-22 14:44:14 +01:00
|
|
|
const int slashPos = tmpDir.lastIndexOf(slash);
|
|
|
|
|
if (slashPos >= 0)
|
|
|
|
|
tmpDir.truncate(slashPos);
|
2011-04-08 13:44:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
{
|
2010-01-11 15:22:17 +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,
|
2010-09-29 12:16:35 +02:00
|
|
|
const QString &directory,
|
|
|
|
|
const QByteArray &url)
|
|
|
|
|
{
|
2010-09-29 12:16:37 +02:00
|
|
|
foreach (IVersionControl *versionControl, allVersionControls()) {
|
|
|
|
|
if (versionControl->displayName() == versionControlType
|
|
|
|
|
&& versionControl->supportsOperation(Core::IVersionControl::CheckoutOperation)) {
|
|
|
|
|
if (versionControl->vcsCheckout(directory, url)) {
|
2011-09-07 14:26:11 +02:00
|
|
|
d->cache(versionControl, directory, directory);
|
2010-09-29 12:16:35 +02:00
|
|
|
return versionControl;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-07 17:34:43 +01:00
|
|
|
bool VcsManager::findVersionControl(const QString &versionControlType)
|
2010-09-29 12:16:35 +02:00
|
|
|
{
|
|
|
|
|
foreach (IVersionControl * versionControl, allVersionControls()) {
|
2010-09-29 12:16:37 +02:00
|
|
|
if (versionControl->displayName() == versionControlType)
|
2010-09-29 12:16:35 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-07 17:34:43 +01:00
|
|
|
QString VcsManager::repositoryUrl(const QString &directory)
|
2010-09-29 12:16:35 +02:00
|
|
|
{
|
|
|
|
|
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)
|
2010-01-11 15:22:17 +01:00
|
|
|
{
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(vc, return true);
|
2010-01-11 15:22:17 +01:00
|
|
|
if (!vc->supportsOperation(IVersionControl::DeleteOperation))
|
2008-12-03 15:04:51 +01:00
|
|
|
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);
|
2008-12-03 15:04:51 +01:00
|
|
|
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
|