Project

General

Profile

Feature #34942 ยป support-git-main-branch.patch

Go MAEDA, 2021-03-20 06:30

View differences:

lib/redmine/scm/adapters/git_adapter.rb
25 25
      class GitAdapter < AbstractAdapter
26 26
        # Git executable name
27 27
        GIT_BIN = Redmine::Configuration['scm_git_command'] || "git"
28
        # Repositories created after 2020 may have a default branch of
29
        # "main" instead of "master"
30
        GIT_DEFAULT_BRANCH_NAMES = %w[main master].freeze
28 31

  
29 32
        class GitBranch < Branch
30 33
          attr_accessor :is_default
......
110 113
        end
111 114

  
112 115
        def default_branch
113
          bras = self.branches
114
          return unless bras
116
          return if branches.blank?
115 117

  
116
          default_bras = bras.detect{|x| x.is_default == true}
117
          return default_bras.to_s if default_bras
118

  
119
          master_bras = bras.detect{|x| x.to_s == 'master'}
120
          master_bras ? 'master' : bras.first.to_s
118
          (
119
            branches.detect(&:is_default) ||
120
            branches.detect {|b| GIT_DEFAULT_BRANCH_NAMES.include?(b.to_s)} ||
121
            branches.first
122
          ).to_s
121 123
        end
122 124

  
123 125
        def entry(path=nil, identifier=nil)
test/unit/lib/redmine/scm/adapters/git_adapter_test.rb
124 124

  
125 125
    def test_default_branch
126 126
      assert_equal 'master-20120212', @adapter.default_branch
127

  
128
      # When no branch is marked as the default, GitAdapter treats
129
      # "main" or "master" branch as the default
130
      b_foo, b_bar, b_main, b_master =
131
        %w[foo bar main master].map do |name|
132
          Redmine::Scm::Adapters::GitAdapter::GitBranch.new(name)
133
        end
134
      @adapter.stubs(:branches).returns([b_foo, b_main, b_bar])
135
      assert_equal 'main', @adapter.default_branch
136
      @adapter.stubs(:branches).returns([b_foo, b_master, b_bar])
137
      assert_equal 'master', @adapter.default_branch
138

  
139
      # The first found branch is treated as the default branch
140
      # when neither "main" nor "master" is found
141
      @adapter.stubs(:branches).returns([b_foo, b_bar])
142
      assert_equal 'foo', @adapter.default_branch
143

  
144
      @adapter.stubs(:branches).returns([])
145
      assert_nil @adapter.default_branch
127 146
    end
128 147

  
129 148
    def test_tags
    (1-1/1)