From c77acc6e0a5ee47df0efbf6ba83cfb683c475abd Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Thu, 24 Sep 2015 23:50:50 +0300 Subject: [PATCH] PatchTool: Fix patching files with CRLF on linux Task-number: QTCREATORBUG-14012 Change-Id: I11db2f86a9c8fb13b3120c380b9d9a0856599d89 Reviewed-by: Alexander Drozdov Reviewed-by: Tobias Hunger --- src/plugins/coreplugin/patchtool.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/plugins/coreplugin/patchtool.cpp b/src/plugins/coreplugin/patchtool.cpp index 9224de5ef6a..75adf358490 100644 --- a/src/plugins/coreplugin/patchtool.cpp +++ b/src/plugins/coreplugin/patchtool.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -82,10 +83,10 @@ void PatchTool::setPatchCommand(const QString &newCommand) s->endGroup(); } -bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirectory, - int strip, bool reverse) +static bool runPatchHelper(const QByteArray &input, const QString &workingDirectory, + int strip, bool reverse, bool withCrlf) { - const QString patch = patchCommand(); + const QString patch = PatchTool::patchCommand(); if (patch.isEmpty()) { MessageManager::write(QApplication::translate("Core::PatchTool", "There is no patch-command configured in the general \"Environment\" settings.")); return false; @@ -94,6 +95,9 @@ bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirector QProcess patchProcess; if (!workingDirectory.isEmpty()) patchProcess.setWorkingDirectory(workingDirectory); + QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); + env.insert(QLatin1String("LC_ALL"), QLatin1String("C")); + patchProcess.setProcessEnvironment(env); QStringList args; // Add argument 'apply' when git is used as patch command since git 2.5/Windows // no longer ships patch.exe. @@ -105,6 +109,8 @@ bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirector args << (QLatin1String("-p") + QString::number(strip)); if (reverse) args << QLatin1String("-R"); + if (withCrlf) + args << QLatin1String("--binary"); MessageManager::write(QApplication::translate("Core::PatchTool", "Executing in %1: %2 %3"). arg(QDir::toNativeSeparators(workingDirectory), QDir::toNativeSeparators(patch), args.join(QLatin1Char(' ')))); @@ -123,8 +129,15 @@ bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirector return false; } - if (!stdOut.isEmpty()) - MessageManager::write(QString::fromLocal8Bit(stdOut)); + if (!stdOut.isEmpty()) { + 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()) MessageManager::write(QString::fromLocal8Bit(stdErr)); @@ -139,5 +152,10 @@ bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirector return true; } +bool PatchTool::runPatch(const QByteArray &input, const QString &workingDirectory, + int strip, bool reverse) +{ + return runPatchHelper(input, workingDirectory, strip, reverse, false); +} } // namespace Core