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/>.
20 #include "CommandLineParser.h"
21 #include "Interview.h"
23 #include "GitRunner.h"
24 #include "hunks/ChangeSet.h"
25 #include "hunks/HunksCursor.h"
30 static const CommandLineOption options
[] = {
31 {"-a, --all", "answer yes to all patches"},
32 {"-i, --interactive", "prompt user interactively"},
37 : AbstractCommand("revert")
39 CommandLineParser::addOptionDefinitions(options
);
40 CommandLineParser::setArgumentDefinition("revert [FILE or DIRECTORY]" );
43 AbstractCommand::ReturnCodes
Revert::run()
45 if (! checkInRepository())
49 CommandLineParser
*args
= CommandLineParser::instance();
50 const bool all
= m_config
.contains("all") && !args
->contains("interactive") || args
->contains("all");
53 changeSet
.fillFromCurrentChanges(rebasedArguments());
55 bool shouldDoRevert
= changeSet
.count() > 0;
57 Logger::info() << "There are no changes to revert!" << endl
;
59 if (shouldDoRevert
&& !all
) { // then do interview
60 HunksCursor
cursor(changeSet
);
61 Interview
interview(cursor
, name());
62 interview
.setUsePager(shouldUsePager());
63 if (! interview
.start()) {
64 Logger::info() << "Revert cancelled." << endl
;
69 if (shouldDoRevert
&& !all
) { // check if there is anything selected
70 shouldDoRevert
= changeSet
.hasAcceptedChanges();
72 Logger::info() << "If you don't want to revert after all, that's fine with me!" <<endl
;
75 if (shouldDoRevert
&& !all
) { // ask user confirmation
76 QString answer
= Interview::ask("Do you really want to revert these changes? ");
77 if (! (answer
.startsWith("y") || answer
.startsWith("Y")))
81 if (! dryRun() && shouldDoRevert
) { // we then write out the patch and call git apply with it
82 QFile
outFile(patchFileName());
83 changeSet
.writeDiff(outFile
, all
? ChangeSet::AllHunks
: ChangeSet::UserSelection
);
86 QStringList arguments
;
87 arguments
<< "apply" << "--apply" << "--reverse" << outFile
.fileName();
89 GitRunner
runner(git
, arguments
);
90 AbstractCommand::ReturnCodes rc
= runner
.start(GitRunner::WaitUntilFinished
);
92 Logger::error() << "internal error; failed to patch, sorry\n";
98 Logger::info() << "Finished reverting." << endl
;
102 QString
Revert::argumentDescription() const
104 return "[FILE or DIRECTORY]";
107 QString
Revert::commandDescription() const
109 return "Revert is used to undo changes made to the working copy which have\n"
110 "not yet been recorded. You will be prompted for which changes you\n"
111 "wish to undo. The last revert can be undone safely using the unrevert\n"
112 "command if the working copy was not modified in the meantime.\n";
116 QString
Revert::patchFileName()
118 return QString::fromLatin1(".git/.vng-revert.diff");
123 make sure we can revert file-renames too
124 check how we handle binary patches (i.e. no dataloss)
125 figure out a way to auto-adjust the line numbers for unrevert.