From 29150cb6d6ce82a8a8c250b14afc669e64d09da5 Mon Sep 17 00:00:00 2001 From: Thomas Zander Date: Thu, 1 May 2008 21:37:57 +0200 Subject: [PATCH] Make record able to use the external editor for writing your commit message. --- Record.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/Record.cpp b/Record.cpp index 51dc953..63f6a88 100644 --- a/Record.cpp +++ b/Record.cpp @@ -151,13 +151,56 @@ AbstractCommand::ReturnCodes Record::run() return Ok; } } - - QString patchName = args->optionArgument("patch-name", m_config.optionArgument("patch-name")); - if (patchName.isEmpty()) - patchName = Interview::ask("What is the patch name? "); + QByteArray patchName = args->optionArgument("patch-name", m_config.optionArgument("patch-name")).toUtf8(); if (dryRun()) return Ok; + if (patchName.isEmpty() && getenv("EDITOR")) { + QObject parent; + QFile *commitMessage; + int i = 0; + do { + commitMessage = new QFile(QString("vng-record-%1").arg(i++), &parent); + } while (commitMessage->exists()); + if (! commitMessage->open(QIODevice::WriteOnly)) { + Logger::error() << "vng-failed. Could not create a temporary file for the commit message '" + << commitMessage->fileName() << "`\n"; + return WriteError; + } + const char * defaultCommitMessage1 = "\n***END OF DESCRIPTION***"; // we will look for this string later + const char * defaultCommitMessage2 = "\n\nPlace the long patch description above the ***END OF DESCRIPTION*** marker."; // \n\nThis patch contains the following changes:\n"; + commitMessage->write("\n", 1); + commitMessage->write(defaultCommitMessage1, strlen(defaultCommitMessage1)); + commitMessage->write(defaultCommitMessage2, strlen(defaultCommitMessage2)); + // TODO + // M foo -2 + + commitMessage->close(); + QDateTime modification = QFileInfo(*commitMessage).lastModified(); + QString command = QString("%1 %2").arg(getenv("EDITOR")).arg(commitMessage->fileName()); + int rc = system(command.toAscii().data()); + if (rc != 0) { + // this will keep patchName empty and we fall throuhg to the interview. + Logger::warn() << "Vng-Warning: Could not start editor '" << getenv("EDITOR") << "`\n"; + Logger::warn().flush(); + } + else if (modification == QFileInfo(*commitMessage).lastModified()) { + Logger::info() << "unchanged, won't commit\n"; + return Ok; + } + else { + // get data until the separator line. + commitMessage->open(QIODevice::ReadOnly); + patchName = commitMessage->readAll(); + commitMessage->close(); + int cuttoff = patchName.indexOf(defaultCommitMessage1); + if (cuttoff > 0) + patchName.truncate(cuttoff); + } + } + if (patchName.isEmpty()) + patchName = Interview::ask("What is the patch name? ").toUtf8(); + ReturnCodes rc = addFilesPerAcceptance(changeSet, all); if (rc != Ok) return rc; @@ -197,7 +240,7 @@ AbstractCommand::ReturnCodes Record::run() Logger::error() << "Git commit-tree failed!, aborting commit\n"; return rc; } - git.write(patchName.toUtf8()); + git.write(patchName); git.write("\n"); git.closeWriteChannel(); Vng::readLine(&git, buf, sizeof(buf)); @@ -233,6 +276,9 @@ AbstractCommand::ReturnCodes Record::run() runner.start(GitRunner::WaitUntilFinished); } + int endOfLine = patchName.indexOf('\n'); + if (endOfLine > 0) + patchName.truncate(endOfLine); Logger::info() << "Finished recording patch `" << patchName << "'" << endl; return Ok; } -- 2.11.4.GIT