Don't allow to unrecord past the branch point
[vng.git] / UnRecord.cpp
blob83ae2d8f08eab22bc40d1010ac2f512a525ebed2
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
4 * Copyright (C) 2003-2005 David Roundy
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "UnRecord.h"
21 // #include "CommandLineParser.h"
22 #include "Interview.h"
23 // #include "GitRunner.h"
24 #include "Logger.h"
25 #include "commits/Commit.h"
26 #include "commits/CommitsCursor.h"
28 #include <QDebug>
30 UnRecord::UnRecord()
31 :AbstractCommand("unrecord")
35 QString UnRecord::argumentDescription() const
37 return QString();
40 QString UnRecord::commandDescription() const
42 return "UnRecord does the opposite of record in that it makes the changes from\n"
43 "patches active changes again which you may record or revert later. The\n"
44 "working copy itself will not change.\n";
47 AbstractCommand::ReturnCodes UnRecord::run()
49 if (! checkInRepository())
50 return NotInRepo;
51 //CommandLineParser *args = CommandLineParser::instance();
53 CommitsCursor cursor;
54 Interview interview(cursor, name());
55 interview.setUsePager(shouldUsePager());
56 if (! interview.start()) {
57 Logger::info() << "unrecord cancelled." << endl;
58 return Ok;
61 Commit commit = cursor.head();
62 Commit acceptedCommit;
63 int unrecordCount = 0;
64 bool oneAutoAcceptedPatch = false;
65 while (true) {
66 // TODO find a way to avoid walking the whole tree.
67 if (commit.acceptance() == Vng::Rejected) // can't use this one.
68 break;
69 else if (commit.acceptance() == Vng::Accepted)
70 acceptedCommit = commit;
71 if (commit.previousCommitsCount() == 0) // at first commit.
72 break;
73 if (commit.acceptance() == Vng::Undecided) {
74 if (acceptedCommit.isValid()) // already found a 'yes'.
75 break;
76 oneAutoAcceptedPatch = true;
78 commit = commit.previous()[0];
79 unrecordCount++;
82 if (!acceptedCommit.isValid()) {
83 Logger::info() << "Ok, if you don't want to unrecord anything, that's fine!\n";
84 return Ok;
86 if (oneAutoAcceptedPatch) {
87 QString answer = Interview::ask(QString("Do you really want to unrecord %1 patches? ").arg(unrecordCount));
88 if (! (answer.startsWith("y") || answer.startsWith("Y")))
89 return Ok;
91 if (dryRun())
92 return Ok;
94 // TODO ;)
95 Logger::info() << "Appologies, this command has not yet been implemented. Use 'git reset HEAD^' for now\n";
97 return Ok;