From 5abce3ab9f1d9c97cbd95a4997ec9da08c3368c0 Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <orgad.shaneh@audiocodes.com>
Date: Sun, 6 Jan 2013 12:04:36 +0200
Subject: [PATCH] Git: Make branch lookup more robust

It might fail because a particular branch points to an invalid commit.
We don't want that to prevent display of all other branches.

Change-Id: I8fe427735351fc458c99396dc1f9d77bc948468e
Reviewed-by: Petar Perisin <petar.perisin@gmail.com>
Reviewed-by: Tobias Hunger <tobias.hunger@digia.com>
---
 src/plugins/git/branchmodel.cpp |  8 ++------
 src/plugins/git/gitclient.cpp   | 34 ++++++++++++++++-----------------
 2 files changed, 18 insertions(+), 24 deletions(-)

diff --git a/src/plugins/git/branchmodel.cpp b/src/plugins/git/branchmodel.cpp
index 801f21381d5..927bd58c8ab 100644
--- a/src/plugins/git/branchmodel.cpp
+++ b/src/plugins/git/branchmodel.cpp
@@ -307,10 +307,8 @@ bool BranchModel::refresh(const QString &workingDirectory, QString *errorMessage
     branchArgs << QLatin1String(GitClient::noColorOption)
                << QLatin1String("-v") << QLatin1String("-a");
     QString output;
-    if (!m_client->synchronousBranchCmd(workingDirectory, branchArgs, &output, errorMessage)) {
+    if (!m_client->synchronousBranchCmd(workingDirectory, branchArgs, &output, errorMessage))
         VcsBase::VcsBaseOutputWindow::instance()->appendError(*errorMessage);
-        return false;
-    }
 
     beginResetModel();
 
@@ -472,10 +470,8 @@ bool BranchModel::branchIsMerged(const QModelIndex &idx)
     QStringList args;
 
     args << QLatin1String("-a") << QLatin1String("--contains") << sha(idx);
-    if (!m_client->synchronousBranchCmd(m_workingDirectory, args, &output, &errorMessage)) {
+    if (!m_client->synchronousBranchCmd(m_workingDirectory, args, &output, &errorMessage))
         VcsBase::VcsBaseOutputWindow::instance()->appendError(errorMessage);
-        return false;
-    }
 
     QStringList lines = output.split(QLatin1Char('\n'), QString::SkipEmptyParts);
     foreach (const QString &l, lines) {
diff --git a/src/plugins/git/gitclient.cpp b/src/plugins/git/gitclient.cpp
index 988883ed2c7..0fdbcc5f98b 100644
--- a/src/plugins/git/gitclient.cpp
+++ b/src/plugins/git/gitclient.cpp
@@ -1363,11 +1363,11 @@ bool GitClient::synchronousBranchCmd(const QString &workingDirectory, QStringLis
     QByteArray outputText;
     QByteArray errorText;
     const bool rc = fullySynchronousGit(workingDirectory, branchArgs, &outputText, &errorText);
+    *output = commandOutputFromLocal8Bit(outputText);
     if (!rc) {
         *errorMessage = tr("Cannot run \"git branch\" in \"%1\": %2").arg(QDir::toNativeSeparators(workingDirectory), commandOutputFromLocal8Bit(errorText));
         return false;
     }
-    *output = commandOutputFromLocal8Bit(outputText);
     return true;
 }
 
@@ -1669,24 +1669,22 @@ QStringList GitClient::synchronousRepositoryBranches(const QString &repositoryUR
     QStringList branches;
     branches << tr("<Detached HEAD>");
     QString headSha;
-    if (resp.result == Utils::SynchronousProcessResponse::Finished) {
-        // split "82bfad2f51d34e98b18982211c82220b8db049b<tab>refs/heads/master"
-        foreach (const QString &line, resp.stdOut.split(QLatin1Char('\n'))) {
-            if (line.endsWith(QLatin1String("\tHEAD"))) {
-                QTC_CHECK(headSha.isNull());
-                headSha = line.left(line.indexOf(QLatin1Char('\t')));
-                continue;
-            }
+    // split "82bfad2f51d34e98b18982211c82220b8db049b<tab>refs/heads/master"
+    foreach (const QString &line, resp.stdOut.split(QLatin1Char('\n'))) {
+        if (line.endsWith(QLatin1String("\tHEAD"))) {
+            QTC_CHECK(headSha.isNull());
+            headSha = line.left(line.indexOf(QLatin1Char('\t')));
+            continue;
+        }
 
-            const QString pattern = QLatin1String("\trefs/heads/");
-            const int pos = line.lastIndexOf(pattern);
-            if (pos != -1) {
-                const QString branchName = line.mid(pos + pattern.count());
-                if (line.startsWith(headSha))
-                    branches[0] = branchName;
-                else
-                    branches.push_back(branchName);
-            }
+        const QString pattern = QLatin1String("\trefs/heads/");
+        const int pos = line.lastIndexOf(pattern);
+        if (pos != -1) {
+            const QString branchName = line.mid(pos + pattern.count());
+            if (line.startsWith(headSha))
+                branches[0] = branchName;
+            else
+                branches.push_back(branchName);
         }
     }
     return branches;
-- 
GitLab