Various fixes for revert/unrevert
[vng.git] / src / commands / Revert.cpp
blobe5ed03ea95aa33c47529d39434e78b29ed110f20
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008-2009 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "Revert.h"
20 #include "CommandLineParser.h"
21 #include "Interview.h"
22 #include "Logger.h"
23 #include "GitRunner.h"
24 #include "hunks/ChangeSet.h"
25 #include "hunks/HunksCursor.h"
27 #include <QProcess>
28 #include <QDebug>
30 static const CommandLineOption options[] = {
31 {"-a, --all", "answer yes to all patches"},
32 {"-i, --interactive", "prompt user interactively"},
33 CommandLineLastOption
36 Revert::Revert()
37 : AbstractCommand("revert")
39 CommandLineParser::addOptionDefinitions(options);
40 CommandLineParser::setArgumentDefinition("revert [FILE or DIRECTORY]" );
43 AbstractCommand::ReturnCodes Revert::run()
45 if (! checkInRepository())
46 return NotInRepo;
47 moveToRoot(static_cast<RebaseOptions>(CheckFileSystem | PrintError));
49 CommandLineParser *args = CommandLineParser::instance();
50 const bool all = (m_config.contains("all") && !args->contains("interactive")) || args->contains("all");
52 ChangeSet changeSet;
53 changeSet.fillFromCurrentChanges(rebasedArguments());
55 changeSet.waitForFinishFirstFile();
56 bool shouldDoRevert = changeSet.count() > 0;
57 if (!shouldDoRevert)
58 Logger::warn() << "There are no changes to revert!" << endl;
60 if (shouldDoRevert && !all) { // then do interview
61 HunksCursor cursor(changeSet);
62 cursor.setConfiguration(m_config);
63 Interview interview(cursor, name());
64 interview.setUsePager(shouldUsePager());
65 if (! interview.start()) {
66 Logger::warn() << "Revert cancelled." << endl;
67 return Ok;
71 if (shouldDoRevert && !all) { // check if there is anything selected
72 shouldDoRevert = changeSet.hasAcceptedChanges();
73 if (! shouldDoRevert)
74 Logger::warn() << "If you don't want to revert after all, that's fine with me!" <<endl;
77 if (shouldDoRevert && !all) { // ask user confirmation
78 QString answer = Interview::ask("Do you really want to revert these changes? ");
79 if (! (answer.startsWith("y") || answer.startsWith("Y")))
80 return Ok;
83 if (! dryRun() && shouldDoRevert) { // we then write out the patch and call git apply with it
84 QFile outFile(patchFileName());
85 changeSet.writeDiff(outFile, all ? ChangeSet::AllHunks : ChangeSet::UserSelection);
87 QProcess git;
88 QStringList arguments;
89 arguments << "apply" << "--apply" << "--reverse" << outFile.fileName();
91 GitRunner runner(git, arguments);
92 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitUntilFinished);
93 if (rc != Ok) {
94 Logger::error() << "vng failed, internal error; failed to patch, sorry\n";
95 outFile.remove();
96 return rc;
99 arguments.clear();
100 arguments << QLatin1String("update-index") << QLatin1String("--add");
101 for (int i = 0; i < changeSet.count(); ++i) {
102 File file = changeSet.file(i);
103 if ((all || file.renameAcceptance() == Vng::Accepted)
104 && file.oldFileName() != file.fileName()) {
105 if (!file.oldFileName().isEmpty())
106 arguments << file.oldFileName();
107 } else if (all || file.changesAcceptance() == Vng::Accepted) {
108 arguments << file.fileName();
111 if (arguments.count() > 2) {
112 runner.setArguments(arguments);
113 runner.start(GitRunner::WaitUntilFinished);
117 Logger::warn() << "Finished reverting." << endl;
118 return Ok;
121 QString Revert::argumentDescription() const
123 return QLatin1String("[FILE or DIRECTORY]");
126 QString Revert::commandDescription() const
128 return QLatin1String("Revert is used to undo changes made to the working copy which have\n"
129 "not yet been recorded. You will be prompted for which changes you\n"
130 "wish to undo. The last revert can be undone safely using the unrevert\n"
131 "command if the working copy was not modified in the meantime.\n");
134 // static
135 QString Revert::patchFileName()
137 return QString::fromLatin1(".git/.vng-revert.diff");
141 check how we handle binary patches (i.e. no dataloss)