Skip to content

Commit

Permalink
PatchContent shows changes only for selected file if size of patch te…
Browse files Browse the repository at this point in the history
…xt exceeds configured limit.

Added configuration option "Max patch size MiB".
If size of patch text exceeds that limit - PatchContent splits it into per-file change
and shows changes for each selected file separately.
Otherwise - no changes, will be shown entire patch content.
  • Loading branch information
agutikov committed Feb 21, 2022
1 parent e367f10 commit 01542eb
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ namespace QGit {
extern const QString ACT_GROUP_KEY;
extern const QString ACT_TEXT_KEY;
extern const QString ACT_FLAGS_KEY;
extern const QString MAX_PATCH_KEY;

// settings default values
extern const QString CMT_TEMPL_DEF;
Expand Down
1 change: 1 addition & 0 deletions src/namespace_def.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ const QString QGit::FLAGS_KEY = "flags";
const QString QGit::PATCH_DIR_KEY = "Patch/last_dir";
const QString QGit::FMT_P_OPT_KEY = "Patch/args";
const QString QGit::AM_P_OPT_KEY = "Patch/args_2";
const QString QGit::MAX_PATCH_KEY = "max_patch_size_show_complete";
const QString QGit::EX_KEY = "Working_dir/exclude_file_path";
const QString QGit::EX_PER_DIR_KEY = "Working_dir/exclude_per_directory_file_name";
const QString QGit::CON_GEOM_KEY = "Console/geometry";
Expand Down
39 changes: 38 additions & 1 deletion src/patchcontent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
#include <QScrollBar>
#include <QTextCharFormat>
#include <QTextStream>
#include <QSettings>
#include "common.h"
#include "domain.h"
#include "git.h"
Expand Down Expand Up @@ -117,6 +119,8 @@ void PatchContent::clear() {
matches.clear();
diffLoaded = false;
seekTarget = !target.isEmpty();
patchByFile.clear();
patchStats.clear();
}

void PatchContent::refresh() {
Expand Down Expand Up @@ -188,6 +192,14 @@ void PatchContent::centerOnFileHeader(StateInfo& st) {
target = st.fileName();
bool combined = (st.isMerge() && !st.allMergeFiles());
git->formatPatchFileHeader(&target, st.sha(), st.diffToSha(), combined, st.allMergeFiles());

auto it = patchByFile.constFind(target);
if (it != patchByFile.constEnd()) {
// If target is in the mapping
// then show per-file changesets.
setPlainText(it.value());
}

seekTarget = !target.isEmpty();
if (seekTarget)
seekTarget = !centerTarget(target);
Expand Down Expand Up @@ -215,6 +227,20 @@ void PatchContent::typeWriterFontChanged() {
setPlainText(toPlainText());
}

void PatchContent::parseDiff(const QString &data) {

static const QString delim = "diff --";
QStringList lst = data.split("\n" + delim, QString::SkipEmptyParts);

patchStats = lst.takeFirst();

for (auto& item : lst) {
QString file_diff = delim + item;
QString first_line = QTextStream(&file_diff).readLine();
patchByFile[first_line] = file_diff;
}
}

void PatchContent::processData(const QByteArray& fileChunk, int* prevLineNum) {

QString newLines;
Expand Down Expand Up @@ -286,7 +312,18 @@ void PatchContent::processData(const QByteArray& fileChunk, int* prevLineNum) {

if (prevLineNum || document()->isEmpty()) { // use the faster setPlainText()

setPlainText(newLines);
QSettings settings;
if (double(newLines.size()) / (1024*1024) > settings.value(QGit::MAX_PATCH_KEY).toDouble()) {
// If patch size exceeds the limit
// editor will show changes for individual files.
parseDiff(newLines);
setPlainText(patchStats);
} else {
// If patch size fits the limit
// editor will show complete patch.
setPlainText(newLines);
}

moveCursor(QTextCursor::Start);
} else {
int topLine = cursorForPosition(QPoint(1, 1)).blockNumber();
Expand Down
10 changes: 10 additions & 0 deletions src/patchcontent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QTextEdit>
#include <QSyntaxHighlighter>
#include "common.h"
#include <QHash>

class Domain;
class Git;
Expand Down Expand Up @@ -63,6 +64,7 @@ public slots:
void centerMatch(int id = 0);
bool centerTarget(SCRef target);
void processData(const QByteArray& data, int* prevLineNum = NULL);
void parseDiff(const QString& data);

Git* git;
DiffHighlighter* diffHighlighter;
Expand All @@ -75,6 +77,14 @@ public slots:
QString target;
bool seekTarget;

// Mapping of first string of file changeset ("diff --git a/... b/...")
// to complete file changeset;
// empty if size of patch less than maxPatchSizeShowComplete.
QHash<QString, QString> patchByFile;
// First section of patch text with changes stats;
// empty if size of patch less than maxPatchSizeShowComplete.
QString patchStats;

struct MatchSelection {
int paraFrom;
int indexFrom;
Expand Down
39 changes: 38 additions & 1 deletion src/settings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>513</width>
<height>377</height>
<height>416</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -576,6 +576,27 @@
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="textLabelMaxPatchSize">
<property name="text">
<string>Max patch size MiB</string>
</property>
<property name="textFormat">
<enum>Qt::PlainText</enum>
</property>
</widget>
</item>
<item>
<widget class="QDoubleSpinBox" name="doubleSpinBoxMaxPatchSize">
<property name="toolTip">
<string>Bigger patches will be shown by-file</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
Expand Down Expand Up @@ -1683,6 +1704,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>doubleSpinBoxMaxPatchSize</sender>
<signal>valueChanged(double)</signal>
<receiver>settingsBase</receiver>
<slot>doubleSpinBoxMaxPatchSize_valueChanged(double)</slot>
<hints>
<hint type="sourcelabel">
<x>20</x>
<y>20</y>
</hint>
<hint type="destinationlabel">
<x>20</x>
<y>20</y>
</hint>
</hints>
</connection>
<connection>
<sender>comboBoxGitConfigSource</sender>
<signal>activated(int)</signal>
Expand Down
7 changes: 7 additions & 0 deletions src/settingsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ SettingsImpl::SettingsImpl(QWidget* p, Git* g, int defTab) : QDialog(p), git(g)
SCRef exPDir(set.value(EX_PER_DIR_KEY, EX_PER_DIR_DEF).toString());
SCRef tmplt(set.value(CMT_TEMPL_KEY, CMT_TEMPL_DEF).toString());
SCRef CMArgs(set.value(CMT_ARGS_KEY).toString());
double MPOpt(set.value(MAX_PATCH_KEY).toDouble());

lineEditApplyPatchExtraOptions->setText(APOpt);
lineEditFormatPatchExtraOptions->setText(FPOpt);
Expand All @@ -77,6 +78,7 @@ SettingsImpl::SettingsImpl(QWidget* p, Git* g, int defTab) : QDialog(p), git(g)
lineEditCommitExtraOptions->setText(CMArgs);
lineEditTypeWriterFont->setText(TYPE_WRITER_FONT.toString());
lineEditTypeWriterFont->setCursorPosition(0); // font description could be long
doubleSpinBoxMaxPatchSize->setValue(MPOpt);

comboBoxDoubleClickAction->setCurrentIndex(set.value(DCLICK_ACT_KEY).toUInt());
setupCodecsCombo();
Expand Down Expand Up @@ -346,6 +348,11 @@ void SettingsImpl::lineEditExternalDiffViewer_textChanged(const QString& s) {
writeSetting(EXT_DIFF_KEY, s);
}

void SettingsImpl::doubleSpinBoxMaxPatchSize_valueChanged(double d) {

writeSetting(MAX_PATCH_KEY, d);
}

void SettingsImpl::lineEditExternalEditor_textChanged(const QString& s) {

writeSetting(EXT_EDITOR_KEY, s);
Expand Down
1 change: 1 addition & 0 deletions src/settingsimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ protected slots:
void lineEditExcludePerDir_textChanged(const QString& s);
void lineEditTemplate_textChanged(const QString& s);
void lineEditCommitExtraOptions_textChanged(const QString& s);
void doubleSpinBoxMaxPatchSize_valueChanged(double d);
void comboBoxDoubleClickAction_activated(int i);
void comboBoxCodecs_activated(int i);
void comboBoxUserSrc_activated(int i);
Expand Down

0 comments on commit 01542eb

Please sign in to comment.