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 //{"--leave-test-directory", "don't remove the test directory"},
50 //{"--remove-test-directory", "remove the test directory"},
51 //{"--edit-long-comment", "Edit the long comment by default"},
52 //{"--skip-long-comment", "Don't give a long comment"},
53 //{"--prompt-long-comment", "Prompt for whether to edit the long comment"},
54 //{"-l, --look-for-adds", "Also look for files that are potentially pending addition"},
55 //{"--dont-look-for-adds", "Don't look for any files that could be added"},
56 //{"--umask UMASK", "specify umask to use when writing."},
57 //{"--posthook COMMAND", "specify command to run after this vng command."},
58 //{"--no-posthook", "Do not run posthook command."},
59 //{"--prompt-posthook", "Prompt before running posthook. [DEFAULT]"},
60 //{"--run-posthook", "Run posthook command without prompting."},
65 : AbstractCommand("record")
67 CommandLineParser::addOptionDefinitions(options
);
68 CommandLineParser::setArgumentDefinition("record [FILE or DIRECTORY]" );
71 QString
Record::argumentDescription() const
73 return "[FILE or DIRECTORY]";
76 QString
Record::commandDescription() const
78 return "Record is used to name a set of changes and record the patch to the repository.\n";
81 AbstractCommand::ReturnCodes
Record::run()
83 if (! checkInRepository())
87 CommandLineParser
*args
= CommandLineParser::instance();
88 const bool all
= m_config
.isEmptyRepo() || m_config
.contains("all")
89 && !args
->contains("interactive") || args
->contains("all");
92 if (! (m_config
.useEditMode() && rebasedArguments().isEmpty()))
93 changeSet
.fillFromCurrentChanges(rebasedArguments());
95 changeSet
.waitForFinishFirstFile();
96 bool shouldDoRecord
= changeSet
.count() > 0;
97 if (!shouldDoRecord
) {
98 Logger::warn() << "No changes!" << endl
;
102 QString email
= args
->optionArgument("author", m_config
.optionArgument("author", getenv("EMAIL")));
103 QStringList environment
;
104 if (! email
.isEmpty()) {
105 QRegExp
re("(.*) <([@\\S]+)>");
106 if (re
.exactMatch(email
)) { // meaning its an email AND name
107 environment
<< "GIT_AUTHOR_NAME="+ re
.cap(1);
108 environment
<< "GIT_COMMITTER_NAME="+ re
.cap(1);
109 environment
<< "GIT_AUTHOR_EMAIL="+ re
.cap(2);
110 environment
<< "GIT_COMMITTER_EMAIL="+ re
.cap(2);
112 else if (!args
->contains("author")) // if its an account or shell wide option; just use the git defs.
113 environment
<< "GIT_AUTHOR_EMAIL="+ email
;
115 Logger::error() << "Author format invalid. Please provide author formatted like; `name <email@host>\n";
116 return InvalidOptions
;
120 if (shouldDoRecord
&& !all
) { // then do interview
121 HunksCursor
cursor(changeSet
);
122 cursor
.setConfiguration(m_config
);
123 Interview
interview(cursor
, name());
124 interview
.setUsePager(shouldUsePager());
125 if (! interview
.start()) {
126 Logger::warn() << "Record cancelled." << endl
;
131 if (shouldDoRecord
&& !all
) { // check if there is anything selected
132 shouldDoRecord
= changeSet
.hasAcceptedChanges();
133 if (! shouldDoRecord
) {
134 Logger::warn() << "Ok, if you don't want to record anything, that's fine!" <<endl
;
138 QByteArray patchName
= args
->optionArgument("patch-name", m_config
.optionArgument("patch-name")).toUtf8();
142 if (patchName
.isEmpty() && getenv("EDITOR")) {
143 class Deleter
: public QObject
{
145 Deleter() : commitMessage(0) { }
148 commitMessage
->remove();
150 QFile
*commitMessage
;
153 QFile
*commitMessage
;
156 commitMessage
= new QFile(QString("vng-record-%1").arg(i
++), &parent
);
157 } while (commitMessage
->exists());
158 parent
.commitMessage
= commitMessage
; // make sure the file is removed from FS.
159 if (! commitMessage
->open(QIODevice::WriteOnly
)) {
160 Logger::error() << "Vng failed. Could not create a temporary file for the record message '"
161 << commitMessage
->fileName() << "`\n";
164 const char * defaultCommitMessage1
= "\n***END OF DESCRIPTION***"; // we will look for this string later
165 const char * defaultCommitMessage2
= "\nPlace the long patch description above the ***END OF DESCRIPTION*** marker.\n\nThis patch contains the following changes:\n\n";
166 commitMessage
->write("\n", 1);
167 commitMessage
->write(defaultCommitMessage1
, strlen(defaultCommitMessage1
));
168 commitMessage
->write(defaultCommitMessage2
, strlen(defaultCommitMessage2
));
170 changeSet
.writeDiff(buffer
, ChangeSet::UserSelection
);
171 ChangeSet actualChanges
;
172 actualChanges
.fillFromDiffFile(buffer
);
173 QTextStream
out (commitMessage
);
174 for (int i
=0; i
< actualChanges
.count(); ++i
) {
175 File file
= actualChanges
.file(i
);
176 file
.outputWhatsChanged(out
, m_config
, true, false);
180 commitMessage
->close();
181 QDateTime modification
= QFileInfo(*commitMessage
).lastModified();
182 QString command
= QString("%1 %2").arg(getenv("EDITOR")).arg(commitMessage
->fileName());
183 int rc
= system(command
.toAscii().data());
185 // this will keep patchName empty and we fall through to the interview.
186 Logger::warn() << "Vng-Warning: Could not start editor '" << getenv("EDITOR") << "`\n";
187 Logger::warn().flush();
189 else if (modification
== QFileInfo(*commitMessage
).lastModified()) {
190 Logger::warn() << "unchanged, won't record\n";
194 // get data until the separator line.
195 commitMessage
->open(QIODevice::ReadOnly
);
196 patchName
= commitMessage
->readAll();
197 commitMessage
->close();
198 int cuttoff
= patchName
.indexOf(defaultCommitMessage1
);
200 patchName
.truncate(cuttoff
);
203 if (patchName
.isEmpty())
204 patchName
= Interview::ask("What is the patch name? ").toUtf8();
206 ReturnCodes rc
= addFilesPerAcceptance(changeSet
, all
);
211 QStringList arguments
;
212 arguments
<< "write-tree";
213 GitRunner
runner(git
, arguments
);
214 rc
= runner
.start(GitRunner::WaitForStandardOutput
);
216 Logger::error() << "Git write-tree failed!, aborting record\n";
220 Vng::readLine(&git
, buf
, sizeof(buf
));
222 git
.waitForFinished(); // patiently wait for it to finish..
223 Logger::debug() << "The tree got git ref; " << tree
;
224 Logger::debug().flush(); // flush since we do an ask next
227 git
.setEnvironment(environment
);
230 // parent = git-cat-file commit HEAD | grep parent
231 // if .git/MERGE_HEAD exists its a merge
232 // then we have multiple heads, one additional per line in .git/MERGE_HEAD
233 // also use .git/MERGE_MSG
235 arguments
<< "commit-tree" << tree
.left(40);
236 if (!m_config
.isEmptyRepo())
237 arguments
<< "-p" << "HEAD" ;
239 runner
.setArguments(arguments
);
240 rc
= runner
.start(GitRunner::WaitUntilReadyForWrite
);
242 Logger::error() << "Git commit-tree failed!, aborting record\n";
245 git
.write(patchName
);
247 git
.closeWriteChannel();
248 Vng::readLine(&git
, buf
, sizeof(buf
));
250 Logger::debug() << "commit is ref; " << commit
;
251 git
.waitForFinished(); // patiently wait for it to finish..
252 if (commit
.isEmpty()) {
253 Logger::error() << "Git update-ref failed to produce a reference!, aborting record\n";
258 arguments
<< "update-ref" << "HEAD" << commit
.left(40);
259 runner
.setArguments(arguments
);
260 rc
= runner
.start(GitRunner::WaitUntilFinished
);
262 Logger::error() << "Git update-ref failed!, aborting record\n";
266 // We removed files from the index in case they were freshly added, but the user didn't want it in this commit.
267 // we have to re-add those files.
269 arguments
<< "update-index" << "--add";
270 for (int i
=0; i
< changeSet
.count(); ++i
) {
271 File file
= changeSet
.file(i
);
272 if (! file
.oldFileName().isEmpty())
273 continue; // not a new added file.
274 if (file
.renameAcceptance() == Vng::Rejected
)
275 arguments
.append(file
.fileName());
277 if (arguments
.count() > 2) {
278 runner
.setArguments(arguments
);
279 runner
.start(GitRunner::WaitUntilFinished
);
282 int endOfLine
= patchName
.indexOf('\n');
284 patchName
.truncate(endOfLine
);
285 Logger::warn() << "Finished recording patch `" << patchName
<< "'" << endl
;
289 AbstractCommand::ReturnCodes
Record::addFilesPerAcceptance(const ChangeSet
&changeSet
, bool allChanges
)
291 typedef QPair
<QString
, QString
> NamePair
;
296 foreach(NamePair pair
, m_fileNames
) {
297 QFile
copy(pair
.second
);
299 if (! pair
.first
.isEmpty()) {
300 QFile
orig(pair
.first
);
301 orig
.rename(copy
.fileName());
305 void append(const QString
&orig
, const QString
©
) {
306 QPair
<QString
, QString
> pair(orig
, copy
);
307 m_fileNames
.append(pair
);
311 QList
< QPair
<QString
, QString
> > m_fileNames
;
313 RevertCopier reverter
;// this will revert all file changes we make when we exit the scope of this method.
314 ChangeSet patchChanges
;
316 QStringList filesForAdd
;
317 QStringList notUsedFiles
;
318 QStringList filesToStopEditing
; //only for useEditMode == true
319 for (int i
=0; i
< changeSet
.count(); ++i
) {
320 File file
= changeSet
.file(i
);
321 bool someEnabled
= false;
322 bool allEnabled
= true;
323 const bool renamed
= file
.fileName() != file
.oldFileName();
324 const bool protectionChanged
= !renamed
&& file
.protection() != file
.oldProtection();
325 if (file
.renameAcceptance() == Vng::Accepted
&& renamed
326 || file
.protectionAcceptance() == Vng::Accepted
&& protectionChanged
) // record it.
329 foreach (Hunk hunk
, file
.hunks()) {
330 Vng::Acceptance a
= hunk
.acceptance();
331 if (a
== Vng::Accepted
|| a
== Vng::MixedAcceptance
)
335 if (someEnabled
&& !allEnabled
)
338 const bool addUnaltered
= allChanges
|| allEnabled
;
339 if (addUnaltered
&& m_config
.useEditMode())
340 filesToStopEditing
<< file
.fileName();
342 const bool removeFromIndex
= !allChanges
&& !someEnabled
; // user said 'no', lets make sure none of the changes are left in the index.
343 if (removeFromIndex
&& ! file
.fileName().isEmpty() && ! file
.oldFileName().isEmpty()) { // just ignore file.
344 notUsedFiles
<< file
.fileName();
347 Logger::debug() << "'" << file
.fileName() << "` addUnaltered: " << addUnaltered
<< ", removeFromIndex: "
348 << removeFromIndex
<< ", someChangesAccepted: " << someEnabled
<< ", allAccepted: " << allEnabled
<< endl
;
349 if (file
.fileName().isEmpty())
350 filesForAdd
<< file
.oldFileName();
352 filesForAdd
<< file
.fileName();
354 filesForAdd
<< file
.oldFileName();
358 continue; // thats easy; whole file to add.
359 if (removeFromIndex
&& file
.fileName().isEmpty()) { // user said no about recording a deleted file.
360 // this is a funny situation; *just* in case the user already somehow added the deletion to the index
361 // we need to reset that to make the index have the full file again. Notice that we need to remove the file afterwards.
362 Q_ASSERT(!file
.oldFileName().isEmpty());
364 QStringList arguments
;
365 arguments
<< "cat-file" << "blob" << file
.oldSha1();
366 GitRunner
runner(git
, arguments
);
367 ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
368 Logger::debug() << "restoring '" << file
.oldFileName() << "`\n";
370 reverter
.append(QString(), file
.oldFileName());
371 QFile
deletedFile(file
.oldFileName());
372 Q_ASSERT(! deletedFile
.exists());
373 bool success
= Vng::copyFile(git
, deletedFile
);
374 git
.waitForFinished();
382 // for the case where only some patches are selected we make a safety copy of the file.
383 QFile
sourceFile(file
.fileName());
384 Q_ASSERT(sourceFile
.exists());
385 QString fileName
= file
.fileName();
386 for(int i
=0; i
< 10; i
++) {
387 fileName
= fileName
+ ".orig";
388 if (sourceFile
.rename(fileName
))
389 break; // successful!
391 reverter
.append(fileName
, file
.fileName());
392 if (removeFromIndex
) // need to rename it only, we won't patch it.
395 patchChanges
.addFile(file
);
396 Logger::debug() << "cp " << fileName
<< " =>" << file
.fileName() << endl
;
397 QFile
orig(fileName
);
398 orig
.open(QIODevice::ReadOnly
);
399 Vng::copyFile(orig
, sourceFile
); // TODO check return code
400 sourceFile
.setPermissions(file
.permissions());
403 QFile
patch(".vng.record." + QString::number(getpid()) + ".diff");
404 patchChanges
.writeDiff(patch
, ChangeSet::InvertedUserSelection
);
407 if (patch
.size() != 0) {
409 QStringList arguments
;
410 arguments
<< "apply" << "--apply" << "--reverse" << patch
.fileName();
411 GitRunner
runner(git
, arguments
);
412 ReturnCodes rc
= runner
.start(GitRunner::WaitUntilFinished
);
414 Logger::error() << "Vng failed: failed to patch, sorry! Aborting record.\n";
415 return rc
; // note that copied files will be moved back to avoid partially patched files lying around.
419 // first clean out the index so that previous stuff other tools did doesn't influence us.
420 if (!m_config
.isEmptyRepo() && ! notUsedFiles
.isEmpty()) {
422 QStringList arguments
;
423 arguments
<< "reset" << "-q" << "HEAD"; // -q stands for quiet.
424 arguments
+= notUsedFiles
;
425 GitRunner
runner(git
, arguments
);
426 ReturnCodes rc
= runner
.start(GitRunner::WaitUntilFinished
);
427 // if (rc != Ok) { // why o why does 'git reset' return 1 when it succeeded just fine.
428 // Logger::error() << "Cleaning the index failed, aborting record\n";
433 // git add of all files to get a nice list of changes into the index.
434 while (!filesForAdd
.isEmpty()) {
436 QStringList arguments
;
437 arguments
<< "update-index" << "--add" << "--remove";
438 int count
= 25; // length of arguments
440 QString first
= filesForAdd
[0];
441 if (count
+ first
.length() > 32000)
443 count
+= first
.length();
444 arguments
.append(first
);
445 filesForAdd
.removeFirst();
446 } while (!filesForAdd
.isEmpty());
447 GitRunner
runner(git
, arguments
);
448 ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
450 Logger::error() << "Vng failed: Did not manage to add files to the git index, aborting record\n";
455 if (m_config
.useEditMode())
456 m_config
.removeEditedFiles(filesToStopEditing
);
458 return Ok
; // exiting scope will revert all files in the local filesystem. We use the index from here on.