Don't allow to unrecord past the branch point
[vng.git] / Revert.cpp
blob240a77db127695663054e6da2a314260f4045ac8
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 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);
42 AbstractCommand::ReturnCodes Revert::run()
44 if (! checkInRepository())
45 return NotInRepo;
46 CommandLineParser *args = CommandLineParser::instance();
47 const bool all = m_config.contains("all") && !args->contains("interactive") || args->contains("all");
49 ChangeSet changeSet;
50 changeSet.fillFromCurrentChanges(rebasedArguments());
52 bool shouldDoRevert = changeSet.count() > 0;
53 if (!shouldDoRevert)
54 Logger::info() << "There are no changes to revert!" << endl;
56 if (shouldDoRevert && !all) { // then do interview
57 HunksCursor cursor(changeSet);
58 Interview interview(cursor, name());
59 interview.setUsePager(shouldUsePager());
60 if (! interview.start()) {
61 Logger::info() << "Revert cancelled." << endl;
62 return Ok;
66 if (shouldDoRevert && !all) { // check if there is anything selected
67 shouldDoRevert = changeSet.hasAcceptedChanges();
68 if (! shouldDoRevert)
69 Logger::info() << "If you don't want to revert after all, that's fine with me!" <<endl;
72 if (shouldDoRevert && !all) { // ask user confirmation
73 QString answer = Interview::ask("Do you really want to revert these changes? ");
74 if (! (answer.startsWith("y") || answer.startsWith("Y")))
75 return Ok;
78 if (! dryRun() && shouldDoRevert) { // we then write out the patch and call git apply with it
79 QFile outFile(patchFileName());
80 changeSet.writeDiff(outFile, all ? ChangeSet::AllHunks : ChangeSet::UserSelection);
82 QProcess git;
83 QStringList arguments;
84 arguments << "apply" << "--apply" << "--reverse" << outFile.fileName();
86 GitRunner runner(git, arguments);
87 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitUntilFinished);
88 if (rc != Ok) {
89 Logger::error() << "internal error; failed to patch, sorry\n";
90 outFile.remove();
91 return rc;
95 Logger::info() << "Finished reverting." << endl;
96 return Ok;
99 QString Revert::argumentDescription() const
101 return "[FILE or DIRECTORY]";
104 QString Revert::commandDescription() const
106 return "Revert is used to undo changes made to the working copy which have\n"
107 "not yet been recorded. You will be prompted for which changes you\n"
108 "wish to undo. The last revert can be undone safely using the unrevert\n"
109 "command if the working copy was not modified in the meantime.\n";
112 // static
113 QString Revert::patchFileName()
115 return QString::fromLatin1(".git/.vng-revert.diff");
119 TODO
120 make sure we can revert file-renames too
121 check how we handle binary patches (i.e. no dataloss)
122 figure out a way to auto-adjust the line numbers for unrevert.