Skip to content

Commit

Permalink
cache blobs, because if files with a lot of hunks is shown in the dif…
Browse files Browse the repository at this point in the history
…fview, it takes really long to load
  • Loading branch information
Murmele committed Dec 10, 2022
1 parent 3b69f6f commit 97352c5
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 35 deletions.
26 changes: 21 additions & 5 deletions src/git/Patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<git_blob *>(obj));

auto b = Blob(reinterpret_cast<git_blob *>(obj));
if (file == Diff::File::NewFile) {
mNewBlob = b;
} else {
mOldBlob = b;
}

return b;
}

Patch::LineStats Patch::lineStats() const {
Expand All @@ -161,7 +177,7 @@ Patch::LineStats Patch::lineStats() const {
return stats;
}

QList<QString> Patch::print() const {
QList<QString> Patch::print() {
if (!this->d) {
// can occur, when the object is created with the default
// constructor.
Expand Down Expand Up @@ -290,7 +306,7 @@ void Patch::setConflictResolution(int hidx, ConflictResolution resolution) {
writeConflictResolutions(repo, map);
}

void Patch::populatePreimage(QList<QList<QByteArray>> &image) const {
void Patch::populatePreimage(QList<QList<QByteArray>> &image) {
// Populate preimage.
// image holds the text and changes are made in this list
// this list is written afterwards back into the file
Expand Down Expand Up @@ -336,7 +352,7 @@ QByteArray Patch::generateResult(QList<QList<QByteArray>> &image,
}

QByteArray Patch::apply(const QBitArray &hunks,
const FilterList &filters) const {
const FilterList &filters) {
QList<QList<QByteArray>> image;
populatePreimage(image);

Expand Down
11 changes: 7 additions & 4 deletions src/git/Patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/*!
Expand All @@ -54,7 +54,7 @@ class Patch {
* \brief print
* \return
*/
QList<QString> print() const;
QList<QString> print();

/*!
* \brief count
Expand Down Expand Up @@ -106,7 +106,7 @@ class Patch {
* \brief populatePreimage
* \param image Populated preimage
*/
void populatePreimage(QList<QList<QByteArray>> &image) const;
void populatePreimage(QList<QList<QByteArray>> &image);
/*!
* Splits the content of fileContent into lines and stores the content in
* image \brief populatePreimage \param image Populated preimage \param
Expand All @@ -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;

Expand Down Expand Up @@ -169,6 +169,9 @@ class Patch {

Patch(git_patch *patch);

Blob mOldBlob;
Blob mNewBlob;

QSharedPointer<git_patch> d;
QList<ConflictHunk> mConflicts;

Expand Down
4 changes: 2 additions & 2 deletions src/ui/DiffView/EditButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "ui/RepoView.h"
#include <QPainterPath>

EditButton::EditButton(const git::Patch &patch, int index, bool binary,
EditButton::EditButton(git::Patch &patch, int index, bool binary,
bool lfs, QWidget *parent)
: Button(parent) {
setObjectName("EditButton");
Expand All @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions src/ui/DiffView/EditButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class EditButton : public Button {
Q_OBJECT

public:
EditButton(const git::Patch &patch, int index, bool binary, bool lfs,
QWidget *parent = nullptr);
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;
Expand Down
10 changes: 5 additions & 5 deletions src/ui/DiffView/FileWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Badge::Label> labels = {
Badge::Label(QChar(git::Diff::statusChar(status)))};
Expand Down Expand Up @@ -334,7 +334,7 @@ void _FileWidget::Header::updateCheckState() {
//###############################################################################

FileWidget::FileWidget(DiffView *view, const git::Diff &diff,
const git::Patch &patch, const git::Patch &staged,
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),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 =
Expand Down
18 changes: 9 additions & 9 deletions src/ui/DiffView/FileWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ class Header : public QFrame {
Q_OBJECT

public:
Header(const git::Diff &diff, const git::Patch &patch, bool binary, bool lfs,
bool submodule, QWidget *parent = nullptr);
void updatePatch(const git::Patch &patch);
Header(const git::Diff &diff, git::Patch &patch, bool binary, bool lfs,
bool submodule, QWidget *parent = nullptr);
void updatePatch(git::Patch &patch);
QCheckBox *check() const;

DisclosureButton *disclosureButton() const;
Expand Down Expand Up @@ -86,13 +86,13 @@ 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,
const QString &name, const QString &path, bool submodule);
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
* hunks \brief updateHunks
Expand All @@ -105,9 +105,9 @@ 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,
const git::Patch &staged, int index, bool lfs,
bool submodule);
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);
QModelIndex modelIndex();

Expand Down
4 changes: 2 additions & 2 deletions src/ui/DiffView/HunkWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const QString noNewLineAtEndOfFile =
HunkWidget::tr("No newline at end of file");
} // namespace

_HunkWidget::Header::Header(const git::Diff &diff, const git::Patch &patch,
_HunkWidget::Header::Header(const git::Diff &diff, git::Patch &patch,
int index, bool lfs, bool submodule,
QWidget *parent)
: QFrame(parent) {
Expand Down Expand Up @@ -179,7 +179,7 @@ void _HunkWidget::Header::mouseDoubleClickEvent(QMouseEvent *event) {
//#############################################################################

HunkWidget::HunkWidget(DiffView *view, const git::Diff &diff,
const git::Patch &patch, const git::Patch &staged,
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) {
Expand Down
10 changes: 5 additions & 5 deletions src/ui/DiffView/HunkWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace _HunkWidget {
class Header : public QFrame {
Q_OBJECT
public:
Header(const git::Diff &diff, const git::Patch &patch, int index, bool lfs,
bool submodule, QWidget *parent = nullptr);
Header(const git::Diff &diff, git::Patch &patch, int index, bool lfs,
bool submodule, QWidget *parent = nullptr);
QCheckBox *check() const;

DisclosureButton *button() const;
Expand Down Expand Up @@ -61,9 +61,9 @@ class HunkWidget : public QFrame {
Q_OBJECT

public:
HunkWidget(DiffView *view, const git::Diff &diff, const git::Patch &patch,
const git::Patch &staged, int index, bool lfs, bool submodule,
QWidget *parent = nullptr);
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;
TextEditor *editor(bool ensureLoaded = true);
void invalidate();
Expand Down

0 comments on commit 97352c5

Please sign in to comment.