Don't allow to unrecord past the branch point
[vng.git] / UnRevert.cpp
blobe608940369bc7c93d01c82106495181d870d21f9
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 "UnRevert.h"
21 #include "CommandLineParser.h"
22 #include "Revert.h"
23 #include "Interview.h"
24 #include "GitRunner.h"
25 #include "Logger.h"
26 #include "hunks/ChangeSet.h"
27 #include "hunks/HunksCursor.h"
29 #include <QDebug>
31 static const CommandLineOption options[] = {
32 {"-a, --all", "answer yes to all patches"},
33 {"-i, --interactive", "prompt user interactively"},
34 CommandLineLastOption
37 UnRevert::UnRevert()
38 :AbstractCommand("unrevert")
40 CommandLineParser::addOptionDefinitions(options);
43 QString UnRevert::argumentDescription() const
45 return QString();
48 QString UnRevert::commandDescription() const
50 return "Unrevert is used to undo the results of a revert command. It is only\n"
51 "guaranteed to work properly if you haven't made any changes since the\n"
52 "revert was performed.\n";
55 AbstractCommand::ReturnCodes UnRevert::run()
57 if (! checkInRepository())
58 return NotInRepo;
59 CommandLineParser *args = CommandLineParser::instance();
60 const bool all = m_config.contains("all") && !args->contains("interactive") || args->contains("all");
62 ChangeSet changeSet;
63 QFile file(Revert::patchFileName());
64 if (file.exists())
65 changeSet.fillFromDiffFile(file);
67 bool shouldDoRevert = changeSet.count() > 0;
68 if (!shouldDoRevert)
69 Logger::info() << "There is nothing to unrevert!" << endl;
71 if (shouldDoRevert && !all) { // then do interview
72 HunksCursor cursor(changeSet);
73 Interview interview(cursor, name());
74 interview.setUsePager(shouldUsePager());
75 if (! interview.start()) {
76 Logger::info() << "Unrevert cancelled." << endl;
77 return Ok;
81 if (! dryRun() && shouldDoRevert) { // we then write out the patch and call git apply with it
82 QFile patchFile(Revert::patchFileName() + ".tmp");
83 changeSet.writeDiff(patchFile, all ? ChangeSet::AllHunks : ChangeSet::UserSelection);
84 QProcess git;
85 QStringList arguments;
86 arguments << "apply" << "--apply" << patchFile.fileName();
87 GitRunner runner(git, arguments);
88 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitUntilFinished);
89 patchFile.remove();
90 if (rc != 0) {
91 Logger::info() << git.readAllStandardError();
92 Logger::info().flush();
93 return rc;
95 changeSet.writeDiff(file, all ? ChangeSet::AllHunks : ChangeSet::InvertedUserSelection);
96 if (file.size() == 0)
97 file.remove();
99 Logger::info() << "Finished unreverting." << endl;
100 return Ok;