Add a warning about tag processing in the standardlayout file
[svn-all-fast-export.git] / src / ruleparser.cpp
blob0f675ddaa20e697721fec4d34b8366cd2aca8b28
1 /*
2 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <QTextStream>
19 #include <QFile>
20 #include <QDebug>
22 #include "ruleparser.h"
24 Rules::Rules(const QString &fn)
25 : filename(fn)
29 Rules::~Rules()
33 QList<Rules::Repository> Rules::repositories()
35 return m_repositories;
38 QList<Rules::Match> Rules::matchRules()
40 return m_matchRules;
43 void Rules::load()
45 QFile file(filename);
46 if (!file.open(QIODevice::ReadOnly))
47 return;
49 // initialize the regexps we will use
50 QRegExp repoLine("create repository\\s+(\\S+)", Qt::CaseInsensitive);
51 QRegExp repoBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive);
53 QRegExp matchLine("match\\s+(.*)", Qt::CaseInsensitive);
54 QRegExp matchActionLine("action\\s+(\\w+)", Qt::CaseInsensitive);
55 QRegExp matchRepoLine("repository\\s+(\\S+)", Qt::CaseInsensitive);
56 QRegExp matchBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive);
57 QRegExp matchRevLine("(min|max) revision (\\d+)", Qt::CaseInsensitive);
59 QTextStream s(&file);
60 enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone;
61 Repository repo;
62 Match match;
63 int lineNumber = 0;
64 while (!s.atEnd()) {
65 ++lineNumber;
66 QString origLine = s.readLine();
67 QString line = origLine;
69 int hash = line.indexOf('#');
70 if (hash != -1)
71 line.truncate(hash);
72 line = line.trimmed();
73 if (line.isEmpty())
74 continue;
76 if (state == ReadingRepository) {
77 if (repoBranchLine.exactMatch(line)) {
78 Repository::Branch branch;
79 branch.name = repoBranchLine.cap(1);
81 repo.branches += branch;
82 continue;
83 } else if (line == "end repository") {
84 m_repositories += repo;
85 state = ReadingNone;
86 continue;
88 } else if (state == ReadingMatch) {
89 if (matchRepoLine.exactMatch(line)) {
90 match.repository = matchRepoLine.cap(1);
91 continue;
92 } else if (matchBranchLine.exactMatch(line)) {
93 match.branch = matchBranchLine.cap(1);
94 continue;
95 } else if (matchRevLine.exactMatch(line)) {
96 if (matchRevLine.cap(1) == "min")
97 match.minRevision = matchRevLine.cap(2).toInt();
98 else // must be max
99 match.maxRevision = matchRevLine.cap(2).toInt();
100 continue;
101 } else if (matchActionLine.exactMatch(line)) {
102 QString action = matchActionLine.cap(1);
103 if (action == "export")
104 match.action = Match::Export;
105 else if (action == "ignore")
106 match.action = Match::Ignore;
107 else if (action == "recurse")
108 match.action = Match::Recurse;
109 else
110 qFatal("Invalid action \"%s\" on line %d", qPrintable(action), lineNumber);
111 continue;
112 } else if (line == "end match") {
113 if (!match.repository.isEmpty())
114 match.action = Match::Export;
115 m_matchRules += match;
116 state = ReadingNone;
117 continue;
121 bool isRepositoryRule = repoLine.exactMatch(line);
122 bool isMatchRule = matchLine.exactMatch(line);
124 if (isRepositoryRule) {
125 // repository rule
126 state = ReadingRepository;
127 repo = Repository(); // clear
128 repo.name = repoLine.cap(1);
129 repo.lineNumber = lineNumber;
130 } else if (isMatchRule) {
131 // match rule
132 state = ReadingMatch;
133 match = Match();
134 match.rx = QRegExp(matchLine.cap(1), Qt::CaseSensitive, QRegExp::RegExp2);
135 match.lineNumber = lineNumber;
136 } else {
137 qFatal("Malformed line in rules file: line %d: %s",
138 lineNumber, qPrintable(origLine));
143 #ifndef QT_NO_DEBUG_STREAM
144 QDebug operator<<(QDebug s, const Rules::Match &rule)
146 s.nospace() << rule.rx.pattern() << " (line " << rule.lineNumber << ")";
147 return s.space();
150 #endif