Extend the commits cursor to have a selection type and thus make
[vng.git] / src / patches / CommitsCursor.cpp
bloba9e1e814c198f3604254253561ff764fb2597a33
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_firstInBranch(m_head.firstCommitInBranch()),
30 m_done(! m_head.isValid()),
31 m_matcher(0),
32 m_selectionType(selectionType)
36 CommitsCursor::~CommitsCursor()
38 delete m_matcher;
41 int CommitsCursor::forward(Scope scope, bool skipAnswered)
43 Q_UNUSED(scope);
44 Q_UNUSED(skipAnswered);
45 // go to a parent commit
46 int newIndex = m_currentIndex;
47 if (m_current.previousCommitsCount() > 0) {
48 Commit parent = m_current.previous()[0];
49 while(true) {
50 if (! parent.isValid())
51 return m_currentIndex;
52 ++newIndex;
53 CommitsMatcher::ExpectedAction action = CommitsMatcher::ShowPatch;
54 if (m_matcher)
55 action = m_matcher->match(parent);
56 if (action == CommitsMatcher::ShowPatch) {
57 m_current = parent;
58 m_currentIndex = newIndex;
59 return newIndex;
61 if (action == CommitsMatcher::Exit) {
62 m_done = true;
63 return m_currentIndex;
65 if (parent.previousCommitsCount() == 0) {// no more commits;
66 m_done = true;
67 return m_currentIndex;
69 parent = parent.previous()[0];
72 return m_currentIndex;
75 int CommitsCursor::back(Scope scope)
77 Q_UNUSED(scope);
78 Commit c = m_current.next();
79 int newIndex = m_currentIndex;
80 while(true) {
81 if (! c.isValid())
82 return m_currentIndex;
83 --newIndex;
84 CommitsMatcher::ExpectedAction action = CommitsMatcher::ShowPatch;
85 if (m_matcher)
86 action = m_matcher->match(c);
87 if (action == CommitsMatcher::ShowPatch) {
88 m_current = c;
89 if (m_current == m_firstInBranch)
90 m_done = true;
91 m_currentIndex = newIndex;
92 return m_currentIndex;
94 if (action == CommitsMatcher::Exit) {
95 m_done = true;
96 return m_currentIndex;
98 c = c.next();
102 void CommitsCursor::setResponse(bool response, Scope scope)
104 Q_UNUSED(scope);
105 m_current.setAcceptance(response ? Vng::Accepted : Vng::Rejected);
106 if ((m_selectionType == SelectRange && !response)
107 || (m_selectionType == SelectOnePatch && response)) // exit when user says 'no'
108 m_done = true;
109 m_oldestIndex = qMax(m_oldestIndex, m_currentIndex);
112 int CommitsCursor::count()
114 return -1; // TODO we _can_ count.. Its just not quite useful in most cases.
117 void CommitsCursor::forceCount()
119 // TODO sure you want to do that??
122 QString CommitsCursor::currentText() const
124 return QString("\n") +
125 m_current.commitTime().toString() + QString(" ") + m_current.author() + QString("\n ") +
126 m_current.logMessage();
129 QString CommitsCursor::helpMessage() const
131 return QString(
132 "How to use unrecord...\n"
133 "y: unrecord this patch\n"
134 "n: don't unrecord it\n\n"
135 // "v: view this patch in full with pager\n"
136 // "x: view a summary of this patch\n"
137 "d: unrecord selected patches\n"
138 "q: cancel unrecord\n\n"
139 "j: skip to next patch\n"
140 "k: back up to previous patch\n"
141 "c: calculate number of patches\n"
142 "h or ?: show this help\n");
145 bool CommitsCursor::isValid() const
147 return !m_done && m_current.previousCommitsCount() > 0;
150 QString CommitsCursor::allowedOptions() const
152 QString allowed = "ynq";
153 if (m_selectionType == SelectRange)
154 allowed += "djk";
155 // if (m_total == -1)
156 // allowed += "c";
157 return allowed;
160 Commit CommitsCursor::head()
162 return m_head;
165 void CommitsCursor::setUseMatcher(bool use)
167 if (! use) {
168 delete m_matcher;
169 m_matcher = 0;
170 return;
172 if (m_matcher)
173 return;
174 m_matcher = new CommitsMatcher();