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 <jaroslaw.kobus@qt.io>
This commit is contained in:
hjk
2021-08-17 13:37:20 +02:00
parent bd4a501f15
commit 59c01481d1
5 changed files with 33 additions and 38 deletions

View File

@@ -30,40 +30,39 @@
#include <utils/environment.h> #include <utils/environment.h>
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <QDir>
#include <QApplication> #include <QApplication>
static const char settingsGroupC[] = "General";
static const char patchCommandKeyC[] = "PatchCommand";
static const char patchCommandDefaultC[] = "patch";
using namespace Utils; using namespace Utils;
namespace Core { namespace Core {
QString PatchTool::patchCommand() const char settingsGroupC[] = "General";
const char patchCommandKeyC[] = "PatchCommand";
const char patchCommandDefaultC[] = "patch";
FilePath PatchTool::patchCommand()
{ {
QSettings *s = ICore::settings(); QSettings *s = ICore::settings();
s->beginGroup(settingsGroupC); s->beginGroup(settingsGroupC);
const QString command = s->value(patchCommandKeyC, patchCommandDefaultC).toString(); const FilePath command = FilePath::fromVariant(s->value(patchCommandKeyC, patchCommandDefaultC));
s->endGroup(); s->endGroup();
return command; return command;
} }
void PatchTool::setPatchCommand(const QString &newCommand) void PatchTool::setPatchCommand(const FilePath &newCommand)
{ {
Utils::QtcSettings *s = ICore::settings(); Utils::QtcSettings *s = ICore::settings();
s->beginGroup(QLatin1String(settingsGroupC)); s->beginGroup(settingsGroupC);
s->setValueWithDefault(QLatin1String(patchCommandKeyC), newCommand, QString(patchCommandKeyC)); s->setValueWithDefault(patchCommandKeyC, newCommand.toVariant(), QVariant(QString(patchCommandDefaultC)));
s->endGroup(); 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) int strip, bool reverse, bool withCrlf)
{ {
const QString patch = PatchTool::patchCommand(); const FilePath patch = PatchTool::patchCommand();
if (patch.isEmpty()) { if (patch.isEmpty()) {
MessageManager::writeDisrupting(QApplication::translate( MessageManager::writeDisrupting(QApplication::translate(
"Core::PatchTool", "Core::PatchTool",
@@ -71,8 +70,7 @@ static bool runPatchHelper(const QByteArray &input, const QString &workingDirect
return false; return false;
} }
if (!Utils::FilePath::fromString(patch).exists() if (!patch.exists() && !patch.searchInDirectories(Environment::systemEnvironment().path()).exists()) {
&& !Utils::Environment::systemEnvironment().searchInPath(patch).exists()) {
MessageManager::writeDisrupting( MessageManager::writeDisrupting(
QApplication::translate("Core::PatchTool", QApplication::translate("Core::PatchTool",
"The patch-command configured in the general \"Environment\" " "The patch-command configured in the general \"Environment\" "
@@ -89,28 +87,25 @@ static bool runPatchHelper(const QByteArray &input, const QString &workingDirect
QStringList args; QStringList args;
// Add argument 'apply' when git is used as patch command since git 2.5/Windows // Add argument 'apply' when git is used as patch command since git 2.5/Windows
// no longer ships patch.exe. // no longer ships patch.exe.
if (patch.endsWith(QLatin1String("git"), Qt::CaseInsensitive) if (patch.endsWith("git") || patch.endsWith("git.exe"))
|| patch.endsWith(QLatin1String("git.exe"), Qt::CaseInsensitive)) { args << "apply";
args << QLatin1String("apply");
}
if (strip >= 0) if (strip >= 0)
args << (QLatin1String("-p") + QString::number(strip)); args << ("-p" + QString::number(strip));
if (reverse) if (reverse)
args << QLatin1String("-R"); args << "-R";
if (withCrlf) if (withCrlf)
args << QLatin1String("--binary"); args << "--binary";
MessageManager::writeDisrupting( MessageManager::writeDisrupting(
QApplication::translate("Core::PatchTool", "Running in %1: %2 %3") QApplication::translate("Core::PatchTool", "Running in %1: %2 %3")
.arg(QDir::toNativeSeparators(workingDirectory), .arg(workingDirectory.toUserOutput(), patch.toUserOutput(), args.join(' ')));
QDir::toNativeSeparators(patch), patchProcess.setCommand({patch, args});
args.join(QLatin1Char(' '))));
patchProcess.setCommand({FilePath::fromString(patch), args});
patchProcess.setWriteData(input); patchProcess.setWriteData(input);
patchProcess.start(); patchProcess.start();
if (!patchProcess.waitForStarted()) { if (!patchProcess.waitForStarted()) {
MessageManager::writeFlashing( MessageManager::writeFlashing(
QApplication::translate("Core::PatchTool", "Unable to launch \"%1\": %2") QApplication::translate("Core::PatchTool", "Unable to launch \"%1\": %2")
.arg(patch, patchProcess.errorString())); .arg(patch.toUserOutput(), patchProcess.errorString()));
return false; return false;
} }
@@ -120,7 +115,7 @@ static bool runPatchHelper(const QByteArray &input, const QString &workingDirect
patchProcess.stopProcess(); patchProcess.stopProcess();
MessageManager::writeFlashing( MessageManager::writeFlashing(
QApplication::translate("Core::PatchTool", "A timeout occurred running \"%1\"") QApplication::translate("Core::PatchTool", "A timeout occurred running \"%1\"")
.arg(patch)); .arg(patch.toUserOutput()));
return false; return false;
} }
@@ -138,20 +133,20 @@ static bool runPatchHelper(const QByteArray &input, const QString &workingDirect
if (patchProcess.exitStatus() != QProcess::NormalExit) { if (patchProcess.exitStatus() != QProcess::NormalExit) {
MessageManager::writeFlashing( MessageManager::writeFlashing(
QApplication::translate("Core::PatchTool", "\"%1\" crashed.").arg(patch)); QApplication::translate("Core::PatchTool", "\"%1\" crashed.").arg(patch.toUserOutput()));
return false; return false;
} }
if (patchProcess.exitCode() != 0) { if (patchProcess.exitCode() != 0) {
MessageManager::writeFlashing( MessageManager::writeFlashing(
QApplication::translate("Core::PatchTool", "\"%1\" failed (exit code %2).") QApplication::translate("Core::PatchTool", "\"%1\" failed (exit code %2).")
.arg(patch) .arg(patch.toUserOutput())
.arg(patchProcess.exitCode())); .arg(patchProcess.exitCode()));
return false; return false;
} }
return true; return true;
} }
bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirectory, bool PatchTool::runPatch(const QByteArray &input, const FilePath &workingDirectory,
int strip, bool reverse) int strip, bool reverse)
{ {
return runPatchHelper(input, workingDirectory, strip, reverse, false); return runPatchHelper(input, workingDirectory, strip, reverse, false);

View File

@@ -27,18 +27,18 @@
#include "core_global.h" #include "core_global.h"
#include <QString> #include <utils/filepath.h>
namespace Core { namespace Core {
class CORE_EXPORT PatchTool class CORE_EXPORT PatchTool
{ {
public: public:
static QString patchCommand(); static Utils::FilePath patchCommand();
static void setPatchCommand(const QString &newCommand); static void setPatchCommand(const Utils::FilePath &newCommand);
// Utility to run the 'patch' command // 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); int strip = 0, bool reverse = false);
}; };

View File

@@ -119,7 +119,7 @@ public:
m_ui.patchChooser->setToolTip(patchToolTip); m_ui.patchChooser->setToolTip(patchToolTip);
m_ui.patchChooser->setExpectedKind(PathChooser::ExistingCommand); m_ui.patchChooser->setExpectedKind(PathChooser::ExistingCommand);
m_ui.patchChooser->setHistoryCompleter(QLatin1String("General.PatchCommand.History")); 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->setChecked(EditorManagerPrivate::autoSaveEnabled());
m_ui.autoSaveCheckBox->setToolTip(tr("Automatically creates temporary copies of " m_ui.autoSaveCheckBox->setToolTip(tr("Automatically creates temporary copies of "
"modified files. If %1 is restarted after " "modified files. If %1 is restarted after "
@@ -265,7 +265,7 @@ void SystemSettingsWidget::apply()
m_ui.externalFileBrowserEdit->text()); m_ui.externalFileBrowserEdit->text());
} }
} }
PatchTool::setPatchCommand(m_ui.patchChooser->filePath().toString()); PatchTool::setPatchCommand(m_ui.patchChooser->filePath());
EditorManagerPrivate::setAutoSaveEnabled(m_ui.autoSaveCheckBox->isChecked()); EditorManagerPrivate::setAutoSaveEnabled(m_ui.autoSaveCheckBox->isChecked());
EditorManagerPrivate::setAutoSaveInterval(m_ui.autoSaveInterval->value()); EditorManagerPrivate::setAutoSaveInterval(m_ui.autoSaveInterval->value());
EditorManagerPrivate::setAutoSuspendEnabled(m_ui.autoSuspendCheckBox->isChecked()); EditorManagerPrivate::setAutoSuspendEnabled(m_ui.autoSuspendCheckBox->isChecked());

View File

@@ -202,7 +202,7 @@ void DiffEditorWidgetController::patch(bool revert, int fileIndex, int chunkInde
return; return;
if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch), if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch),
contentsCopyDir, 0, revert)) { FilePath::fromString(contentsCopyDir), 0, revert)) {
QString errorString; QString errorString;
if (textDocument->reload(&errorString, FilePath::fromString(contentsCopyFileName))) if (textDocument->reload(&errorString, FilePath::fromString(contentsCopyFileName)))
m_document->reload(); m_document->reload();

View File

@@ -1539,7 +1539,7 @@ bool VcsBaseEditorWidget::canApplyDiffChunk(const DiffChunk &dc) const
bool VcsBaseEditorWidget::applyDiffChunk(const DiffChunk &dc, bool revert) const bool VcsBaseEditorWidget::applyDiffChunk(const DiffChunk &dc, bool revert) const
{ {
return Core::PatchTool::runPatch(dc.asPatch(d->m_workingDirectory), 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 QString VcsBaseEditorWidget::fileNameFromDiffSpecification(const QTextBlock &inBlock, QString *header) const