From a8fd4cf015f5fa10459dbdeafaced1cd70323023 Mon Sep 17 00:00:00 2001
From: Ivan Evtuhovich <i.evtuhovich@scalaxy.ru>
Date: Fri, 11 Mar 2011 10:53:59 +0300
Subject: [PATCH] Show branches through the git branch command

---
 app/models/changeset.rb                 |   10 +++++-----
 lib/redmine/scm/adapters/git_adapter.rb |   16 ++++++----------
 2 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/app/models/changeset.rb b/app/models/changeset.rb
index e67d2f5..ddaea56 100644
--- a/app/models/changeset.rb
+++ b/app/models/changeset.rb
@@ -162,7 +162,11 @@ class Changeset < ActiveRecord::Base
                   :from_path => change[:from_path],
                   :from_revision => change[:from_revision])
   end
-  
+
+  def branches
+    repository.scm.get_branches(scmid)
+  end
+
   private
 
   # Finds issues that can be referenced by the commit message
@@ -181,10 +185,6 @@ class Changeset < ActiveRecord::Base
     return @short_comments, @long_comments
   end
 
-  def branches
-    repository.scm.get_branches(scmid)
-  end
-
   def self.to_utf8(str)
     return str if /\A[\r\n\t\x20-\x7e]*\Z/n.match(str) # for us-ascii
     encoding = Setting.commit_logs_encoding.to_s.strip
diff --git a/lib/redmine/scm/adapters/git_adapter.rb b/lib/redmine/scm/adapters/git_adapter.rb
index ff70958..a078e63 100644
--- a/lib/redmine/scm/adapters/git_adapter.rb
+++ b/lib/redmine/scm/adapters/git_adapter.rb
@@ -46,19 +46,15 @@ module Redmine
 
         def get_branches(scmid)
           result = []
-          branches.each do |branch|
-            cmd = "#{GIT_BIN} --git-dir #{target('')} log --pretty=oneline #{branch}"
-            shellout(cmd) do |io|
-              io.each_line do |line|
-                if line.include?(scmid)
-                  result << branch
-                  break
-                end
-              end
+          cmd = "#{GIT_BIN} --git-dir #{target('')} branch --no-color --contains #{scmid}"
+          shellout(cmd) do |io|
+            io.each_line do |line|
+              line = line[1..-1] if line =~ /^\*/ # current branch marked by star
+              result << line.strip
             end
           end
 
-          result.uniq
+          result.sort!
         end
 
         def tags
-- 
1.7.1

