PatchTool: Fix patching files with CRLF on linux

Task-number: QTCREATORBUG-14012
Change-Id: I11db2f86a9c8fb13b3120c380b9d9a0856599d89
Reviewed-by: Alexander Drozdov <adrozdoff@gmail.com>
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
Orgad Shaneh
2015-09-24 23:50:50 +03:00
committed by Orgad Shaneh
parent ff39b02115
commit c77acc6e0a

View File

@@ -34,6 +34,7 @@
#include <utils/synchronousprocess.h> #include <utils/synchronousprocess.h>
#include <QProcess> #include <QProcess>
#include <QProcessEnvironment>
#include <QDir> #include <QDir>
#include <QApplication> #include <QApplication>
@@ -82,10 +83,10 @@ void PatchTool::setPatchCommand(const QString &newCommand)
s->endGroup(); s->endGroup();
} }
bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirectory, static bool runPatchHelper(const QByteArray &input, const QString &workingDirectory,
int strip, bool reverse) int strip, bool reverse, bool withCrlf)
{ {
const QString patch = patchCommand(); const QString patch = PatchTool::patchCommand();
if (patch.isEmpty()) { if (patch.isEmpty()) {
MessageManager::write(QApplication::translate("Core::PatchTool", "There is no patch-command configured in the general \"Environment\" settings.")); MessageManager::write(QApplication::translate("Core::PatchTool", "There is no patch-command configured in the general \"Environment\" settings."));
return false; return false;
@@ -94,6 +95,9 @@ bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirector
QProcess patchProcess; QProcess patchProcess;
if (!workingDirectory.isEmpty()) if (!workingDirectory.isEmpty())
patchProcess.setWorkingDirectory(workingDirectory); patchProcess.setWorkingDirectory(workingDirectory);
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert(QLatin1String("LC_ALL"), QLatin1String("C"));
patchProcess.setProcessEnvironment(env);
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.
@@ -105,6 +109,8 @@ bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirector
args << (QLatin1String("-p") + QString::number(strip)); args << (QLatin1String("-p") + QString::number(strip));
if (reverse) if (reverse)
args << QLatin1String("-R"); args << QLatin1String("-R");
if (withCrlf)
args << QLatin1String("--binary");
MessageManager::write(QApplication::translate("Core::PatchTool", "Executing in %1: %2 %3"). MessageManager::write(QApplication::translate("Core::PatchTool", "Executing in %1: %2 %3").
arg(QDir::toNativeSeparators(workingDirectory), arg(QDir::toNativeSeparators(workingDirectory),
QDir::toNativeSeparators(patch), args.join(QLatin1Char(' ')))); QDir::toNativeSeparators(patch), args.join(QLatin1Char(' '))));
@@ -123,8 +129,15 @@ bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirector
return false; return false;
} }
if (!stdOut.isEmpty()) if (!stdOut.isEmpty()) {
MessageManager::write(QString::fromLocal8Bit(stdOut)); if (stdOut.contains("(different line endings)") && !withCrlf) {
QByteArray crlfInput = input;
crlfInput.replace('\n', "\r\n");
return runPatchHelper(crlfInput, workingDirectory, strip, reverse, true);
} else {
MessageManager::write(QString::fromLocal8Bit(stdOut));
}
}
if (!stdErr.isEmpty()) if (!stdErr.isEmpty())
MessageManager::write(QString::fromLocal8Bit(stdErr)); MessageManager::write(QString::fromLocal8Bit(stdErr));
@@ -139,5 +152,10 @@ bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirector
return true; return true;
} }
bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirectory,
int strip, bool reverse)
{
return runPatchHelper(input, workingDirectory, strip, reverse, false);
}
} // namespace Core } // namespace Core