VcsBasePlugin: Use more FilePath

Change-Id: I7bc80245b093b210439efdf3ea353b52b288dcc0
Reviewed-by: hjk <hjk@qt.io>
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Jarek Kobus
2022-10-04 12:54:55 +02:00
parent 34f9229eef
commit c666c93882
8 changed files with 55 additions and 56 deletions

View File

@@ -196,7 +196,7 @@ void VcsManager::resetVersionControlForDirectory(const FilePath &inputDirectory)
} }
IVersionControl* VcsManager::findVersionControlForDirectory(const FilePath &inputDirectory, IVersionControl* VcsManager::findVersionControlForDirectory(const FilePath &inputDirectory,
QString *topLevelDirectory) FilePath *topLevelDirectory)
{ {
using StringVersionControlPair = QPair<QString, IVersionControl *>; using StringVersionControlPair = QPair<QString, IVersionControl *>;
using StringVersionControlPairs = QList<StringVersionControlPair>; using StringVersionControlPairs = QList<StringVersionControlPair>;
@@ -217,7 +217,7 @@ IVersionControl* VcsManager::findVersionControlForDirectory(const FilePath &inpu
auto cachedData = d->findInCache(directory); auto cachedData = d->findInCache(directory);
if (cachedData) { if (cachedData) {
if (topLevelDirectory) if (topLevelDirectory)
*topLevelDirectory = cachedData->topLevel; *topLevelDirectory = FilePath::fromString(cachedData->topLevel);
return cachedData->versionControl; return cachedData->versionControl;
} }
@@ -273,7 +273,7 @@ IVersionControl* VcsManager::findVersionControlForDirectory(const FilePath &inpu
// return result // return result
if (topLevelDirectory) if (topLevelDirectory)
*topLevelDirectory = allThatCanManage.first().first; *topLevelDirectory = FilePath::fromString(allThatCanManage.first().first);
IVersionControl *versionControl = allThatCanManage.first().second; IVersionControl *versionControl = allThatCanManage.first().second;
const bool isVcsConfigured = versionControl->isConfigured(); const bool isVcsConfigured = versionControl->isConfigured();
if (!isVcsConfigured || d->m_unconfiguredVcs) { if (!isVcsConfigured || d->m_unconfiguredVcs) {
@@ -308,9 +308,9 @@ IVersionControl* VcsManager::findVersionControlForDirectory(const FilePath &inpu
FilePath VcsManager::findTopLevelForDirectory(const FilePath &directory) FilePath VcsManager::findTopLevelForDirectory(const FilePath &directory)
{ {
QString result; FilePath result;
findVersionControlForDirectory(directory, &result); findVersionControlForDirectory(directory, &result);
return FilePath::fromString(result); return result;
} }
QStringList VcsManager::repositories(const IVersionControl *vc) QStringList VcsManager::repositories(const IVersionControl *vc)
@@ -568,14 +568,12 @@ void CorePlugin::testVcsManager()
vcsA->setManagedDirectories(makeHash(dirsVcsA)); vcsA->setManagedDirectories(makeHash(dirsVcsA));
vcsB->setManagedDirectories(makeHash(dirsVcsB)); vcsB->setManagedDirectories(makeHash(dirsVcsB));
QString realTopLevel = QLatin1String("ABC"); // Make sure this gets cleared if needed.
// From VCSes: // From VCSes:
int expectedCount = 0; int expectedCount = 0;
for (const QString &result : qAsConst(results)) { for (const QString &result : qAsConst(results)) {
// qDebug() << "Expecting:" << result; // qDebug() << "Expecting:" << result;
QStringList split = result.split(QLatin1Char(':')); const QStringList split = result.split(QLatin1Char(':'));
QCOMPARE(split.count(), 4); QCOMPARE(split.count(), 4);
QVERIFY(split.at(3) == QLatin1String("*") || split.at(3) == QLatin1String("-")); QVERIFY(split.at(3) == QLatin1String("*") || split.at(3) == QLatin1String("-"));
@@ -589,9 +587,10 @@ void CorePlugin::testVcsManager()
++expectedCount; ++expectedCount;
IVersionControl *vcs; IVersionControl *vcs;
FilePath realTopLevel;
vcs = VcsManager::findVersionControlForDirectory( vcs = VcsManager::findVersionControlForDirectory(
FilePath::fromString(makeString(directory)), &realTopLevel); FilePath::fromString(makeString(directory)), &realTopLevel);
QCOMPARE(realTopLevel, makeString(topLevel)); QCOMPARE(realTopLevel.toString(), makeString(topLevel));
if (vcs) if (vcs)
QCOMPARE(vcs->id().toString(), vcsId); QCOMPARE(vcs->id().toString(), vcsId);
else else

View File

@@ -43,7 +43,7 @@ public:
static void resetVersionControlForDirectory(const Utils::FilePath &inputDirectory); static void resetVersionControlForDirectory(const Utils::FilePath &inputDirectory);
static IVersionControl *findVersionControlForDirectory(const Utils::FilePath &directory, static IVersionControl *findVersionControlForDirectory(const Utils::FilePath &directory,
QString *topLevelDirectory = nullptr); Utils::FilePath *topLevelDirectory = nullptr);
static Utils::FilePath findTopLevelForDirectory(const Utils::FilePath &directory); static Utils::FilePath findTopLevelForDirectory(const Utils::FilePath &directory);
static QStringList repositories(const IVersionControl *); static QStringList repositories(const IVersionControl *);

View File

@@ -3847,10 +3847,10 @@ FilePath GitClient::fileWorkingDirectory(const QString &file)
IEditor *GitClient::openShowEditor(const FilePath &workingDirectory, const QString &ref, IEditor *GitClient::openShowEditor(const FilePath &workingDirectory, const QString &ref,
const QString &path, ShowEditor showSetting) const QString &path, ShowEditor showSetting)
{ {
QString topLevel; const FilePath topLevel = VcsManager::findTopLevelForDirectory(workingDirectory);
VcsManager::findVersionControlForDirectory(workingDirectory, &topLevel); const QString topLevelString = topLevel.toString();
const QString relativePath = QDir(topLevel).relativeFilePath(path); const QString relativePath = QDir(topLevelString).relativeFilePath(path);
const QByteArray content = synchronousShow(FilePath::fromString(topLevel), ref + ":" + relativePath); const QByteArray content = synchronousShow(topLevel, ref + ":" + relativePath);
if (showSetting == ShowEditor::OnlyIfDifferent) { if (showSetting == ShowEditor::OnlyIfDifferent) {
if (content.isEmpty()) if (content.isEmpty())
return nullptr; return nullptr;
@@ -3866,7 +3866,7 @@ IEditor *GitClient::openShowEditor(const FilePath &workingDirectory, const QStri
} }
const QString documentId = QLatin1String(Git::Constants::GIT_PLUGIN) const QString documentId = QLatin1String(Git::Constants::GIT_PLUGIN)
+ QLatin1String(".GitShow.") + topLevel + QLatin1String(".GitShow.") + topLevelString
+ QLatin1String(".") + relativePath; + QLatin1String(".") + relativePath;
QString title = tr("Git Show %1:%2").arg(ref).arg(relativePath); QString title = tr("Git Show %1:%2").arg(ref).arg(relativePath);
IEditor *editor = EditorManager::openEditorWithContents(Id(), &title, content, documentId, IEditor *editor = EditorManager::openEditorWithContents(Id(), &title, content, documentId,

View File

@@ -198,7 +198,7 @@ static bool isGitDirectory(const FilePath &path)
{ {
static IVersionControl *gitVc = VcsManager::versionControl(VcsBase::Constants::VCS_ID_GIT); static IVersionControl *gitVc = VcsManager::versionControl(VcsBase::Constants::VCS_ID_GIT);
QTC_ASSERT(gitVc, return false); QTC_ASSERT(gitVc, return false);
return gitVc == VcsManager::findVersionControlForDirectory(path, nullptr); return gitVc == VcsManager::findVersionControlForDirectory(path);
} }
GitGrep::GitGrep(GitClient *client) GitGrep::GitGrep(GitClient *client)

View File

@@ -721,7 +721,7 @@ bool FlatModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int r
struct VcsInfo { struct VcsInfo {
Core::IVersionControl *vcs = nullptr; Core::IVersionControl *vcs = nullptr;
QString repoDir; FilePath repoDir;
bool operator==(const VcsInfo &other) const { bool operator==(const VcsInfo &other) const {
return vcs == other.vcs && repoDir == other.repoDir; return vcs == other.vcs && repoDir == other.repoDir;
} }

View File

@@ -311,9 +311,9 @@ void ProjectTree::updateFileWarning(Core::IDocument *document, const QString &te
if (filePath.canonicalPath().isChildOf(projectDir.canonicalPath())) if (filePath.canonicalPath().isChildOf(projectDir.canonicalPath()))
return; return;
// External file. Test if it under the same VCS // External file. Test if it under the same VCS
QString topLevel; FilePath topLevel;
if (Core::VcsManager::findVersionControlForDirectory(projectDir, &topLevel) if (Core::VcsManager::findVersionControlForDirectory(projectDir, &topLevel)
&& filePath.isChildOf(FilePath::fromString(topLevel))) { && filePath.isChildOf(topLevel)) {
return; return;
} }
} }

View File

@@ -76,17 +76,17 @@ public:
inline bool hasProject() const { return !currentProjectTopLevel.isEmpty(); } inline bool hasProject() const { return !currentProjectTopLevel.isEmpty(); }
inline bool isEmpty() const { return !hasFile() && !hasProject(); } inline bool isEmpty() const { return !hasFile() && !hasProject(); }
QString currentFile; FilePath currentFile;
QString currentFileName; QString currentFileName;
QString currentPatchFile; FilePath currentPatchFile;
QString currentPatchFileDisplayName; QString currentPatchFileDisplayName;
QString currentFileDirectory; FilePath currentFileDirectory;
QString currentFileTopLevel; FilePath currentFileTopLevel;
QString currentProjectPath; FilePath currentProjectPath;
QString currentProjectName; QString currentProjectName;
QString currentProjectTopLevel; FilePath currentProjectTopLevel;
}; };
void State::clearFile() void State::clearFile()
@@ -218,15 +218,15 @@ QString StateListener::windowTitleVcsTopic(const FilePath &filePath)
} }
if (searchPath.isEmpty()) if (searchPath.isEmpty())
return QString(); return QString();
QString topLevelPath; FilePath topLevelPath;
IVersionControl *vc = VcsManager::findVersionControlForDirectory( IVersionControl *vc = VcsManager::findVersionControlForDirectory(
searchPath, &topLevelPath); searchPath, &topLevelPath);
return (vc && !topLevelPath.isEmpty()) ? vc->vcsTopic(FilePath::fromString(topLevelPath)) : QString(); return (vc && !topLevelPath.isEmpty()) ? vc->vcsTopic(topLevelPath) : QString();
} }
static inline QString displayNameOfEditor(const QString &fileName) static inline QString displayNameOfEditor(const FilePath &fileName)
{ {
IDocument *document = DocumentModel::documentForFilePath(FilePath::fromString(fileName)); IDocument *document = DocumentModel::documentForFilePath(fileName);
if (document) if (document)
return document->displayName(); return document->displayName();
return QString(); return QString();
@@ -240,18 +240,16 @@ void StateListener::slotStateChanged()
State state; State state;
IDocument *currentDocument = EditorManager::currentDocument(); IDocument *currentDocument = EditorManager::currentDocument();
if (currentDocument) { if (currentDocument) {
state.currentFile = currentDocument->filePath().toString(); state.currentFile = currentDocument->filePath();
if (state.currentFile.isEmpty() || currentDocument->isTemporary()) if (state.currentFile.isEmpty() || currentDocument->isTemporary())
state.currentFile = VcsBase::source(currentDocument); state.currentFile = FilePath::fromString(VcsBase::source(currentDocument));
} }
// Get the file and its control. Do not use the file unless we find one // Get the file and its control. Do not use the file unless we find one
IVersionControl *fileControl = nullptr; IVersionControl *fileControl = nullptr;
if (!state.currentFile.isEmpty()) { if (!state.currentFile.isEmpty()) {
QFileInfo currentFi(state.currentFile); if (state.currentFile.exists()) {
if (currentFi.exists()) {
// Quick check: Does it look like a patch? // Quick check: Does it look like a patch?
const bool isPatch = state.currentFile.endsWith(".patch") const bool isPatch = state.currentFile.endsWith(".patch")
|| state.currentFile.endsWith(".diff"); || state.currentFile.endsWith(".diff");
@@ -261,18 +259,18 @@ void StateListener::slotStateChanged()
state.currentPatchFile = state.currentFile; state.currentPatchFile = state.currentFile;
state.currentPatchFileDisplayName = displayNameOfEditor(state.currentPatchFile); state.currentPatchFileDisplayName = displayNameOfEditor(state.currentPatchFile);
if (state.currentPatchFileDisplayName.isEmpty()) if (state.currentPatchFileDisplayName.isEmpty())
state.currentPatchFileDisplayName = currentFi.fileName(); state.currentPatchFileDisplayName = state.currentFile.fileName();
} }
if (currentFi.isDir()) { if (state.currentFile.isDir()) {
state.currentFile.clear(); state.currentFile.clear();
state.currentFileDirectory = currentFi.absoluteFilePath(); state.currentFileDirectory = state.currentFile.absoluteFilePath();
} else { } else {
state.currentFileDirectory = currentFi.absolutePath(); state.currentFileDirectory = state.currentFile.absolutePath();
state.currentFileName = currentFi.fileName(); state.currentFileName = state.currentFile.fileName();
} }
fileControl = VcsManager::findVersionControlForDirectory( fileControl = VcsManager::findVersionControlForDirectory(
FilePath::fromString(state.currentFileDirectory), &state.currentFileTopLevel); state.currentFileDirectory, &state.currentFileTopLevel);
} }
if (!fileControl) if (!fileControl)
@@ -286,10 +284,10 @@ void StateListener::slotStateChanged()
currentProject = SessionManager::startupProject(); currentProject = SessionManager::startupProject();
if (currentProject) { if (currentProject) {
state.currentProjectPath = currentProject->projectDirectory().toString(); state.currentProjectPath = currentProject->projectDirectory();
state.currentProjectName = currentProject->displayName(); state.currentProjectName = currentProject->displayName();
projectControl = VcsManager::findVersionControlForDirectory( projectControl = VcsManager::findVersionControlForDirectory(
FilePath::fromString(state.currentProjectPath), &state.currentProjectTopLevel); state.currentProjectPath, &state.currentProjectTopLevel);
if (projectControl) { if (projectControl) {
// If we have both, let the file's one take preference // If we have both, let the file's one take preference
if (fileControl && projectControl != fileControl) if (fileControl && projectControl != fileControl)
@@ -353,7 +351,7 @@ VcsBasePluginState &VcsBasePluginState::operator=(const VcsBasePluginState &rhs)
QString VcsBasePluginState::currentFile() const QString VcsBasePluginState::currentFile() const
{ {
return data->m_state.currentFile; return data->m_state.currentFile.toString();
} }
QString VcsBasePluginState::currentFileName() const QString VcsBasePluginState::currentFileName() const
@@ -363,23 +361,24 @@ QString VcsBasePluginState::currentFileName() const
FilePath VcsBasePluginState::currentFileTopLevel() const FilePath VcsBasePluginState::currentFileTopLevel() const
{ {
return FilePath::fromString(data->m_state.currentFileTopLevel); return data->m_state.currentFileTopLevel;
} }
FilePath VcsBasePluginState::currentFileDirectory() const FilePath VcsBasePluginState::currentFileDirectory() const
{ {
return FilePath::fromString(data->m_state.currentFileDirectory); return data->m_state.currentFileDirectory;
} }
QString VcsBasePluginState::relativeCurrentFile() const QString VcsBasePluginState::relativeCurrentFile() const
{ {
QTC_ASSERT(hasFile(), return QString()); QTC_ASSERT(hasFile(), return {});
return QDir(data->m_state.currentFileTopLevel).relativeFilePath(data->m_state.currentFile); return QDir(data->m_state.currentFileTopLevel.toString()).relativeFilePath(
data->m_state.currentFile.toString());
} }
QString VcsBasePluginState::currentPatchFile() const QString VcsBasePluginState::currentPatchFile() const
{ {
return data->m_state.currentPatchFile; return data->m_state.currentPatchFile.toString();
} }
QString VcsBasePluginState::currentPatchFileDisplayName() const QString VcsBasePluginState::currentPatchFileDisplayName() const
@@ -389,7 +388,7 @@ QString VcsBasePluginState::currentPatchFileDisplayName() const
FilePath VcsBasePluginState::currentProjectPath() const FilePath VcsBasePluginState::currentProjectPath() const
{ {
return FilePath::fromString(data->m_state.currentProjectPath); return data->m_state.currentProjectPath;
} }
QString VcsBasePluginState::currentProjectName() const QString VcsBasePluginState::currentProjectName() const
@@ -399,15 +398,16 @@ QString VcsBasePluginState::currentProjectName() const
FilePath VcsBasePluginState::currentProjectTopLevel() const FilePath VcsBasePluginState::currentProjectTopLevel() const
{ {
return FilePath::fromString(data->m_state.currentProjectTopLevel); return data->m_state.currentProjectTopLevel;
} }
QString VcsBasePluginState::relativeCurrentProject() const QString VcsBasePluginState::relativeCurrentProject() const
{ {
QTC_ASSERT(hasProject(), return QString()); QTC_ASSERT(hasProject(), return QString());
if (data->m_state.currentProjectTopLevel != data->m_state.currentProjectPath) if (data->m_state.currentProjectTopLevel == data->m_state.currentProjectPath)
return QDir(data->m_state.currentProjectTopLevel).relativeFilePath(data->m_state.currentProjectPath); return {};
return QString(); return QDir(data->m_state.currentProjectTopLevel.toString()).relativeFilePath(
data->m_state.currentProjectPath.toString());
} }
bool VcsBasePluginState::hasTopLevel() const bool VcsBasePluginState::hasTopLevel() const
@@ -417,7 +417,7 @@ bool VcsBasePluginState::hasTopLevel() const
FilePath VcsBasePluginState::topLevel() const FilePath VcsBasePluginState::topLevel() const
{ {
return FilePath::fromString(hasFile() ? data->m_state.currentFileTopLevel : data->m_state.currentProjectTopLevel); return hasFile() ? data->m_state.currentFileTopLevel : data->m_state.currentProjectTopLevel;
} }
bool VcsBasePluginState::equals(const Internal::State &rhs) const bool VcsBasePluginState::equals(const Internal::State &rhs) const

View File

@@ -95,10 +95,10 @@ bool VcsPlugin::initialize(const QStringList &arguments, QString *errorMessage)
tr("The current version control topic (branch or tag) identification of the current project."), tr("The current version control topic (branch or tag) identification of the current project."),
[]() -> QString { []() -> QString {
IVersionControl *vc = nullptr; IVersionControl *vc = nullptr;
QString topLevel; FilePath topLevel;
if (Project *project = ProjectTree::currentProject()) if (Project *project = ProjectTree::currentProject())
vc = VcsManager::findVersionControlForDirectory(project->projectDirectory(), &topLevel); vc = VcsManager::findVersionControlForDirectory(project->projectDirectory(), &topLevel);
return vc ? vc->vcsTopic(FilePath::fromString(topLevel)) : QString(); return vc ? vc->vcsTopic(topLevel) : QString();
}); });
expander->registerVariable(Constants::VAR_VCS_TOPLEVELPATH, expander->registerVariable(Constants::VAR_VCS_TOPLEVELPATH,