Use 'record' in debug messages instead of 'commit'. Make record also work when the...
[vng.git] / UnRecord.cpp
blobe882f93775e42f9281bb05cad77dba9374fb50ba
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 static const CommandLineOption options[] = {
31 {"--from-match PATTERN", "select changes starting with a patch matching PATTERN"},
32 {"--from-patch REGEXP", "select changes starting with a patch matching REGEXP"},
33 //{"--from-tag REGEXP", "select changes starting with a tag matching REGEXP"},
34 {"-n, --last NUMBER", "select the last NUMBER patches"},
35 {"--match PATTERN", "select patches matching PATTERN"},
36 {"-p, --patches REGEXP", "select patches matching REGEXP"},
37 //{"-t REGEXP,--tags REGEXP", "select tags matching REGEXP"},
38 CommandLineLastOption
41 UnRecord::UnRecord()
42 :AbstractCommand("unrecord")
44 CommandLineParser::addOptionDefinitions(options);
47 QString UnRecord::argumentDescription() const
49 return QString();
52 QString UnRecord::commandDescription() const
54 return "UnRecord does the opposite of record in that it makes the changes from\n"
55 "patches active changes again which you may record or revert later. The\n"
56 "working copy itself will not change.\n";
59 AbstractCommand::ReturnCodes UnRecord::run()
61 if (! checkInRepository())
62 return NotInRepo;
63 CommandLineParser *args = CommandLineParser::instance();
65 CommitsCursor cursor;
66 cursor.setUseMatcher(true);
67 if (args->contains("last")) {
68 int count = args->optionArgument("last").toInt();
69 for (int i = 0; i < count && cursor.isValid(); i++) {
70 cursor.setResponse(true);
71 cursor.forward();
73 } else {
74 Interview interview(cursor, name());
75 interview.setUsePager(shouldUsePager());
76 if (! interview.start()) {
77 Logger::info() << "unrecord cancelled." << endl;
78 return Ok;
82 Commit commit = cursor.head();
83 Commit acceptedCommit;
84 int unrecordCount = 0;
85 bool oneAutoAcceptedPatch = false;
86 while (true) {
87 if (commit.acceptance() == Vng::Rejected) // can't use this one.
88 break;
89 else if (commit.acceptance() == Vng::Accepted)
90 acceptedCommit = commit;
91 if (commit.previousCommitsCount() == 0) // at first commit.
92 break;
93 if (commit.acceptance() == Vng::Undecided) {
94 if (acceptedCommit.isValid()) // already found a 'yes'.
95 break;
96 oneAutoAcceptedPatch = true;
98 commit = commit.previous()[0];
99 if (unrecordCount >= cursor.oldestCommitAltered())
100 break;
101 unrecordCount++;
104 if (!acceptedCommit.isValid()) {
105 Logger::info() << "Ok, if you don't want to unrecord anything, that's fine!\n";
106 return Ok;
108 if (oneAutoAcceptedPatch) {
109 QString answer = Interview::ask(QString("Do you really want to unrecord %1 patches? ").arg(unrecordCount));
110 if (! (answer.startsWith("y") || answer.startsWith("Y")))
111 return Ok;
113 if (dryRun())
114 return Ok;
116 QProcess git;
117 QStringList arguments;
118 arguments << "update-ref" << "HEAD" << commit.commitTreeIsm();
119 GitRunner runner(git, arguments);
120 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitUntilFinished);
121 if (rc != Ok) {
122 Logger::error() << "Failed to update the ref, sorry.\n";
123 return rc;
125 // TODO find all files that were added in the commit(s) we just reverted. The files are there, but
126 // vng / git doesn't know about them. So we need to add them explicitly.
128 return Ok;