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/>.
21 #include "CommandLineParser.h"
23 #include "Interview.h"
24 #include "GitRunner.h"
26 #include "hunks/ChangeSet.h"
27 #include "hunks/HunksCursor.h"
31 static const CommandLineOption options
[] = {
32 {"-a, --all", "answer yes to all patches"},
33 {"-i, --interactive", "prompt user interactively"},
38 :AbstractCommand("unrevert")
40 CommandLineParser::addOptionDefinitions(options
);
43 QString
UnRevert::argumentDescription() const
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())
59 CommandLineParser
*args
= CommandLineParser::instance();
60 const bool all
= m_config
.contains("all") && !args
->contains("interactive") || args
->contains("all");
63 QFile
file(Revert::patchFileName());
65 changeSet
.fillFromDiffFile(file
);
67 bool shouldDoRevert
= changeSet
.count() > 0;
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
;
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
);
85 QStringList arguments
;
86 arguments
<< "apply" << "--apply" << patchFile
.fileName();
87 GitRunner
runner(git
, arguments
);
88 AbstractCommand::ReturnCodes rc
= runner
.start(GitRunner::WaitUntilFinished
);
91 Logger::info() << git
.readAllStandardError();
92 Logger::info().flush();
95 changeSet
.writeDiff(file
, all
? ChangeSet::AllHunks
: ChangeSet::InvertedUserSelection
);
99 Logger::info() << "Finished unreverting." << endl
;