2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
|
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"
|
2013-06-11 13:59:06 +03:00
|
|
|
#include "idocument.h"
|
|
|
|
|
|
2013-08-29 16:36:42 +02:00
|
|
|
#include <coreplugin/dialogs/addtovcsdialog.h>
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2014-06-16 18:25:52 +04:00
|
|
|
#include <utils/algorithm.h>
|
2022-08-02 09:27:21 +02:00
|
|
|
#include <utils/fileutils.h>
|
2020-06-17 12:23:44 +02:00
|
|
|
#include <utils/infobar.h>
|
2010-01-11 15:22:17 +01:00
|
|
|
#include <utils/qtcassert.h>
|
2020-06-17 12:23:44 +02:00
|
|
|
#include <vcsbase/vcsbaseconstants.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>
|
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
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
#include <optional>
|
|
|
|
|
|
2020-06-26 13:59:38 +02:00
|
|
|
using namespace Utils;
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
2013-12-06 13:38:36 +01:00
|
|
|
#if defined(WITH_TESTS)
|
|
|
|
|
const char TEST_PREFIX[] = "/8E3A9BA0-0B97-40DF-AEC1-2BDF9FC9EDBE/";
|
|
|
|
|
#endif
|
|
|
|
|
|
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:
|
2017-06-28 12:57:47 +02:00
|
|
|
IVersionControl *versionControl = nullptr;
|
2022-10-04 15:13:46 +02:00
|
|
|
FilePath topLevel;
|
2011-04-08 13:44:48 +02:00
|
|
|
};
|
|
|
|
|
|
2022-08-26 10:30:00 +02:00
|
|
|
std::optional<VcsInfo> findInCache(const QString &dir) const
|
2011-04-08 13:44:48 +02:00
|
|
|
{
|
2022-08-26 10:30:00 +02:00
|
|
|
QTC_ASSERT(QDir(dir).isAbsolute(), return std::nullopt);
|
|
|
|
|
QTC_ASSERT(!dir.endsWith(QLatin1Char('/')), return std::nullopt);
|
|
|
|
|
QTC_ASSERT(QDir::fromNativeSeparators(dir) == dir, return std::nullopt);
|
2011-09-13 15:05:50 +00:00
|
|
|
|
2017-06-28 12:57:47 +02:00
|
|
|
const auto it = m_cachedMatches.constFind(dir);
|
2022-08-26 10:30:00 +02:00
|
|
|
return it == m_cachedMatches.constEnd() ? std::nullopt : std::make_optional(it.value());
|
2011-04-08 13:44:48 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-03 18:53:44 +02:00
|
|
|
void clearCache()
|
|
|
|
|
{
|
|
|
|
|
m_cachedMatches.clear();
|
|
|
|
|
}
|
|
|
|
|
|
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('/');
|
2022-05-02 17:25:11 +02:00
|
|
|
const QList<QString> keys = m_cachedMatches.keys();
|
|
|
|
|
for (const QString &key : keys) {
|
2011-09-13 15:05:50 +00:00
|
|
|
if (key == dir || key.startsWith(dirSlash))
|
|
|
|
|
m_cachedMatches.remove(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-04 15:13:46 +02:00
|
|
|
void cache(IVersionControl *vc, const FilePath &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);
|
2022-10-04 15:13:46 +02:00
|
|
|
const QString topLevelString = topLevel.toString();
|
|
|
|
|
QTC_ASSERT(FilePath::fromString(dir).isChildOf(topLevel)
|
|
|
|
|
|| topLevelString == dir || topLevel.isEmpty(), return);
|
2012-01-13 14:26:26 +01:00
|
|
|
QTC_ASSERT((topLevel.isEmpty() && !vc) || (!topLevel.isEmpty() && vc), return);
|
2012-01-12 14:27:40 +01:00
|
|
|
|
2011-09-13 15:05:50 +00:00
|
|
|
QString tmpDir = dir;
|
2011-12-22 14:44:14 +01:00
|
|
|
const QChar slash = QLatin1Char('/');
|
2022-10-04 15:13:46 +02:00
|
|
|
while (tmpDir.count() >= topLevelString.count() && !tmpDir.isEmpty()) {
|
2022-10-04 14:50:32 +02:00
|
|
|
m_cachedMatches.insert(tmpDir, {vc, topLevel});
|
2013-03-11 22:02:29 +02:00
|
|
|
// if no vc was found, this might mean we're inside a repo internal directory (.git)
|
|
|
|
|
// Cache only input directory, not parents
|
|
|
|
|
if (!vc)
|
|
|
|
|
break;
|
2011-12-22 14:44:14 +01:00
|
|
|
const int slashPos = tmpDir.lastIndexOf(slash);
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (slashPos >= 0)
|
2011-12-22 14:44:14 +01:00
|
|
|
tmpDir.truncate(slashPos);
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
else
|
2011-12-22 14:44:14 +01:00
|
|
|
tmpDir.clear();
|
2011-04-08 13:44:48 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 12:57:47 +02:00
|
|
|
QList<IVersionControl *> m_versionControlList;
|
|
|
|
|
QMap<QString, VcsInfo> m_cachedMatches;
|
2017-06-15 13:42:59 +02:00
|
|
|
IVersionControl *m_unconfiguredVcs = nullptr;
|
2014-03-05 11:27:21 +01:00
|
|
|
|
2021-11-10 16:28:53 +01:00
|
|
|
FilePaths m_cachedAdditionalToolsPaths;
|
2017-06-15 13:42:59 +02:00
|
|
|
bool m_cachedAdditionalToolsPathsDirty = true;
|
2008-12-02 12:01:29 +01:00
|
|
|
};
|
|
|
|
|
|
2017-06-15 13:42:59 +02:00
|
|
|
static VcsManagerPrivate *d = nullptr;
|
|
|
|
|
static VcsManager *m_instance = nullptr;
|
2013-06-25 13:37:21 +02:00
|
|
|
|
2010-12-07 17:34:43 +01:00
|
|
|
VcsManager::VcsManager(QObject *parent) :
|
2013-06-25 13:37:21 +02:00
|
|
|
QObject(parent)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2013-08-30 17:13:29 +02:00
|
|
|
m_instance = this;
|
2013-06-25 13:37:21 +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
|
|
|
{
|
2017-06-15 13:42:59 +02:00
|
|
|
m_instance = nullptr;
|
2011-09-07 14:26:11 +02:00
|
|
|
delete d;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2017-06-15 13:37:33 +02:00
|
|
|
void VcsManager::addVersionControl(IVersionControl *vc)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!d->m_versionControlList.contains(vc), return);
|
|
|
|
|
d->m_versionControlList.append(vc);
|
|
|
|
|
}
|
|
|
|
|
|
2013-11-27 15:57:51 +01:00
|
|
|
VcsManager *VcsManager::instance()
|
2013-08-30 17:13:29 +02:00
|
|
|
{
|
|
|
|
|
return m_instance;
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-07 17:34:43 +01:00
|
|
|
void VcsManager::extensionsInitialized()
|
2009-11-11 14:32:54 +01:00
|
|
|
{
|
|
|
|
|
// Change signal connections
|
2022-07-19 19:09:23 +02:00
|
|
|
const QList<IVersionControl *> vcs = versionControls();
|
|
|
|
|
for (IVersionControl *vc : vcs) {
|
|
|
|
|
connect(vc, &IVersionControl::filesChanged, DocumentManager::instance(),
|
|
|
|
|
[](const QStringList &fileNames) {
|
|
|
|
|
DocumentManager::notifyFilesChangedInternally(
|
2022-08-02 09:27:21 +02:00
|
|
|
FileUtils::toFilePathList(fileNames));
|
2022-07-19 19:09:23 +02:00
|
|
|
});
|
|
|
|
|
connect(vc, &IVersionControl::repositoryChanged,
|
2016-02-02 09:10:54 +02:00
|
|
|
m_instance, &VcsManager::repositoryChanged);
|
2022-07-19 19:09:23 +02:00
|
|
|
connect(vc, &IVersionControl::configurationChanged, m_instance, [vc] {
|
|
|
|
|
m_instance->handleConfigurationChanges(vc);
|
|
|
|
|
});
|
2009-11-11 14:32:54 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-15 13:37:33 +02:00
|
|
|
const QList<IVersionControl *> VcsManager::versionControls()
|
2015-01-19 15:36:07 +01:00
|
|
|
{
|
2017-06-15 13:37:33 +02:00
|
|
|
return d->m_versionControlList;
|
2015-01-19 15:36:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IVersionControl *VcsManager::versionControl(Id id)
|
|
|
|
|
{
|
2015-09-04 14:28:22 +02:00
|
|
|
return Utils::findOrDefault(versionControls(), Utils::equal(&Core::IVersionControl::id, id));
|
2015-01-19 15:36:07 +01:00
|
|
|
}
|
|
|
|
|
|
2016-02-15 23:11:14 +02:00
|
|
|
static QString absoluteWithNoTrailingSlash(const QString &directory)
|
|
|
|
|
{
|
|
|
|
|
QString res = QDir(directory).absolutePath();
|
|
|
|
|
if (res.endsWith(QLatin1Char('/')))
|
|
|
|
|
res.chop(1);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-30 09:24:21 +02:00
|
|
|
void VcsManager::resetVersionControlForDirectory(const FilePath &inputDirectory)
|
2011-09-13 15:05:50 +00:00
|
|
|
{
|
|
|
|
|
if (inputDirectory.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2021-07-30 09:24:21 +02:00
|
|
|
const QString directory = absoluteWithNoTrailingSlash(inputDirectory.toString());
|
2011-09-13 15:05:50 +00:00
|
|
|
d->resetCache(directory);
|
2021-07-30 16:46:27 +02:00
|
|
|
emit m_instance->repositoryChanged(FilePath::fromString(directory));
|
2011-09-13 15:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-30 16:46:27 +02:00
|
|
|
IVersionControl* VcsManager::findVersionControlForDirectory(const FilePath &inputDirectory,
|
2022-10-04 12:54:55 +02:00
|
|
|
FilePath *topLevelDirectory)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2022-10-04 15:13:46 +02:00
|
|
|
using FilePathVersionControlPair = QPair<FilePath, IVersionControl *>;
|
|
|
|
|
using FilePathVersionControlPairs = QList<FilePathVersionControlPair>;
|
2013-11-28 14:20:46 +01:00
|
|
|
if (inputDirectory.isEmpty()) {
|
|
|
|
|
if (topLevelDirectory)
|
|
|
|
|
topLevelDirectory->clear();
|
2017-06-15 13:42:59 +02:00
|
|
|
return nullptr;
|
2013-11-28 14:20:46 +01:00
|
|
|
}
|
2010-05-20 16:24:39 +02:00
|
|
|
|
2013-01-30 11:28:47 +01:00
|
|
|
// Make sure we an absolute path:
|
2021-07-30 16:46:27 +02:00
|
|
|
QString directory = absoluteWithNoTrailingSlash(inputDirectory.toString());
|
2014-02-21 15:26:11 +01:00
|
|
|
#ifdef WITH_TESTS
|
2022-09-21 12:57:00 +02:00
|
|
|
if (!directory.isEmpty() && directory[0].isLetter()
|
|
|
|
|
&& directory.indexOf(QLatin1Char(':') + QLatin1String(TEST_PREFIX)) == 1) {
|
2014-02-21 15:26:11 +01:00
|
|
|
directory = directory.mid(2);
|
2022-09-21 12:57:00 +02:00
|
|
|
}
|
2014-02-21 15:26:11 +01:00
|
|
|
#endif
|
2017-06-28 12:57:47 +02:00
|
|
|
auto cachedData = d->findInCache(directory);
|
2011-04-08 13:44:48 +02:00
|
|
|
if (cachedData) {
|
2009-12-08 14:26:41 +01:00
|
|
|
if (topLevelDirectory)
|
2022-10-04 15:13:46 +02:00
|
|
|
*topLevelDirectory = cachedData->topLevel;
|
2011-04-08 13:44:48 +02:00
|
|
|
return cachedData->versionControl;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2011-04-08 13:44:48 +02:00
|
|
|
// Nothing: ask the IVersionControls directly.
|
2022-10-04 15:13:46 +02:00
|
|
|
FilePathVersionControlPairs allThatCanManage;
|
2010-12-07 12:26:42 +01:00
|
|
|
|
2022-05-02 17:25:11 +02:00
|
|
|
const QList<IVersionControl *> versionControlList = versionControls();
|
|
|
|
|
for (IVersionControl *versionControl : versionControlList) {
|
2021-07-29 09:31:09 +02:00
|
|
|
FilePath topLevel;
|
|
|
|
|
if (versionControl->managesDirectory(FilePath::fromString(directory), &topLevel))
|
2022-10-04 15:13:46 +02:00
|
|
|
allThatCanManage.push_back({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.
|
2022-10-04 15:13:46 +02:00
|
|
|
Utils::sort(allThatCanManage, [](const FilePathVersionControlPair &l,
|
|
|
|
|
const FilePathVersionControlPair &r) {
|
|
|
|
|
return l.first.toString().size() > r.first.toString().size();
|
2014-06-16 18:25:52 +04:00
|
|
|
});
|
2010-12-07 12:26:42 +01:00
|
|
|
|
2011-04-08 13:44:48 +02:00
|
|
|
if (allThatCanManage.isEmpty()) {
|
2022-10-04 15:13:46 +02:00
|
|
|
d->cache(nullptr, {}, 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();
|
2017-06-15 13:42:59 +02:00
|
|
|
return nullptr;
|
2010-12-07 12:26:42 +01:00
|
|
|
}
|
2011-04-08 13:44:48 +02:00
|
|
|
|
|
|
|
|
// Register Vcs(s) with the cache
|
2016-02-15 23:11:14 +02:00
|
|
|
QString tmpDir = absoluteWithNoTrailingSlash(directory);
|
2013-12-06 13:38:36 +01:00
|
|
|
#if defined WITH_TESTS
|
|
|
|
|
// Force caching of test directories (even though they do not exist):
|
|
|
|
|
if (directory.startsWith(QLatin1String(TEST_PREFIX)))
|
|
|
|
|
tmpDir = directory;
|
|
|
|
|
#endif
|
2013-10-11 16:10:08 +03:00
|
|
|
// directory might refer to a historical directory which doesn't exist.
|
|
|
|
|
// In this case, don't cache it.
|
|
|
|
|
if (!tmpDir.isEmpty()) {
|
|
|
|
|
const QChar slash = QLatin1Char('/');
|
2022-10-04 15:13:46 +02:00
|
|
|
for (auto i = allThatCanManage.constBegin(); i != allThatCanManage.constEnd(); ++i) {
|
|
|
|
|
const QString firstString = i->first.toString();
|
2013-10-30 11:21:20 +02:00
|
|
|
// If topLevel was already cached for another VC, skip this one
|
2022-10-04 15:13:46 +02:00
|
|
|
if (tmpDir.count() < firstString.count())
|
2013-10-30 11:21:20 +02:00
|
|
|
continue;
|
2013-10-11 16:10:08 +03:00
|
|
|
d->cache(i->second, i->first, tmpDir);
|
2022-10-04 15:13:46 +02:00
|
|
|
tmpDir = firstString;
|
2013-10-11 16:10:08 +03: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)
|
2022-10-04 15:13:46 +02:00
|
|
|
*topLevelDirectory = allThatCanManage.first().first;
|
2013-06-11 13:59:06 +03:00
|
|
|
IVersionControl *versionControl = allThatCanManage.first().second;
|
2013-06-13 20:40:18 +03:00
|
|
|
const bool isVcsConfigured = versionControl->isConfigured();
|
|
|
|
|
if (!isVcsConfigured || d->m_unconfiguredVcs) {
|
|
|
|
|
Id vcsWarning("VcsNotConfiguredWarning");
|
2013-07-09 11:52:44 +02:00
|
|
|
IDocument *curDocument = EditorManager::currentDocument();
|
2013-06-13 20:40:18 +03:00
|
|
|
if (isVcsConfigured) {
|
|
|
|
|
if (curDocument && d->m_unconfiguredVcs == versionControl) {
|
|
|
|
|
curDocument->infoBar()->removeInfo(vcsWarning);
|
2017-06-15 13:42:59 +02:00
|
|
|
d->m_unconfiguredVcs = nullptr;
|
2013-06-11 13:59:06 +03:00
|
|
|
}
|
2013-06-13 20:40:18 +03:00
|
|
|
return versionControl;
|
|
|
|
|
} else {
|
2020-06-17 12:23:44 +02:00
|
|
|
Utils::InfoBar *infoBar = curDocument ? curDocument->infoBar() : nullptr;
|
2013-06-26 14:02:45 +02:00
|
|
|
if (infoBar && infoBar->canInfoBeAdded(vcsWarning)) {
|
2020-06-17 12:23:44 +02:00
|
|
|
Utils::InfoBarEntry info(vcsWarning,
|
|
|
|
|
tr("%1 repository was detected but %1 is not configured.")
|
|
|
|
|
.arg(versionControl->displayName()),
|
|
|
|
|
Utils::InfoBarEntry::GlobalSuppression::Enabled);
|
2013-06-13 20:40:18 +03:00
|
|
|
d->m_unconfiguredVcs = versionControl;
|
2022-01-17 16:47:31 +01:00
|
|
|
info.addCustomButton(ICore::msgShowOptionsDialog(), []() {
|
2014-09-03 10:34:52 +02:00
|
|
|
QTC_ASSERT(d->m_unconfiguredVcs, return);
|
2015-02-23 11:07:38 +01:00
|
|
|
ICore::showOptionsDialog(d->m_unconfiguredVcs->id());
|
2014-09-03 10:34:52 +02:00
|
|
|
});
|
|
|
|
|
|
2013-06-13 20:40:18 +03:00
|
|
|
infoBar->addInfo(info);
|
|
|
|
|
}
|
2017-06-15 13:42:59 +02:00
|
|
|
return nullptr;
|
2013-06-11 13:59:06 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return versionControl;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-30 16:46:27 +02:00
|
|
|
FilePath VcsManager::findTopLevelForDirectory(const FilePath &directory)
|
2014-07-02 22:52:53 +03:00
|
|
|
{
|
2022-10-04 12:54:55 +02:00
|
|
|
FilePath result;
|
2014-07-02 22:52:53 +03:00
|
|
|
findVersionControlForDirectory(directory, &result);
|
2022-10-04 12:54:55 +02:00
|
|
|
return result;
|
2014-07-02 22:52:53 +03:00
|
|
|
}
|
|
|
|
|
|
2022-10-04 15:13:46 +02:00
|
|
|
FilePaths VcsManager::repositories(const IVersionControl *versionControl)
|
2012-04-20 17:47:37 +02:00
|
|
|
{
|
2022-10-04 15:13:46 +02:00
|
|
|
FilePaths result;
|
2017-06-28 12:57:47 +02:00
|
|
|
for (auto it = d->m_cachedMatches.constBegin(); it != d->m_cachedMatches.constEnd(); ++it) {
|
2022-10-04 15:13:46 +02:00
|
|
|
if (it.value().versionControl == versionControl)
|
2017-06-28 12:57:47 +02:00
|
|
|
result.append(it.value().topLevel);
|
|
|
|
|
}
|
2012-04-20 17:47:37 +02:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 17:51:23 +02:00
|
|
|
bool VcsManager::promptToDelete(IVersionControl *versionControl, const QString &fileName)
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2020-09-03 17:51:23 +02:00
|
|
|
return promptToDelete(versionControl, {Utils::FilePath::fromString(fileName)}).isEmpty();
|
2010-01-11 15:22:17 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-03 17:51:23 +02:00
|
|
|
FilePaths VcsManager::promptToDelete(const FilePaths &filePaths)
|
2010-01-11 15:22:17 +01:00
|
|
|
{
|
2020-09-03 17:51:23 +02:00
|
|
|
// Categorize files by their parent directory, so we won't call
|
|
|
|
|
// findVersionControlForDirectory() more often than necessary.
|
|
|
|
|
QMap<FilePath, FilePaths> filesByParentDir;
|
2021-06-08 12:54:22 +02:00
|
|
|
for (const FilePath &fp : filePaths)
|
|
|
|
|
filesByParentDir[fp.absolutePath()].append(fp);
|
2020-09-03 17:51:23 +02:00
|
|
|
|
|
|
|
|
// Categorize by version control system.
|
2020-11-17 00:23:17 +01:00
|
|
|
QHash<IVersionControl *, FilePaths> filesByVersionControl;
|
2020-09-03 17:51:23 +02:00
|
|
|
for (auto it = filesByParentDir.cbegin(); it != filesByParentDir.cend(); ++it) {
|
2021-07-30 16:46:27 +02:00
|
|
|
IVersionControl * const vc = findVersionControlForDirectory(it.key());
|
2020-09-03 17:51:23 +02:00
|
|
|
if (vc)
|
|
|
|
|
filesByVersionControl[vc] << it.value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove the files.
|
|
|
|
|
FilePaths failedFiles;
|
|
|
|
|
for (auto it = filesByVersionControl.cbegin(); it != filesByVersionControl.cend(); ++it)
|
|
|
|
|
failedFiles << promptToDelete(it.key(), it.value());
|
|
|
|
|
|
|
|
|
|
return failedFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePaths VcsManager::promptToDelete(IVersionControl *vc, const FilePaths &filePaths)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(vc, return {});
|
2010-01-11 15:22:17 +01:00
|
|
|
if (!vc->supportsOperation(IVersionControl::DeleteOperation))
|
2020-09-03 17:51:23 +02:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
const QString fileListForUi = "<ul><li>" + transform(filePaths, [](const FilePath &fp) {
|
|
|
|
|
return fp.toUserOutput();
|
|
|
|
|
}).join("</li><li>") + "</li></ul>";
|
2010-12-07 17:34:43 +01:00
|
|
|
const QString title = tr("Version Control");
|
2021-03-18 11:27:33 +01:00
|
|
|
const QString msg = tr("Remove the following files from the version control system (%2)?"
|
2020-09-03 17:51:23 +02:00
|
|
|
"%1Note: This might remove the local file.").arg(fileListForUi, vc->displayName());
|
2008-12-02 12:01:29 +01:00
|
|
|
const QMessageBox::StandardButton button =
|
2014-03-11 18:09:23 +01:00
|
|
|
QMessageBox::question(ICore::dialogParent(), title, msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
|
2008-12-03 15:04:51 +01:00
|
|
|
if (button != QMessageBox::Yes)
|
2020-09-03 17:51:23 +02:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
FilePaths failedFiles;
|
|
|
|
|
for (const FilePath &fp : filePaths) {
|
2021-07-29 09:31:09 +02:00
|
|
|
if (!vc->vcsDelete(fp))
|
2020-09-03 17:51:23 +02:00
|
|
|
failedFiles << fp;
|
|
|
|
|
}
|
|
|
|
|
return failedFiles;
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2012-07-27 15:43:00 +02:00
|
|
|
QString VcsManager::msgAddToVcsTitle()
|
|
|
|
|
{
|
|
|
|
|
return tr("Add to Version Control");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString VcsManager::msgPromptToAddToVcs(const QStringList &files, const IVersionControl *vc)
|
|
|
|
|
{
|
|
|
|
|
return files.size() == 1
|
|
|
|
|
? tr("Add the file\n%1\nto version control (%2)?")
|
|
|
|
|
.arg(files.front(), vc->displayName())
|
|
|
|
|
: tr("Add the files\n%1\nto version control (%2)?")
|
|
|
|
|
.arg(files.join(QString(QLatin1Char('\n'))), vc->displayName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString VcsManager::msgAddToVcsFailedTitle()
|
|
|
|
|
{
|
|
|
|
|
return tr("Adding to Version Control Failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString VcsManager::msgToAddToVcsFailed(const QStringList &files, const IVersionControl *vc)
|
|
|
|
|
{
|
|
|
|
|
return files.size() == 1
|
2015-03-03 13:45:59 -08:00
|
|
|
? tr("Could not add the file\n%1\nto version control (%2)\n")
|
|
|
|
|
.arg(files.front(), vc->displayName())
|
2012-07-27 15:43:00 +02:00
|
|
|
: tr("Could not add the following files to version control (%1)\n%2")
|
|
|
|
|
.arg(vc->displayName(), files.join(QString(QLatin1Char('\n'))));
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-10 16:28:53 +01:00
|
|
|
FilePaths VcsManager::additionalToolsPath()
|
2014-03-05 11:27:21 +01:00
|
|
|
{
|
|
|
|
|
if (d->m_cachedAdditionalToolsPathsDirty) {
|
|
|
|
|
d->m_cachedAdditionalToolsPaths.clear();
|
2021-11-10 16:28:53 +01:00
|
|
|
for (IVersionControl *vc : versionControls())
|
2014-03-05 11:27:21 +01:00
|
|
|
d->m_cachedAdditionalToolsPaths.append(vc->additionalToolsPath());
|
|
|
|
|
d->m_cachedAdditionalToolsPathsDirty = false;
|
|
|
|
|
}
|
|
|
|
|
return d->m_cachedAdditionalToolsPaths;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 15:53:08 +02:00
|
|
|
void VcsManager::promptToAdd(const FilePath &directory, const FilePaths &filePaths)
|
2012-07-18 13:41:13 +04:00
|
|
|
{
|
2021-09-28 15:53:08 +02:00
|
|
|
IVersionControl *vc = findVersionControlForDirectory(directory);
|
2014-11-16 10:52:41 +02:00
|
|
|
if (!vc || !vc->supportsOperation(IVersionControl::AddOperation))
|
2012-07-18 13:41:13 +04:00
|
|
|
return;
|
|
|
|
|
|
2021-09-28 15:53:08 +02:00
|
|
|
const FilePaths unmanagedFiles = vc->unmanagedFiles(filePaths);
|
2013-10-02 00:18:39 +03:00
|
|
|
if (unmanagedFiles.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
2020-06-02 09:10:40 +02:00
|
|
|
Internal::AddToVcsDialog dlg(ICore::dialogParent(), VcsManager::msgAddToVcsTitle(),
|
2013-10-02 00:18:39 +03:00
|
|
|
unmanagedFiles, vc->displayName());
|
2013-06-14 00:03:27 +02:00
|
|
|
if (dlg.exec() == QDialog::Accepted) {
|
2012-07-18 13:41:13 +04:00
|
|
|
QStringList notAddedToVc;
|
2021-07-29 09:31:09 +02:00
|
|
|
for (const FilePath &file : unmanagedFiles) {
|
2021-09-28 15:53:08 +02:00
|
|
|
if (!vc->vcsAdd(directory.resolvePath(file)))
|
2021-07-29 09:31:09 +02:00
|
|
|
notAddedToVc << file.toUserOutput();
|
2012-07-18 13:41:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!notAddedToVc.isEmpty()) {
|
2020-06-02 09:10:40 +02:00
|
|
|
QMessageBox::warning(ICore::dialogParent(),
|
|
|
|
|
VcsManager::msgAddToVcsFailedTitle(),
|
2012-07-27 15:43:00 +02:00
|
|
|
VcsManager::msgToAddToVcsFailed(notAddedToVc, vc));
|
2012-07-18 13:41:13 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 10:02:58 +02:00
|
|
|
void VcsManager::emitRepositoryChanged(const FilePath &repository)
|
2013-06-09 06:00:29 +03:00
|
|
|
{
|
2021-08-11 10:02:58 +02:00
|
|
|
emit m_instance->repositoryChanged(repository);
|
2013-06-09 06:00:29 +03:00
|
|
|
}
|
|
|
|
|
|
2013-06-03 18:53:44 +02:00
|
|
|
void VcsManager::clearVersionControlCache()
|
|
|
|
|
{
|
2022-05-02 17:25:11 +02:00
|
|
|
const QStringList repoList = d->m_cachedMatches.keys();
|
2013-06-03 18:53:44 +02:00
|
|
|
d->clearCache();
|
2022-05-02 17:25:11 +02:00
|
|
|
for (const QString &repo : repoList)
|
2021-07-30 16:46:27 +02:00
|
|
|
emit m_instance->repositoryChanged(FilePath::fromString(repo));
|
2013-06-03 18:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-19 19:09:23 +02:00
|
|
|
void VcsManager::handleConfigurationChanges(IVersionControl *vc)
|
2014-03-05 11:27:21 +01:00
|
|
|
{
|
|
|
|
|
d->m_cachedAdditionalToolsPathsDirty = true;
|
2022-07-19 19:09:23 +02:00
|
|
|
emit configurationChanged(vc);
|
2014-03-05 11:27:21 +01:00
|
|
|
}
|
|
|
|
|
|
2008-12-02 15:08:31 +01:00
|
|
|
} // namespace Core
|
2013-12-06 13:38:36 +01:00
|
|
|
|
|
|
|
|
#if defined(WITH_TESTS)
|
|
|
|
|
|
|
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
|
|
#include "coreplugin.h"
|
|
|
|
|
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
|
|
|
|
|
namespace Core {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
const char ID_VCS_A[] = "A";
|
|
|
|
|
const char ID_VCS_B[] = "B";
|
|
|
|
|
|
2021-07-29 09:31:09 +02:00
|
|
|
using FileHash = QHash<FilePath, FilePath>;
|
2013-12-06 13:38:36 +01:00
|
|
|
|
|
|
|
|
static FileHash makeHash(const QStringList &list)
|
|
|
|
|
{
|
|
|
|
|
FileHash result;
|
2021-07-29 09:31:09 +02:00
|
|
|
for (const QString &i : list) {
|
2013-12-06 13:38:36 +01:00
|
|
|
QStringList parts = i.split(QLatin1Char(':'));
|
|
|
|
|
QTC_ASSERT(parts.count() == 2, continue);
|
2021-07-29 09:31:09 +02:00
|
|
|
result.insert(FilePath::fromString(QString::fromLatin1(TEST_PREFIX) + parts.at(0)),
|
|
|
|
|
FilePath::fromString(QString::fromLatin1(TEST_PREFIX) + parts.at(1)));
|
2013-12-06 13:38:36 +01:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QString makeString(const QString &s)
|
|
|
|
|
{
|
|
|
|
|
if (s.isEmpty())
|
|
|
|
|
return QString();
|
|
|
|
|
return QString::fromLatin1(TEST_PREFIX) + s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CorePlugin::testVcsManager_data()
|
|
|
|
|
{
|
|
|
|
|
// avoid conflicts with real files and directories:
|
|
|
|
|
|
|
|
|
|
QTest::addColumn<QStringList>("dirsVcsA"); // <directory>:<toplevel>
|
|
|
|
|
QTest::addColumn<QStringList>("dirsVcsB"); // <directory>:<toplevel>
|
|
|
|
|
// <directory>:<toplevel>:<vcsid>:<- from cache, * from VCS>
|
|
|
|
|
QTest::addColumn<QStringList>("results");
|
|
|
|
|
|
|
|
|
|
QTest::newRow("A and B next to each other")
|
2017-02-22 15:09:35 +01:00
|
|
|
<< QStringList({"a:a", "a/1:a", "a/2:a", "a/2/5:a", "a/2/5/6:a"})
|
|
|
|
|
<< QStringList({"b:b", "b/3:b", "b/4:b"})
|
|
|
|
|
<< QStringList({":::-", // empty directory to look up
|
|
|
|
|
"c:::*", // Neither in A nor B
|
|
|
|
|
"a:a:A:*", // in A
|
|
|
|
|
"b:b:B:*", // in B
|
|
|
|
|
"b/3:b:B:*", // in B
|
|
|
|
|
"b/4:b:B:*", // in B
|
|
|
|
|
"a/1:a:A:*", // in A
|
|
|
|
|
"a/2:a:A:*", // in A
|
|
|
|
|
":::-", // empty directory to look up
|
|
|
|
|
"a/2/5/6:a:A:*", // in A
|
|
|
|
|
"a/2/5:a:A:-", // in A (cached from before!)
|
|
|
|
|
// repeat: These need to come from the cache now:
|
|
|
|
|
"c:::-", // Neither in A nor B
|
|
|
|
|
"a:a:A:-", // in A
|
|
|
|
|
"b:b:B:-", // in B
|
|
|
|
|
"b/3:b:B:-", // in B
|
|
|
|
|
"b/4:b:B:-", // in B
|
|
|
|
|
"a/1:a:A:-", // in A
|
|
|
|
|
"a/2:a:A:-", // in A
|
|
|
|
|
"a/2/5/6:a:A:-", // in A
|
|
|
|
|
"a/2/5:a:A:-" // in A
|
2017-02-09 14:00:07 +01:00
|
|
|
});
|
2013-12-06 13:38:36 +01:00
|
|
|
QTest::newRow("B in A")
|
2017-02-22 15:09:35 +01:00
|
|
|
<< QStringList({"a:a", "a/1:a", "a/2:a", "a/2/5:a", "a/2/5/6:a"})
|
|
|
|
|
<< QStringList({"a/1/b:a/1/b", "a/1/b/3:a/1/b", "a/1/b/4:a/1/b", "a/1/b/3/5:a/1/b",
|
|
|
|
|
"a/1/b/3/5/6:a/1/b"})
|
|
|
|
|
<< QStringList({"a:a:A:*", // in A
|
|
|
|
|
"c:::*", // Neither in A nor B
|
|
|
|
|
"a/3:::*", // Neither in A nor B
|
|
|
|
|
"a/1/b/x:::*", // Neither in A nor B
|
|
|
|
|
"a/1/b:a/1/b:B:*", // in B
|
|
|
|
|
"a/1:a:A:*", // in A
|
|
|
|
|
"a/1/b/../../2:a:A:*" // in A
|
2017-02-09 14:00:07 +01:00
|
|
|
});
|
2013-12-06 13:38:36 +01:00
|
|
|
QTest::newRow("A and B") // first one wins...
|
2017-02-22 15:09:35 +01:00
|
|
|
<< QStringList({"a:a", "a/1:a", "a/2:a"})
|
|
|
|
|
<< QStringList({"a:a", "a/1:a", "a/2:a"})
|
|
|
|
|
<< QStringList({"a/2:a:A:*"});
|
2013-12-06 13:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CorePlugin::testVcsManager()
|
|
|
|
|
{
|
|
|
|
|
// setup:
|
2017-06-15 13:37:33 +02:00
|
|
|
QList<IVersionControl *> orig = Core::d->m_versionControlList;
|
|
|
|
|
TestVersionControl *vcsA(new TestVersionControl(ID_VCS_A, QLatin1String("A")));
|
|
|
|
|
TestVersionControl *vcsB(new TestVersionControl(ID_VCS_B, QLatin1String("B")));
|
|
|
|
|
|
|
|
|
|
Core::d->m_versionControlList = {vcsA, vcsB};
|
2013-12-06 13:38:36 +01:00
|
|
|
|
|
|
|
|
// test:
|
|
|
|
|
QFETCH(QStringList, dirsVcsA);
|
|
|
|
|
QFETCH(QStringList, dirsVcsB);
|
|
|
|
|
QFETCH(QStringList, results);
|
|
|
|
|
|
|
|
|
|
vcsA->setManagedDirectories(makeHash(dirsVcsA));
|
|
|
|
|
vcsB->setManagedDirectories(makeHash(dirsVcsB));
|
|
|
|
|
|
|
|
|
|
// From VCSes:
|
|
|
|
|
int expectedCount = 0;
|
2022-05-02 17:25:11 +02:00
|
|
|
for (const QString &result : qAsConst(results)) {
|
2013-12-06 13:38:36 +01:00
|
|
|
// qDebug() << "Expecting:" << result;
|
|
|
|
|
|
2022-10-04 12:54:55 +02:00
|
|
|
const QStringList split = result.split(QLatin1Char(':'));
|
2013-12-06 13:38:36 +01:00
|
|
|
QCOMPARE(split.count(), 4);
|
|
|
|
|
QVERIFY(split.at(3) == QLatin1String("*") || split.at(3) == QLatin1String("-"));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const QString directory = split.at(0);
|
|
|
|
|
const QString topLevel = split.at(1);
|
|
|
|
|
const QString vcsId = split.at(2);
|
|
|
|
|
bool fromCache = split.at(3) == QLatin1String("-");
|
|
|
|
|
|
|
|
|
|
if (!fromCache && !directory.isEmpty())
|
|
|
|
|
++expectedCount;
|
|
|
|
|
|
|
|
|
|
IVersionControl *vcs;
|
2022-10-04 12:54:55 +02:00
|
|
|
FilePath realTopLevel;
|
2021-07-30 16:46:27 +02:00
|
|
|
vcs = VcsManager::findVersionControlForDirectory(
|
|
|
|
|
FilePath::fromString(makeString(directory)), &realTopLevel);
|
2022-10-04 12:54:55 +02:00
|
|
|
QCOMPARE(realTopLevel.toString(), makeString(topLevel));
|
2013-12-06 13:38:36 +01:00
|
|
|
if (vcs)
|
|
|
|
|
QCOMPARE(vcs->id().toString(), vcsId);
|
|
|
|
|
else
|
|
|
|
|
QCOMPARE(QString(), vcsId);
|
|
|
|
|
QCOMPARE(vcsA->dirCount(), expectedCount);
|
|
|
|
|
QCOMPARE(vcsA->fileCount(), 0);
|
|
|
|
|
QCOMPARE(vcsB->dirCount(), expectedCount);
|
|
|
|
|
QCOMPARE(vcsB->fileCount(), 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// teardown:
|
2017-06-15 13:37:33 +02:00
|
|
|
qDeleteAll(Core::d->m_versionControlList);
|
|
|
|
|
Core::d->m_versionControlList = orig;
|
2013-12-06 13:38:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace Core
|
|
|
|
|
|
|
|
|
|
#endif
|