diff --git a/src/git/Patch.cpp b/src/git/Patch.cpp index 0543c5ae3..4c19bb69d 100644 --- a/src/git/Patch.cpp +++ b/src/git/Patch.cpp @@ -135,7 +135,15 @@ bool Patch::isLfsPointer() const { return false; } -Blob Patch::blob(Diff::File file) const { +Blob Patch::blob(Diff::File file) { + if (file == Diff::File::NewFile) { + if (mNewBlob.isValid()) + return mNewBlob; + } else { + if (mOldBlob.isValid()) + return mOldBlob; + } + git_repository *repo = git_patch_owner(d.data()); if (!repo) return Blob(); @@ -146,7 +154,15 @@ Blob Patch::blob(Diff::File file) const { git_object *obj = nullptr; git_object_lookup(&obj, repo, &id, GIT_OBJECT_BLOB); - return Blob(reinterpret_cast(obj)); + + auto b = Blob(reinterpret_cast(obj)); + if (file == Diff::File::NewFile) { + mNewBlob = b; + } else { + mOldBlob = b; + } + + return b; } Patch::LineStats Patch::lineStats() const { @@ -161,7 +177,7 @@ Patch::LineStats Patch::lineStats() const { return stats; } -QList Patch::print() const { +QList Patch::print() { if (!this->d) { // can occur, when the object is created with the default // constructor. @@ -290,7 +306,7 @@ void Patch::setConflictResolution(int hidx, ConflictResolution resolution) { writeConflictResolutions(repo, map); } -void Patch::populatePreimage(QList> &image) const { +void Patch::populatePreimage(QList> &image) { // Populate preimage. // image holds the text and changes are made in this list // this list is written afterwards back into the file @@ -335,8 +351,7 @@ QByteArray Patch::generateResult(QList> &image, return filtered; } -QByteArray Patch::apply(const QBitArray &hunks, - const FilterList &filters) const { +QByteArray Patch::apply(const QBitArray &hunks, const FilterList &filters) { QList> image; populatePreimage(image); diff --git a/src/git/Patch.h b/src/git/Patch.h index d02248151..419610dc6 100644 --- a/src/git/Patch.h +++ b/src/git/Patch.h @@ -45,7 +45,7 @@ class Patch { bool isBinary() const; bool isLfsPointer() const; - Blob blob(Diff::File file) const; + Blob blob(Diff::File file); LineStats lineStats() const; /*! @@ -54,7 +54,7 @@ class Patch { * \brief print * \return */ - QList print() const; + QList print(); /*! * \brief count @@ -106,7 +106,7 @@ class Patch { * \brief populatePreimage * \param image Populated preimage */ - void populatePreimage(QList> &image) const; + void populatePreimage(QList> &image); /*! * Splits the content of fileContent into lines and stores the content in * image \brief populatePreimage \param image Populated preimage \param @@ -124,7 +124,7 @@ class Patch { * \return edited file */ QByteArray apply(const QBitArray &hunks, - const FilterList &filters = FilterList()) const; + const FilterList &filters = FilterList()); QByteArray apply(int hidx, QByteArray &hunkData, QByteArray fileContent, const FilterList &filters = FilterList()) const; @@ -169,6 +169,9 @@ class Patch { Patch(git_patch *patch); + Blob mOldBlob; + Blob mNewBlob; + QSharedPointer d; QList mConflicts; diff --git a/src/ui/DiffView/EditButton.cpp b/src/ui/DiffView/EditButton.cpp index 91e3b0bcc..6f89f513a 100644 --- a/src/ui/DiffView/EditButton.cpp +++ b/src/ui/DiffView/EditButton.cpp @@ -3,8 +3,8 @@ #include "ui/RepoView.h" #include -EditButton::EditButton(const git::Patch &patch, int index, bool binary, - bool lfs, QWidget *parent) +EditButton::EditButton(git::Patch &patch, int index, bool binary, bool lfs, + QWidget *parent) : Button(parent) { setObjectName("EditButton"); @@ -24,7 +24,7 @@ EditButton::EditButton(const git::Patch &patch, int index, bool binary, setVisible(!binary && !lfs); } -void EditButton::updatePatch(const git::Patch &patch, int index, bool init) { +void EditButton::updatePatch(git::Patch &patch, int index, bool init) { if ((!isEnabled() || !isVisible()) && !init) return; diff --git a/src/ui/DiffView/EditButton.h b/src/ui/DiffView/EditButton.h index 98599c2e0..a73b8123f 100644 --- a/src/ui/DiffView/EditButton.h +++ b/src/ui/DiffView/EditButton.h @@ -9,10 +9,10 @@ class EditButton : public Button { Q_OBJECT public: - EditButton(const git::Patch &patch, int index, bool binary, bool lfs, + EditButton(git::Patch &patch, int index, bool binary, bool lfs, QWidget *parent = nullptr); - void updatePatch(const git::Patch &patch, int index, bool init = false); + void updatePatch(git::Patch &patch, int index, bool init = false); protected: void paintEvent(QPaintEvent *event) override; diff --git a/src/ui/DiffView/FileWidget.cpp b/src/ui/DiffView/FileWidget.cpp index 505494de2..033b5eb7b 100644 --- a/src/ui/DiffView/FileWidget.cpp +++ b/src/ui/DiffView/FileWidget.cpp @@ -30,7 +30,7 @@ namespace { bool disclosure = false; } -_FileWidget::Header::Header(const git::Diff &diff, const git::Patch &patch, +_FileWidget::Header::Header(const git::Diff &diff, git::Patch &patch, bool binary, bool lfs, bool submodule, QWidget *parent) : QFrame(parent), mDiff(diff), mPatch(patch), mSubmodule(submodule) { @@ -184,7 +184,7 @@ _FileWidget::Header::Header(const git::Diff &diff, const git::Patch &patch, updateCheckState(); } -void _FileWidget::Header::updatePatch(const git::Patch &patch) { +void _FileWidget::Header::updatePatch(git::Patch &patch) { auto status = patch.status(); QList labels = { Badge::Label(QChar(git::Diff::statusChar(status)))}; @@ -333,10 +333,10 @@ void _FileWidget::Header::updateCheckState() { //############### FileWidget ########################################### //############################################################################### -FileWidget::FileWidget(DiffView *view, const git::Diff &diff, - const git::Patch &patch, const git::Patch &staged, - const QModelIndex modelIndex, const QString &name, - const QString &path, bool submodule, QWidget *parent) +FileWidget::FileWidget(DiffView *view, const git::Diff &diff, git::Patch &patch, + const git::Patch &staged, const QModelIndex modelIndex, + const QString &name, const QString &path, bool submodule, + QWidget *parent) : QWidget(parent), mView(view), mDiff(diff), mPatch(patch), mModelIndex(modelIndex) { auto stageState = static_cast( @@ -520,7 +520,7 @@ git::Patch::ConflictResolution _FileWidget::Header::resolution() const { return mResolution; } -void FileWidget::updatePatch(const git::Patch &patch, const git::Patch &staged, +void FileWidget::updatePatch(git::Patch &patch, const git::Patch &staged, const QString &name, const QString &path, bool submodule) { mHeader->updatePatch(patch); @@ -578,7 +578,7 @@ QWidget *FileWidget::addImage(DisclosureButton *button, const git::Patch patch, return images; } -HunkWidget *FileWidget::addHunk(const git::Diff &diff, const git::Patch &patch, +HunkWidget *FileWidget::addHunk(const git::Diff &diff, git::Patch &patch, const git::Patch &staged, int index, bool lfs, bool submodule) { HunkWidget *hunk = diff --git a/src/ui/DiffView/FileWidget.h b/src/ui/DiffView/FileWidget.h index a53e4c35c..80a545521 100644 --- a/src/ui/DiffView/FileWidget.h +++ b/src/ui/DiffView/FileWidget.h @@ -28,9 +28,9 @@ class Header : public QFrame { Q_OBJECT public: - Header(const git::Diff &diff, const git::Patch &patch, bool binary, bool lfs, + Header(const git::Diff &diff, git::Patch &patch, bool binary, bool lfs, bool submodule, QWidget *parent = nullptr); - void updatePatch(const git::Patch &patch); + void updatePatch(git::Patch &patch); QCheckBox *check() const; DisclosureButton *disclosureButton() const; @@ -86,12 +86,12 @@ class FileWidget : public QWidget { Q_OBJECT public: - FileWidget(DiffView *view, const git::Diff &diff, const git::Patch &patch, + FileWidget(DiffView *view, const git::Diff &diff, git::Patch &patch, const git::Patch &staged, const QModelIndex modelIndex, const QString &name, const QString &path, bool submodule, QWidget *parent = nullptr); bool isEmpty(); - void updatePatch(const git::Patch &patch, const git::Patch &staged, + void updatePatch(git::Patch &patch, const git::Patch &staged, const QString &name, const QString &path, bool submodule); /*! * Update hunks after index change and emits the current stage state of the @@ -105,7 +105,7 @@ class FileWidget : public QWidget { QWidget *addImage(DisclosureButton *button, const git::Patch patch, bool lfs = false); - HunkWidget *addHunk(const git::Diff &diff, const git::Patch &patch, + HunkWidget *addHunk(const git::Diff &diff, git::Patch &patch, const git::Patch &staged, int index, bool lfs, bool submodule); void setStageState(git::Index::StagedState state); diff --git a/src/ui/DiffView/HunkWidget.cpp b/src/ui/DiffView/HunkWidget.cpp index 6ff3569ab..24afe48ea 100644 --- a/src/ui/DiffView/HunkWidget.cpp +++ b/src/ui/DiffView/HunkWidget.cpp @@ -34,9 +34,8 @@ const QString noNewLineAtEndOfFile = HunkWidget::tr("No newline at end of file"); } // namespace -_HunkWidget::Header::Header(const git::Diff &diff, const git::Patch &patch, - int index, bool lfs, bool submodule, - QWidget *parent) +_HunkWidget::Header::Header(const git::Diff &diff, git::Patch &patch, int index, + bool lfs, bool submodule, QWidget *parent) : QFrame(parent) { setObjectName("HunkHeader"); mCheck = new QCheckBox(this); @@ -178,9 +177,9 @@ void _HunkWidget::Header::mouseDoubleClickEvent(QMouseEvent *event) { //########## HunkWidget ############################################### //############################################################################# -HunkWidget::HunkWidget(DiffView *view, const git::Diff &diff, - const git::Patch &patch, const git::Patch &staged, - int index, bool lfs, bool submodule, QWidget *parent) +HunkWidget::HunkWidget(DiffView *view, const git::Diff &diff, git::Patch &patch, + const git::Patch &staged, int index, bool lfs, + bool submodule, QWidget *parent) : QFrame(parent), mView(view), mPatch(patch), mStaged(staged), mIndex(index), mLfs(lfs) { setObjectName("HunkWidget"); diff --git a/src/ui/DiffView/HunkWidget.h b/src/ui/DiffView/HunkWidget.h index 8e67c3618..ef12e9017 100644 --- a/src/ui/DiffView/HunkWidget.h +++ b/src/ui/DiffView/HunkWidget.h @@ -22,7 +22,7 @@ namespace _HunkWidget { class Header : public QFrame { Q_OBJECT public: - Header(const git::Diff &diff, const git::Patch &patch, int index, bool lfs, + Header(const git::Diff &diff, git::Patch &patch, int index, bool lfs, bool submodule, QWidget *parent = nullptr); QCheckBox *check() const; @@ -61,7 +61,7 @@ class HunkWidget : public QFrame { Q_OBJECT public: - HunkWidget(DiffView *view, const git::Diff &diff, const git::Patch &patch, + HunkWidget(DiffView *view, const git::Diff &diff, git::Patch &patch, const git::Patch &staged, int index, bool lfs, bool submodule, QWidget *parent = nullptr); _HunkWidget::Header *header() const;