IVersionControl: Implement topic cache in the base class

Derivatives need to derive TopicCache, implement its pure virtual
functions and pass it in IVersionControl's constructor.

Change-Id: I3a904c84541fda95eee75296f86441c4bae55d79
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
This commit is contained in:
Orgad Shaneh
2014-02-18 23:14:50 +02:00
committed by Orgad Shaneh
parent c68ebeed2e
commit cd48c5e513
4 changed files with 112 additions and 28 deletions

View File

@@ -2024,28 +2024,13 @@ bool GitClient::synchronousHeadRefs(const QString &workingDirectory, QStringList
return true;
}
struct TopicData
{
QDateTime timeStamp;
QString topic;
};
// Retrieve topic (branch, tag or HEAD hash)
QString GitClient::synchronousTopic(const QString &workingDirectory)
{
static QHash<QString, TopicData> topicCache;
QString gitDir = findGitDirForRepository(workingDirectory);
if (gitDir.isEmpty())
return QString();
TopicData &data = topicCache[gitDir];
QDateTime lastModified = QFileInfo(gitDir + QLatin1String("/HEAD")).lastModified();
if (lastModified == data.timeStamp)
return data.topic;
data.timeStamp = lastModified;
// First try to find branch
QString branch = synchronousCurrentLocalBranch(workingDirectory);
if (!branch.isEmpty())
return data.topic = branch;
return branch;
// Detached HEAD, try a tag or remote branch
QStringList references;
@@ -2059,10 +2044,8 @@ QString GitClient::synchronousTopic(const QString &workingDirectory)
foreach (const QString &ref, references) {
int derefInd = ref.indexOf(dereference);
if (ref.startsWith(tagStart)) {
return data.topic = ref.mid(tagStart.size(),
(derefInd == -1) ? -1 : derefInd - tagStart.size());
}
if (ref.startsWith(tagStart))
return ref.mid(tagStart.size(), (derefInd == -1) ? -1 : derefInd - tagStart.size());
if (ref.startsWith(remoteStart)) {
remoteBranch = ref.mid(remoteStart.size(),
(derefInd == -1) ? -1 : derefInd - remoteStart.size());
@@ -2070,7 +2053,7 @@ QString GitClient::synchronousTopic(const QString &workingDirectory)
}
// No tag
return data.topic = remoteBranch.isEmpty() ? tr("Detached HEAD") : remoteBranch;
return remoteBranch.isEmpty() ? tr("Detached HEAD") : remoteBranch;
}
bool GitClient::synchronousRevParseCmd(const QString &workingDirectory, const QString &ref,