From 59c01481d1d84a77fde8a058b531203f614c98c2 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 17 Aug 2021 13:37:20 +0200 Subject: [PATCH] Core: PatchTool code cosmetics Some more FilePath use, QLatin*, static, ... Also fix default settings values, amends e2eab0e0 insofar. Change-Id: I8e40ca9629351db3b3095636b4ea29a204f86da6 Reviewed-by: Jarek Kobus --- src/plugins/coreplugin/patchtool.cpp | 55 +++++++++---------- src/plugins/coreplugin/patchtool.h | 8 +-- src/plugins/coreplugin/systemsettings.cpp | 4 +- .../diffeditor/diffeditorwidgetcontroller.cpp | 2 +- src/plugins/vcsbase/vcsbaseeditor.cpp | 2 +- 5 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/plugins/coreplugin/patchtool.cpp b/src/plugins/coreplugin/patchtool.cpp index ce40a79d017..ac79162ba16 100644 --- a/src/plugins/coreplugin/patchtool.cpp +++ b/src/plugins/coreplugin/patchtool.cpp @@ -30,40 +30,39 @@ #include #include -#include #include -static const char settingsGroupC[] = "General"; -static const char patchCommandKeyC[] = "PatchCommand"; -static const char patchCommandDefaultC[] = "patch"; - using namespace Utils; namespace Core { -QString PatchTool::patchCommand() +const char settingsGroupC[] = "General"; +const char patchCommandKeyC[] = "PatchCommand"; +const char patchCommandDefaultC[] = "patch"; + +FilePath PatchTool::patchCommand() { QSettings *s = ICore::settings(); s->beginGroup(settingsGroupC); - const QString command = s->value(patchCommandKeyC, patchCommandDefaultC).toString(); + const FilePath command = FilePath::fromVariant(s->value(patchCommandKeyC, patchCommandDefaultC)); s->endGroup(); return command; } -void PatchTool::setPatchCommand(const QString &newCommand) +void PatchTool::setPatchCommand(const FilePath &newCommand) { Utils::QtcSettings *s = ICore::settings(); - s->beginGroup(QLatin1String(settingsGroupC)); - s->setValueWithDefault(QLatin1String(patchCommandKeyC), newCommand, QString(patchCommandKeyC)); + s->beginGroup(settingsGroupC); + s->setValueWithDefault(patchCommandKeyC, newCommand.toVariant(), QVariant(QString(patchCommandDefaultC))); s->endGroup(); } -static bool runPatchHelper(const QByteArray &input, const QString &workingDirectory, +static bool runPatchHelper(const QByteArray &input, const FilePath &workingDirectory, int strip, bool reverse, bool withCrlf) { - const QString patch = PatchTool::patchCommand(); + const FilePath patch = PatchTool::patchCommand(); if (patch.isEmpty()) { MessageManager::writeDisrupting(QApplication::translate( "Core::PatchTool", @@ -71,8 +70,7 @@ static bool runPatchHelper(const QByteArray &input, const QString &workingDirect return false; } - if (!Utils::FilePath::fromString(patch).exists() - && !Utils::Environment::systemEnvironment().searchInPath(patch).exists()) { + if (!patch.exists() && !patch.searchInDirectories(Environment::systemEnvironment().path()).exists()) { MessageManager::writeDisrupting( QApplication::translate("Core::PatchTool", "The patch-command configured in the general \"Environment\" " @@ -89,28 +87,25 @@ static bool runPatchHelper(const QByteArray &input, const QString &workingDirect QStringList args; // Add argument 'apply' when git is used as patch command since git 2.5/Windows // no longer ships patch.exe. - if (patch.endsWith(QLatin1String("git"), Qt::CaseInsensitive) - || patch.endsWith(QLatin1String("git.exe"), Qt::CaseInsensitive)) { - args << QLatin1String("apply"); - } + if (patch.endsWith("git") || patch.endsWith("git.exe")) + args << "apply"; + if (strip >= 0) - args << (QLatin1String("-p") + QString::number(strip)); + args << ("-p" + QString::number(strip)); if (reverse) - args << QLatin1String("-R"); + args << "-R"; if (withCrlf) - args << QLatin1String("--binary"); + args << "--binary"; MessageManager::writeDisrupting( QApplication::translate("Core::PatchTool", "Running in %1: %2 %3") - .arg(QDir::toNativeSeparators(workingDirectory), - QDir::toNativeSeparators(patch), - args.join(QLatin1Char(' ')))); - patchProcess.setCommand({FilePath::fromString(patch), args}); + .arg(workingDirectory.toUserOutput(), patch.toUserOutput(), args.join(' '))); + patchProcess.setCommand({patch, args}); patchProcess.setWriteData(input); patchProcess.start(); if (!patchProcess.waitForStarted()) { MessageManager::writeFlashing( QApplication::translate("Core::PatchTool", "Unable to launch \"%1\": %2") - .arg(patch, patchProcess.errorString())); + .arg(patch.toUserOutput(), patchProcess.errorString())); return false; } @@ -120,7 +115,7 @@ static bool runPatchHelper(const QByteArray &input, const QString &workingDirect patchProcess.stopProcess(); MessageManager::writeFlashing( QApplication::translate("Core::PatchTool", "A timeout occurred running \"%1\"") - .arg(patch)); + .arg(patch.toUserOutput())); return false; } @@ -138,20 +133,20 @@ static bool runPatchHelper(const QByteArray &input, const QString &workingDirect if (patchProcess.exitStatus() != QProcess::NormalExit) { MessageManager::writeFlashing( - QApplication::translate("Core::PatchTool", "\"%1\" crashed.").arg(patch)); + QApplication::translate("Core::PatchTool", "\"%1\" crashed.").arg(patch.toUserOutput())); return false; } if (patchProcess.exitCode() != 0) { MessageManager::writeFlashing( QApplication::translate("Core::PatchTool", "\"%1\" failed (exit code %2).") - .arg(patch) + .arg(patch.toUserOutput()) .arg(patchProcess.exitCode())); return false; } return true; } -bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirectory, +bool PatchTool::runPatch(const QByteArray &input, const FilePath &workingDirectory, int strip, bool reverse) { return runPatchHelper(input, workingDirectory, strip, reverse, false); diff --git a/src/plugins/coreplugin/patchtool.h b/src/plugins/coreplugin/patchtool.h index 674b23001fc..fad3bb3e0b6 100644 --- a/src/plugins/coreplugin/patchtool.h +++ b/src/plugins/coreplugin/patchtool.h @@ -27,18 +27,18 @@ #include "core_global.h" -#include +#include namespace Core { class CORE_EXPORT PatchTool { public: - static QString patchCommand(); - static void setPatchCommand(const QString &newCommand); + static Utils::FilePath patchCommand(); + static void setPatchCommand(const Utils::FilePath &newCommand); // Utility to run the 'patch' command - static bool runPatch(const QByteArray &input, const QString &workingDirectory = QString(), + static bool runPatch(const QByteArray &input, const Utils::FilePath &workingDirectory = {}, int strip = 0, bool reverse = false); }; diff --git a/src/plugins/coreplugin/systemsettings.cpp b/src/plugins/coreplugin/systemsettings.cpp index e6e25361f62..b9787764369 100644 --- a/src/plugins/coreplugin/systemsettings.cpp +++ b/src/plugins/coreplugin/systemsettings.cpp @@ -119,7 +119,7 @@ public: m_ui.patchChooser->setToolTip(patchToolTip); m_ui.patchChooser->setExpectedKind(PathChooser::ExistingCommand); m_ui.patchChooser->setHistoryCompleter(QLatin1String("General.PatchCommand.History")); - m_ui.patchChooser->setPath(PatchTool::patchCommand()); + m_ui.patchChooser->setFilePath(PatchTool::patchCommand()); m_ui.autoSaveCheckBox->setChecked(EditorManagerPrivate::autoSaveEnabled()); m_ui.autoSaveCheckBox->setToolTip(tr("Automatically creates temporary copies of " "modified files. If %1 is restarted after " @@ -265,7 +265,7 @@ void SystemSettingsWidget::apply() m_ui.externalFileBrowserEdit->text()); } } - PatchTool::setPatchCommand(m_ui.patchChooser->filePath().toString()); + PatchTool::setPatchCommand(m_ui.patchChooser->filePath()); EditorManagerPrivate::setAutoSaveEnabled(m_ui.autoSaveCheckBox->isChecked()); EditorManagerPrivate::setAutoSaveInterval(m_ui.autoSaveInterval->value()); EditorManagerPrivate::setAutoSuspendEnabled(m_ui.autoSuspendCheckBox->isChecked()); diff --git a/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp b/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp index 12b3c5a56b9..3927364da3d 100644 --- a/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp +++ b/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp @@ -202,7 +202,7 @@ void DiffEditorWidgetController::patch(bool revert, int fileIndex, int chunkInde return; if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch), - contentsCopyDir, 0, revert)) { + FilePath::fromString(contentsCopyDir), 0, revert)) { QString errorString; if (textDocument->reload(&errorString, FilePath::fromString(contentsCopyFileName))) m_document->reload(); diff --git a/src/plugins/vcsbase/vcsbaseeditor.cpp b/src/plugins/vcsbase/vcsbaseeditor.cpp index 1bfe550bc46..d24b96dd5f8 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.cpp +++ b/src/plugins/vcsbase/vcsbaseeditor.cpp @@ -1539,7 +1539,7 @@ bool VcsBaseEditorWidget::canApplyDiffChunk(const DiffChunk &dc) const bool VcsBaseEditorWidget::applyDiffChunk(const DiffChunk &dc, bool revert) const { return Core::PatchTool::runPatch(dc.asPatch(d->m_workingDirectory), - d->m_workingDirectory, 0, revert); + FilePath::fromString(d->m_workingDirectory), 0, revert); } QString VcsBaseEditorWidget::fileNameFromDiffSpecification(const QTextBlock &inBlock, QString *header) const