Fix logical error (deleted paths was ignored)
[svn2git:cuboxs-uqs-svn2git.git] / src / ruleparser.cpp
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 3 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
24 Rules::Rules(const QString &fn)
25     : filename(fn)
26 {
27 }
28
29 Rules::~Rules()
30 {
31 }
32
33 QList<Rules::Repository> Rules::repositories()
34 {
35     return m_repositories;
36 }
37
38 QList<Rules::Match> Rules::matchRules()
39 {
40     return m_matchRules;
41 }
42
43 void Rules::load()
44 {
45     QFile file(filename);
46     if (!file.open(QIODevice::ReadOnly))
47         return;
48
49     // initialize the regexps we will use
50     QRegExp repoLine("create repository\\s+(\\S+)", Qt::CaseInsensitive);
51
52     QRegExp matchLine("match\\s+(.*)", Qt::CaseInsensitive);
53     QRegExp matchActionLine("action\\s+(\\w+)", Qt::CaseInsensitive);
54     QRegExp matchRepoLine("repository\\s+(\\S+)", Qt::CaseInsensitive);
55     QRegExp matchBranchLine("branch\\s+(\\S+)", Qt::CaseInsensitive);
56     QRegExp matchRevLine("(min|max) revision (\\d+)", Qt::CaseInsensitive);
57     QRegExp matchAnnotateLine("annotated\\s+(\\S+)", Qt::CaseInsensitive);
58     QRegExp matchPrefixLine("prefix\\s+(\\S+)", Qt::CaseInsensitive);
59
60     QTextStream s(&file);
61     enum { ReadingNone, ReadingRepository, ReadingMatch } state = ReadingNone;
62     Repository repo;
63     Match match;
64     int lineNumber = 0;
65     while (!s.atEnd()) {
66         ++lineNumber;
67         QString origLine = s.readLine();
68         QString line = origLine;
69
70         int hash = line.indexOf('#');
71         if (hash != -1)
72             line.truncate(hash);
73         line = line.trimmed();
74         if (line.isEmpty())
75             continue;
76
77         if (state == ReadingRepository) {
78             if (matchBranchLine.exactMatch(line)) {
79                 Repository::Branch branch;
80                 branch.name = matchBranchLine.cap(1);
81
82                 repo.branches += branch;
83                 continue;
84             } else if (line == "end repository") {
85                 m_repositories += repo;
86                 state = ReadingNone;
87                 continue;
88             }
89         } else if (state == ReadingMatch) {
90             if (matchRepoLine.exactMatch(line)) {
91                 match.repository = matchRepoLine.cap(1);
92                 continue;
93             } else if (matchBranchLine.exactMatch(line)) {
94                 match.branch = matchBranchLine.cap(1);
95                 continue;
96             } else if (matchRevLine.exactMatch(line)) {
97                 if (matchRevLine.cap(1) == "min")
98                     match.minRevision = matchRevLine.cap(2).toInt();
99                 else            // must be max
100                     match.maxRevision = matchRevLine.cap(2).toInt();
101                 continue;
102             } else if (matchPrefixLine.exactMatch(line)) {
103                 match.prefix = matchPrefixLine.cap(1);
104                 if( match.prefix.startsWith('/'))
105                     match.prefix = match.prefix.mid(1);
106                 if( match.prefix.endsWith('/'))
107                     match.prefix.chop(1);
108                 continue;
109             } else if (matchActionLine.exactMatch(line)) {
110                 QString action = matchActionLine.cap(1);
111                 if (action == "export")
112                     match.action = Match::Export;
113                 else if (action == "ignore")
114                     match.action = Match::Ignore;
115                 else if (action == "recurse")
116                     match.action = Match::Recurse;
117                 else
118                     qFatal("Invalid action \"%s\" on line %d", qPrintable(action), lineNumber);
119                 continue;
120             } else if (matchAnnotateLine.exactMatch(line)) {
121                 match.annotate = matchAnnotateLine.cap(1) == "true";
122                 continue;
123             } else if (line == "end match") {
124                 if (!match.repository.isEmpty())
125                     match.action = Match::Export;
126                 m_matchRules += match;
127                 state = ReadingNone;
128                 continue;
129             }
130         }
131
132         bool isRepositoryRule = repoLine.exactMatch(line);
133         bool isMatchRule = matchLine.exactMatch(line);
134
135         if (isRepositoryRule) {
136             // repository rule
137             state = ReadingRepository;
138             repo = Repository(); // clear
139             repo.name = repoLine.cap(1);
140             repo.lineNumber = lineNumber;
141         } else if (isMatchRule) {
142             // match rule
143             state = ReadingMatch;
144             match = Match();
145             match.rx = QRegExp(matchLine.cap(1), Qt::CaseSensitive, QRegExp::RegExp2);
146             match.lineNumber = lineNumber;
147         } else {
148             qFatal("Malformed line in rules file: line %d: %s",
149                    lineNumber, qPrintable(origLine));
150         }
151     }
152 }
153
154 #ifndef QT_NO_DEBUG_STREAM
155 QDebug operator<<(QDebug s, const Rules::Match &rule)
156 {
157     s.nospace() << rule.rx.pattern() << " (line " << rule.lineNumber << ")";
158     return s.space();
159 }
160
161 #endif