From bf4ca06c1e0342ea8115a77a4030c161785454c7 Mon Sep 17 00:00:00 2001
From: Tobias Hunger <tobias.hunger@nokia.com>
Date: Mon, 15 Aug 2011 11:37:04 +0000
Subject: [PATCH] Fix possible crash in git branch dialog
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Do not crash when checking out a branch while not currently
on any branch.

Task-number: QTCREATORBUG-5784
Change-Id: I5d007e91c1778e6f09a5d543990d948bc1427599
Reviewed-on: http://codereview.qt.nokia.com/2958
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Robert Löhning <robert.loehning@nokia.com>
---
 src/plugins/git/branchmodel.cpp | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/plugins/git/branchmodel.cpp b/src/plugins/git/branchmodel.cpp
index 5d1712aeee6..3b562c6816e 100644
--- a/src/plugins/git/branchmodel.cpp
+++ b/src/plugins/git/branchmodel.cpp
@@ -448,8 +448,11 @@ void BranchModel::checkoutBranch(const QModelIndex &idx)
     }
     if (m_client->synchronousCheckoutBranch(m_workingDirectory, branch, &errorMessage)) {
         if (errorMessage.isEmpty()) {
-            static_cast<BranchNode *>(currentBranch().internalPointer())->current = false;
-            emit dataChanged(currentBranch(), currentBranch());
+            QModelIndex currentIdx = currentBranch();
+            if (currentIdx.isValid()) {
+                static_cast<BranchNode *>(currentIdx.internalPointer())->current = false;
+                emit dataChanged(currentBranch(), currentBranch());
+            }
             static_cast<BranchNode *>(idx.internalPointer())->current = true;
             emit dataChanged(idx, idx);
         } else {
-- 
GitLab