Make test more complete
[vng.git] / patches / CommitsCursor.cpp
blob41531c3233d60d2ffd79b14c76b4d09abc7b7945
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/>.
19 #include "CommitsCursor.h"
20 #include "CommitsMatcher.h"
22 #include <QDebug>
24 CommitsCursor::CommitsCursor()
25 : m_currentIndex(1),
26 m_oldestIndex(0),
27 m_head("HEAD"),
28 m_current(m_head),
29 m_firstInBranch(m_head.firstCommitInBranch()),
30 m_done(! m_head.isValid()),
31 m_matcher(0)
35 CommitsCursor::~CommitsCursor()
37 delete m_matcher;
40 int CommitsCursor::forward(Scope scope, bool skipAnswered)
42 Q_UNUSED(scope);
43 Q_UNUSED(skipAnswered);
44 // go to a parent commit
45 int newIndex = m_currentIndex;
46 if (m_current.previousCommitsCount() > 0) {
47 Commit parent = m_current.previous()[0];
48 while(true) {
49 if (! parent.isValid())
50 return m_currentIndex;
51 ++newIndex;
52 CommitsMatcher::ExpectedAction action = CommitsMatcher::ShowPatch;
53 if (m_matcher)
54 action = m_matcher->match(parent);
55 if (action == CommitsMatcher::ShowPatch) {
56 m_current = parent;
57 m_currentIndex = newIndex;
58 return newIndex;
60 if (action == CommitsMatcher::Exit) {
61 m_done = true;
62 return m_currentIndex;
64 if (parent.previousCommitsCount() == 0) {// no more commits;
65 m_done = true;
66 return m_currentIndex;
68 parent = parent.previous()[0];
71 return m_currentIndex;
74 int CommitsCursor::back(Scope scope)
76 Q_UNUSED(scope);
77 Commit c = m_current.next();
78 int newIndex = m_currentIndex;
79 while(true) {
80 if (! c.isValid())
81 return m_currentIndex;
82 --newIndex;
83 CommitsMatcher::ExpectedAction action = CommitsMatcher::ShowPatch;
84 if (m_matcher)
85 action = m_matcher->match(c);
86 if (action == CommitsMatcher::ShowPatch) {
87 m_current = c;
88 if (m_current == m_firstInBranch)
89 m_done = true;
90 m_currentIndex = newIndex;
91 return m_currentIndex;
93 if (action == CommitsMatcher::Exit) {
94 m_done = true;
95 return m_currentIndex;
97 c = c.next();
101 void CommitsCursor::setResponse(bool response, Scope scope)
103 Q_UNUSED(scope);
104 m_current.setAcceptance(response ? Vng::Accepted : Vng::Rejected);
105 if (! response) // exit when user says 'no'
106 m_done = true;
107 m_oldestIndex = qMax(m_oldestIndex, m_currentIndex);
110 int CommitsCursor::count()
112 return -1; // TODO we _can_ count.. Its just not quite useful in most cases.
115 void CommitsCursor::forceCount()
117 // TODO sure you want to do that??
120 QString CommitsCursor::currentText() const
122 return QString("\n") +
123 m_current.commitTime().toString() + QString(" ") + m_current.author() + QString("\n ") +
124 m_current.logMessage();
127 QString CommitsCursor::helpMessage() const
129 return QString(
130 "How to use unrecord...\n"
131 "y: unrecord this patch\n"
132 "n: don't unrecord it\n\n"
133 // "v: view this patch in full with pager\n"
134 // "x: view a summary of this patch\n"
135 "d: unrecord selected patches\n"
136 "q: cancel unrecord\n\n"
137 "j: skip to next patch\n"
138 "k: back up to previous patch\n"
139 "c: calculate number of patches\n"
140 "h or ?: show this help\n");
143 bool CommitsCursor::isValid() const
145 return !m_done && m_current.previousCommitsCount() > 0;
148 QString CommitsCursor::allowedOptions() const
150 QString allowed = "ynqdjk";
151 // if (m_total == -1)
152 // allowed += "c";
153 return allowed;
156 Commit CommitsCursor::head()
158 return m_head;
161 void CommitsCursor::setUseMatcher(bool use)
163 if (! use) {
164 delete m_matcher;
165 m_matcher = 0;
166 return;
168 if (m_matcher)
169 return;
170 m_matcher = new CommitsMatcher();