File indexing completed on 2024-04-14 05:37:12

0001 /*
0002     SPDX-FileCopyrightText: 2002-2004 Otto Bruggeman <otto.bruggeman@home.nl>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "cvsdiffparser.h"
0008 
0009 // lib
0010 #include "modellist.h"
0011 #include <komparediff2_logging.h>
0012 // Qt
0013 #include <QRegularExpression>
0014 
0015 using namespace KompareDiff2;
0016 
0017 CVSDiffParser::CVSDiffParser(const ModelList *list, const QStringList &diff)
0018     : ParserBase(list, diff)
0019 {
0020     // The regexps needed for context cvs diff parsing, the rest is the same as in parserbase.cpp
0021     // third capture in header1 is non optional for cvs diff, it is the revision
0022     m_contextDiffHeader1.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("\\*\\*\\* ([^\\t]+)\\t([^\\t]+)\\t(.*)\\n")));
0023     m_contextDiffHeader2.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("--- ([^\\t]+)\\t([^\\t]+)(|\\t(.*))\\n")));
0024 
0025     m_normalDiffHeader.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("Index: (.*)\\n")));
0026 }
0027 
0028 CVSDiffParser::~CVSDiffParser() = default;
0029 
0030 Format CVSDiffParser::determineFormat()
0031 {
0032     //     qCDebug(KOMPAREDIFF2_LOG) << "Determining the format of the CVSDiff";
0033 
0034     QRegularExpression normalRE(QStringLiteral("[0-9]+[0-9,]*[acd][0-9]+[0-9,]*"));
0035     QRegularExpression unifiedRE(QStringLiteral("^--- [^\\t]+\\t"));
0036     QRegularExpression contextRE(QStringLiteral("^\\*\\*\\* [^\\t]+\\t"));
0037     QRegularExpression rcsRE(QStringLiteral("^[acd][0-9]+ [0-9]+"));
0038     QRegularExpression edRE(QStringLiteral("^[0-9]+[0-9,]*[acd]"));
0039 
0040     for (const QString &diffLine : std::as_const(m_diffLines)) {
0041         if (diffLine.indexOf(normalRE, 0) == 0) {
0042 //             qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from a Normal diff...";
0043             return Normal;
0044         }
0045         if (diffLine.indexOf(unifiedRE, 0) == 0) {
0046 //             qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from a Unified diff...";
0047             return Unified;
0048         }
0049         if (diffLine.indexOf(contextRE, 0) == 0) {
0050 //             qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from a Context diff...";
0051             return Context;
0052         }
0053         if (diffLine.indexOf(rcsRE, 0) == 0) {
0054 //             qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from a RCS diff...";
0055             return RCS;
0056         }
0057         if (diffLine.indexOf(edRE, 0) == 0) {
0058 //             qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from an ED diff...";
0059             return Ed;
0060         }
0061     }
0062     //     qCDebug(KOMPAREDIFF2_LOG) << "Difflines are from an unknown diff...";
0063     return UnknownFormat;
0064 }
0065 
0066 bool CVSDiffParser::parseNormalDiffHeader()
0067 {
0068     qCDebug(KOMPAREDIFF2_LOG) << "CVSDiffParser::parseNormalDiffHeader()";
0069     bool result = false;
0070 
0071     QStringList::ConstIterator diffEnd = m_diffLines.end();
0072 
0073     while (m_diffIterator != diffEnd) {
0074         const auto normalDiffHeaderMatch = m_normalDiffHeader.match(*m_diffIterator);
0075         if (normalDiffHeaderMatch.hasMatch()) {
0076             qCDebug(KOMPAREDIFF2_LOG) << "Matched length Header = " << normalDiffHeaderMatch.capturedLength();
0077             qCDebug(KOMPAREDIFF2_LOG) << "Matched string Header = " << normalDiffHeaderMatch.captured(0);
0078 
0079             m_currentModel = new DiffModel();
0080             m_currentModel->setSourceFile(normalDiffHeaderMatch.captured(1));
0081             m_currentModel->setDestinationFile(normalDiffHeaderMatch.captured(1));
0082 
0083             result = true;
0084 
0085             ++m_diffIterator;
0086             break;
0087         } else {
0088             qCDebug(KOMPAREDIFF2_LOG) << "No match for: " << (*m_diffIterator);
0089         }
0090         ++m_diffIterator;
0091     }
0092 
0093     if (result == false) {
0094         // Set this to the first line again and hope it is a single file diff
0095         m_diffIterator = m_diffLines.begin();
0096         m_currentModel = new DiffModel();
0097         m_singleFileDiff = true;
0098     }
0099 
0100     return result;
0101 }
0102 
0103 bool CVSDiffParser::parseEdDiffHeader()
0104 {
0105     return false;
0106 }
0107 
0108 bool CVSDiffParser::parseRCSDiffHeader()
0109 {
0110     return false;
0111 }
0112 
0113 bool CVSDiffParser::parseEdHunkHeader()
0114 {
0115     return false;
0116 }
0117 
0118 bool CVSDiffParser::parseRCSHunkHeader()
0119 {
0120     return false;
0121 }
0122 
0123 bool CVSDiffParser::parseEdHunkBody()
0124 {
0125     return false;
0126 }
0127 
0128 bool CVSDiffParser::parseRCSHunkBody()
0129 {
0130     return false;
0131 }