Commit 3a394f9d authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Show num stat in git status if config enabled


Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent 35468462
......@@ -32,62 +32,62 @@ GitUtils::GitParsedStatus GitUtils::parseStatus(const QByteArray &raw)
switch (xy) {
case StatusXY::QQ:
untracked.append({QString::fromUtf8(file, size), GitStatus::Untracked, 'U'});
untracked.append({QString::fromUtf8(file, size), GitStatus::Untracked, 'U', 0, 0});
break;
case StatusXY::II:
untracked.append({QString::fromUtf8(file, size), GitStatus::Ignored, 'I'});
untracked.append({QString::fromUtf8(file, size), GitStatus::Ignored, 'I', 0, 0});
break;
case StatusXY::DD:
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_BothDeleted, x});
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_BothDeleted, x, 0, 0});
break;
case StatusXY::AU:
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_AddedByUs, x});
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_AddedByUs, x, 0, 0});
break;
case StatusXY::UD:
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_DeletedByThem, x});
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_DeletedByThem, x, 0, 0});
break;
case StatusXY::UA:
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_AddedByThem, x});
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_AddedByThem, x, 0, 0});
break;
case StatusXY::DU:
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_DeletedByUs, x});
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_DeletedByUs, x, 0, 0});
break;
case StatusXY::AA:
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_BothAdded, x});
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_BothAdded, x, 0, 0});
break;
case StatusXY::UU:
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_BothModified, x});
unmerge.append({QString::fromUtf8(file, size), GitStatus::Unmerge_BothModified, x, 0, 0});
break;
}
switch (x) {
case 'M':
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Modified, x});
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Modified, x, 0, 0});
break;
case 'A':
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Added, x});
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Added, x, 0, 0});
break;
case 'D':
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Deleted, x});
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Deleted, x, 0, 0});
break;
case 'R':
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Renamed, x});
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Renamed, x, 0, 0});
break;
case 'C':
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Copied, x});
staged.append({QString::fromUtf8(file, size), GitStatus::Index_Copied, x, 0, 0});
break;
}
switch (y) {
case 'M':
changed.append({QString::fromUtf8(file, size), GitStatus::WorkingTree_Modified, y});
changed.append({QString::fromUtf8(file, size), GitStatus::WorkingTree_Modified, y, 0, 0});
break;
case 'D':
changed.append({QString::fromUtf8(file, size), GitStatus::WorkingTree_Deleted, y});
changed.append({QString::fromUtf8(file, size), GitStatus::WorkingTree_Deleted, y, 0, 0});
break;
case 'A':
changed.append({QString::fromUtf8(file, size), GitStatus::WorkingTree_IntentToAdd, y});
changed.append({QString::fromUtf8(file, size), GitStatus::WorkingTree_IntentToAdd, y, 0, 0});
break;
}
}
......@@ -126,3 +126,57 @@ QString GitUtils::statusString(GitUtils::GitStatus s)
}
return QString();
}
static bool getNum(const QByteArray &numBytes, int *num)
{
bool res = false;
*num = numBytes.toInt(&res);
return res;
}
GitUtils::GitParsedStatus GitUtils::parseDiffNumStat(GitUtils::GitParsedStatus s, const QByteArray &raw)
{
auto list = raw.split(0x00);
for (const auto &l : list) {
auto cols = l.split('\t');
if (cols.length() < 3) {
continue;
}
int add = 0;
if (!getNum(cols.at(0), &add)) {
continue;
}
int sub = 0;
if (!getNum(cols.at(1), &sub)) {
continue;
}
auto file = cols.at(2);
s.addNumStat(add, sub, QString::fromUtf8(file));
// we are only concerned with staged + modified
}
return s;
}
void GitUtils::GitParsedStatus::addNumStat(int add, int sub, const QString &file)
{
// look in modified first, then staged
auto i = std::find_if(changed.begin(), changed.end(), [&file](const StatusItem &si) {
return si.file == file;
});
if (i != changed.end()) {
i->add = add;
i->sub = sub;
return;
}
i = std::find_if(staged.begin(), staged.end(), [&file](const StatusItem &si) {
return si.file == file;
});
if (i != staged.end()) {
i->add = add;
i->sub = sub;
return;
}
}
......@@ -53,6 +53,8 @@ struct StatusItem {
QString file;
GitStatus status;
char statusChar;
int add;
int sub;
};
struct GitParsedStatus {
......@@ -60,10 +62,14 @@ struct GitParsedStatus {
QVector<StatusItem> unmerge;
QVector<StatusItem> staged;
QVector<StatusItem> changed;
void addNumStat(int add, int sub, const QString &file);
};
GitParsedStatus parseStatus(const QByteArray &raw);
GitParsedStatus parseDiffNumStat(GitParsedStatus s, const QByteArray &raw);
QString statusString(GitStatus s);
}
......
......@@ -120,7 +120,16 @@ QVariant GitStatusModel::data(const QModelIndex &index, int role) const
}
return filename;
} else {
return QString(QLatin1Char(m_nodes[rootIndex].at(row).statusChar));
if (!m_showNumStat) {
return QString(QLatin1Char(m_nodes[rootIndex].at(row).statusChar));
}
int a = m_nodes[rootIndex].at(row).add;
int s = m_nodes[rootIndex].at(row).sub;
auto add = QString::number(a);
auto sub = QString::number(s);
add.append(QStringLiteral("+ ") + sub + QStringLiteral("- "));
add.append(QString(QLatin1Char(m_nodes[rootIndex].at(row).statusChar)));
return add;
}
} else if (role == FileNameRole) {
return m_nodes[rootIndex].at(row).file;
......@@ -148,13 +157,14 @@ QVariant GitStatusModel::data(const QModelIndex &index, int role) const
return {};
}
void GitStatusModel::addItems(GitUtils::GitParsedStatus status)
void GitStatusModel::addItems(GitUtils::GitParsedStatus status, bool numStat)
{
beginResetModel();
m_nodes[Staged] = std::move(status.staged);
m_nodes[Changed] = std::move(status.changed);
m_nodes[Conflict] = std::move(status.unmerge);
m_nodes[Untrack] = std::move(status.untracked);
m_showNumStat = numStat;
endResetModel();
}
......
......@@ -25,7 +25,7 @@ public:
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override;
void addItems(GitUtils::GitParsedStatus status);
void addItems(GitUtils::GitParsedStatus status, bool numStat);
QVector<int> emptyRows();
const QVector<GitUtils::StatusItem> &untrackedFiles() const
......@@ -50,6 +50,7 @@ public:
private:
QVector<GitUtils::StatusItem> m_nodes[4];
bool m_showNumStat;
};
#endif // GITSTATUSMODEL_H
......@@ -556,11 +556,39 @@ void GitWidget::hideEmptyTreeNodes()
void GitWidget::parseStatusReady()
{
GitUtils::GitParsedStatus s = m_gitStatusWatcher.result();
m_model->addItems(std::move(s));
if (m_pluginView->plugin()->showGitStatusWithNumStat()) {
numStatForStatus(std::move(s));
return;
}
m_model->addItems(std::move(s), false);
hideEmptyTreeNodes();
}
void GitWidget::numStatForStatus(GitUtils::GitParsedStatus status)
{
disconnect(&git, &QProcess::finished, nullptr, nullptr);
const auto args = QStringList{QStringLiteral("diff"), QStringLiteral("HEAD"), QStringLiteral("--numstat"), QStringLiteral("-z")};
connect(&git, &QProcess::finished, this, [this, status{std::move(status)}](int exitCode, QProcess::ExitStatus es) mutable {
disconnect(&git, &QProcess::finished, nullptr, nullptr);
if (es != QProcess::NormalExit || exitCode != 0) {
sendMessage(i18n("Failed to get diff --numstat: %1", QString::fromUtf8(git.readAllStandardError())), true);
// just use the simple status data in model
m_model->addItems(std::move(status), false);
} else {
status = GitUtils::parseDiffNumStat(std::move(status), git.readAllStandardOutput());
m_model->addItems(status, true);
}
hideEmptyTreeNodes();
});
git.setArguments(args);
git.start();
}
bool GitWidget::eventFilter(QObject *o, QEvent *e)
{
if (e->type() == QEvent::ContextMenu) {
......
......@@ -80,6 +80,7 @@ private:
void launchExternalDiffTool(const QString &file, bool staged);
void commitChanges(const QString &msg, const QString &desc, bool signOff);
void applyDiff(const QString &fileName, bool staged, bool hunk, KTextEditor::View *v);
void numStatForStatus(GitUtils::GitParsedStatus status);
QMenu *stashMenu();
void hideEmptyTreeNodes();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment