Fix parsing 'last' in Changes to give an error when we don't pass a number
[vng.git] / src / patches / CommitsMatcher.cpp
blobd441799a5aa0afb13dd2e65a2c430d7d8157d81f
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include "CommitsMatcher.h"
19 #include "Commit.h"
20 #include "../CommandLineParser.h"
22 CommitsMatcher::CommitsMatcher()
23 : useRegExp(false), useRegExpTo(false), useMatcher(false), useMatcherTo(false)
25 CommandLineParser *args = CommandLineParser::instance();
27 if (args->contains("to-match")) {
28 m_matcherTo = QStringMatcher(args->optionArgument("to-match"), Qt::CaseInsensitive);
29 useMatcherTo = true;
31 else if (args->contains("to-patch")) {
32 m_regExpTo = QRegExp(args->optionArgument("to-patch"), Qt::CaseInsensitive, QRegExp::RegExp2);
33 useRegExpTo = true;
36 m_state = BeforeFrom;
37 if (args->contains("from-match")) {
38 m_matcher = QStringMatcher(args->optionArgument("from-match"), Qt::CaseInsensitive);
39 useMatcher = true;
41 else if (args->contains("from-patch")) {
42 m_regExp = QRegExp(args->optionArgument("from-patch"), Qt::CaseInsensitive, QRegExp::RegExp2);
43 useRegExp = true;
45 else {
46 m_state = MatchingPerItem;
47 findNormalMatchers();
51 CommitsMatcher::ExpectedAction CommitsMatcher::match(const Commit &commit)
53 switch (m_state) {
54 case AllClear: return ShowPatch;
55 case BeforeFrom:
56 if ((useMatcher && m_matcher.indexIn(commit.author()) == -1
57 && m_matcher.indexIn(commit.logMessage()) == -1)
58 || (useRegExp && m_regExp.indexIn(commit.author()) == -1
59 && m_regExp.indexIn(commit.logMessage()) == -1))
60 return SkipPatch;
61 m_state = MatchingPerItem;
62 findNormalMatchers();
63 return match(commit);
64 case MatchingPerItem:
65 if ((useMatcher && m_matcher.indexIn(commit.author()) == -1
66 && m_matcher.indexIn(commit.logMessage()) == -1)
67 || (useRegExp && m_regExp.indexIn(commit.author()) == -1
68 && m_regExp.indexIn(commit.logMessage()) == -1))
69 return SkipPatch;
70 if (!useMatcherTo && !useRegExpTo)
71 return ShowPatch;
72 case SearchingForTo:
73 if ((useMatcherTo && m_matcherTo.indexIn(commit.author()) == -1
74 && m_matcherTo.indexIn(commit.logMessage()) == -1)
75 || (useRegExpTo && m_regExpTo.indexIn(commit.author()) == -1
76 && m_regExpTo.indexIn(commit.logMessage()) == -1))
77 return ShowPatch;
78 m_state = AfterTo;
79 return ShowPatch;
80 default:
81 case AfterTo:
82 return Exit;
86 void CommitsMatcher::findNormalMatchers()
88 CommandLineParser *args = CommandLineParser::instance();
89 if (args->contains("match")) {
90 m_matcher = QStringMatcher(args->optionArgument("match"), Qt::CaseInsensitive);
91 useMatcher = true;
93 else if (args->contains("patches")) {
94 m_regExp = QRegExp(args->optionArgument("patches"), Qt::CaseInsensitive, QRegExp::RegExp2);
95 useRegExp = true;
97 else if (useMatcherTo || useRegExpTo)
98 m_state = SearchingForTo;
99 else
100 m_state = AllClear;