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/qtcprocess.h>
#include <QDir>
#include <QApplication>
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);

View File

@@ -27,18 +27,18 @@
#include "core_global.h"
#include <QString>
#include <utils/filepath.h>
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);
};

View File

@@ -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());

View File

@@ -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();

View File

@@ -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