Fix parsing 'last' in Changes to give an error when we don't pass a number
[vng.git] / src / patches / CommitsCursor.cpp
bloba3f0ada9b423a6c61616fd8b18b99f2368a32d53
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(SelectionType selectionType)
25 : m_currentIndex(1),
26 m_oldestIndex(0),
27 m_head("HEAD"),
28 m_current(m_head),
29 m_done(! m_head.isValid()),
30 m_matcher(0),
31 m_selectionType(selectionType)
33 if (selectionType == SelectRange)
34 m_firstInBranch = m_head.firstCommitInBranch();
37 CommitsCursor::~CommitsCursor()
39 delete m_matcher;
42 int CommitsCursor::forward(Scope scope, bool skipAnswered)
44 Q_UNUSED(scope);
45 Q_UNUSED(skipAnswered);
46 // go to a parent commit
47 int newIndex = m_currentIndex;
48 if (m_current.previousCommitsCount() > 0) {
49 Commit parent = m_current.previous()[0];
50 while(true) {
51 if (! parent.isValid())
52 return m_currentIndex;
53 ++newIndex;
54 CommitsMatcher::ExpectedAction action = CommitsMatcher::ShowPatch;
55 if (m_matcher)
56 action = m_matcher->match(parent);
57 if (action == CommitsMatcher::ShowPatch) {
58 m_current = parent;
59 m_currentIndex = newIndex;
60 return newIndex;
62 if (action == CommitsMatcher::Exit) {
63 m_done = true;
64 return m_currentIndex;
66 if (parent.previousCommitsCount() == 0) {// no more commits;
67 m_done = true;
68 return m_currentIndex;
70 parent = parent.previous()[0];
73 return m_currentIndex;
76 int CommitsCursor::back(Scope scope)
78 Q_UNUSED(scope);
79 Commit c = m_current.next();
80 int newIndex = m_currentIndex;
81 while(true) {
82 if (! c.isValid())
83 return m_currentIndex;
84 --newIndex;
85 CommitsMatcher::ExpectedAction action = CommitsMatcher::ShowPatch;
86 if (m_matcher)
87 action = m_matcher->match(c);
88 if (action == CommitsMatcher::ShowPatch) {
89 m_current = c;
90 if (m_current == m_firstInBranch)
91 m_done = true;
92 m_currentIndex = newIndex;
93 return m_currentIndex;
95 if (action == CommitsMatcher::Exit) {
96 m_done = true;
97 return m_currentIndex;
99 c = c.next();
103 void CommitsCursor::setResponse(bool response, Scope scope)
105 Q_UNUSED(scope);
106 m_current.setAcceptance(response ? Vng::Accepted : Vng::Rejected);
107 if ((m_selectionType == SelectRange && !response)
108 || (m_selectionType == SelectOnePatch && response)) // exit when user says 'no'
109 m_done = true;
110 m_oldestIndex = qMax(m_oldestIndex, m_currentIndex);
113 int CommitsCursor::count()
115 return -1; // TODO we _can_ count.. Its just not quite useful in most cases.
118 void CommitsCursor::forceCount()
120 // TODO sure you want to do that??
123 QString CommitsCursor::currentText() const
125 return QString("\n") +
126 m_current.commitTime().toString() + QString(" ") + m_current.author() + QString("\n ") +
127 m_current.logMessage();
130 QString CommitsCursor::helpMessage() const
132 return QString(
133 "How to use unrecord...\n"
134 "y: unrecord this patch\n"
135 "n: don't unrecord it\n\n"
136 // "v: view this patch in full with pager\n"
137 // "x: view a summary of this patch\n"
138 "d: unrecord selected patches\n"
139 "q: cancel unrecord\n\n"
140 "j: skip to next patch\n"
141 "k: back up to previous patch\n"
142 "c: calculate number of patches\n"
143 "h or ?: show this help\n");
146 bool CommitsCursor::isValid() const
148 return !m_done && m_current.previousCommitsCount() > 0;
151 QString CommitsCursor::allowedOptions() const
153 QString allowed = "ynq";
154 if (m_selectionType == SelectRange)
155 allowed += "djk";
156 // if (m_total == -1)
157 // allowed += "c";
158 return allowed;
161 Commit CommitsCursor::head()
163 return m_head;
166 void CommitsCursor::setUseMatcher(bool use)
168 if (! use) {
169 delete m_matcher;
170 m_matcher = 0;
171 return;
173 if (m_matcher)
174 return;
175 m_matcher = new CommitsMatcher();