Make reverting/unreverting a binary file work.
authorThomas Zander <thomas.zander@trolltech.com>
Thu, 23 Jul 2009 18:43:04 +0000 (23 21:43 +0300)
committerThomas Zander <thomas.zander@trolltech.com>
Thu, 23 Jul 2009 18:43:04 +0000 (23 21:43 +0300)
Make writing out a patch for a binary file work partly.
It now writes a header with all the sha1s etc, but not the file data.
At least for a revert (i.e. git apply) this is enough :)

src/hunks/ChangeSet.cpp

index 0624016..7f7b9ce 100644 (file)
@@ -538,7 +538,6 @@ void ChangeSet::writeDiff(QIODevice &outDevice, ChangeSet::Selection selection)
     outDevice.open(QIODevice::WriteOnly | QIODevice::Truncate);
     QDataStream diff(&outDevice);
     foreach(File file, d->files) {
-        bool fileHeaderWritten = false;
         if ((selection == AllHunks
                     || (selection == UserSelection && file.renameAcceptance() == Vng::Accepted))
                 && !file.oldFileName().isEmpty() && !file.fileName().isEmpty()
@@ -546,7 +545,40 @@ void ChangeSet::writeDiff(QIODevice &outDevice, ChangeSet::Selection selection)
             writeRenameDiff(diff, file);
             continue;
         }
+        if (file.isBinary() && (selection == AllHunks || (selection == UserSelection
+                        && file.binaryChangeAcceptance() == Vng::Accepted))) {
+            diff.writeRawData("diff --git a/", 13);
+            QByteArray fileName = file.oldFileName();
+            if (fileName.isEmpty())
+                fileName = file.fileName();
+            diff.writeRawData(fileName.data(), fileName.size());
+            diff.writeRawData(" b/", 3);
+            fileName = file.fileName();
+            if (fileName.isEmpty())
+                fileName = file.oldFileName();
+            diff.writeRawData(fileName.data(), fileName.size());
+            if (file.oldFileName().isEmpty()) {
+                diff.writeRawData("\nnew file mode ", 15);
+                QByteArray protection = file.protection().toLatin1();
+                diff.writeRawData(protection.data(), protection.size());
+            } else if (file.fileName().isEmpty()) {
+                diff.writeRawData("\ndeleted file mode ", 19);
+                QByteArray protection = file.oldProtection().toLatin1();
+                diff.writeRawData(protection.data(), protection.size());
+            }
+            diff.writeRawData("\nindex ", 7);
+            QByteArray sha1 = file.oldSha1().toLatin1();
+            diff.writeRawData(sha1.data(), sha1.size());
+            diff.writeRawData("..", 2);
+            sha1 = file.sha1().toLatin1();
+            diff.writeRawData(sha1.data(), sha1.size());
+            diff.writeRawData("\nGIT binary patch\n", 18);
+            //file.writeBinaryDataAsPatch(outDevice); // TODO :)
+            diff.writeRawData("literal 0\nHcmV?d00001\n\n", 23);
+            continue;
+        }
 
+        bool fileHeaderWritten = false;
         foreach(Hunk hunk, file.hunks()) {
             if (selection == AllHunks
                 || (selection == UserSelection