Finish up the matcher for basic .gitignore matching
[vng.git] / UnRecord.cpp
blob4c4243b7278db5716f2af75f01c0b73a98e241a1
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 "UnRecord.h"
21 #include "CommandLineParser.h"
22 #include "Interview.h"
23 #include "GitRunner.h"
24 #include "Logger.h"
25 #include "commits/Commit.h"
26 #include "commits/CommitsCursor.h"
28 #include <QDebug>
30 static const CommandLineOption options[] = {
31 {"--from-match PATTERN", "select changes starting with a patch matching PATTERN"},
32 {"--from-patch REGEXP", "select changes starting with a patch matching REGEXP"},
33 //{"--from-tag REGEXP", "select changes starting with a tag matching REGEXP"},
34 //{"--last NUMBER", "select the last NUMBER patches"},
35 {"--match PATTERN", "select patches matching PATTERN"},
36 {"-p, --patches REGEXP", "select patches matching REGEXP"},
37 //{"-t REGEXP,--tags REGEXP", "select tags matching REGEXP"},
38 CommandLineLastOption
41 UnRecord::UnRecord()
42 :AbstractCommand("unrecord")
44 CommandLineParser::addOptionDefinitions(options);
47 QString UnRecord::argumentDescription() const
49 return QString();
52 QString UnRecord::commandDescription() const
54 return "UnRecord does the opposite of record in that it makes the changes from\n"
55 "patches active changes again which you may record or revert later. The\n"
56 "working copy itself will not change.\n";
59 AbstractCommand::ReturnCodes UnRecord::run()
61 if (! checkInRepository())
62 return NotInRepo;
63 //CommandLineParser *args = CommandLineParser::instance();
65 CommitsCursor cursor;
66 cursor.setUseMatcher(true);
67 Interview interview(cursor, name());
68 interview.setUsePager(shouldUsePager());
69 if (! interview.start()) {
70 Logger::info() << "unrecord cancelled." << endl;
71 return Ok;
74 Commit commit = cursor.head();
75 Commit acceptedCommit;
76 int unrecordCount = 0;
77 bool oneAutoAcceptedPatch = false;
78 while (true) {
79 if (commit.acceptance() == Vng::Rejected) // can't use this one.
80 break;
81 else if (commit.acceptance() == Vng::Accepted)
82 acceptedCommit = commit;
83 if (commit.previousCommitsCount() == 0) // at first commit.
84 break;
85 if (commit.acceptance() == Vng::Undecided) {
86 if (acceptedCommit.isValid()) // already found a 'yes'.
87 break;
88 oneAutoAcceptedPatch = true;
90 commit = commit.previous()[0];
91 if (unrecordCount >= cursor.oldestCommitAltered())
92 break;
93 unrecordCount++;
96 if (!acceptedCommit.isValid()) {
97 Logger::info() << "Ok, if you don't want to unrecord anything, that's fine!\n";
98 return Ok;
100 if (oneAutoAcceptedPatch) {
101 QString answer = Interview::ask(QString("Do you really want to unrecord %1 patches? ").arg(unrecordCount));
102 if (! (answer.startsWith("y") || answer.startsWith("Y")))
103 return Ok;
105 if (dryRun())
106 return Ok;
108 QProcess git;
109 QStringList arguments;
110 arguments << "update-ref" << "HEAD" << commit.commitTreeIsm();
111 GitRunner runner(git, arguments);
112 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitUntilFinished);
113 if (rc != Ok) {
114 Logger::error() << "Failed to update the ref, sorry.\n";
115 return rc;
117 // TODO find all files that were added in the commit(s) we just reverted. The files are there, but
118 // vng / git doesn't know about them. So we need to add them explicitly.
120 return Ok;