Special case binary files for various cases so we can detect and use them
[vng.git] / hunks / ChangeSet.cpp
blob276174a6289d5df586af5da2a93a3ef6f003d540
1 /*
2 * This file is part of the vng project
3 * Copyright (C) 2008 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"
23 #include "../Vng.h"
25 #include <QProcess>
26 #include <QThread>
27 #include <QMutexLocker>
28 #include <QMutex>
29 #include <QWaitCondition>
30 #include <QDebug>
32 class HunksFetcher : public QThread
34 public:
35 HunksFetcher(const QList<File> &files, ChangeSet &changeSet, bool changeSetOnIndex)
36 : m_files(files),
37 m_changeSet(changeSet),
38 m_changeSetOnIndex(changeSetOnIndex),
39 m_interrupted(false)
43 void run()
45 setPriority(QThread::LowPriority);
46 foreach(File file, m_files) {
47 if (m_interrupted)
48 break;
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: `" << file.oldFileName() << "' => `" << file.fileName() << "'\n";
55 Logger::debug() << " +- Unchanged file, skipping\n";
56 m_changeSet.removeFile(file);
57 QProcess git;
58 QStringList arguments;
59 arguments << "update-index" << "-q" << "--refresh" << file.fileName();
60 GitRunner runner(git, arguments);
61 runner.start(GitRunner::WaitUntilFinished);
62 continue;
64 if (file.fileName().isEmpty() || file.oldFileName().isEmpty())
65 file.setProtectionAcceptance(Vng::Accepted);
67 int i=0;
68 if (Logger::verbosity() >= Logger::Debug) {
69 Logger::debug() << "file: `" << file.oldFileName() << "' => `" << file.fileName() << "'\n";
70 if (file.isBinary())
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();
85 void interrupt()
87 m_interrupted = true;
90 private:
91 QList<File> m_files;
92 ChangeSet &m_changeSet;
93 bool m_changeSetOnIndex, m_interrupted;
96 class ChangeSet::Private
98 public:
99 Private() :
100 changeSetOnIndex(false),
101 hunksFetcher(0),
102 ref(1),
103 finishedOneHunk(true)
107 ~Private() {
108 if (hunksFetcher) {
109 hunksFetcher->interrupt();
110 hunksFetcher->wait();
111 delete hunksFetcher;
115 QList<File> files;
116 bool changeSetOnIndex; // the changesSet shows the changes of the working dir
117 QMutex fileAccessLock;
118 QWaitCondition fileAccessWaiter;
119 QWaitCondition cursorAccessWaiter;
120 QMutex cursorAccessLock;
121 File lockedFile;
122 HunksFetcher *hunksFetcher;
123 #if QT_VERSION >= 0x040400
124 QAtomicInt ref;
125 #else
126 int ref;
127 #endif
128 bool finishedOneHunk;
131 ChangeSet::ChangeSet()
132 : d(new Private())
136 ChangeSet::~ChangeSet()
138 #if QT_VERSION >= 0x040400
139 if (!d->ref.deref())
140 #else
141 if (--d->ref == 0)
142 #endif
143 delete d;
146 ChangeSet::ChangeSet(const ChangeSet &other)
147 : d(other.d)
149 #if QT_VERSION >= 0x040400
150 d->ref.ref();
151 #else
152 d->ref++;
153 #endif
156 AbstractCommand::ReturnCodes ChangeSet::fillFromDiffFile(QIODevice &file)
158 file.open(QIODevice::ReadOnly);
160 foreach (File f, readGitDiff(file))
161 addFile(f);
163 if (Logger::verbosity() >= Logger::Debug) {
164 foreach(File f, d->files) {
165 Logger::debug() << "changes in file: " << f.fileName() << endl;
166 int i=0;
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.
185 QProcess git;
186 QStringList arguments;
187 arguments << "ls-files" << "-s";
188 GitRunner runner(git, arguments);
189 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
190 if (rc)
191 return rc;
192 char buf[1024];
193 while(true) {
194 qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
195 if (lineLength == -1)
196 break;
197 File file;
198 file.setProtection(QString::fromAscii(buf, 6));
199 file.setSha1(QString::fromAscii(buf+7, 40));
200 file.setFileName(QByteArray(buf + 50, lineLength - 51));
201 d->files.append(file);
203 return AbstractCommand::Ok;
206 QProcess git;
207 QStringList arguments;
208 arguments << "diff-index" << "-M" << "HEAD";
209 if (! paths.isEmpty())
210 arguments << "--" << paths;
212 // for each line
213 // new file:
214 // :000000 100644 0000000000000000000000000000000000000000 8c3ae1d344f18b23c3bdde5d26658b70b03c65d9 A bar
215 // rename main.cpp => notmain.cpp
216 // :100644 100644 e58cfe72cb7a9559a0090886bea5b0ce00db6b47 477c729e5abbd701eb708df563e7e4f749b50435 R074 main.cpp notmain.cpp
217 // normal change
218 // :100644 100644 0bdd73e9ea0ba026f6796799946c4bfc9dd1b0b8 0000000000000000000000000000000000000000 M test
220 GitRunner runner(git, arguments);
221 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
222 if (rc)
223 return rc;
224 char buf[1024];
225 while(true) {
226 qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
227 if (lineLength == -1)
228 break;
229 if (lineLength > 0 && buf[0] != ':') // not a diff line, ignore.
230 continue;
232 File file;
233 file.setOldProtection(QString::fromAscii(buf+1, 6));
234 file.setProtection(QString::fromAscii(buf+8, 6));
235 file.setOldSha1(QString::fromAscii(buf+15, 40));
236 file.setSha1(QString::fromAscii(buf+56, 40));
237 int offset = 98;
238 while (buf[offset] != '\t' && offset < lineLength)
239 offset++;
240 if (buf[97] == 'R') { // rename
241 int tab = offset + 1;
242 while (buf[tab] != '\t' && tab < lineLength)
243 tab++;
244 file.setOldFileName(QByteArray(buf + offset + 1, tab - offset - 1));
245 file.setFileName(QByteArray(buf + tab + 1, lineLength - tab - 2));
247 else if (buf[97] == 'C') { // Copied file
248 int tab = offset + 1;
249 while (buf[tab] != '\t' && tab < lineLength)
250 tab++;
251 QByteArray filename(buf + offset + 1, tab - offset - 1);
252 file.setOldFileName(filename);
253 file.setFileName(filename);
255 else {
256 QByteArray filename(buf + offset + 1, lineLength - offset - 2);
257 if (buf[97] != 'A') // Add
258 file.setOldFileName(filename);
259 if (buf[97] != 'D') // Delete
260 file.setFileName(filename);
262 d->files.append(file);
264 if (doGenerateHunks)
265 generateHunks();
267 return AbstractCommand::Ok;
270 void ChangeSet::generateHunks()
272 Q_ASSERT(d->hunksFetcher == 0);
273 d->hunksFetcher = new HunksFetcher(d->files, *this, d->changeSetOnIndex);
274 d->finishedOneHunk = false;
275 d->hunksFetcher->start();
278 void ChangeSet::lockFile(const File &file)
280 // qDebug() << "ChangeSet::lockFile";
281 QMutexLocker ml(&d->fileAccessLock);
282 d->lockedFile = file;
283 if (d->files.count() == 0 || d->files.at(0) != file) { // as soon as we have done a file, we can start the interaction
284 // qDebug() << " unlock cursorAccessWaiter";
285 d->cursorAccessLock.lock();
286 d->finishedOneHunk = true;
287 d->cursorAccessWaiter.wakeAll();
288 d->cursorAccessLock.unlock();
291 d->fileAccessWaiter.wakeAll();
292 // qDebug() << "~ChangeSet::lockFile";
295 void ChangeSet::removeFile(const File &file)
297 QMutexLocker ml(&d->fileAccessLock);
298 // TODO move the cursor if this file is the current file.
299 d->files.removeAll(file);
300 d->fileAccessWaiter.wakeAll();
303 void ChangeSet::allHunksFetched()
305 // qDebug() << "ChangeSet::allHunksFetched";
306 QMutexLocker ml(&d->fileAccessLock);
307 d->lockedFile = File();
308 d->fileAccessWaiter.wakeAll();
309 // qDebug() << "~ChangeSet::allHunksFetched";
312 bool ChangeSet::hasAllHunks() const
314 return d->hunksFetcher == 0 || d->hunksFetcher->isFinished();
317 // static
318 QList<File> ChangeSet::readGitDiff(QIODevice &git, File *fileToDiff)
320 QList<File> filesInDiff;
322 // parse the output and create objects for each hunk. (note how that can be made multi-threading)
323 // we have to have the filename in the hunk too to allow skipping a whole hunk
324 char buf[1024];
325 bool inPatch = false;
326 // a diff can be multiple files, or just the one fileToDiff. Lets keep one File object to point to the current file.
327 File file;
328 Hunk hunk;
329 while(true) {
330 qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
331 if (lineLength == -1)
332 break;
333 QString line = QString::fromLocal8Bit(buf, lineLength);
335 const bool newfile = line.startsWith("--- /dev/null");
336 if (line.length() > 6 && (newfile || line.startsWith("--- a/"))) {
337 file.addHunk(hunk);
338 hunk = Hunk();
339 if (file.isValid())
340 filesInDiff << file;
341 if (fileToDiff)
342 file = File(*fileToDiff);
343 else {
344 file = File();
345 if (!newfile) {
346 QByteArray array(buf + 6, strlen(buf) - 7);
347 file.setOldFileName(array);
350 inPatch = false;
352 else if (fileToDiff == 0 && line.length() > 6 && line.startsWith("+++ b/")) {
353 QByteArray array(buf + 6, strlen(buf) - 7);
354 file.setFileName(array);
356 else if (line.length() > 5 && line.startsWith("@@ -")) {
357 file.addHunk(hunk);
358 hunk = Hunk();
359 inPatch = true;
361 else if (inPatch && line.startsWith("diff --git "))
362 inPatch = false;
363 else if (line.startsWith("Binary files a/") && line.indexOf(" differ") > 0) { // TODO does git do translations?
364 Q_ASSERT(fileToDiff);
365 fileToDiff->setBinary(true);
366 git.close();
367 return filesInDiff;
369 if (inPatch) {
370 QByteArray array(buf, lineLength);
371 hunk.addLine(array);
374 file.addHunk(hunk);
375 if (fileToDiff == 0 && file.isValid())
376 filesInDiff << file;
377 git.close();
378 return filesInDiff;
381 void ChangeSet::addFile(const File &file)
383 if (file.isValid())
384 d->files << file;
387 int ChangeSet::count() const
389 return d->files.count();
392 void ChangeSet::writeDiff(QIODevice &file, ChangeSet::Selection selection) const
394 waitFinishGenerateHunks();
395 file.open(QIODevice::WriteOnly | QIODevice::Truncate);
396 QDataStream diff(&file);
397 foreach(File file, d->files) {
398 bool fileHeaderWritten = false;
399 foreach(Hunk hunk, file.hunks()) {
400 if (selection == AllHunks
401 || selection == UserSelection
402 && (hunk.acceptance() == Vng::Accepted || hunk.acceptance() == Vng::MixedAcceptance)
403 || selection == InvertedUserSelection && hunk.acceptance() != Vng::Accepted) {
404 if (!fileHeaderWritten) {
405 diff.writeRawData("--- a/", 6);
406 diff.writeRawData(file.oldFileName().data(), file.oldFileName().size());
407 diff.writeRawData("\n+++ b/", 7);
408 diff.writeRawData(file.fileName().data(), file.fileName().size());
409 diff.writeRawData("\n", 1);
410 fileHeaderWritten = true;
412 QByteArray acceptedPatch;
413 if (selection == InvertedUserSelection)
414 acceptedPatch =hunk.rejectedPatch();
415 else if (selection == AllHunks) {
416 acceptedPatch = hunk.header();
417 acceptedPatch.append(hunk.patch());
419 else
420 acceptedPatch = hunk.acceptedPatch();
421 diff.writeRawData(acceptedPatch.data(), acceptedPatch.size());
425 file.close();
428 bool ChangeSet::hasAcceptedChanges() const
430 waitFinishGenerateHunks();
431 foreach(File file, d->files) {
432 if (file.renameAcceptance() == Vng::Accepted && file.fileName() != file.oldFileName())
433 return true;
434 if (file.protectionAcceptance() == Vng::Accepted && file.protection() != file.oldProtection())
435 return true;
436 if (file.isBinary() && file.binaryChangeAcceptance() == Vng::Accepted)
437 return true;
438 foreach(Hunk hunk, file.hunks()) {
439 Vng::Acceptance a = hunk.acceptance();
440 if (a == Vng::Accepted || a == Vng::MixedAcceptance)
441 return true;
444 return false;
447 File ChangeSet::file(int index) const
449 // qDebug() << "ChangeSet::file" << index << d->finishedOneHunk;
450 waitForFinishFirstFile();
451 QMutexLocker ml2(&d->fileAccessLock);
452 while (d->files.count() > index && d->files[index] == d->lockedFile)
453 // { qDebug() << " waiting for file to be unlocked";
454 d->fileAccessWaiter.wait(&d->fileAccessLock);
455 // }
456 if (d->files.count() <= index)
457 return File();
459 // qDebug() << "ChangeSet::~file";
460 return d->files[index];
463 ChangeSet &ChangeSet::operator=(const ChangeSet &other)
465 #if QT_VERSION >= 0x040400
466 other.d->ref.ref();
467 if (!d->ref.deref())
468 #else
469 other.d->ref++;
470 if (--d->ref == 0)
471 #endif
472 delete d;
473 d = other.d;
474 return *this;
477 void ChangeSet::waitFinishGenerateHunks() const
479 if (d->hunksFetcher)
480 d->hunksFetcher->wait();
483 void ChangeSet::waitForFinishFirstFile() const
485 d->cursorAccessLock.lock();
486 if (! d->finishedOneHunk)
487 d->cursorAccessWaiter.wait(&d->cursorAccessLock);
488 d->cursorAccessLock.unlock();