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"
28 ChangeSet::ChangeSet()
32 AbstractCommand::ReturnCodes
ChangeSet::fillFromDiffFile(QFile
&file
)
34 file
.open(QIODevice::ReadOnly
);
36 foreach (File f
, readGitDiff(file
))
39 if (Logger::verbosity() >= Logger::Debug
) {
40 foreach(File f
, m_files
) {
41 Logger::debug() << "changes in file: " << f
.fileName() << endl
;
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.
61 QStringList arguments
;
62 arguments
<< "ls-files" << "-s";
63 GitRunner
runner(git
, arguments
);
64 AbstractCommand::ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
69 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
73 file
.setProtection(QString::fromAscii(buf
, 6));
74 file
.setSha1(QString::fromAscii(buf
+7, 40));
75 file
.setFileName(QByteArray(buf
+ 50, lineLength
- 51));
78 return AbstractCommand::Ok
;
82 QStringList arguments
;
83 arguments
<< "diff-index" << "-M" << "HEAD";
84 if (! paths
.isEmpty())
85 arguments
<< "--" << paths
;
89 // :000000 100644 0000000000000000000000000000000000000000 8c3ae1d344f18b23c3bdde5d26658b70b03c65d9 A bar
90 // rename main.cpp => notmain.cpp
91 // :100644 100644 e58cfe72cb7a9559a0090886bea5b0ce00db6b47 477c729e5abbd701eb708df563e7e4f749b50435 R074 main.cpp notmain.cpp
93 // :100644 100644 0bdd73e9ea0ba026f6796799946c4bfc9dd1b0b8 0000000000000000000000000000000000000000 M test
95 GitRunner
runner(git
, arguments
);
96 AbstractCommand::ReturnCodes rc
= runner
.start(GitRunner::WaitForStandardOutput
);
101 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
102 if (lineLength
== -1)
104 if (lineLength
> 0 && buf
[0] != ':') // not a diff line, ignore.
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));
113 while (buf
[offset
] != '\t' && offset
< lineLength
)
115 if (buf
[97] == 'R') { // rename
116 int tab
= offset
+ 1;
117 while (buf
[tab
] != '\t' && tab
< lineLength
)
119 file
.setOldFileName(QByteArray(buf
+ offset
+ 1, tab
- offset
- 1));
120 file
.setFileName(QByteArray(buf
+ tab
+ 1, lineLength
- tab
- 2));
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
);
133 return AbstractCommand::Ok
;
136 void ChangeSet::generateHunks()
138 // TODO make the following run in another thread
139 foreach(File file
, m_files
) {
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
);
147 QStringList arguments
;
148 arguments
<< "update-index" << "-q" << "--refresh" << file
.fileName();
149 GitRunner
runner(git
, arguments
);
150 runner
.start(GitRunner::WaitUntilFinished
);
155 if (Logger::verbosity() >= Logger::Debug
) {
156 Logger::debug() << "file: `" << file
.oldFileName() << "' => `" << file
.fileName() << "'\n";
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();
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
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.
184 qint64 lineLength
= Vng::readLine(&git
, buf
, sizeof(buf
));
185 if (lineLength
== -1)
187 QString line
= QString::fromLocal8Bit(buf
, lineLength
);
189 const bool newfile
= line
.startsWith("--- /dev/null");
190 if (line
.length() > 6 && (newfile
|| line
.startsWith("--- a/"))) {
196 file
= File(*fileToDiff
);
200 QByteArray
array(buf
+ 6, strlen(buf
) - 7);
201 file
.setOldFileName(array
);
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("@@ -")) {
215 else if (inPatch
&& line
.startsWith("diff --git "))
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);
224 QByteArray
array(buf
, lineLength
);
229 if (fileToDiff
== 0 && file
.isValid())
235 void ChangeSet::addFile(const File
&file
)
241 QList
<File
> ChangeSet::files() const
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());
278 acceptedPatch
= hunk
.acceptedPatch();
279 diff
.writeRawData(acceptedPatch
.data(), acceptedPatch
.size());
286 bool ChangeSet::hasAcceptedChanges() const
288 foreach(File file
, files()) {
289 if (file
.renameAcceptance() == Vng::Accepted
&& file
.fileName() != file
.oldFileName())
291 if (file
.protectionAcceptance() == Vng::Accepted
&& file
.protection() != file
.oldProtection())
293 foreach(Hunk hunk
, file
.hunks()) {
294 Vng::Acceptance a
= hunk
.acceptance();
295 if (a
== Vng::Accepted
|| a
== Vng::MixedAcceptance
)