From 18cb19809287d6870c5cd968449946c4e99b44af Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Tue, 22 Apr 2008 21:21:32 +0200 Subject: [PATCH] Add CommitsCursor::setUserMatcher(bool use); and make UnRecord use it. --- UnRecord.cpp | 15 ++++++++++- commits/CommitsCursor.cpp | 67 ++++++++++++++++++++++++++++++++++++++++------- commits/CommitsCursor.h | 11 ++++++++ 3 files changed, 82 insertions(+), 11 deletions(-) diff --git a/UnRecord.cpp b/UnRecord.cpp index 0deb7da..4c4243b 100644 --- a/UnRecord.cpp +++ b/UnRecord.cpp @@ -18,7 +18,7 @@ */ #include "UnRecord.h" -// #include "CommandLineParser.h" +#include "CommandLineParser.h" #include "Interview.h" #include "GitRunner.h" #include "Logger.h" @@ -27,9 +27,21 @@ #include +static const CommandLineOption options[] = { + {"--from-match PATTERN", "select changes starting with a patch matching PATTERN"}, + {"--from-patch REGEXP", "select changes starting with a patch matching REGEXP"}, + //{"--from-tag REGEXP", "select changes starting with a tag matching REGEXP"}, + //{"--last NUMBER", "select the last NUMBER patches"}, + {"--match PATTERN", "select patches matching PATTERN"}, + {"-p, --patches REGEXP", "select patches matching REGEXP"}, + //{"-t REGEXP,--tags REGEXP", "select tags matching REGEXP"}, + CommandLineLastOption +}; + UnRecord::UnRecord() :AbstractCommand("unrecord") { + CommandLineParser::addOptionDefinitions(options); } QString UnRecord::argumentDescription() const @@ -51,6 +63,7 @@ AbstractCommand::ReturnCodes UnRecord::run() //CommandLineParser *args = CommandLineParser::instance(); CommitsCursor cursor; + cursor.setUseMatcher(true); Interview interview(cursor, name()); interview.setUsePager(shouldUsePager()); if (! interview.start()) { diff --git a/commits/CommitsCursor.cpp b/commits/CommitsCursor.cpp index 3e73667..282fc79 100644 --- a/commits/CommitsCursor.cpp +++ b/commits/CommitsCursor.cpp @@ -17,6 +17,7 @@ */ #include "CommitsCursor.h" +#include "CommitsMatcher.h" #include @@ -26,12 +27,14 @@ CommitsCursor::CommitsCursor() m_head("HEAD"), m_current(m_head), m_firstInBranch(m_head.firstCommitInBranch()), - m_done(! m_head.isValid()) + m_done(! m_head.isValid()), + m_matcher(0) { } CommitsCursor::~CommitsCursor() { + delete m_matcher; } int CommitsCursor::forward(Scope scope, bool skipAnswered) @@ -39,27 +42,59 @@ int CommitsCursor::forward(Scope scope, bool skipAnswered) Q_UNUSED(scope); Q_UNUSED(skipAnswered); // go to a parent commit + int newIndex = m_currentIndex; if (m_current.previousCommitsCount() > 0) { Commit parent = m_current.previous()[0]; - if (parent.isValid()) { - m_current = parent; - m_currentIndex++; + while(true) { + if (! parent.isValid()) + return m_currentIndex; + ++newIndex; + CommitsMatcher::ExpectedAction action = CommitsMatcher::ShowPatch; + if (m_matcher) + action = m_matcher->match(parent); + if (action == CommitsMatcher::ShowPatch) { + m_current = parent; + m_currentIndex = newIndex; + return newIndex; + } + if (action == CommitsMatcher::Exit) { + m_done = true; + return m_currentIndex; + } + if (parent.previousCommitsCount() == 0) {// no more commits; + m_done = true; + return m_currentIndex; + } + parent = parent.previous()[0]; } } - return m_currentIndex; } int CommitsCursor::back(Scope scope) { Q_UNUSED(scope); Commit c = m_current.next(); - if (c.isValid()) { - m_current = c; - m_currentIndex--; - if (m_current == m_firstInBranch) + int newIndex = m_currentIndex; + while(true) { + if (! c.isValid()) + return m_currentIndex; + --newIndex; + CommitsMatcher::ExpectedAction action = CommitsMatcher::ShowPatch; + if (m_matcher) + action = m_matcher->match(c); + if (action == CommitsMatcher::ShowPatch) { + m_current = c; + if (m_current == m_firstInBranch) + m_done = true; + m_currentIndex = newIndex; + return m_currentIndex; + } + if (action == CommitsMatcher::Exit) { m_done = true; + return m_currentIndex; + } + c = c.next(); } - return m_currentIndex; } void CommitsCursor::setResponse(bool response, Scope scope) @@ -122,3 +157,15 @@ Commit CommitsCursor::head() return m_head; } +void CommitsCursor::setUseMatcher(bool use) +{ + if (! use) { + delete m_matcher; + m_matcher = 0; + return; + } + if (m_matcher) + return; + m_matcher = new CommitsMatcher(); +} + diff --git a/commits/CommitsCursor.h b/commits/CommitsCursor.h index 056c8f1..c99c825 100644 --- a/commits/CommitsCursor.h +++ b/commits/CommitsCursor.h @@ -22,6 +22,8 @@ #include "../InterviewCursor.h" #include "Commit.h" +class CommitsMatcher; + class CommitsCursor : public InterviewCursor { public: CommitsCursor(); @@ -40,14 +42,23 @@ public: Commit head(); + /// returns the index into the list of patches that is the oldest patch the user adjusted. int oldestCommitAltered() const { return m_oldestIndex; } + /** + * make the cursor use the matcher to match only commits that match the command line arguments. + * @param use if true the next call to forward() or back() will use the CommitsMatcher, otherwise it won't. + * The default is to use no matcher. + */ + void setUseMatcher(bool use); + private: int m_currentIndex, m_oldestIndex; Commit m_head; Commit m_current; Commit m_firstInBranch; bool m_done; + CommitsMatcher *m_matcher; }; #endif -- 2.11.4.GIT