Notice when the pager is killed and kill the git process so we can have a lot better...
[vng.git] / hunks / ChangeSet.cpp
blob6b8bc8b59942fb0e95e92cd362b7dedab5f27d4b
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"
22 #include "../AbstractCommand.h"
23 #include "../Vng.h"
25 #include <QProcess>
26 #include <QDebug>
28 ChangeSet::ChangeSet()
32 AbstractCommand::ReturnCodes ChangeSet::fillFromDiffFile(QFile &file)
34 file.open(QIODevice::ReadOnly);
36 foreach (File f, readGitDiff(file))
37 addFile(f);
39 if (Logger::verbosity() >= Logger::Debug) {
40 foreach(File f, m_files) {
41 Logger::debug() << "changes in file: " << f.fileName() << endl;
42 int i=0;
43 foreach(Hunk h, f.hunks()) {
44 Logger::debug() << " +-(" << i++ << ") @" << h.lineNumber() << "; " << h.patch().size() << " bytes\n";
45 for(int i = 0; i < h.subHunkCount(); i++) {
46 Logger::debug() << " " << i <<"/"<< h.subHunkCount() <<"; "<< h.subHunk(i).size() <<" bytes\n";
50 Logger::debug().flush();
52 return AbstractCommand::Ok;
55 AbstractCommand::ReturnCodes ChangeSet::fillFromCurrentChanges(const QStringList &paths)
57 QDir refs(".git/refs/heads");
58 const bool emptyRepo = refs.count() == 2; // only '.' and '..'
59 if (emptyRepo) { // all files added are new, just add all.
60 QProcess git;
61 QStringList arguments;
62 arguments << "ls-files" << "-s";
63 GitRunner runner(git, arguments);
64 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
65 if (rc)
66 return rc;
67 char buf[1024];
68 while(true) {
69 qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
70 if (lineLength == -1)
71 break;
72 File file;
73 file.setProtection(QString::fromAscii(buf, 6));
74 file.setSha1(QString::fromAscii(buf+7, 40));
75 file.setFileName(QByteArray(buf + 50, lineLength - 51));
76 m_files.append(file);
78 return AbstractCommand::Ok;
81 QProcess git;
82 QStringList arguments;
83 arguments << "diff-index" << "-M" << "HEAD";
84 if (! paths.isEmpty())
85 arguments << "--" << paths;
87 // for each line
88 // new file:
89 // :000000 100644 0000000000000000000000000000000000000000 8c3ae1d344f18b23c3bdde5d26658b70b03c65d9 A bar
90 // rename main.cpp => notmain.cpp
91 // :100644 100644 e58cfe72cb7a9559a0090886bea5b0ce00db6b47 477c729e5abbd701eb708df563e7e4f749b50435 R074 main.cpp notmain.cpp
92 // normal change
93 // :100644 100644 0bdd73e9ea0ba026f6796799946c4bfc9dd1b0b8 0000000000000000000000000000000000000000 M test
95 GitRunner runner(git, arguments);
96 AbstractCommand::ReturnCodes rc = runner.start(GitRunner::WaitForStandardOutput);
97 if (rc)
98 return rc;
99 char buf[1024];
100 while(true) {
101 qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
102 if (lineLength == -1)
103 break;
104 if (lineLength > 0 && buf[0] != ':') // not a diff line, ignore.
105 continue;
107 File file;
108 file.setOldProtection(QString::fromAscii(buf+1, 6));
109 file.setProtection(QString::fromAscii(buf+8, 6));
110 file.setOldSha1(QString::fromAscii(buf+15, 40));
111 file.setSha1(QString::fromAscii(buf+56, 40));
112 int offset = 98;
113 while (buf[offset] != '\t' && offset < lineLength)
114 offset++;
115 if (buf[97] == 'R') { // rename
116 int tab = offset + 1;
117 while (buf[tab] != '\t' && tab < lineLength)
118 tab++;
119 file.setOldFileName(QByteArray(buf + offset + 1, tab - offset - 1));
120 file.setFileName(QByteArray(buf + tab + 1, lineLength - tab - 2));
122 else {
123 QByteArray filename(buf + offset + 1, lineLength - offset - 2);
124 if (buf[97] != 'A') // Add
125 file.setOldFileName(filename);
126 if (buf[97] != 'D') // Delete
127 file.setFileName(filename);
129 m_files.append(file);
131 generateHunks();
133 return AbstractCommand::Ok;
136 void ChangeSet::generateHunks()
138 // TODO make the following run in another thread
139 foreach(File file, m_files) {
140 file.fetchHunks();
142 if (!file.isBinary() && file.count() == 0 && !file.hasChanged()) { // No change in file at all.
143 Logger::debug() << "file: `" << file.oldFileName() << "' => `" << file.fileName() << "'\n";
144 Logger::debug() << " +- Unchanged file, skipping\n";
145 m_files.removeAll(file);
146 QProcess git;
147 QStringList arguments;
148 arguments << "update-index" << "-q" << "--refresh" << file.fileName();
149 GitRunner runner(git, arguments);
150 runner.start(GitRunner::WaitUntilFinished);
151 continue;
154 int i=0;
155 if (Logger::verbosity() >= Logger::Debug) {
156 Logger::debug() << "file: `" << file.oldFileName() << "' => `" << file.fileName() << "'\n";
157 if (file.isBinary())
158 Logger::debug() << " +- is a binary file" << endl;
159 Logger::debug() << " +- " << file.oldProtection() << " => " << file.protection() << endl;
160 foreach(Hunk h, file.hunks()) {
161 Logger::debug() << " +-(" << i++ << ") @" << h.lineNumber() << "; " << h.patch().size() << " bytes\n";
162 for(int i = 0; i < h.subHunkCount(); i++) {
163 Logger::debug() << " " << i <<"/"<< h.subHunkCount() <<"; "<< h.subHunk(i).size() <<" bytes\n";
168 Logger::debug().flush();
171 // static
172 QList<File> ChangeSet::readGitDiff(QIODevice &git, File *fileToDiff)
174 QList<File> filesInDiff;
176 // parse the output and create objects for each hunk. (note how that can be made multi-threading)
177 // we have to have the filename in the hunk too to allow skipping a whole hunk
178 char buf[1024];
179 bool inPatch = false;
180 // a diff can be multiple files, or just the one fileToDiff. Lets keep one File object to point to the current file.
181 File file;
182 Hunk hunk;
183 while(true) {
184 qint64 lineLength = Vng::readLine(&git, buf, sizeof(buf));
185 if (lineLength == -1)
186 break;
187 QString line = QString::fromLocal8Bit(buf, lineLength);
189 const bool newfile = line.startsWith("--- /dev/null");
190 if (line.length() > 6 && (newfile || line.startsWith("--- a/"))) {
191 file.addHunk(hunk);
192 hunk = Hunk();
193 if (file.isValid())
194 filesInDiff << file;
195 if (fileToDiff)
196 file = File(*fileToDiff);
197 else {
198 file = File();
199 if (!newfile) {
200 QByteArray array(buf + 6, strlen(buf) - 7);
201 file.setOldFileName(array);
204 inPatch = false;
206 else if (fileToDiff == 0 && line.length() > 6 && line.startsWith("+++ b/")) {
207 QByteArray array(buf + 6, strlen(buf) - 7);
208 file.setFileName(array);
210 else if (line.length() > 5 && line.startsWith("@@ -")) {
211 file.addHunk(hunk);
212 hunk = Hunk();
213 inPatch = true;
215 else if (inPatch && line.startsWith("diff --git "))
216 inPatch = false;
217 else if (line.startsWith("Binary files a/") && line.indexOf(" differ") > 0) { // TODO does git do translations?
218 Q_ASSERT(fileToDiff);
219 fileToDiff->setBinary(true);
220 git.close();
221 return filesInDiff;
223 if (inPatch) {
224 QByteArray array(buf, lineLength);
225 hunk.addLine(array);
228 file.addHunk(hunk);
229 if (fileToDiff == 0 && file.isValid())
230 filesInDiff << file;
231 git.close();
232 return filesInDiff;
235 void ChangeSet::addFile(const File &file)
237 if (file.isValid())
238 m_files << file;
241 QList<File> ChangeSet::files() const
243 return m_files;
246 int ChangeSet::count() const
248 return m_files.count();
251 void ChangeSet::writeDiff(QFile &file, ChangeSet::Selection selection) const
253 file.open(QIODevice::WriteOnly | QIODevice::Truncate);
254 QDataStream diff(&file);
255 foreach(File file, m_files) {
256 bool fileHeaderWritten = false;
257 foreach(Hunk hunk, file.hunks()) {
258 if (selection == AllHunks
259 || selection == UserSelection
260 && (hunk.acceptance() == Vng::Accepted || hunk.acceptance() == Vng::MixedAcceptance)
261 || selection == InvertedUserSelection && hunk.acceptance() != Vng::Accepted) {
262 if (!fileHeaderWritten) {
263 diff.writeRawData("--- a/", 6);
264 diff.writeRawData(file.oldFileName().data(), file.oldFileName().size());
265 diff.writeRawData("\n+++ b/", 7);
266 diff.writeRawData(file.fileName().data(), file.fileName().size());
267 diff.writeRawData("\n", 1);
268 fileHeaderWritten = true;
270 QByteArray acceptedPatch;
271 if (selection == InvertedUserSelection)
272 acceptedPatch =hunk.rejectedPatch();
273 else if (selection == AllHunks) {
274 acceptedPatch = hunk.header();
275 acceptedPatch.append(hunk.patch());
277 else
278 acceptedPatch = hunk.acceptedPatch();
279 diff.writeRawData(acceptedPatch.data(), acceptedPatch.size());
283 file.close();
286 bool ChangeSet::hasAcceptedChanges() const
288 foreach(File file, files()) {
289 if (file.renameAcceptance() == Vng::Accepted && file.fileName() != file.oldFileName())
290 return true;
291 if (file.protectionAcceptance() == Vng::Accepted && file.protection() != file.oldProtection())
292 return true;
293 foreach(Hunk hunk, file.hunks()) {
294 Vng::Acceptance a = hunk.acceptance();
295 if (a == Vng::Accepted || a == Vng::MixedAcceptance)
296 return true;
299 return false;