2 * This file is part of the vng project
3 * Copyright (C) 2008-2009 Thomas Zander <tzander@trolltech.com>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "ChangeSet.h"
20 #include "../GitRunner.h"
21 #include "../Logger.h" // for debugging level only
22 #include "../AbstractCommand.h"
27 #include <QMutexLocker>
29 #include <QWaitCondition>
32 class HunksFetcher
: public QThread
35 HunksFetcher(const QList
<File
> &files
, ChangeSet
&changeSet
, bool changeSetOnIndex
)
37 m_changeSet(changeSet
),
38 m_changeSetOnIndex(changeSetOnIndex
),
45 setPriority(QThread::LowPriority
);
46 foreach(File file
, m_files
) {
49 m_changeSet
.lockFile(file
);
51 file
.fetchHunks(m_changeSetOnIndex
);
53 if (!file
.isBinary() && file
.count() == 0 && !file
.hasChanged()) { // No change in file at all.
54 Logger::debug() << "file: `" << QString::fromUtf8(file
.oldFileName()) << "' => `" << QString::fromUtf8(file
.fileName()) << "'\n";
55 Logger::debug() << " +- Unchanged file, skipping\n";
56 m_changeSet
.removeFile(file
);
58 QStringList arguments
;
59 arguments
<< "update-index" << "-q" << "--refresh" << file
.fileName();
60 GitRunner
runner(git
, arguments
);
61 runner
.start(GitRunner::WaitUntilFinished
);
64 if (file
.fileName().isEmpty() || file
.oldFileName().isEmpty())
65 file
.setProtectionAcceptance(Vng::Accepted
);
68 if (Logger::verbosity() >= Logger::Debug
) {
69 Logger::debug() << "file: `" << QString::fromUtf8(file
.oldFileName()) << "' => `" << QString::fromUtf8(file
.fileName()) << "'\n";
71 Logger::debug() << " +- is a binary file" << endl
;
72 Logger::debug() << " +- " << file
.oldProtection() << " => " << file
.protection() << endl
;
73 foreach(Hunk h
, file
.hunks()) {
74 Logger::debug() << " +-(" << i
++ << ") @" << h
.lineNumber() << "; " << h
.patch().size() << " bytes\n";
75 for(int i
= 0; i
< h
.subHunkCount(); i
++) {
76 Logger::debug() << " " << i
<<"/"<< h
.subHunkCount() <<"; "<< h
.subHunk(i
).size() <<" bytes\n";
81 m_changeSet
.lockFile(File());
82 m_changeSet
.allHunksFetched();
92 ChangeSet
&m_changeSet
;
93 bool m_changeSetOnIndex
, m_interrupted
;
96 class ChangeSet::Private
100 changeSetOnIndex(false),
103 finishedOneHunk(true)
109 hunksFetcher
->interrupt();
110 hunksFetcher
->wait();
116 bool changeSetOnIndex
; // the changesSet shows the changes of the working dir
117 QMutex fileAccessLock
;
118 QWaitCondition fileAccessWaiter
;
119 QWaitCondition cursorAccessWaiter
;
120 QMutex cursorAccessLock
;
122 HunksFetcher
*hunksFetcher
;
123 #if QT_VERSION >= 0x040400
128 bool finishedOneHunk
;
131 ChangeSet::ChangeSet()
136 ChangeSet::~ChangeSet()
138 #if QT_VERSION >= 0x040400
146 ChangeSet::ChangeSet(const ChangeSet
&other
)
149 #if QT_VERSION >= 0x040400
156 AbstractCommand::ReturnCodes
ChangeSet::fillFromDiffFile(QIODevice
&file
)
158 file
.open(QIODevice::ReadOnly
);
160 foreach (File f
, readGitDiff(file
))
163 if (Logger::verbosity() >= Logger::Debug
) {
164 foreach(File f
, d
->files
) {
165 Logger::debug() << "changes in file: " << f
.fileName() << endl
;
167 foreach(Hunk h
, f
.hunks()) {
168 Logger::debug() << " +-(" << i
++ << ") @" << h
.lineNumber() << "; " << h
.patch().size() << " bytes\n";
169 for(int i
= 0; i
< h
.subHunkCount(); i
++) {
170 Logger::debug() << " " << i
<<"/"<< h
.subHunkCount() <<"; "<< h
.subHunk(i
).size() <<" bytes\n";
174 Logger::debug().flush();
176 return AbstractCommand::Ok
;
179 AbstractCommand::ReturnCodes
ChangeSet::fillFromCurrentChanges(const QStringList
&paths
, bool doGenerateHunks
)
181 d
->changeSetOnIndex
= true;
182 QDir
refs(".git/refs/heads");
183 const bool emptyRepo
= refs
.count() == 2; // only '.' and '..'
184 if (emptyRepo
) { // all files added are new, just add all.
186 QStringList arguments
;
187 arguments
<< "ls-files" << "-s";
188 GitRunner
runner(git
, arguments
);
189 AbstractCommand::ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
194 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
195 if (lineLength
== -1)
198 file
.setProtection(QString::fromAscii(buf
, 6));
199 file
.setSha1(QString::fromAscii(buf
+7, 40));
200 file
.setFileName(File::escapeGitFilename(QByteArray(buf
+ 50, lineLength
- 51)));
201 d
->files
.append(file
);
203 return AbstractCommand::Ok
;
206 // TODO the below misses the usecase of vng add and then a filesystem rm. Use diff-index --cached to show those.
208 QStringList arguments
;
209 arguments
<< "diff-index" << "-M" << "HEAD";
210 if (! paths
.isEmpty())
211 arguments
<< "--" << paths
;
215 // :000000 100644 0000000000000000000000000000000000000000 8c3ae1d344f18b23c3bdde5d26658b70b03c65d9 A bar
216 // rename main.cpp => notmain.cpp
217 // :100644 100644 e58cfe72cb7a9559a0090886bea5b0ce00db6b47 477c729e5abbd701eb708df563e7e4f749b50435 R074 main.cpp notmain.cpp
219 // :100644 100644 0bdd73e9ea0ba026f6796799946c4bfc9dd1b0b8 0000000000000000000000000000000000000000 M test
221 GitRunner
runner(git
, arguments
);
222 AbstractCommand::ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
227 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
228 if (lineLength
== -1)
230 if (lineLength
> 0 && buf
[0] != ':') // not a diff line, ignore.
234 file
.setOldProtection(QString::fromAscii(buf
+1, 6));
235 file
.setProtection(QString::fromAscii(buf
+8, 6));
236 file
.setOldSha1(QString::fromAscii(buf
+15, 40));
237 file
.setSha1(QString::fromAscii(buf
+56, 40));
239 while (buf
[offset
] != '\t' && offset
< lineLength
)
241 if (buf
[97] == 'R') { // rename
242 int tab
= offset
+ 1;
243 while (buf
[tab
] != '\t' && tab
< lineLength
)
245 file
.setOldFileName(File::escapeGitFilename(QByteArray(buf
+ offset
+ 1, tab
- offset
- 1)));
246 file
.setFileName(File::escapeGitFilename(QByteArray(buf
+ tab
+ 1, lineLength
- tab
- 2)));
248 else if (buf
[97] == 'C') { // Copied file
249 int tab
= offset
+ 1;
250 while (buf
[tab
] != '\t' && tab
< lineLength
)
252 QByteArray
filename(buf
+ offset
+ 1, tab
- offset
- 1);
253 filename
= File::escapeGitFilename(filename
);
254 file
.setOldFileName(filename
);
255 file
.setFileName(filename
);
258 QByteArray
filename(buf
+ offset
+ 1, lineLength
- offset
- 2);
259 filename
= File::escapeGitFilename(filename
);
260 if (buf
[97] != 'A') // Add
261 file
.setOldFileName(filename
);
262 if (buf
[97] != 'D') // Delete
263 file
.setFileName(filename
);
265 d
->files
.append(file
);
270 // call git-diff-files which will find all files that have been removed from the filesystem but are in the index.
271 // Since we don't like the index we just remove them from the index here.
273 arguments
<< "diff-files";
274 if (! paths
.isEmpty())
275 arguments
<< "--" << paths
;
276 runner
.setArguments(arguments
);
277 rc
= runner
.start(GitRunner::WaitForStandardOutput
);
281 arguments
<< "update-index" << "--remove";
283 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
284 if (lineLength
== -1)
286 if (lineLength
> 0 && buf
[0] != ':') // not a diff line, ignore.
288 if (lineLength
< 97 ||buf
[97] != 'D')
292 while (buf
[offset
] != '\t' && offset
< lineLength
)
294 arguments
.append(QString::fromUtf8(File::escapeGitFilename(QByteArray(buf
+ offset
+ 1, lineLength
- offset
- 2))));
296 if (arguments
.count() > 2) {
297 runner
.setArguments(arguments
);
298 runner
.start(GitRunner::WaitUntilFinished
);
301 return AbstractCommand::Ok
;
304 void ChangeSet::generateHunks()
306 Q_ASSERT(d
->hunksFetcher
== 0);
307 d
->hunksFetcher
= new HunksFetcher(d
->files
, *this, d
->changeSetOnIndex
);
308 d
->finishedOneHunk
= false;
309 d
->hunksFetcher
->start();
312 void ChangeSet::lockFile(const File
&file
)
314 // qDebug() << "ChangeSet::lockFile";
315 QMutexLocker
ml(&d
->fileAccessLock
);
316 d
->lockedFile
= file
;
317 if (d
->files
.count() == 0 || d
->files
.at(0) != file
) { // as soon as we have done a file, we can start the interaction
318 // qDebug() << " unlock cursorAccessWaiter";
319 d
->cursorAccessLock
.lock();
320 d
->finishedOneHunk
= true;
321 d
->cursorAccessWaiter
.wakeAll();
322 d
->cursorAccessLock
.unlock();
325 d
->fileAccessWaiter
.wakeAll();
326 // qDebug() << "~ChangeSet::lockFile";
329 void ChangeSet::removeFile(const File
&file
)
331 QMutexLocker
ml(&d
->fileAccessLock
);
332 // TODO move the cursor if this file is the current file.
333 d
->files
.removeAll(file
);
334 d
->fileAccessWaiter
.wakeAll();
337 void ChangeSet::allHunksFetched()
339 // qDebug() << "ChangeSet::allHunksFetched";
340 QMutexLocker
ml(&d
->fileAccessLock
);
341 d
->lockedFile
= File();
342 d
->fileAccessWaiter
.wakeAll();
343 // qDebug() << "~ChangeSet::allHunksFetched";
346 bool ChangeSet::hasAllHunks() const
348 return d
->hunksFetcher
== 0 || d
->hunksFetcher
->isFinished();
352 QList
<File
> ChangeSet::readGitDiff(QIODevice
&git
, File
*fileToDiff
)
354 QList
<File
> filesInDiff
;
355 QList
<File
> newFiles
;
356 QList
<File
> removedFiles
;
358 // parse the output and create objects for each hunk. (note how that can be made multi-threading)
359 // we have to have the filename in the hunk too to allow skipping a whole hunk
366 // a diff can be multiple files, or just the one fileToDiff. Lets keep one File object to point to the current file.
371 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
372 if (lineLength
== -1 || state
== Empty
) {
375 if (file
.isValid()) {
376 if (file
.oldFileName().isEmpty())
378 else if (file
.fileName().isEmpty())
379 removedFiles
<< file
;
384 file
= File(*fileToDiff
);
390 if (lineLength
== -1)
392 QString line
= QString::fromLocal8Bit(buf
, lineLength
);
394 const bool newfile
= line
.startsWith("--- /dev/null");
395 if (line
.length() > 6 && (newfile
|| line
.startsWith("--- a/")
396 || line
.startsWith("--- \"a/"))) {
397 if (!newfile
&& fileToDiff
== 0) {
398 if (line
[4].unicode() == '"') { // git-encoding...
399 QByteArray
array(buf
+ 7, strlen(buf
) - 8);
401 file
.setOldFileName(File::escapeGitFilename(array
));
403 QByteArray
array(buf
+ 6, strlen(buf
) - 7);
404 file
.setOldFileName(File::escapeGitFilename(array
));
407 state
= state
== InPatch
? Empty
: InHeader
;
409 else if (fileToDiff
== 0 && line
.length() > 6 &&
410 (line
.startsWith("+++ b/") || line
.startsWith("+++ \"b"))) {
412 if (line
[4].unicode() == '"') { // git-encoding...
413 QByteArray
array(buf
+ 7, strlen(buf
) - 8);
415 file
.setFileName(File::escapeGitFilename(array
));
417 file
.setFileName(QByteArray(buf
+ 6, strlen(buf
) - 7));
420 else if (line
.length() > 5 && line
.startsWith("@@ -")) {
425 else if (line
.startsWith("diff --git ")) {
426 state
= state
== InPatch
? Empty
: InHeader
;
429 else if (line
.startsWith("Binary files a/") && line
.indexOf(" differ") > 0) {
430 Q_ASSERT(fileToDiff
);
431 fileToDiff
->setBinary(true);
435 else if (line
.startsWith("index ")) {
436 int dot
= line
.indexOf(QLatin1Char('.'), 6);
437 if (dot
> 0 && line
.length() > dot
+3) {
438 file
.setOldSha1(line
.mid(6, dot
-6));
439 int space
= line
.indexOf(QLatin1Char(' '), dot
);
441 space
= line
.length()-1; // cut off the linefeed
443 file
.setProtection(line
.mid(space
+1).trimmed());
445 file
.setSha1(line
.mid(dot
+2, space
- dot
- 2));
447 state
= state
== InPatch
? Empty
: InHeader
;
449 else if (line
.startsWith("deleted file mode ")) {
450 file
.setProtection(line
.mid(18).trimmed());
451 state
= state
== InPatch
? Empty
: InHeader
;
453 else if (line
.startsWith("new file mode ")) {
454 file
.setProtection(line
.mid(13).trimmed());
455 state
= state
== InPatch
? Empty
: InHeader
;
457 if (state
== InPatch
) {
458 QByteArray
array(buf
, lineLength
);
464 // try to find out if there are renames
465 foreach (File removedFile
, removedFiles
) {
466 foreach (File addedFile
, newFiles
) {
467 if (!addedFile
.sha1().isEmpty() && removedFile
.oldSha1() == addedFile
.sha1()) {
468 // TODO if this is a partial sha1 we may want to check some of the content
469 newFiles
.removeAll(addedFile
);
470 addedFile
.setOldSha1(removedFile
.oldSha1());
471 addedFile
.setOldFileName(removedFile
.oldFileName());
472 addedFile
.setOldProtection(removedFile
.oldProtection());
473 filesInDiff
<< addedFile
;
482 void ChangeSet::addFile(const File
&file
)
488 int ChangeSet::count() const
490 return d
->files
.count();
493 void ChangeSet::writeDiff(QIODevice
&outDevice
, ChangeSet::Selection selection
) const
495 waitFinishGenerateHunks();
496 outDevice
.open(QIODevice::WriteOnly
| QIODevice::Truncate
);
497 QDataStream
diff(&outDevice
);
498 foreach(File file
, d
->files
) {
499 bool fileHeaderWritten
= false;
500 if ((selection
== AllHunks
501 || (selection
== UserSelection
&& file
.renameAcceptance() == Vng::Accepted
))
502 && !file
.oldFileName().isEmpty()
503 && file
.oldFileName() != file
.fileName()) {
504 writeRenameDiff(diff
, file
);
508 foreach(Hunk hunk
, file
.hunks()) {
509 if (selection
== AllHunks
510 || (selection
== UserSelection
511 && (hunk
.acceptance() == Vng::Accepted
|| hunk
.acceptance() == Vng::MixedAcceptance
))
512 || (selection
== InvertedUserSelection
&& hunk
.acceptance() != Vng::Accepted
)) {
513 if (!fileHeaderWritten
) {
514 if (file
.oldFileName().isEmpty()) { // new file
515 diff
.writeRawData("--- /dev/null", 13);
517 diff
.writeRawData("--- a/", 6);
518 diff
.writeRawData(file
.oldFileName().data(), file
.oldFileName().size());
520 if (file
.fileName().isEmpty()) { // deleted file
521 diff
.writeRawData("\n+++ /dev/null\n", 15);
523 diff
.writeRawData("\n+++ b/", 7);
524 diff
.writeRawData(file
.fileName().data(), file
.fileName().size());
525 diff
.writeRawData("\n", 1);
527 fileHeaderWritten
= true;
529 QByteArray acceptedPatch
;
530 if (selection
== InvertedUserSelection
)
531 acceptedPatch
=hunk
.rejectedPatch();
532 else if (selection
== AllHunks
) {
533 acceptedPatch
= hunk
.header();
534 acceptedPatch
.append(hunk
.patch());
537 acceptedPatch
= hunk
.acceptedPatch();
538 diff
.writeRawData(acceptedPatch
.data(), acceptedPatch
.size());
545 void ChangeSet::writeRenameDiff(QDataStream
&out
, const File
&file
) const
547 out
.writeRawData("diff --git a/", 13);
548 out
.writeRawData(file
.fileName().data(), file
.fileName().size());
549 out
.writeRawData(" b/", 3);
550 out
.writeRawData(file
.fileName().data(), file
.fileName().size());
551 out
.writeRawData("\nnew file mode ", 15);
552 QByteArray tmp
= file
.protection().toLatin1();
553 out
.writeRawData(tmp
.data(), tmp
.length());
554 out
.writeRawData("\nindex 0000000..", 16);
555 tmp
= file
.sha1().toLatin1();
556 out
.writeRawData(tmp
.data(), 7);
557 out
.writeRawData("\n--- /dev/null\n+++ b/", 21);
558 out
.writeRawData(file
.fileName().data(), file
.fileName().size());
561 QStringList arguments
;
562 arguments
<< "cat-file" << "blob" << file
.sha1();
563 GitRunner
runner(git
, arguments
);
564 runner
.start(GitRunner::WaitForStandardOutput
);
565 QList
<QByteArray
> lines
;
568 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
569 if (lineLength
== -1)
571 lines
.append(QByteArray(buf
, lineLength
));
574 QByteArray countAsString
= QString::number(lines
.count()).toLatin1();
575 out
.writeRawData("\n@@ -0,0 +1,", 12);
576 out
.writeRawData(countAsString
.data(), countAsString
.length());
577 out
.writeRawData(" @@\n", 4);
578 foreach (const QByteArray
&line
, lines
) {
579 out
.writeRawData("+", 1);
580 out
.writeRawData(line
.data(), line
.length());
584 out
.writeRawData("\ndiff --git a/", 14);
585 out
.writeRawData(file
.oldFileName().data(), file
.oldFileName().size());
586 out
.writeRawData(" b/", 3);
587 out
.writeRawData(file
.oldFileName().data(), file
.oldFileName().size());
588 out
.writeRawData("\ndeleted file mode ", 19);
589 tmp
= file
.oldProtection().toLatin1();
590 out
.writeRawData(tmp
.data(), tmp
.length());
591 out
.writeRawData("\nindex ", 7);
592 tmp
= file
.oldSha1().toLatin1();
593 out
.writeRawData(tmp
.data(), 7);
594 out
.writeRawData("..0000000\n--- a/", 16);
595 out
.writeRawData(file
.oldFileName().data(), file
.oldFileName().size());
597 out
.writeRawData("\n+++ /dev/null\n@@ -1,", 21);
598 out
.writeRawData(countAsString
.data(), countAsString
.length());
599 out
.writeRawData(" +0,0 @@\n", 9);
601 foreach (const QByteArray
&line
, lines
) {
602 out
.writeRawData("-", 1);
603 out
.writeRawData(line
.data(), line
.length());
607 bool ChangeSet::hasAcceptedChanges() const
609 waitFinishGenerateHunks();
610 foreach(File file
, d
->files
) {
611 if (file
.renameAcceptance() == Vng::Accepted
&& file
.fileName() != file
.oldFileName())
613 if (file
.protectionAcceptance() == Vng::Accepted
&& file
.protection() != file
.oldProtection())
615 if (file
.isBinary() && file
.binaryChangeAcceptance() == Vng::Accepted
)
617 foreach(Hunk hunk
, file
.hunks()) {
618 Vng::Acceptance a
= hunk
.acceptance();
619 if (a
== Vng::Accepted
|| a
== Vng::MixedAcceptance
)
626 File
ChangeSet::file(int index
) const
628 // qDebug() << "ChangeSet::file" << index << d->finishedOneHunk;
629 waitForFinishFirstFile();
630 QMutexLocker
ml2(&d
->fileAccessLock
);
631 while (d
->files
.count() > index
&& d
->files
[index
] == d
->lockedFile
)
632 // { qDebug() << " waiting for file to be unlocked";
633 d
->fileAccessWaiter
.wait(&d
->fileAccessLock
);
635 if (d
->files
.count() <= index
)
638 // qDebug() << "ChangeSet::~file";
639 return d
->files
[index
];
642 ChangeSet
&ChangeSet::operator=(const ChangeSet
&other
)
644 #if QT_VERSION >= 0x040400
656 void ChangeSet::waitFinishGenerateHunks() const
659 d
->hunksFetcher
->wait();
662 void ChangeSet::waitForFinishFirstFile() const
664 d
->cursorAccessLock
.lock();
665 if (! d
->finishedOneHunk
)
666 d
->cursorAccessWaiter
.wait(&d
->cursorAccessLock
);
667 d
->cursorAccessLock
.unlock();