Many fixes and cleanups
[vng.git] / src / commands / Record.cpp
blob013fa4531900e1eb5169bf1e2c340ae09c0852ce
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 Thomas Zander <tzander@trolltech.com>
4 * Copyright (C) 2002-2004 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 "Record.h"
20 #include "CommandLineParser.h"
21 #include "GitRunner.h"
22 #include "hunks/ChangeSet.h"
23 #include "hunks/HunksCursor.h"
24 #include "Logger.h"
25 #include "Interview.h"
26 #include "Vng.h"
28 #include <QDebug>
29 #include <QBuffer>
31 #ifdef Q_OS_WIN
32 # include <cstdlib>
33 # define getpid() rand()
34 #else
35 # include <sys/types.h>
36 # include <unistd.h>
37 #endif
40 static const CommandLineOption options[] = {
41 {"-a, --all", "answer yes to all patches"},
42 {"-i, --interactive", "prompt user interactively"},
43 {"-m, --patch-name PATCHNAME", "name of patch"},
44 {"-A, --author EMAIL", "specify author id"},
45 //{"--logfile FILE", "give patch name and comment in file"},
46 //{"--delete-logfile", "delete the logfile when done"},
47 //{"--no-test", "don't run the test script"},
48 //{"--test", "run the test script"},
49 //{"-l, --look-for-adds", "Also look for files that are potentially pending addition"},
50 //{"--dont-look-for-adds", "Don't look for any files that could be added"},
51 //{"--posthook COMMAND", "specify command to run after this vng command."},
52 //{"--no-posthook", "Do not run posthook command."},
53 //{"--prompt-posthook", "Prompt before running posthook. [DEFAULT]"},
54 //{"--run-posthook", "Run posthook command without prompting."},
55 CommandLineLastOption
58 Record::Record()
59 : AbstractCommand("record"),
60 m_mode(Unset),
61 m_editComment(false),
62 m_all(false)
64 CommandLineParser::addOptionDefinitions(options);
65 CommandLineParser::setArgumentDefinition("record [FILE or DIRECTORY]" );
68 QString Record::argumentDescription() const
70 return "[FILE or DIRECTORY]";
73 QString Record::commandDescription() const
75 return "Record is used to name a set of changes and record the patch to the repository.\n";
78 void Record::setPatchName(const QByteArray &message)
80 m_patchName = message;
83 QByteArray Record::patchName() const
85 return m_patchName;
88 void Record::setUsageMode(UsageMode mode)
90 m_mode = mode;
93 QString Record::sha1() const
95 return m_sha1;
98 AbstractCommand::ReturnCodes Record::run()
100 CommandLineParser *args = CommandLineParser::instance();
101 m_all = m_config.isEmptyRepo()
102 || (m_config.contains("all") && !args->contains("interactive"))
103 || args->contains("all");
104 m_patchName = args->optionArgument("patch-name", m_config.optionArgument("patch-name")).toUtf8();
105 m_author = args->optionArgument("author", m_config.optionArgument("author"));
107 return record();
110 AbstractCommand::ReturnCodes Record::record()
112 if (! checkInRepository())
113 return NotInRepo;
114 moveToRoot(CheckFileSystem);
116 if (m_mode != Unset)
117 m_all = m_mode == AllChanges;
118 const bool needHunks = !m_all || m_patchName.isEmpty();
120 ChangeSet changeSet;
121 changeSet.fillFromCurrentChanges(rebasedArguments(), needHunks);
123 changeSet.waitForFinishFirstFile();
124 bool shouldDoRecord = changeSet.count() > 0;
125 if (!shouldDoRecord) {
126 Logger::warn() << "No changes!" << endl;
127 return Ok;
130 QString email = m_author;
131 if (email.isEmpty())
132 email = getenv("EMAIL");
133 QStringList environment;
134 if (! email.isEmpty()) {
135 QRegExp re("(.*) <([@\\S]+)>");
136 if (re.exactMatch(email)) { // meaning its an email AND name
137 environment << "GIT_AUTHOR_NAME="+ re.cap(1);
138 environment << "GIT_COMMITTER_NAME="+ re.cap(1);
139 environment << "GIT_AUTHOR_EMAIL="+ re.cap(2);
140 environment << "GIT_COMMITTER_EMAIL="+ re.cap(2);
142 else if (m_author.isEmpty()) { // if its an account or shell wide option; just use the git defs.
143 environment << "GIT_AUTHOR_EMAIL="+ email;
144 environment << "GIT_COMMITTER_EMAIL="+ email;
146 else {
147 Logger::error() << "Author format invalid. Please provide author formatted like; `name <email@host>\n";
148 return InvalidOptions;
152 if (shouldDoRecord && !m_all && m_mode != Index) { // then do interview
153 HunksCursor cursor(changeSet);
154 cursor.setConfiguration(m_config);
155 Interview interview(cursor, name());
156 interview.setUsePager(shouldUsePager());
157 if (! interview.start()) {
158 Logger::warn() << "Cancelled." << endl;
159 return UserCancelled;
163 if (shouldDoRecord && !m_all && m_mode != Index) { // check if there is anything selected
164 shouldDoRecord = changeSet.hasAcceptedChanges();
165 if (! shouldDoRecord) {
166 Logger::warn() << "Ok, if you don't want to record anything, that's fine!" <<endl;
167 return UserCancelled;
170 if (dryRun())
171 return Ok;
173 if ((m_editComment || m_patchName.isEmpty()) && getenv("EDITOR")) {
174 class Deleter : public QObject {
175 public:
176 Deleter() : commitMessage(0) { }
177 ~Deleter() {
178 if (commitMessage)
179 commitMessage->remove();
181 QFile *commitMessage;
183 Deleter parent;
184 QFile *commitMessage;
185 int i = 0;
186 do {
187 commitMessage = new QFile(QString("vng-record-%1").arg(i++), &parent);
188 } while (commitMessage->exists());
189 parent.commitMessage = commitMessage; // make sure the file is removed from FS.
190 if (! commitMessage->open(QIODevice::WriteOnly)) {
191 Logger::error() << "Vng failed. Could not create a temporary file for the record message '"
192 << commitMessage->fileName() << "`\n";
193 return WriteError;
195 const char * defaultCommitMessage1 = "\n***END OF DESCRIPTION***"; // we will look for this string later
196 const char * defaultCommitMessage2 = "\nPlace the long patch description above the ***END OF DESCRIPTION*** marker.\n\nThis patch contains the following changes:\n\n";
197 if (! m_patchName.isEmpty())
198 commitMessage->write(m_patchName);
199 else
200 commitMessage->write("\n", 1);
201 commitMessage->write(defaultCommitMessage1, strlen(defaultCommitMessage1));
202 commitMessage->write(defaultCommitMessage2, strlen(defaultCommitMessage2));
203 QBuffer buffer;
204 changeSet.writeDiff(buffer, m_all ? ChangeSet::AllHunks : ChangeSet::UserSelection);
205 ChangeSet actualChanges;
206 actualChanges.fillFromDiffFile(buffer);
207 QTextStream out (commitMessage);
208 for (int i=0; i < actualChanges.count(); ++i) {
209 File file = actualChanges.file(i);
210 file.outputWhatsChanged(out, m_config, true, false);
212 for (int i=0; i < changeSet.count(); ++i) {
213 File file = changeSet.file(i);
214 if (file.isBinary() && (m_all || file.binaryChangeAcceptance() == Vng::Accepted))
215 out << "M " << QString::fromLocal8Bit(file.fileName());
216 else if (file.fileName().isEmpty() && (m_all || file.renameAcceptance() == Vng::Accepted))
217 out << "D " << QString::fromLocal8Bit(file.oldFileName());
219 out.flush();
221 commitMessage->close();
222 QDateTime modification = QFileInfo(*commitMessage).lastModified();
223 QString command = QString("%1 %2").arg(getenv("EDITOR")).arg(commitMessage->fileName());
224 int rc = system(command.toAscii().data());
225 if (rc != 0) {
226 // this will keep patchName empty and we fall through to the interview.
227 Logger::warn() << "Vng-Warning: Could not start editor '" << getenv("EDITOR") << "`\n";
228 Logger::warn().flush();
230 else if (modification == QFileInfo(*commitMessage).lastModified()) {
231 Logger::warn() << "unchanged, won't record\n";
232 return UserCancelled;
234 else {
235 // get data until the separator line.
236 commitMessage->open(QIODevice::ReadOnly);
237 m_patchName = commitMessage->readAll();
238 commitMessage->close();
239 int cuttoff = m_patchName.indexOf(defaultCommitMessage1);
240 if (cuttoff > 0)
241 m_patchName.truncate(cuttoff);
242 for (int i = m_patchName.length()-1; i >= 0; --i) {
243 if (m_patchName[i] == '\n' || m_patchName[i] == '\r' || m_patchName[i] == ' ')
244 m_patchName.resize(i); // shrink
245 else break;
249 if (m_patchName.isEmpty())
250 m_patchName = Interview::ask("What is the patch name? ").toUtf8();
252 ReturnCodes rc = addFilesPerAcceptance(changeSet, m_all);
253 if (rc != Ok)
254 return rc;
256 QProcess git;
257 QStringList arguments;
258 arguments << "write-tree";
259 GitRunner runner(git, arguments);
260 rc = runner.start(GitRunner::WaitForStandardOutput);
261 if (rc != Ok) {
262 Logger::error() << "Git write-tree failed!, aborting record\n";
263 return rc;
265 char buf[1024];
266 Vng::readLine(&git, buf, sizeof(buf));
267 QString tree(buf);
268 git.waitForFinished(); // patiently wait for it to finish..
269 Logger::debug() << "The tree got git ref; " << tree;
270 Logger::debug().flush(); // flush since we do an ask next
272 arguments.clear();
273 git.setEnvironment(environment);
275 arguments << "commit-tree" << tree.left(40);
276 if (!m_config.isEmptyRepo())
277 arguments << "-p" << "HEAD" ;
279 runner.setArguments(arguments);
280 rc = runner.start(GitRunner::WaitUntilReadyForWrite);
281 if (rc != Ok) {
282 Logger::error() << "Git commit-tree failed!, aborting record\n";
283 return rc;
285 git.write(m_patchName);
286 git.write("\n");
287 git.closeWriteChannel();
288 Vng::readLine(&git, buf, sizeof(buf));
289 QString commit(buf);
290 Logger::debug() << "commit is ref; " << commit;
291 git.waitForFinished(); // patiently wait for it to finish..
292 if (commit.isEmpty()) {
293 Logger::error() << "Git update-ref failed to produce a reference!, aborting record\n";
294 return GitFailed;
296 m_sha1 = commit.left(40);
298 arguments.clear();
299 arguments << "update-ref" << "HEAD" << m_sha1;
300 runner.setArguments(arguments);
301 rc = runner.start(GitRunner::WaitUntilFinished);
302 if (rc != Ok) {
303 Logger::error() << "Git update-ref failed!, aborting record\n";
304 return rc;
307 // We removed files from the index in case they were freshly added, but the user didn't want it in this commit.
308 // we have to re-add those files.
309 arguments.clear();
310 arguments << "update-index" << "--add";
311 for (int i=0; i < changeSet.count(); ++i) {
312 File file = changeSet.file(i);
313 if (! file.oldFileName().isEmpty())
314 continue; // not a new added file.
315 if (file.renameAcceptance() == Vng::Rejected)
316 arguments.append(file.fileName());
318 if (arguments.count() > 2) {
319 runner.setArguments(arguments);
320 runner.start(GitRunner::WaitUntilFinished);
323 int endOfLine = m_patchName.indexOf('\n');
324 if (endOfLine > 0)
325 m_patchName.truncate(endOfLine);
326 Logger::warn() << "Finished recording patch `" << m_patchName << "'" << endl;
327 return Ok;
330 AbstractCommand::ReturnCodes Record::addFilesPerAcceptance(const ChangeSet &changeSet, bool allChanges)
332 typedef QPair<QString, QString> NamePair;
333 class RevertCopier {
334 public:
335 RevertCopier() {}
336 ~RevertCopier() {
337 foreach(NamePair pair, m_fileNames) {
338 QFile copy(pair.second);
339 copy.remove();
340 if (! pair.first.isEmpty()) {
341 QFile orig(pair.first);
342 orig.rename(copy.fileName());
346 void append(const QString &orig, const QString &copy) {
347 QPair<QString, QString> pair(orig, copy);
348 m_fileNames.append(pair);
350 private:
351 // thats orig, copy
352 QList< QPair<QString, QString> > m_fileNames;
354 RevertCopier reverter;// this will revert all file changes we make when we exit the scope of this method.
355 ChangeSet patchChanges;
357 QStringList filesForAdd;
358 QStringList notUsedFiles;
359 for (int i=0; i < changeSet.count(); ++i) {
360 File file = changeSet.file(i);
361 bool someEnabled = false;
362 bool allEnabled = true;
363 const bool renamed = file.fileName() != file.oldFileName();
364 const bool protectionChanged = !renamed && file.protection() != file.oldProtection();
365 if ((file.renameAcceptance() == Vng::Accepted && renamed)
366 || (file.protectionAcceptance() == Vng::Accepted && protectionChanged)) // record it.
367 someEnabled = true;
369 foreach (Hunk hunk, file.hunks()) {
370 Vng::Acceptance a = hunk.acceptance();
371 if (a == Vng::Accepted) {
372 someEnabled = true;
373 } else if (a == Vng::MixedAcceptance) {
374 someEnabled = true;
375 allEnabled = false;
376 } else {
377 allEnabled = false;
379 if (someEnabled && !allEnabled)
380 break;
382 const bool addUnaltered = allChanges || allEnabled;
384 const bool removeFromIndex = !allChanges && !someEnabled; // user said 'no', lets make sure none of the changes are left in the index.
385 if (removeFromIndex && ! file.fileName().isEmpty() && ! file.oldFileName().isEmpty()) { // just ignore file.
386 notUsedFiles << file.fileName();
387 continue;
389 Logger::debug() << "'" << file.fileName() << "` addUnaltered: " << addUnaltered << ", removeFromIndex: "
390 << removeFromIndex << ", someChangesAccepted: " << someEnabled << ", allAccepted: " << allEnabled << endl;
391 if (file.fileName().isEmpty())
392 filesForAdd << file.oldFileName();
393 else {
394 filesForAdd << file.fileName();
395 if (renamed)
396 filesForAdd << file.oldFileName();
399 if (addUnaltered)
400 continue; // thats easy; whole file to add.
401 if (removeFromIndex && file.fileName().isEmpty()) { // user said no about recording a deleted file.
402 // this is a funny situation; *just* in case the user already somehow added the deletion to the index
403 // we need to reset that to make the index have the full file again. Notice that we need to remove the file afterwards.
404 Q_ASSERT(!file.oldFileName().isEmpty());
405 QProcess git;
406 QStringList arguments;
407 arguments << "cat-file" << "blob" << file.oldSha1();
408 GitRunner runner(git, arguments);
409 ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
410 Logger::debug() << "restoring '" << file.oldFileName() << "`\n";
411 if (rc == Ok) {
412 reverter.append(QString(), file.oldFileName());
413 QFile deletedFile(file.oldFileName());
414 Q_ASSERT(! deletedFile.exists());
415 bool success = Vng::copyFile(git, deletedFile);
416 git.waitForFinished();
417 deletedFile.close();
418 if (! success)
419 return WriteError;
421 continue;
424 // for the case where only some patches are selected we make a safety copy of the file.
425 QFile sourceFile(file.fileName());
426 Q_ASSERT(sourceFile.exists());
427 QString fileName = file.fileName();
428 for(int i=0; i < 10; i++) {
429 fileName = fileName + ".orig";
430 if (sourceFile.rename(fileName))
431 break; // successful!
433 reverter.append(fileName, file.fileName());
434 if (removeFromIndex) // need to rename it only, we won't patch it.
435 continue;
437 patchChanges.addFile(file);
438 Logger::debug() << "cp " << fileName << " =>" << file.fileName() << endl;
439 QFile orig(fileName);
440 orig.open(QIODevice::ReadOnly);
441 Vng::copyFile(orig, sourceFile); // TODO check return code
442 sourceFile.setPermissions(file.permissions());
445 QFile patch(".vng.record." + QString::number(getpid()) + ".diff");
446 patchChanges.writeDiff(patch, ChangeSet::InvertedUserSelection);
447 patch.close();
449 if (patch.size() != 0) {
450 QProcess git;
451 QStringList arguments;
452 arguments << "apply" << "--apply" << "--reverse" << patch.fileName();
453 GitRunner runner(git, arguments);
454 ReturnCodes rc = runner.start(GitRunner::WaitUntilFinished);
455 if (rc != Ok) {
456 Logger::error() << "Vng failed: failed to patch, sorry! Aborting record.\n";
457 return rc; // note that copied files will be moved back to avoid partially patched files lying around.
460 patch.remove();
461 // first clean out the index so that previous stuff other tools did doesn't influence us.
462 if (!m_config.isEmptyRepo() && ! notUsedFiles.isEmpty()) {
463 QProcess git;
464 QStringList arguments;
465 arguments << "reset" << "--mixed" << "-q" << "HEAD"; // -q stands for quiet.
466 arguments += notUsedFiles;
467 GitRunner runner(git, arguments);
468 runner.start(GitRunner::WaitUntilFinished);
471 // git add of all files to get a nice list of changes into the index.
472 while (!filesForAdd.isEmpty()) {
473 QProcess git;
474 QStringList arguments;
475 arguments << "update-index" << "--add" << "--remove";
476 int count = 25; // length of arguments
477 do {
478 QString first = filesForAdd[0];
479 if (count + first.length() > 32000)
480 break;
481 count += first.length();
482 arguments.append(first);
483 filesForAdd.removeFirst();
484 } while (!filesForAdd.isEmpty());
485 GitRunner runner(git, arguments);
486 ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
487 if (rc != Ok) {
488 Logger::error() << "Vng failed: Did not manage to add files to the git index, aborting record\n";
489 return rc;
493 return Ok; // exiting scope will revert all files in the local filesystem. We use the index from here on.