diffeditorplugin.cpp 35.3 KB
Newer Older
1 2
/****************************************************************************
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 5 6 7 8 9 10 11 12
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
Eike Ziller's avatar
Eike Ziller committed
13 14
** conditions see http://www.qt.io/licensing.  For further information
** use the contact form at http://www.qt.io/contact-us.
15 16 17
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 25 26 27 28 29 30 31
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/

#include "diffeditorplugin.h"
32
#include "diffeditor.h"
33
#include "diffeditorconstants.h"
jkobus's avatar
jkobus committed
34
#include "diffeditordocument.h"
35
#include "diffeditorfactory.h"
jkobus's avatar
jkobus committed
36
#include "diffeditormanager.h"
jkobus's avatar
jkobus committed
37 38
#include "diffeditorreloader.h"
#include "differ.h"
39 40 41

#include <QFileDialog>
#include <QTextCodec>
42
#include <QtPlugin>
43 44 45 46 47

#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/coreconstants.h>
#include <coreplugin/editormanager/editormanager.h>
48
#include <coreplugin/icore.h>
49

50
namespace DiffEditor {
51 52
namespace Internal {

jkobus's avatar
jkobus committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
class SimpleDiffEditorReloader : public DiffEditorReloader
{
    Q_OBJECT
public:
    SimpleDiffEditorReloader(QObject *parent,
                             const QString &leftFileName,
                             const QString &rightFileName);

protected:
    void reload();

private:
    QString m_leftFileName;
    QString m_rightFileName;
};

SimpleDiffEditorReloader::SimpleDiffEditorReloader(QObject *parent,
                                                   const QString &leftFileName,
                                                   const QString &rightFileName)
    : DiffEditorReloader(parent),
      m_leftFileName(leftFileName),
      m_rightFileName(rightFileName)
{
}

void SimpleDiffEditorReloader::reload()
{
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    QString errorString;
    Utils::TextFileFormat format;
    format.codec = Core::EditorManager::defaultTextCodec();

    QString leftText;
    if (Utils::TextFileFormat::readFile(m_leftFileName,
                                    format.codec,
                                    &leftText, &format, &errorString)
            != Utils::TextFileFormat::ReadSuccess) {

        return;
    }

    QString rightText;
    if (Utils::TextFileFormat::readFile(m_rightFileName,
                                    format.codec,
                                    &rightText, &format, &errorString)
            != Utils::TextFileFormat::ReadSuccess) {

        return;
    }
jkobus's avatar
jkobus committed
101 102 103 104 105 106 107 108 109 110 111

    Differ differ;
    QList<Diff> diffList = differ.cleanupSemantics(
                differ.diff(leftText, rightText));

    QList<Diff> leftDiffList;
    QList<Diff> rightDiffList;
    Differ::splitDiffList(diffList, &leftDiffList, &rightDiffList);
    QList<Diff> outputLeftDiffList;
    QList<Diff> outputRightDiffList;

112
    if (controller()->isIgnoreWhitespace()) {
jkobus's avatar
jkobus committed
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        const QList<Diff> leftIntermediate =
                Differ::moveWhitespaceIntoEqualities(leftDiffList);
        const QList<Diff> rightIntermediate =
                Differ::moveWhitespaceIntoEqualities(rightDiffList);
        Differ::ignoreWhitespaceBetweenEqualities(leftIntermediate,
                                                  rightIntermediate,
                                                  &outputLeftDiffList,
                                                  &outputRightDiffList);
    } else {
        outputLeftDiffList = leftDiffList;
        outputRightDiffList = rightDiffList;
    }

    const ChunkData chunkData = DiffUtils::calculateOriginalData(
                outputLeftDiffList, outputRightDiffList);
    FileData fileData = DiffUtils::calculateContextData(
129
                chunkData, controller()->contextLinesNumber(), 0);
jkobus's avatar
jkobus committed
130 131 132 133 134 135
    fileData.leftFileInfo.fileName = m_leftFileName;
    fileData.rightFileInfo.fileName = m_rightFileName;

    QList<FileData> fileDataList;
    fileDataList << fileData;

136
    controller()->requestSaveState();
137
    controller()->setDiffFiles(fileDataList);
138
    controller()->requestRestoreState();
jkobus's avatar
jkobus committed
139 140 141 142 143 144

    reloadFinished();
}

/////////////////

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
DiffEditorPlugin::DiffEditorPlugin()
{
}

DiffEditorPlugin::~DiffEditorPlugin()
{
}

bool DiffEditorPlugin::initialize(const QStringList &arguments, QString *errorMessage)
{
    Q_UNUSED(arguments)
    Q_UNUSED(errorMessage)

    //register actions
    Core::ActionContainer *toolsContainer =
        Core::ActionManager::actionContainer(Core::Constants::M_TOOLS);
jkobus's avatar
jkobus committed
161 162
    toolsContainer->insertGroup(Core::Constants::G_TOOLS_OPTIONS,
                                Constants::G_TOOLS_DIFF);
163 164 165 166 167 168 169 170 171 172 173

    Core::Context globalcontext(Core::Constants::C_GLOBAL);

    QAction *diffAction = new QAction(tr("Diff..."), this);
    Core::Command *diffCommand = Core::ActionManager::registerAction(diffAction,
                             "DiffEditor.Diff", globalcontext);
    connect(diffAction, SIGNAL(triggered()), this, SLOT(diff()));
    toolsContainer->addAction(diffCommand, Constants::G_TOOLS_DIFF);

    addAutoReleasedObject(new DiffEditorFactory(this));

jkobus's avatar
jkobus committed
174 175
    new DiffEditorManager(this);

176 177 178 179 180 181 182 183 184
    return true;
}

void DiffEditorPlugin::extensionsInitialized()
{
}

void DiffEditorPlugin::diff()
{
185
    QString fileName1 = QFileDialog::getOpenFileName(Core::ICore::dialogParent(),
186
                                                     tr("Select First File for Diff"),
187 188 189 190
                                                     QString());
    if (fileName1.isNull())
        return;

191
    QString fileName2 = QFileDialog::getOpenFileName(Core::ICore::dialogParent(),
192
                                                     tr("Select Second File for Diff"),
193 194 195 196 197
                                                     QString());
    if (fileName2.isNull())
        return;


jkobus's avatar
jkobus committed
198 199 200
    const QString documentId = QLatin1String("Diff ") + fileName1
            + QLatin1String(", ") + fileName2;
    DiffEditorDocument *document = DiffEditorManager::find(documentId);
201 202 203 204
    QString title = tr("Diff \"%1\", \"%2\"").arg(fileName1).arg(fileName2);
    document = DiffEditorManager::findOrCreate(documentId, title);
    if (!document)
        return;
jkobus's avatar
jkobus committed
205

206 207
    DiffEditorController *controller = document->controller();
    if (!controller->reloader()) {
jkobus's avatar
jkobus committed
208 209
        SimpleDiffEditorReloader *reloader =
                new SimpleDiffEditorReloader(controller, fileName1, fileName2);
210
        controller->setReloader(reloader);
jkobus's avatar
jkobus committed
211
    }
jkobus's avatar
jkobus committed
212

jkobus's avatar
jkobus committed
213
    Core::EditorManager::activateEditorForDocument(document);
jkobus's avatar
jkobus committed
214

jkobus's avatar
jkobus committed
215
    document->controller()->requestReload();
216 217
}

jkobus's avatar
jkobus committed
218 219 220 221 222 223 224 225 226 227 228 229
} // namespace Internal
} // namespace DiffEditor

#ifdef WITH_TESTS

#include <QTest>

#include "diffutils.h"

Q_DECLARE_METATYPE(DiffEditor::ChunkData)
Q_DECLARE_METATYPE(QList<DiffEditor::FileData>)

Orgad Shaneh's avatar
Orgad Shaneh committed
230 231
static inline QString _(const char *string) { return QString::fromLatin1(string); }

jkobus's avatar
jkobus committed
232
void DiffEditor::Internal::DiffEditorPlugin::testMakePatch_data()
233
{
jkobus's avatar
jkobus committed
234 235 236 237 238 239
    QTest::addColumn<ChunkData>("sourceChunk");
    QTest::addColumn<QString>("leftFileName");
    QTest::addColumn<QString>("rightFileName");
    QTest::addColumn<bool>("lastChunk");
    QTest::addColumn<QString>("patchText");

Orgad Shaneh's avatar
Orgad Shaneh committed
240 241
    const QString fileName = _("a.txt");
    const QString header = _("--- ") + fileName + _("\n+++ ") + fileName + _("\n");
jkobus's avatar
jkobus committed
242 243

    QList<RowData> rows;
Orgad Shaneh's avatar
Orgad Shaneh committed
244 245
    rows << RowData(_("ABCD"), TextLineData::Separator);
    rows << RowData(_("EFGH"));
jkobus's avatar
jkobus committed
246 247
    ChunkData chunk;
    chunk.rows = rows;
Orgad Shaneh's avatar
Orgad Shaneh committed
248 249 250
    QString patchText = header + _("@@ -1,2 +1,1 @@\n"
                                   "-ABCD\n"
                                   " EFGH\n");
jkobus's avatar
jkobus committed
251 252 253 254 255 256 257 258 259
    QTest::newRow("Simple not a last chunk") << chunk
                            << fileName
                            << fileName
                            << false
                            << patchText;

    ///////////

    // chunk the same here
Orgad Shaneh's avatar
Orgad Shaneh committed
260 261 262 263
    patchText = header + _("@@ -1,2 +1,1 @@\n"
                           "-ABCD\n"
                           " EFGH\n"
                           "\\ No newline at end of file\n");
jkobus's avatar
jkobus committed
264 265

    QTest::newRow("Simple last chunk") << chunk
jkobus's avatar
jkobus committed
266 267 268 269 270 271 272 273
                            << fileName
                            << fileName
                            << true
                            << patchText;

    ///////////

    rows.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
274 275
    rows << RowData(_("ABCD"));
    rows << RowData(_(""), TextLineData::Separator);
jkobus's avatar
jkobus committed
276
    chunk.rows = rows;
Orgad Shaneh's avatar
Orgad Shaneh committed
277 278 279 280
    patchText = header + _("@@ -1,1 +1,1 @@\n"
                           "-ABCD\n"
                           "+ABCD\n"
                           "\\ No newline at end of file\n");
jkobus's avatar
jkobus committed
281

jkobus's avatar
jkobus committed
282
    QTest::newRow("EOL in last line removed") << chunk
jkobus's avatar
jkobus committed
283 284 285 286 287 288 289 290
                            << fileName
                            << fileName
                            << true
                            << patchText;

    ///////////

    // chunk the same here
Orgad Shaneh's avatar
Orgad Shaneh committed
291 292 293
    patchText = header + _("@@ -1,2 +1,1 @@\n"
                           " ABCD\n"
                           "-\n");
jkobus's avatar
jkobus committed
294

jkobus's avatar
jkobus committed
295
    QTest::newRow("Last empty line removed") << chunk
jkobus's avatar
jkobus committed
296 297 298 299 300 301 302
                            << fileName
                            << fileName
                            << false
                            << patchText;

    ///////////

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    rows.clear();
    rows << RowData(_("ABCD"));
    rows << RowData(_(""), TextLineData::Separator);
    rows << RowData(_(""), TextLineData::Separator);
    chunk.rows = rows;
    patchText = header + _("@@ -1,2 +1,1 @@\n"
                           "-ABCD\n"
                           "-\n"
                           "+ABCD\n"
                           "\\ No newline at end of file\n");

    QTest::newRow("Two last EOLs removed") << chunk
                            << fileName
                            << fileName
                            << true
                            << patchText;

    ///////////

jkobus's avatar
jkobus committed
322
    rows.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
323 324
    rows << RowData(_("ABCD"));
    rows << RowData(TextLineData::Separator, _(""));
jkobus's avatar
jkobus committed
325
    chunk.rows = rows;
Orgad Shaneh's avatar
Orgad Shaneh committed
326 327 328 329
    patchText = header + _("@@ -1,1 +1,1 @@\n"
                           "-ABCD\n"
                           "\\ No newline at end of file\n"
                           "+ABCD\n");
jkobus's avatar
jkobus committed
330

jkobus's avatar
jkobus committed
331
    QTest::newRow("EOL to last line added") << chunk
jkobus's avatar
jkobus committed
332 333 334 335 336 337 338 339
                            << fileName
                            << fileName
                            << true
                            << patchText;

    ///////////

    // chunk the same here
Orgad Shaneh's avatar
Orgad Shaneh committed
340 341 342
    patchText = header + _("@@ -1,1 +1,2 @@\n"
                           " ABCD\n"
                           "+\n");
jkobus's avatar
jkobus committed
343

jkobus's avatar
jkobus committed
344
    QTest::newRow("Last empty line added") << chunk
jkobus's avatar
jkobus committed
345 346 347 348 349 350 351 352
                            << fileName
                            << fileName
                            << false
                            << patchText;

    ///////////

    rows.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
353
    rows << RowData(_("ABCD"), _("EFGH"));
jkobus's avatar
jkobus committed
354
    chunk.rows = rows;
Orgad Shaneh's avatar
Orgad Shaneh committed
355 356 357
    patchText = header + _("@@ -1,1 +1,1 @@\n"
                           "-ABCD\n"
                           "+EFGH\n");
jkobus's avatar
jkobus committed
358 359 360 361

    QTest::newRow("Last line with a newline modified") << chunk
                            << fileName
                            << fileName
jkobus's avatar
jkobus committed
362
                            << false
jkobus's avatar
jkobus committed
363 364 365 366
                            << patchText;

    ///////////

jkobus's avatar
jkobus committed
367
    rows.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
368 369
    rows << RowData(_("ABCD"), _("EFGH"));
    rows << RowData(_(""));
jkobus's avatar
jkobus committed
370
    chunk.rows = rows;
Orgad Shaneh's avatar
Orgad Shaneh committed
371 372 373 374
    patchText = header + _("@@ -1,2 +1,2 @@\n"
                           "-ABCD\n"
                           "+EFGH\n"
                           " \n");
jkobus's avatar
jkobus committed
375 376 377 378 379 380 381 382 383
    QTest::newRow("Not a last line with a newline modified") << chunk
                            << fileName
                            << fileName
                            << false
                            << patchText;

    ///////////

    rows.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
384
    rows << RowData(_("ABCD"), _("EFGH"));
jkobus's avatar
jkobus committed
385
    chunk.rows = rows;
Orgad Shaneh's avatar
Orgad Shaneh committed
386 387 388 389 390
    patchText = header + _("@@ -1,1 +1,1 @@\n"
                           "-ABCD\n"
                           "\\ No newline at end of file\n"
                           "+EFGH\n"
                           "\\ No newline at end of file\n");
jkobus's avatar
jkobus committed
391 392 393 394 395 396 397 398 399 400

    QTest::newRow("Last line without a newline modified") << chunk
                            << fileName
                            << fileName
                            << true
                            << patchText;

    ///////////

    // chunk the same here
Orgad Shaneh's avatar
Orgad Shaneh committed
401 402 403
    patchText = header + _("@@ -1,1 +1,1 @@\n"
                           "-ABCD\n"
                           "+EFGH\n");
jkobus's avatar
jkobus committed
404 405 406 407 408 409 410 411 412
    QTest::newRow("Not a last line without a newline modified") << chunk
                            << fileName
                            << fileName
                            << false
                            << patchText;

    ///////////

    rows.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
413 414
    rows << RowData(_("ABCD"), _("EFGH"));
    rows << RowData(_("IJKL"));
jkobus's avatar
jkobus committed
415
    chunk.rows = rows;
Orgad Shaneh's avatar
Orgad Shaneh committed
416 417 418 419 420
    patchText = header + _("@@ -1,2 +1,2 @@\n"
                           "-ABCD\n"
                           "+EFGH\n"
                           " IJKL\n"
                           "\\ No newline at end of file\n");
jkobus's avatar
jkobus committed
421 422 423 424 425 426 427 428 429 430 431

    QTest::newRow("Last but one line modified, last line without a newline")
            << chunk
            << fileName
            << fileName
            << true
            << patchText;

    ///////////

    // chunk the same here
Orgad Shaneh's avatar
Orgad Shaneh committed
432 433 434 435
    patchText = header + _("@@ -1,2 +1,2 @@\n"
                           "-ABCD\n"
                           "+EFGH\n"
                           " IJKL\n");
jkobus's avatar
jkobus committed
436 437 438 439 440 441 442

    QTest::newRow("Last but one line modified, last line with a newline")
            << chunk
            << fileName
            << fileName
            << false
            << patchText;
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462

    ///////////

    rows.clear();
    rows << RowData(_("ABCD"));
    rows << RowData(TextLineData::Separator, _(""));
    rows << RowData(_(""), _("EFGH"));
    chunk.rows = rows;
    patchText = header + _("@@ -1,1 +1,3 @@\n"
                           " ABCD\n"
                           "+\n"
                           "+EFGH\n"
                           "\\ No newline at end of file\n");

    QTest::newRow("Blank line followed by No newline")
            << chunk
            << fileName
            << fileName
            << true
            << patchText;
463 464
}

jkobus's avatar
jkobus committed
465 466 467 468 469 470 471 472 473 474
void DiffEditor::Internal::DiffEditorPlugin::testMakePatch()
{
    QFETCH(ChunkData, sourceChunk);
    QFETCH(QString, leftFileName);
    QFETCH(QString, rightFileName);
    QFETCH(bool, lastChunk);
    QFETCH(QString, patchText);

    QString result = DiffUtils::makePatch(sourceChunk, leftFileName, rightFileName, lastChunk);

jkobus's avatar
jkobus committed
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    QCOMPARE(result, patchText);

    bool ok;
    QList<FileData> resultList = DiffUtils::readPatch(result, false, &ok);

    QVERIFY(ok);
    QCOMPARE(resultList.count(), 1);
    for (int i = 0; i < resultList.count(); i++) {
        const FileData &resultFileData = resultList.at(i);
        QCOMPARE(resultFileData.leftFileInfo.fileName, leftFileName);
        QCOMPARE(resultFileData.rightFileInfo.fileName, rightFileName);
        QCOMPARE(resultFileData.chunks.count(), 1);
        for (int j = 0; j < resultFileData.chunks.count(); j++) {
            const ChunkData &resultChunkData = resultFileData.chunks.at(j);
            QCOMPARE(resultChunkData.leftStartingLineNumber, sourceChunk.leftStartingLineNumber);
            QCOMPARE(resultChunkData.rightStartingLineNumber, sourceChunk.rightStartingLineNumber);
            QCOMPARE(resultChunkData.contextChunk, sourceChunk.contextChunk);
            QCOMPARE(resultChunkData.rows.count(), sourceChunk.rows.count());
            for (int k = 0; k < sourceChunk.rows.count(); k++) {
                const RowData &sourceRowData = sourceChunk.rows.at(k);
                const RowData &resultRowData = resultChunkData.rows.at(k);
                QCOMPARE(resultRowData.equal, sourceRowData.equal);
                QCOMPARE(resultRowData.leftLine.text, sourceRowData.leftLine.text);
                QCOMPARE(resultRowData.leftLine.textLineType, sourceRowData.leftLine.textLineType);
                QCOMPARE(resultRowData.rightLine.text, sourceRowData.rightLine.text);
                QCOMPARE(resultRowData.rightLine.textLineType, sourceRowData.rightLine.textLineType);
            }
        }
    }
jkobus's avatar
jkobus committed
504 505 506 507 508 509 510
}

void DiffEditor::Internal::DiffEditorPlugin::testReadPatch_data()
{
    QTest::addColumn<QString>("sourcePatch");
    QTest::addColumn<QList<FileData> >("fileDataList");

Orgad Shaneh's avatar
Orgad Shaneh committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
    QString patch = _("diff --git a/src/plugins/diffeditor/diffeditor.cpp b/src/plugins/diffeditor/diffeditor.cpp\n"
                      "index eab9e9b..082c135 100644\n"
                      "--- a/src/plugins/diffeditor/diffeditor.cpp\n"
                      "+++ b/src/plugins/diffeditor/diffeditor.cpp\n"
                      "@@ -187,9 +187,6 @@ void DiffEditor::ctor()\n"
                      "     m_controller = m_document->controller();\n"
                      "     m_guiController = new DiffEditorGuiController(m_controller, this);\n"
                      " \n"
                      "-//    m_sideBySideEditor->setDiffEditorGuiController(m_guiController);\n"
                      "-//    m_unifiedEditor->setDiffEditorGuiController(m_guiController);\n"
                      "-\n"
                      "     connect(m_controller, SIGNAL(cleared(QString)),\n"
                      "             this, SLOT(slotCleared(QString)));\n"
                      "     connect(m_controller, SIGNAL(diffContentsChanged(QList<DiffEditorController::DiffFilesContents>,QString)),\n"
                      "diff --git a/src/plugins/diffeditor/diffutils.cpp b/src/plugins/diffeditor/diffutils.cpp\n"
                      "index 2f641c9..f8ff795 100644\n"
                      "--- a/src/plugins/diffeditor/diffutils.cpp\n"
                      "+++ b/src/plugins/diffeditor/diffutils.cpp\n"
                      "@@ -464,5 +464,12 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,\n"
                      "     return diffText;\n"
                      " }\n"
                      " \n"
                      "+FileData DiffUtils::makeFileData(const QString &patch)\n"
                      "+{\n"
                      "+    FileData fileData;\n"
                      "+\n"
                      "+    return fileData;\n"
                      "+}\n"
                      "+\n"
                      " } // namespace Internal\n"
                      " } // namespace DiffEditor\n"
                      "diff --git a/new b/new\n"
                      "new file mode 100644\n"
                      "index 0000000..257cc56\n"
                      "--- /dev/null\n"
                      "+++ b/new\n"
                      "@@ -0,0 +1 @@\n"
                      "+foo\n"
                      "diff --git a/deleted b/deleted\n"
                      "deleted file mode 100644\n"
                      "index 257cc56..0000000\n"
                      "--- a/deleted\n"
                      "+++ /dev/null\n"
                      "@@ -1 +0,0 @@\n"
                      "-foo\n"
                      "diff --git a/empty b/empty\n"
                      "new file mode 100644\n"
                      "index 0000000..e69de29\n"
                      "diff --git a/empty b/empty\n"
                      "deleted file mode 100644\n"
                      "index e69de29..0000000\n"
                      "diff --git a/file a.txt b/file b.txt\n"
                      "similarity index 99%\n"
                      "copy from file a.txt\n"
                      "copy to file b.txt\n"
                      "index 1234567..9876543\n"
                      "--- a/file a.txt\n"
                      "+++ b/file b.txt\n"
                      "@@ -20,3 +20,3 @@\n"
                      " A\n"
                      "-B\n"
                      "+C\n"
                      " D\n"
                      "diff --git a/file a.txt b/file b.txt\n"
                      "similarity index 99%\n"
                      "rename from file a.txt\n"
                      "rename to file b.txt\n"
                      );
jkobus's avatar
jkobus committed
579 580

    FileData fileData1;
Orgad Shaneh's avatar
Orgad Shaneh committed
581 582
    fileData1.leftFileInfo = DiffFileInfo(_("src/plugins/diffeditor/diffeditor.cpp"), _("eab9e9b"));
    fileData1.rightFileInfo = DiffFileInfo(_("src/plugins/diffeditor/diffeditor.cpp"), _("082c135"));
jkobus's avatar
jkobus committed
583
    ChunkData chunkData1;
Orgad Shaneh's avatar
Orgad Shaneh committed
584 585
    chunkData1.leftStartingLineNumber = 186;
    chunkData1.rightStartingLineNumber = 186;
jkobus's avatar
jkobus committed
586
    QList<RowData> rows1;
Orgad Shaneh's avatar
Orgad Shaneh committed
587 588 589 590 591 592 593 594 595
    rows1 << RowData(_("    m_controller = m_document->controller();"));
    rows1 << RowData(_("    m_guiController = new DiffEditorGuiController(m_controller, this);"));
    rows1 << RowData(_(""));
    rows1 << RowData(_("//    m_sideBySideEditor->setDiffEditorGuiController(m_guiController);"), TextLineData::Separator);
    rows1 << RowData(_("//    m_unifiedEditor->setDiffEditorGuiController(m_guiController);"), TextLineData::Separator);
    rows1 << RowData(_(""), TextLineData::Separator);
    rows1 << RowData(_("    connect(m_controller, SIGNAL(cleared(QString)),"));
    rows1 << RowData(_("            this, SLOT(slotCleared(QString)));"));
    rows1 << RowData(_("    connect(m_controller, SIGNAL(diffContentsChanged(QList<DiffEditorController::DiffFilesContents>,QString)),"));
jkobus's avatar
jkobus committed
596
    chunkData1.rows = rows1;
Orgad Shaneh's avatar
Orgad Shaneh committed
597
    fileData1.chunks << chunkData1;
jkobus's avatar
jkobus committed
598 599

    FileData fileData2;
Orgad Shaneh's avatar
Orgad Shaneh committed
600 601
    fileData2.leftFileInfo = DiffFileInfo(_("src/plugins/diffeditor/diffutils.cpp"), _("2f641c9"));
    fileData2.rightFileInfo = DiffFileInfo(_("src/plugins/diffeditor/diffutils.cpp"), _("f8ff795"));
jkobus's avatar
jkobus committed
602
    ChunkData chunkData2;
Orgad Shaneh's avatar
Orgad Shaneh committed
603 604
    chunkData2.leftStartingLineNumber = 463;
    chunkData2.rightStartingLineNumber = 463;
jkobus's avatar
jkobus committed
605
    QList<RowData> rows2;
Orgad Shaneh's avatar
Orgad Shaneh committed
606 607 608 609 610 611 612 613 614 615 616 617
    rows2 << RowData(_("    return diffText;"));
    rows2 << RowData(_("}"));
    rows2 << RowData(_(""));
    rows2 << RowData(TextLineData::Separator, _("FileData DiffUtils::makeFileData(const QString &patch)"));
    rows2 << RowData(TextLineData::Separator, _("{"));
    rows2 << RowData(TextLineData::Separator, _("    FileData fileData;"));
    rows2 << RowData(TextLineData::Separator, _(""));
    rows2 << RowData(TextLineData::Separator, _("    return fileData;"));
    rows2 << RowData(TextLineData::Separator, _("}"));
    rows2 << RowData(TextLineData::Separator, _(""));
    rows2 << RowData(_("} // namespace Internal"));
    rows2 << RowData(_("} // namespace DiffEditor"));
jkobus's avatar
jkobus committed
618
    chunkData2.rows = rows2;
Orgad Shaneh's avatar
Orgad Shaneh committed
619
    fileData2.chunks << chunkData2;
jkobus's avatar
jkobus committed
620

621
    FileData fileData3;
Orgad Shaneh's avatar
Orgad Shaneh committed
622 623
    fileData3.leftFileInfo = DiffFileInfo(_("new"), _("0000000"));
    fileData3.rightFileInfo = DiffFileInfo(_("new"), _("257cc56"));
624
    fileData3.fileOperation = FileData::NewFile;
625 626 627 628
    ChunkData chunkData3;
    chunkData3.leftStartingLineNumber = -1;
    chunkData3.rightStartingLineNumber = 0;
    QList<RowData> rows3;
Orgad Shaneh's avatar
Orgad Shaneh committed
629
    rows3 << RowData(TextLineData::Separator, _("foo"));
630
    TextLineData textLineData3(TextLineData::TextLine);
Orgad Shaneh's avatar
Orgad Shaneh committed
631
    rows3 << RowData(TextLineData::Separator, textLineData3);
632
    chunkData3.rows = rows3;
Orgad Shaneh's avatar
Orgad Shaneh committed
633
    fileData3.chunks << chunkData3;
634 635

    FileData fileData4;
Orgad Shaneh's avatar
Orgad Shaneh committed
636 637
    fileData4.leftFileInfo = DiffFileInfo(_("deleted"), _("257cc56"));
    fileData4.rightFileInfo = DiffFileInfo(_("deleted"), _("0000000"));
638
    fileData4.fileOperation = FileData::DeleteFile;
639 640 641 642
    ChunkData chunkData4;
    chunkData4.leftStartingLineNumber = 0;
    chunkData4.rightStartingLineNumber = -1;
    QList<RowData> rows4;
Orgad Shaneh's avatar
Orgad Shaneh committed
643
    rows4 << RowData(_("foo"), TextLineData::Separator);
644
    TextLineData textLineData4(TextLineData::TextLine);
Orgad Shaneh's avatar
Orgad Shaneh committed
645
    rows4 << RowData(textLineData4, TextLineData::Separator);
646
    chunkData4.rows = rows4;
Orgad Shaneh's avatar
Orgad Shaneh committed
647
    fileData4.chunks << chunkData4;
648

649
    FileData fileData5;
Orgad Shaneh's avatar
Orgad Shaneh committed
650 651
    fileData5.leftFileInfo = DiffFileInfo(_("empty"), _("0000000"));
    fileData5.rightFileInfo = DiffFileInfo(_("empty"), _("e69de29"));
652
    fileData5.fileOperation = FileData::NewFile;
653 654

    FileData fileData6;
Orgad Shaneh's avatar
Orgad Shaneh committed
655 656
    fileData6.leftFileInfo = DiffFileInfo(_("empty"), _("e69de29"));
    fileData6.rightFileInfo = DiffFileInfo(_("empty"), _("0000000"));
657 658 659
    fileData6.fileOperation = FileData::DeleteFile;

    FileData fileData7;
Orgad Shaneh's avatar
Orgad Shaneh committed
660 661
    fileData7.leftFileInfo = DiffFileInfo(_("file a.txt"), _("1234567"));
    fileData7.rightFileInfo = DiffFileInfo(_("file b.txt"), _("9876543"));
662 663 664 665 666
    fileData7.fileOperation = FileData::CopyFile;
    ChunkData chunkData7;
    chunkData7.leftStartingLineNumber = 19;
    chunkData7.rightStartingLineNumber = 19;
    QList<RowData> rows7;
Orgad Shaneh's avatar
Orgad Shaneh committed
667 668 669
    rows7 << RowData(_("A"));
    rows7 << RowData(_("B"), _("C"));
    rows7 << RowData(_("D"));
670
    chunkData7.rows = rows7;
Orgad Shaneh's avatar
Orgad Shaneh committed
671
    fileData7.chunks << chunkData7;
672 673

    FileData fileData8;
Orgad Shaneh's avatar
Orgad Shaneh committed
674 675
    fileData8.leftFileInfo = DiffFileInfo(_("file a.txt"));
    fileData8.rightFileInfo = DiffFileInfo(_("file b.txt"));
676
    fileData8.fileOperation = FileData::RenameFile;
677

jkobus's avatar
jkobus committed
678 679
    QList<FileData> fileDataList1;
    fileDataList1 << fileData1 << fileData2 << fileData3 << fileData4 << fileData5 << fileData6 << fileData7 << fileData8;
jkobus's avatar
jkobus committed
680 681

    QTest::newRow("Git patch") << patch
jkobus's avatar
jkobus committed
682 683 684 685
                               << fileDataList1;

    //////////////

Orgad Shaneh's avatar
Orgad Shaneh committed
686 687 688 689 690 691 692 693 694 695 696 697
    patch = _("diff --git a/file foo.txt b/file foo.txt\n"
              "index 1234567..9876543 100644\n"
              "--- a/file foo.txt\n"
              "+++ b/file foo.txt\n"
              "@@ -50,4 +50,5 @@ void DiffEditor::ctor()\n"
              " A\n"
              " B\n"
              " C\n"
              "+\n");

    fileData1.leftFileInfo = DiffFileInfo(_("file foo.txt"), _("1234567"));
    fileData1.rightFileInfo = DiffFileInfo(_("file foo.txt"), _("9876543"));
jkobus's avatar
jkobus committed
698 699 700 701
    fileData1.fileOperation = FileData::ChangeFile;
    chunkData1.leftStartingLineNumber = 49;
    chunkData1.rightStartingLineNumber = 49;
    rows1.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
702 703 704 705
    rows1 << RowData(_("A"));
    rows1 << RowData(_("B"));
    rows1 << RowData(_("C"));
    rows1 << RowData(TextLineData::Separator, _(""));
jkobus's avatar
jkobus committed
706 707
    chunkData1.rows = rows1;
    fileData1.chunks.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
708
    fileData1.chunks << chunkData1;
jkobus's avatar
jkobus committed
709 710 711 712 713 714 715 716 717

    QList<FileData> fileDataList2;
    fileDataList2 << fileData1;

    QTest::newRow("Added line") << patch
                                << fileDataList2;

    //////////////

Orgad Shaneh's avatar
Orgad Shaneh committed
718 719 720 721 722 723 724 725 726 727 728
    patch = _("diff --git a/file foo.txt b/file foo.txt\n"
              "index 1234567..9876543 100644\n"
              "--- a/file foo.txt\n"
              "+++ b/file foo.txt\n"
              "@@ -1,1 +1,1 @@\n"
              "-ABCD\n"
              "\\ No newline at end of file\n"
              "+ABCD\n");

    fileData1.leftFileInfo = DiffFileInfo(_("file foo.txt"), _("1234567"));
    fileData1.rightFileInfo = DiffFileInfo(_("file foo.txt"), _("9876543"));
jkobus's avatar
jkobus committed
729 730 731 732
    fileData1.fileOperation = FileData::ChangeFile;
    chunkData1.leftStartingLineNumber = 0;
    chunkData1.rightStartingLineNumber = 0;
    rows1.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
733 734
    rows1 << RowData(_("ABCD"));
    rows1 << RowData(TextLineData::Separator, _(""));
jkobus's avatar
jkobus committed
735 736
    chunkData1.rows = rows1;
    fileData1.chunks.clear();
Orgad Shaneh's avatar
Orgad Shaneh committed
737
    fileData1.chunks << chunkData1;
jkobus's avatar
jkobus committed
738 739 740 741 742 743

    QList<FileData> fileDataList3;
    fileDataList3 << fileData1;

    QTest::newRow("Last newline added to a line without newline") << patch
                                << fileDataList3;
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

    patch = _("diff --git a/difftest.txt b/difftest.txt\n"
              "index 1234567..9876543 100644\n"
              "--- a/difftest.txt\n"
              "+++ b/difftest.txt\n"
              "@@ -2,5 +2,5 @@ void func()\n"
              " A\n"
              " B\n"
              "-C\n"
              "+Z\n"
              " D\n"
              " \n"
              "@@ -9,2 +9,4 @@ void OtherFunc()\n"
              " \n"
              " D\n"
              "+E\n"
              "+F\n"
              );

    fileData1.leftFileInfo = DiffFileInfo(_("difftest.txt"), _("1234567"));
    fileData1.rightFileInfo = DiffFileInfo(_("difftest.txt"), _("9876543"));
    fileData1.fileOperation = FileData::ChangeFile;
    chunkData1.leftStartingLineNumber = 1;
    chunkData1.rightStartingLineNumber = 1;
    rows1.clear();
    rows1 << RowData(_("A"));
    rows1 << RowData(_("B"));
    rows1 << RowData(_("C"), _("Z"));
    rows1 << RowData(_("D"));
    rows1 << RowData(_(""));
    chunkData1.rows = rows1;

    chunkData2.leftStartingLineNumber = 8;
    chunkData2.rightStartingLineNumber = 8;
    rows2.clear();
    rows2 << RowData(_(""));
    rows2 << RowData(_("D"));
    rows2 << RowData(TextLineData::Separator, _("E"));
    rows2 << RowData(TextLineData::Separator, _("F"));
    chunkData2.rows = rows2;
    fileData1.chunks.clear();
    fileData1.chunks << chunkData1;
    fileData1.chunks << chunkData2;

    QList<FileData> fileDataList4;
    fileDataList4 << fileData1;

    QTest::newRow("2 chunks - first ends with blank line") << patch
                                << fileDataList4;
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823

    //////////////

    patch = _("diff --git a/file foo.txt b/file foo.txt\n"
              "index 1234567..9876543 100644\n"
              "--- a/file foo.txt\n"
              "+++ b/file foo.txt\n"
              "@@ -1,1 +1,3 @@ void DiffEditor::ctor()\n"
              " ABCD\n"
              "+\n"
              "+EFGH\n"
              "\\ No newline at end of file\n");

    fileData1.leftFileInfo = DiffFileInfo(_("file foo.txt"), _("1234567"));
    fileData1.rightFileInfo = DiffFileInfo(_("file foo.txt"), _("9876543"));
    fileData1.fileOperation = FileData::ChangeFile;
    chunkData1.leftStartingLineNumber = 0;
    chunkData1.rightStartingLineNumber = 0;
    rows1.clear();
    rows1 << RowData(_("ABCD"));
    rows1 << RowData(TextLineData::Separator, _(""));
    rows1 << RowData(_(""), _("EFGH"));
    chunkData1.rows = rows1;
    fileData1.chunks.clear();
    fileData1.chunks << chunkData1;

    QList<FileData> fileDataList5;
    fileDataList5 << fileData1;

    QTest::newRow("Blank line followed by No newline") << patch
                                << fileDataList5;
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

    //////////////

    // Based on 953cdb97
    patch = _("diff --git a/src/plugins/texteditor/basetextdocument.h b/src/plugins/texteditor/textdocument.h\n"
              "similarity index 100%\n"
              "rename from src/plugins/texteditor/basetextdocument.h\n"
              "rename to src/plugins/texteditor/textdocument.h\n"
              "diff --git a/src/plugins/texteditor/basetextdocumentlayout.cpp b/src/plugins/texteditor/textdocumentlayout.cpp\n"
              "similarity index 79%\n"
              "rename from src/plugins/texteditor/basetextdocumentlayout.cpp\n"
              "rename to src/plugins/texteditor/textdocumentlayout.cpp\n"
              "index 0121933..01cc3a0 100644\n"
              "--- a/src/plugins/texteditor/basetextdocumentlayout.cpp\n"
              "+++ b/src/plugins/texteditor/textdocumentlayout.cpp\n"
              "@@ -2,5 +2,5 @@ void func()\n"
              " A\n"
              " B\n"
              "-C\n"
              "+Z\n"
              " D\n"
              " \n"
              );

    fileData1 = FileData();
    fileData1.leftFileInfo = DiffFileInfo(_("src/plugins/texteditor/basetextdocument.h"));
    fileData1.rightFileInfo = DiffFileInfo(_("src/plugins/texteditor/textdocument.h"));
    fileData1.fileOperation = FileData::RenameFile;
    fileData2 = FileData();
    fileData2.leftFileInfo = DiffFileInfo(_("src/plugins/texteditor/basetextdocumentlayout.cpp"), _("0121933"));
    fileData2.rightFileInfo = DiffFileInfo(_("src/plugins/texteditor/textdocumentlayout.cpp"), _("01cc3a0"));
    fileData2.fileOperation = FileData::RenameFile;
    chunkData2.leftStartingLineNumber = 1;
    chunkData2.rightStartingLineNumber = 1;
    rows2.clear();
    rows2 << RowData(_("A"));
    rows2 << RowData(_("B"));
    rows2 << RowData(_("C"), _("Z"));
    rows2 << RowData(_("D"));
    rows2 << RowData(_(""));
    chunkData2.rows = rows2;
    fileData2.chunks.clear();
    fileData2.chunks << chunkData2;

    QList<FileData> fileDataList6;
    fileDataList6 << fileData1 << fileData2;

    QTest::newRow("Multiple renames") << patch
                                      << fileDataList6;

874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
    //////////////

    // Dirty submodule
    patch = _("diff --git a/src/shared/qbs b/src/shared/qbs\n"
              "--- a/src/shared/qbs\n"
              "+++ b/src/shared/qbs\n"
              "@@ -1 +1 @@\n"
              "-Subproject commit eda76354077a427d692fee05479910de31040d3f\n"
              "+Subproject commit eda76354077a427d692fee05479910de31040d3f-dirty\n"
              );
    fileData1 = FileData();
    fileData1.leftFileInfo = DiffFileInfo(_("src/shared/qbs"));
    fileData1.rightFileInfo = DiffFileInfo(_("src/shared/qbs"));
    chunkData1.leftStartingLineNumber = 0;
    chunkData1.rightStartingLineNumber = 0;
    rows1.clear();
    rows1 << RowData(_("Subproject commit eda76354077a427d692fee05479910de31040d3f"),
                     _("Subproject commit eda76354077a427d692fee05479910de31040d3f-dirty"));
    chunkData1.rows = rows1;
    fileData1.chunks.clear();
    fileData1.chunks <<  chunkData1;

    QList<FileData> fileDataList7;
    fileDataList7 << fileData1;
    QTest::newRow("Dirty submodule") << patch
                                     << fileDataList7;
jkobus's avatar
jkobus committed
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
}

void DiffEditor::Internal::DiffEditorPlugin::testReadPatch()
{
    QFETCH(QString, sourcePatch);
    QFETCH(QList<FileData>, fileDataList);

    bool ok;
    QList<FileData> result = DiffUtils::readPatch(sourcePatch, false, &ok);

    QVERIFY(ok);
    QCOMPARE(fileDataList.count(), result.count());
    for (int i = 0; i < fileDataList.count(); i++) {
        const FileData &origFileData = fileDataList.at(i);
        const FileData &resultFileData = result.at(i);
Orgad Shaneh's avatar
Orgad Shaneh committed
915 916 917 918 919
        QCOMPARE(resultFileData.leftFileInfo.fileName, origFileData.leftFileInfo.fileName);
        QCOMPARE(resultFileData.leftFileInfo.typeInfo, origFileData.leftFileInfo.typeInfo);
        QCOMPARE(resultFileData.rightFileInfo.fileName, origFileData.rightFileInfo.fileName);
        QCOMPARE(resultFileData.rightFileInfo.typeInfo, origFileData.rightFileInfo.typeInfo);
        QCOMPARE(resultFileData.chunks.count(), origFileData.chunks.count());
920
        QCOMPARE(resultFileData.fileOperation, origFileData.fileOperation);
jkobus's avatar
jkobus committed
921 922 923
        for (int j = 0; j < origFileData.chunks.count(); j++) {
            const ChunkData &origChunkData = origFileData.chunks.at(j);
            const ChunkData &resultChunkData = resultFileData.chunks.at(j);
Orgad Shaneh's avatar
Orgad Shaneh committed
924 925 926 927
            QCOMPARE(resultChunkData.leftStartingLineNumber, origChunkData.leftStartingLineNumber);
            QCOMPARE(resultChunkData.rightStartingLineNumber, origChunkData.rightStartingLineNumber);
            QCOMPARE(resultChunkData.contextChunk, origChunkData.contextChunk);
            QCOMPARE(resultChunkData.rows.count(), origChunkData.rows.count());
jkobus's avatar
jkobus committed
928 929 930
            for (int k = 0; k < origChunkData.rows.count(); k++) {
                const RowData &origRowData = origChunkData.rows.at(k);
                const RowData &resultRowData = resultChunkData.rows.at(k);
Orgad Shaneh's avatar
Orgad Shaneh committed
931 932 933 934 935
                QCOMPARE(resultRowData.equal, origRowData.equal);
                QCOMPARE(resultRowData.leftLine.text, origRowData.leftLine.text);
                QCOMPARE(resultRowData.leftLine.textLineType, origRowData.leftLine.textLineType);
                QCOMPARE(resultRowData.rightLine.text, origRowData.rightLine.text);
                QCOMPARE(resultRowData.rightLine.textLineType, origRowData.rightLine.textLineType);
jkobus's avatar
jkobus committed
936 937 938 939 940 941 942 943
            }
        }
    }
}

#endif // WITH_TESTS

#include "diffeditorplugin.moc"