KompareDiff2

perforceparser.cpp
1/*
2SPDX-FileCopyrightText: 2002-2004 Otto Bruggeman <otto.bruggeman@home.nl>
3
4SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "perforceparser.h"
8
9#include <QRegularExpression>
10
11#include <komparediffdebug.h>
12#include "diffmodel.h"
13
14using namespace Diff2;
15
16PerforceParser::PerforceParser(const KompareModelList* list, const QStringList& diff) : ParserBase(list, diff)
17{
18 m_contextDiffHeader1.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("==== (.*) - (.*) ====\\n")));
19 m_contextDiffHeader1.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
20 m_normalDiffHeader.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("==== (.*) - (.*) ====\\n")));
21 m_normalDiffHeader.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
22 m_rcsDiffHeader.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("==== (.*) - (.*) ====\\n")));
23 m_rcsDiffHeader.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
24 m_unifiedDiffHeader1.setPattern(QRegularExpression::anchoredPattern(QStringLiteral("==== (.*) - (.*) ====\\n")));
25 m_unifiedDiffHeader1.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
26}
27
28PerforceParser::~PerforceParser()
29{
30}
31
32enum Kompare::Format PerforceParser::determineFormat()
33{
34 qCDebug(LIBKOMPAREDIFF2) << "Determining the format of the Perforce Diff";
35
36 QRegularExpression unifiedRE(QStringLiteral("^@@"));
37 QRegularExpression contextRE(QStringLiteral("^\\*{15}"));
38 QRegularExpression normalRE(QStringLiteral("^\\d+(|,\\d+)[acd]\\d+(|,\\d+)"));
39 QRegularExpression rcsRE(QStringLiteral("^[acd]\\d+ \\d+"));
40 // Summary is not supported since it gives no useful parsable info
41
42 QStringList::ConstIterator it = m_diffLines.begin();
43
44 while (it != m_diffLines.end())
45 {
46 if (it->indexOf(unifiedRE, 0) == 0)
47 {
48 qCDebug(LIBKOMPAREDIFF2) << "Difflines are from a Unified diff...";
49 return Kompare::Unified;
50 }
51 else if (it->indexOf(contextRE, 0) == 0)
52 {
53 qCDebug(LIBKOMPAREDIFF2) << "Difflines are from a Context diff...";
54 return Kompare::Context;
55 }
56 else if (it->indexOf(normalRE, 0) == 0)
57 {
58 qCDebug(LIBKOMPAREDIFF2) << "Difflines are from a Normal diff...";
59 return Kompare::Normal;
60 }
61 else if (it->indexOf(rcsRE, 0) == 0)
62 {
63 qCDebug(LIBKOMPAREDIFF2) << "Difflines are from a RCS diff...";
64 return Kompare::RCS;
65 }
66 ++it;
67 }
68 qCDebug(LIBKOMPAREDIFF2) << "Difflines are from an unknown diff...";
69 return Kompare::UnknownFormat;
70}
71
72bool PerforceParser::parseContextDiffHeader()
73{
74// qCDebug(LIBKOMPAREDIFF2) << "ParserBase::parseContextDiffHeader()";
75 bool result = false;
76
77 QStringList::ConstIterator itEnd = m_diffLines.end();
78
79 const QRegularExpression sourceFileRE(QRegularExpression::anchoredPattern(QStringLiteral("([^\\#]+)#(\\d+)")));
80 const QRegularExpression destinationFileRE(QRegularExpression::anchoredPattern(QStringLiteral("([^\\#]+)#(|\\d+)")));
81
82 while (m_diffIterator != itEnd)
83 {
84 const auto contextDiffHeader1Match = m_contextDiffHeader1.match(*(m_diffIterator)++);
85 if (contextDiffHeader1Match.hasMatch())
86 {
87// qCDebug(LIBKOMPAREDIFF2) << "Matched length Header1 = " << contextDiffHeader1Match.capturedLength();
88// qCDebug(LIBKOMPAREDIFF2) << "Matched string Header1 = " << contextDiffHeader1Match.captured( 0 );
89// qCDebug(LIBKOMPAREDIFF2) << "First capture Header1 = " << contextDiffHeader1Match.captured( 1 );
90// qCDebug(LIBKOMPAREDIFF2) << "Second capture Header1 = " << contextDiffHeader1Match.captured( 2 );
91
92 m_currentModel = new DiffModel();
93 const auto sourceFileREMatch = sourceFileRE.match(contextDiffHeader1Match.captured(1));
94 const auto destinationFileREMatch = destinationFileRE.match(contextDiffHeader1Match.captured(2));
95 qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << sourceFileREMatch.capturedLength();
96 qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << destinationFileREMatch.capturedLength();
97 qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << sourceFileREMatch.capturedTexts();
98 qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << destinationFileREMatch.capturedTexts();
99 qCDebug(LIBKOMPAREDIFF2) << "Source File : " << sourceFileREMatch.captured(1);
100 qCDebug(LIBKOMPAREDIFF2) << "Destination File : " << destinationFileREMatch.captured(1);
101 m_currentModel->setSourceFile(sourceFileREMatch.captured(1));
102 m_currentModel->setDestinationFile(destinationFileREMatch.captured(1));
103
104 result = true;
105
106 break;
107 }
108 else
109 {
110 qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << contextDiffHeader1Match.capturedLength();
111 qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << contextDiffHeader1Match.capturedTexts();
112 }
113 }
114
115 return result;
116}
117
118bool PerforceParser::parseNormalDiffHeader()
119{
120 bool result = false;
121
122 QStringList::ConstIterator itEnd = m_diffLines.end();
123
124 QRegularExpression sourceFileRE(QRegularExpression::anchoredPattern(QStringLiteral("([^\\#]+)#(\\d+)")));
125 QRegularExpression destinationFileRE(QRegularExpression::anchoredPattern(QStringLiteral("([^\\#]+)#(|\\d+)")));
126
127 while (m_diffIterator != itEnd)
128 {
129 qCDebug(LIBKOMPAREDIFF2) << "Line = " << *m_diffIterator;
130 qCDebug(LIBKOMPAREDIFF2) << "String length = " << (*m_diffIterator).length();
131 const auto normalDiffHeaderMatch = m_normalDiffHeader.match(*(m_diffIterator)++);
132 if (normalDiffHeaderMatch.hasMatch())
133 {
134 qCDebug(LIBKOMPAREDIFF2) << "Matched length Header1 = " << normalDiffHeaderMatch.capturedLength();
135 qCDebug(LIBKOMPAREDIFF2) << "Matched string Header1 = " << normalDiffHeaderMatch.captured(0);
136 qCDebug(LIBKOMPAREDIFF2) << "First capture Header1 = \"" << normalDiffHeaderMatch.captured(1) << "\"";
137 qCDebug(LIBKOMPAREDIFF2) << "Second capture Header1 = \"" << normalDiffHeaderMatch.captured(2) << "\"";
138
139 m_currentModel = new DiffModel();
140 const auto sourceFileREMatch = sourceFileRE.match(normalDiffHeaderMatch.captured(1));
141 const auto destinationFileREMatch = destinationFileRE.match(normalDiffHeaderMatch.captured(2));
142 qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << sourceFileREMatch.capturedLength();
143 qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << destinationFileREMatch.capturedLength();
144 qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << sourceFileREMatch.capturedTexts();
145 qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << destinationFileREMatch.capturedTexts();
146 qCDebug(LIBKOMPAREDIFF2) << "Source File : " << sourceFileREMatch.captured(1);
147 qCDebug(LIBKOMPAREDIFF2) << "Destination File : " << destinationFileREMatch.captured(1);
148 m_currentModel->setSourceFile(sourceFileREMatch.captured(1));
149 m_currentModel->setDestinationFile(destinationFileREMatch.captured(1));
150
151 result = true;
152
153 break;
154 }
155 else
156 {
157 qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << normalDiffHeaderMatch.capturedLength();
158 qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << normalDiffHeaderMatch.capturedTexts();
159 }
160 }
161
162 return result;
163}
164
165bool PerforceParser::parseRCSDiffHeader()
166{
167 return false;
168}
169
170bool PerforceParser::parseUnifiedDiffHeader()
171{
172 bool result = false;
173
174 QStringList::ConstIterator itEnd = m_diffLines.end();
175
176 QRegularExpression sourceFileRE(QRegularExpression::anchoredPattern(QStringLiteral("([^\\#]+)#(\\d+)")));
177 QRegularExpression destinationFileRE(QRegularExpression::anchoredPattern(QStringLiteral("([^\\#]+)#(|\\d+)")));
178
179 while (m_diffIterator != itEnd)
180 {
181// qCDebug(LIBKOMPAREDIFF2) << "Line = " << *m_diffIterator;
182// qCDebug(LIBKOMPAREDIFF2) << "String length = " << (*m_diffIterator).length();
183 const auto unifiedDiffHeader1Match = m_unifiedDiffHeader1.match(*(m_diffIterator)++);
184 if (unifiedDiffHeader1Match.hasMatch())
185 {
186// qCDebug(LIBKOMPAREDIFF2) << "Matched length Header1 = " << unifiedDiffHeader1Match.capturedLength();
187// qCDebug(LIBKOMPAREDIFF2) << "Matched string Header1 = " << unifiedDiffHeader1Match.captured( 0 );
188// qCDebug(LIBKOMPAREDIFF2) << "First capture Header1 = \"" << unifiedDiffHeader1Match.captured( 1 ) << "\"";
189// qCDebug(LIBKOMPAREDIFF2) << "Second capture Header1 = \"" << unifiedDiffHeader1Match.captured( 2 ) << "\"";
190
191 m_currentModel = new DiffModel();
192 const auto sourceFileREMatch = sourceFileRE.match(unifiedDiffHeader1Match.captured(1));
193 const auto destinationFileREMatch = destinationFileRE.match(unifiedDiffHeader1Match.captured(2));
194// qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << sourceFileREMatch.capturedLength();
195// qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << destinationFileREMatch.capturedLength();
196// qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << sourceFileREMatch.capturedTexts();
197// qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << destinationFileREMatch.capturedTexts();
198// qCDebug(LIBKOMPAREDIFF2) << "Source File : " << sourceFileREMatch.captured( 1 );
199// qCDebug(LIBKOMPAREDIFF2) << "Destination File : " << destinationFileREMatch.captured( 1 );
200 m_currentModel->setSourceFile(sourceFileREMatch.captured(1));
201 m_currentModel->setDestinationFile(destinationFileREMatch.captured(1));
202
203 result = true;
204
205 break;
206 }
207 else
208 {
209// qCDebug(LIBKOMPAREDIFF2) << "Matched length = " << unifiedDiffHeader1Match.capturedLength();
210// qCDebug(LIBKOMPAREDIFF2) << "Captured texts = " << unifiedDiffHeader1Match.capturedTexts();
211 }
212 }
213
214 return result;
215}
216
A model describing the differences between two files.
Definition diffmodel.h:26
Diff2 namespace.
KIOCORE_EXPORT QStringList list(const QString &fileClass)
Format
Patch format enum.
Definition kompare.h:26
typedef ConstIterator
iterator begin()
iterator end()
QRegularExpressionMatch match(QStringView subjectView, qsizetype offset, MatchType matchType, MatchOptions matchOptions) const const
QString anchoredPattern(QStringView expression)
qsizetype capturedLength(QStringView name) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Apr 27 2024 22:10:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.