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/>.
20 #include "CommandLineParser.h"
21 #include "GitRunner.h"
22 #include "hunks/ChangeSet.h"
23 #include "hunks/HunksCursor.h"
25 #include "Interview.h"
33 # define getpid() rand()
35 # include <sys/types.h>
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."},
59 : AbstractCommand("record"),
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
88 void Record::setUsageMode(UsageMode mode
)
93 QString
Record::sha1() const
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"));
110 AbstractCommand::ReturnCodes
Record::record()
112 if (! checkInRepository())
114 moveToRoot(CheckFileSystem
);
117 m_all
= m_mode
== AllChanges
;
118 const bool needHunks
= !m_all
|| m_patchName
.isEmpty();
121 changeSet
.fillFromCurrentChanges(rebasedArguments(), needHunks
);
123 changeSet
.waitForFinishFirstFile();
124 bool shouldDoRecord
= changeSet
.count() > 0;
125 if (!shouldDoRecord
) {
126 Logger::warn() << "No changes!" << endl
;
130 QString email
= m_author
;
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
;
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
;
173 if ((m_editComment
|| m_patchName
.isEmpty()) && getenv("EDITOR")) {
174 class Deleter
: public QObject
{
176 Deleter() : commitMessage(0) { }
179 commitMessage
->remove();
181 QFile
*commitMessage
;
184 QFile
*commitMessage
;
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";
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
);
200 commitMessage
->write("\n", 1);
201 commitMessage
->write(defaultCommitMessage1
, strlen(defaultCommitMessage1
));
202 commitMessage
->write(defaultCommitMessage2
, strlen(defaultCommitMessage2
));
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());
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());
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
;
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
);
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
249 if (m_patchName
.isEmpty())
250 m_patchName
= Interview::ask("What is the patch name? ").toUtf8();
252 ReturnCodes rc
= addFilesPerAcceptance(changeSet
, m_all
);
257 QStringList arguments
;
258 arguments
<< "write-tree";
259 GitRunner
runner(git
, arguments
);
260 rc
= runner
.start(GitRunner::WaitForStandardOutput
);
262 Logger::error() << "Git write-tree failed!, aborting record\n";
266 Vng::readLine(&git
, buf
, sizeof(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
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
);
282 Logger::error() << "Git commit-tree failed!, aborting record\n";
285 git
.write(m_patchName
);
287 git
.closeWriteChannel();
288 Vng::readLine(&git
, buf
, sizeof(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";
296 m_sha1
= commit
.left(40);
299 arguments
<< "update-ref" << "HEAD" << m_sha1
;
300 runner
.setArguments(arguments
);
301 rc
= runner
.start(GitRunner::WaitUntilFinished
);
303 Logger::error() << "Git update-ref failed!, aborting record\n";
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.
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');
325 m_patchName
.truncate(endOfLine
);
326 Logger::warn() << "Finished recording patch `" << m_patchName
<< "'" << endl
;
330 AbstractCommand::ReturnCodes
Record::addFilesPerAcceptance(const ChangeSet
&changeSet
, bool allChanges
)
332 typedef QPair
<QString
, QString
> NamePair
;
337 foreach(NamePair pair
, m_fileNames
) {
338 QFile
copy(pair
.second
);
340 if (! pair
.first
.isEmpty()) {
341 QFile
orig(pair
.first
);
342 orig
.rename(copy
.fileName());
346 void append(const QString
&orig
, const QString
©
) {
347 QPair
<QString
, QString
> pair(orig
, copy
);
348 m_fileNames
.append(pair
);
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.
369 foreach (Hunk hunk
, file
.hunks()) {
370 Vng::Acceptance a
= hunk
.acceptance();
371 if (a
== Vng::Accepted
) {
373 } else if (a
== Vng::MixedAcceptance
) {
379 if (someEnabled
&& !allEnabled
)
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();
389 Logger::debug() << "'" << file
.fileName() << "` addUnaltered: " << addUnaltered
<< ", removeFromIndex: "
390 << removeFromIndex
<< ", someChangesAccepted: " << someEnabled
<< ", allAccepted: " << allEnabled
<< endl
;
391 if (file
.fileName().isEmpty())
392 filesForAdd
<< file
.oldFileName();
394 filesForAdd
<< file
.fileName();
396 filesForAdd
<< file
.oldFileName();
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());
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";
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();
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.
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
);
449 if (patch
.size() != 0) {
451 QStringList arguments
;
452 arguments
<< "apply" << "--apply" << "--reverse" << patch
.fileName();
453 GitRunner
runner(git
, arguments
);
454 ReturnCodes rc
= runner
.start(GitRunner::WaitUntilFinished
);
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.
461 // first clean out the index so that previous stuff other tools did doesn't influence us.
462 if (!m_config
.isEmptyRepo() && ! notUsedFiles
.isEmpty()) {
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()) {
474 QStringList arguments
;
475 arguments
<< "update-index" << "--add" << "--remove";
476 int count
= 25; // length of arguments
478 QString first
= filesForAdd
[0];
479 if (count
+ first
.length() > 32000)
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
);
488 Logger::error() << "Vng failed: Did not manage to add files to the git index, aborting record\n";
493 return Ok
; // exiting scope will revert all files in the local filesystem. We use the index from here on.