From 39755c09b9cf03e64723ffcbef100c0d52fffb7c Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <orgad.shaneh@audiocodes.com>
Date: Fri, 19 Jul 2013 15:28:26 +0300
Subject: [PATCH] Git: Only show Tags root node when tags are enabled and exist

Create it on demand. Remove on clear

Change-Id: Ic29e82a859f99b5d739c25be83aa6c40a1ee2cc8
Reviewed-by: Nikita Baryshnikov <nib952051@gmail.com>
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
---
 src/plugins/git/branchmodel.cpp | 22 +++++++++++++++-------
 src/plugins/git/branchmodel.h   |  1 +
 2 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/src/plugins/git/branchmodel.cpp b/src/plugins/git/branchmodel.cpp
index ea7d64795c2..ee961b63710 100644
--- a/src/plugins/git/branchmodel.cpp
+++ b/src/plugins/git/branchmodel.cpp
@@ -202,8 +202,6 @@ BranchModel::BranchModel(GitClient *client, QObject *parent) :
     // Abuse the sha field for ref prefix
     m_rootNode->append(new BranchNode(tr("Local Branches"), QLatin1String("refs/heads")));
     m_rootNode->append(new BranchNode(tr("Remote Branches"), QLatin1String("refs/remotes")));
-    if (m_client->settings()->boolValue(GitSettings::showTagsKey))
-        m_rootNode->append(new BranchNode(tr("Tags"), QLatin1String("refs/tags")));
 }
 
 BranchModel::~BranchModel()
@@ -335,6 +333,8 @@ void BranchModel::clear()
     foreach (BranchNode *root, m_rootNode->children)
         while (root->count())
             delete root->children.takeLast();
+    if (hasTags())
+        m_rootNode->children.takeLast();
 
     m_currentBranch = 0;
 }
@@ -457,6 +457,11 @@ QString BranchModel::sha(const QModelIndex &idx) const
     return node->sha;
 }
 
+bool BranchModel::hasTags() const
+{
+    return m_rootNode->children.count() > Tags;
+}
+
 bool BranchModel::isLocal(const QModelIndex &idx) const
 {
     if (!idx.isValid())
@@ -475,7 +480,7 @@ bool BranchModel::isLeaf(const QModelIndex &idx) const
 
 bool BranchModel::isTag(const QModelIndex &idx) const
 {
-    if (!idx.isValid())
+    if (!idx.isValid() || !hasTags())
         return false;
     return indexToNode(idx)->isTag();
 }
@@ -640,14 +645,17 @@ void BranchModel::parseOutputLine(const QString &line)
     nameParts.removeFirst(); // remove refs...
 
     BranchNode *root = 0;
-    if (nameParts.first() == QLatin1String("heads"))
+    if (nameParts.first() == QLatin1String("heads")) {
         root = m_rootNode->children.at(LocalBranches);
-    else if (nameParts.first() == QLatin1String("remotes"))
+    } else if (nameParts.first() == QLatin1String("remotes")) {
         root = m_rootNode->children.at(RemoteBranches);
-    else if (showTags && nameParts.first() == QLatin1String("tags"))
+    } else if (showTags && nameParts.first() == QLatin1String("tags")) {
+        if (!hasTags()) // Tags is missing, add it
+            m_rootNode->append(new BranchNode(tr("Tags"), QLatin1String("refs/tags")));
         root = m_rootNode->children.at(Tags);
-    else
+    } else {
         return;
+    }
 
     nameParts.removeFirst();
 
diff --git a/src/plugins/git/branchmodel.h b/src/plugins/git/branchmodel.h
index 896d8ffabc6..f110944105f 100644
--- a/src/plugins/git/branchmodel.h
+++ b/src/plugins/git/branchmodel.h
@@ -73,6 +73,7 @@ public:
     QString fullName(const QModelIndex &idx, bool includePrefix = false) const;
     QStringList localBranchNames() const;
     QString sha(const QModelIndex &idx) const;
+    bool hasTags() const;
     bool isLocal(const QModelIndex &idx) const;
     bool isLeaf(const QModelIndex &idx) const;
     bool isTag(const QModelIndex &idx) const;
-- 
GitLab