KompareDiff2

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

KDE's Doxygen guidelines are available online.