Git: Show at least 5 entries in Branches even if they're old

Fixes: QTCREATORBUG-22372
Change-Id: Ia4e0477a4e810c3c922cebbad1e44d295abca524
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Orgad Shaneh
2019-05-05 23:49:08 +03:00
committed by Orgad Shaneh
parent ea81b676c7
commit 1888e52d7c
2 changed files with 63 additions and 12 deletions

View File

@@ -36,6 +36,8 @@
#include <QDateTime> #include <QDateTime>
#include <QFont> #include <QFont>
#include <set>
using namespace VcsBase; using namespace VcsBase;
namespace Git { namespace Git {
@@ -214,7 +216,8 @@ public:
} }
bool hasTags() const { return rootNode->children.count() > Tags; } bool hasTags() const { return rootNode->children.count() > Tags; }
void parseOutputLine(const QString &line); void parseOutputLine(const QString &line, bool force = false);
void flushOldEntries();
GitClient *client; GitClient *client;
QString workingDirectory; QString workingDirectory;
@@ -226,6 +229,17 @@ public:
QStringList obsoleteLocalBranches; QStringList obsoleteLocalBranches;
Utils::FileSystemWatcher fsWatcher; Utils::FileSystemWatcher fsWatcher;
bool oldBranchesIncluded = false; bool oldBranchesIncluded = false;
struct OldEntry
{
QString line;
QDateTime dateTime;
bool operator<(const OldEntry &other) const { return dateTime < other.dateTime; }
};
BranchNode *currentRoot = nullptr;
QString currentRemote;
std::set<OldEntry> oldEntries;
}; };
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@@ -410,6 +424,7 @@ bool BranchModel::refresh(const QString &workingDirectory, QString *errorMessage
const QStringList lines = output.split('\n'); const QStringList lines = output.split('\n');
for (const QString &l : lines) for (const QString &l : lines)
d->parseOutputLine(l); d->parseOutputLine(l);
d->flushOldEntries();
if (d->currentBranch) { if (d->currentBranch) {
if (d->currentBranch->isLocal()) if (d->currentBranch->isLocal())
@@ -723,7 +738,7 @@ Utils::optional<QString> BranchModel::remoteName(const QModelIndex &idx) const
return Utils::nullopt; return Utils::nullopt;
} }
void BranchModel::Private::parseOutputLine(const QString &line) void BranchModel::Private::parseOutputLine(const QString &line, bool force)
{ {
if (line.size() < 3) if (line.size() < 3)
return; return;
@@ -744,14 +759,10 @@ void BranchModel::Private::parseOutputLine(const QString &line)
dateTime = QDateTime::fromSecsSinceEpoch(timeT); dateTime = QDateTime::fromSecsSinceEpoch(timeT);
} }
if (!oldBranchesIncluded && !current && dateTime.isValid()) { bool isOld = false;
if (!oldBranchesIncluded && !force && !current && dateTime.isValid()) {
const qint64 age = dateTime.daysTo(QDateTime::currentDateTime()); const qint64 age = dateTime.daysTo(QDateTime::currentDateTime());
if (age > Constants::OBSOLETE_COMMIT_AGE_IN_DAYS) { isOld = age > Constants::OBSOLETE_COMMIT_AGE_IN_DAYS;
const QString heads = "refs/heads/";
if (fullName.startsWith(heads))
obsoleteLocalBranches.append(fullName.mid(heads.size()));
return;
}
} }
bool showTags = client->settings().boolValue(GitSettings::showTagsKey); bool showTags = client->settings().boolValue(GitSettings::showTagsKey);
@@ -760,18 +771,45 @@ void BranchModel::Private::parseOutputLine(const QString &line)
nameParts.removeFirst(); // remove refs... nameParts.removeFirst(); // remove refs...
BranchNode *root = nullptr; BranchNode *root = nullptr;
BranchNode *oldEntriesRoot = nullptr;
RootNodes rootType;
if (nameParts.first() == "heads") { if (nameParts.first() == "heads") {
root = rootNode->children.at(LocalBranches); rootType = LocalBranches;
if (isOld)
obsoleteLocalBranches.append(fullName.mid(sizeof("refs/heads/")-1));
} else if (nameParts.first() == "remotes") { } else if (nameParts.first() == "remotes") {
root = rootNode->children.at(RemoteBranches); rootType = RemoteBranches;
const QString remoteName = nameParts.at(1);
root = rootNode->children.at(rootType);
oldEntriesRoot = root->childOfName(remoteName);
if (!oldEntriesRoot)
oldEntriesRoot = root->append(new BranchNode(remoteName));
} else if (showTags && nameParts.first() == "tags") { } else if (showTags && nameParts.first() == "tags") {
if (!hasTags()) // Tags is missing, add it if (!hasTags()) // Tags is missing, add it
rootNode->append(new BranchNode(tr("Tags"), "refs/tags")); rootNode->append(new BranchNode(tr("Tags"), "refs/tags"));
root = rootNode->children.at(Tags); rootType = Tags;
} else { } else {
return; return;
} }
root = rootNode->children.at(rootType);
if (!oldEntriesRoot)
oldEntriesRoot = root;
if (isOld) {
if (oldEntriesRoot->children.size() > Constants::MAX_OBSOLETE_COMMITS_TO_DISPLAY)
return;
if (currentRoot != oldEntriesRoot) {
flushOldEntries();
currentRoot = oldEntriesRoot;
}
const bool eraseOldestEntry = oldEntries.size() >= Constants::MAX_OBSOLETE_COMMITS_TO_DISPLAY;
if (!eraseOldestEntry || dateTime > oldEntries.begin()->dateTime) {
if (eraseOldestEntry)
oldEntries.erase(oldEntries.begin());
oldEntries.insert(Private::OldEntry{line, dateTime});
}
return;
}
nameParts.removeFirst(); nameParts.removeFirst();
// limit depth of list. Git basically only ever wants one / and considers the rest as part of // limit depth of list. Git basically only ever wants one / and considers the rest as part of
@@ -790,6 +828,18 @@ void BranchModel::Private::parseOutputLine(const QString &line)
currentBranch = newNode; currentBranch = newNode;
} }
void BranchModel::Private::flushOldEntries()
{
if (!currentRoot)
return;
for (int size = currentRoot->children.size(); size > 0 && !oldEntries.empty(); --size)
oldEntries.erase(oldEntries.begin());
for (const Private::OldEntry &entry : oldEntries)
parseOutputLine(entry.line, true);
oldEntries.clear();
currentRoot = nullptr;
}
BranchNode *BranchModel::indexToNode(const QModelIndex &index) const BranchNode *BranchModel::indexToNode(const QModelIndex &index) const
{ {
if (index.column() > 1) if (index.column() > 1)

View File

@@ -51,6 +51,7 @@ const char SUBMIT_MIMETYPE[] = "text/vnd.qtcreator.git.submit";
const char C_GITEDITORID[] = "Git Editor"; const char C_GITEDITORID[] = "Git Editor";
const int OBSOLETE_COMMIT_AGE_IN_DAYS = 90; const int OBSOLETE_COMMIT_AGE_IN_DAYS = 90;
const int MAX_OBSOLETE_COMMITS_TO_DISPLAY = 5;
const char EXPAND_BRANCHES[] = "Branches: <Expand>"; const char EXPAND_BRANCHES[] = "Branches: <Expand>";