]> andersk Git - svn-all-fast-export.git/blame - src/ruleparser.cpp
remove the source branch in the rules
[svn-all-fast-export.git] / src / ruleparser.cpp
CommitLineData
01592f9e
TM
1/*
2 * Copyright (C) 2007 Thiago Macieira <thiago@kde.org>
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18#include <QTextStream>
19#include <QFile>
20#include <QDebug>
21
22#include "ruleparser.h"
23
24Rules::Rules(const QString &fn)
25 : filename(fn)
26{
27}
28
12cd84df
TM
29Rules::~Rules()
30{
31}
32
01592f9e
TM
33QList<Rules::Repository> Rules::repositories()
34{
35 return m_repositories;
36}
37
38QList<Rules::Match> Rules::matchRules()
39{
40 return m_matchRules;
41}
42
43void Rules::load()
44{
45 QFile file(filename);
46 if (!file.open(QIODevice::ReadOnly))
47 return;
48
49 // initialize the regexps we will use
5d3d11ec 50 QRegExp repoLine("create repository\\s+(\\S+)", Qt::CaseInsensitive);
973fa95b 51 QRegExp repoBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive);
5b8a1c0c 52
01592f9e 53 QRegExp matchLine("match\\s+(.*)", Qt::CaseInsensitive);
5d3d11ec 54 QRegExp matchRepoLine("repository\\s+(\\S+)", Qt::CaseInsensitive);
01592f9e 55 QRegExp matchBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive);
5b8a1c0c 56 QRegExp matchRevLine("(min|max) revision (\\d+)", Qt::CaseInsensitive);
01592f9e
TM
57
58 QTextStream s(&file);
59 enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone;
60 Repository repo;
61 Match match;
1129b425 62 int lineNumber = 0;
01592f9e 63 while (!s.atEnd()) {
1129b425 64 ++lineNumber;
01592f9e 65 QString origLine = s.readLine();
c7d45e66 66 QString line = origLine;
01592f9e
TM
67
68 int hash = line.indexOf('#');
c7d45e66 69 if (hash != -1)
01592f9e 70 line.truncate(hash);
c7d45e66 71 line = line.trimmed();
01592f9e
TM
72 if (line.isEmpty())
73 continue;
74
75 if (state == ReadingRepository) {
76 if (repoBranchLine.exactMatch(line)) {
77 Repository::Branch branch;
c7d45e66 78 branch.name = repoBranchLine.cap(1);
01592f9e
TM
79
80 repo.branches += branch;
add9aaf4 81 continue;
c7d45e66
TM
82 } else if (line == "end repository") {
83 m_repositories += repo;
84 state = ReadingNone;
85 continue;
01592f9e
TM
86 }
87 } else if (state == ReadingMatch) {
add9aaf4 88 if (matchRepoLine.exactMatch(line)) {
c7d45e66 89 match.repository = matchRepoLine.cap(1);
add9aaf4
TM
90 continue;
91 } else if (matchBranchLine.exactMatch(line)) {
c7d45e66 92 match.branch = matchBranchLine.cap(1);
add9aaf4 93 continue;
5b8a1c0c
TM
94 } else if (matchRevLine.exactMatch(line)) {
95 if (matchRevLine.cap(1) == "min")
96 match.minRevision = matchRevLine.cap(2).toInt();
97 else // must be max
98 match.maxRevision = matchRevLine.cap(2).toInt();
99 continue;
c7d45e66
TM
100 } else if (line == "end match") {
101 m_matchRules += match;
102 state = ReadingNone;
add9aaf4
TM
103 continue;
104 }
01592f9e
TM
105 }
106
107 bool isRepositoryRule = repoLine.exactMatch(line);
108 bool isMatchRule = matchLine.exactMatch(line);
01592f9e
TM
109
110 if (isRepositoryRule) {
111 // repository rule
112 state = ReadingRepository;
113 repo = Repository(); // clear
c7d45e66 114 repo.name = repoLine.cap(1);
107fa167 115 repo.lineNumber = lineNumber;
01592f9e
TM
116 } else if (isMatchRule) {
117 // match rule
118 state = ReadingMatch;
119 match = Match();
c7d45e66 120 match.rx = QRegExp(matchLine.cap(1), Qt::CaseSensitive, QRegExp::RegExp2);
107fa167 121 match.lineNumber = lineNumber;
01592f9e 122 } else {
1129b425
TM
123 qFatal("Malformed line in rules file: line %d: %s",
124 lineNumber, qPrintable(origLine));
01592f9e
TM
125 }
126 }
127}
This page took 0.05847 seconds and 5 git commands to generate.