Fix creating a diff for a deleted file.
authorThomas Zander <thomas.zander@trolltech.com>
Thu, 23 Jul 2009 18:38:12 +0000 (23 21:38 +0300)
committerThomas Zander <thomas.zander@trolltech.com>
Thu, 23 Jul 2009 18:38:12 +0000 (23 21:38 +0300)
src/hunks/File.cpp

index 6d00d4d..fd746b6 100644 (file)
@@ -296,17 +296,37 @@ void File::fetchHunks(bool againstHead)
             AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
             const char * prefix = (added ? "+" : "-");
             if (rc == AbstractCommand::Ok) {
-                char buf[1024];
-                Hunk hunk; // its one hunk.
-                QByteArray header("@@ -1,0 +1,0 @@", 15);
-                hunk.addLine(header);
+                QList<QByteArray> lines;
+                char buf[8096];
+                int bytesToInspect = 100;
                 while(true) {
                     qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
                     if (lineLength == -1)
                         break;
-                    QByteArray array(prefix, 1);
-                    array.append(buf);
-                    hunk.addLine(array);
+                    if (bytesToInspect > 0) { // detect binary files
+                        const int max = qMin(bytesToInspect, (int) lineLength);
+                        for (int i = 0; i < max; ++i) {
+                            if (buf[i] == 0) {
+                                m_file.setBinary(true);
+                                return;
+                            }
+                        }
+                        bytesToInspect -= max;
+                    }
+                    QByteArray line;
+                    line.reserve(lineLength + 1);
+                    line.append(prefix, 1);
+                    line.append(buf, lineLength);
+                    lines.append(line);
+                }
+
+                Hunk hunk; // its one hunk.
+                QByteArray header("@@ -1,", 6);
+                header.append(QString::number(lines.count()).toLatin1());
+                header.append(" +0,0 @@\n", 9);
+                hunk.addLine(header);
+                foreach (const QByteArray &line, lines) {
+                    hunk.addLine(line);
                 }
                 m_file.addHunk(hunk);
             }