summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGo MAEDA <maeda@farend.jp>2021-03-26 03:48:32 +0000
committerGo MAEDA <maeda@farend.jp>2021-03-26 03:48:32 +0000
commit919c9cfdd0a58491b248e28ff521911a1b383089 (patch)
tree413ccc3db7236d08cce8e49e28bfb25838898e56
parent9c56418f4e72a278efba6c5a4403ae4157d02421 (diff)
downloadredmine-919c9cfdd0a58491b248e28ff521911a1b383089.tar.gz
redmine-919c9cfdd0a58491b248e28ff521911a1b383089.zip
Support for Git repositories with default branch "main" (#34942).
Patch by Go MAEDA. git-svn-id: http://svn.redmine.org/redmine/trunk@20851 e93f8b46-1217-0410-a6f0-8f06a7374b81
-rw-r--r--lib/redmine/scm/adapters/git_adapter.rb16
-rw-r--r--test/unit/lib/redmine/scm/adapters/git_adapter_test.rb19
2 files changed, 28 insertions, 7 deletions
diff --git a/lib/redmine/scm/adapters/git_adapter.rb b/lib/redmine/scm/adapters/git_adapter.rb
index d3c9449e5..b85bd0296 100644
--- a/lib/redmine/scm/adapters/git_adapter.rb
+++ b/lib/redmine/scm/adapters/git_adapter.rb
@@ -25,6 +25,9 @@ module Redmine
class GitAdapter < AbstractAdapter
# Git executable name
GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
+ # Repositories created after 2020 may have a default branch of
+ # "main" instead of "master"
+ GIT_DEFAULT_BRANCH_NAMES = %w[main master].freeze
class GitBranch < Branch
attr_accessor :is_default
@@ -110,14 +113,13 @@ module Redmine
end
def default_branch
- bras = self.branches
- return unless bras
+ return if branches.blank?
- default_bras = bras.detect{|x| x.is_default == true}
- return default_bras.to_s if default_bras
-
- master_bras = bras.detect{|x| x.to_s == 'master'}
- master_bras ? 'master' : bras.first.to_s
+ (
+ branches.detect(&:is_default) ||
+ branches.detect {|b| GIT_DEFAULT_BRANCH_NAMES.include?(b.to_s)} ||
+ branches.first
+ ).to_s
end
def entry(path=nil, identifier=nil)
diff --git a/test/unit/lib/redmine/scm/adapters/git_adapter_test.rb b/test/unit/lib/redmine/scm/adapters/git_adapter_test.rb
index c8cbd26b0..b81f9a61b 100644
--- a/test/unit/lib/redmine/scm/adapters/git_adapter_test.rb
+++ b/test/unit/lib/redmine/scm/adapters/git_adapter_test.rb
@@ -124,6 +124,25 @@ class GitAdapterTest < ActiveSupport::TestCase
def test_default_branch
assert_equal 'master-20120212', @adapter.default_branch
+
+ # When no branch is marked as the default, GitAdapter treats
+ # "main" or "master" branch as the default
+ b_foo, b_bar, b_main, b_master =
+ %w[foo bar main master].map do |name|
+ Redmine::Scm::Adapters::GitAdapter::GitBranch.new(name)
+ end
+ @adapter.stubs(:branches).returns([b_foo, b_main, b_bar])
+ assert_equal 'main', @adapter.default_branch
+ @adapter.stubs(:branches).returns([b_foo, b_master, b_bar])
+ assert_equal 'master', @adapter.default_branch
+
+ # The first found branch is treated as the default branch
+ # when neither "main" nor "master" is found
+ @adapter.stubs(:branches).returns([b_foo, b_bar])
+ assert_equal 'foo', @adapter.default_branch
+
+ @adapter.stubs(:branches).returns([])
+ assert_nil @adapter.default_branch
end
def test_tags