Skip to content

Commit

Permalink
DiffEditor: Fix saving diff containing new or deleted files
Browse files Browse the repository at this point in the history
Fix also the case when new file was empty.
Fix some const correctness.

Change-Id: Ied71c3de0398914e595fbf542f1b8ec3659d69b6
Reviewed-by: Tobias Hunger <[email protected]>
  • Loading branch information
JarekKobus committed Aug 10, 2017
1 parent e3ce4b1 commit 6213369
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/plugins/diffeditor/diffeditorplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ void DiffEditor::Internal::DiffEditorPlugin::testMakePatch()
QFETCH(bool, lastChunk);
QFETCH(QString, patchText);

QString result = DiffUtils::makePatch(sourceChunk, leftFileName, rightFileName, lastChunk);
const QString result = DiffUtils::makePatch(sourceChunk, leftFileName, rightFileName, lastChunk);

QCOMPARE(result, patchText);

Expand Down
72 changes: 55 additions & 17 deletions src/plugins/diffeditor/diffutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,34 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,
return diffText;
}

static QString leftFileName(const FileData &fileData, unsigned formatFlags)
{
QString diffText;
QTextStream str(&diffText);
if (fileData.fileOperation == FileData::NewFile) {
str << "/dev/null";
} else {
if (formatFlags & DiffUtils::AddLevel)
str << "a/";
str << fileData.leftFileInfo.fileName;
}
return diffText;
}

static QString rightFileName(const FileData &fileData, unsigned formatFlags)
{
QString diffText;
QTextStream str(&diffText);
if (fileData.fileOperation == FileData::DeleteFile) {
str << "/dev/null";
} else {
if (formatFlags & DiffUtils::AddLevel)
str << "b/";
str << fileData.rightFileInfo.fileName;
}
return diffText;
}

QString DiffUtils::makePatch(const QList<FileData> &fileDataList, unsigned formatFlags)
{
QString diffText;
Expand All @@ -497,26 +525,36 @@ QString DiffUtils::makePatch(const QList<FileData> &fileDataList, unsigned forma
str << "diff --git a/" << fileData.leftFileInfo.fileName
<< " b/" << fileData.rightFileInfo.fileName << '\n';
}
if (fileData.fileOperation == FileData::NewFile
|| fileData.fileOperation == FileData::DeleteFile) { // git only?
if (fileData.fileOperation == FileData::NewFile)
str << "new";
else
str << "deleted";
str << " file mode 100644\n";
}
str << "index " << fileData.leftFileInfo.typeInfo << ".." << fileData.rightFileInfo.typeInfo;
if (fileData.fileOperation == FileData::ChangeFile)
str << " 100644";
str << "\n";

if (fileData.binaryFiles) {
str << "Binary files ";
if (formatFlags & AddLevel)
str << "a/";
str << fileData.leftFileInfo.fileName << " and ";
if (formatFlags & AddLevel)
str << "b/";
str << fileData.rightFileInfo.fileName << " differ\n";
str << leftFileName(fileData, formatFlags);
str << " and ";
str << rightFileName(fileData, formatFlags);
str << " differ\n";
} else {
str << "--- ";
if (formatFlags & AddLevel)
str << "a/";
str << fileData.leftFileInfo.fileName << "\n+++ ";
if (formatFlags & AddLevel)
str << "b/";
str << fileData.rightFileInfo.fileName << '\n';
for (int j = 0; j < fileData.chunks.count(); j++) {
str << makePatch(fileData.chunks.at(j),
(j == fileData.chunks.count() - 1)
&& fileData.lastChunkAtTheEndOfFile);
if (!fileData.chunks.isEmpty()) {
str << "--- ";
str << leftFileName(fileData, formatFlags) << "\n";
str << "+++ ";
str << rightFileName(fileData, formatFlags) << "\n";
for (int j = 0; j < fileData.chunks.count(); j++) {
str << makePatch(fileData.chunks.at(j),
(j == fileData.chunks.count() - 1)
&& fileData.lastChunkAtTheEndOfFile);
}
}
}
}
Expand Down

0 comments on commit 6213369

Please sign in to comment.