Commit 42047134 authored by Igor Drozdov's avatar Igor Drozdov

Create main branch by default for snippets

Extract the main over master logic into a separate module
This module can be removed when the migration is over
parent b46b74b5
......@@ -2535,7 +2535,7 @@ class Project < ApplicationRecord
def default_branch_or_main
return default_branch if default_branch
Feature.enabled?(:main_branch_over_master, self, default_enabled: :yaml) ? 'main' : 'master'
Gitlab::DefaultBranch.value(project: self)
end
def ci_config_path_or_default
......
......@@ -20,7 +20,6 @@ class Snippet < ApplicationRecord
extend ::Gitlab::Utils::Override
MAX_FILE_COUNT = 10
MASTER_BRANCH = 'master'
cache_markdown_field :title, pipeline: :single_line
cache_markdown_field :description
......@@ -316,19 +315,19 @@ class Snippet < ApplicationRecord
override :default_branch
def default_branch
super || MASTER_BRANCH
super || Gitlab::DefaultBranch.value(project: project)
end
def repository_storage
snippet_repository&.shard_name || Repository.pick_storage_shard
end
# Repositories are created by default with the `master` branch.
# Repositories are created with a default branch. This branch
# can be different from the default branch set in the platform.
# This method changes the `HEAD` file to point to the existing
# default branch in case it's not master.
# default branch in case it's different.
def change_head_to_default_branch
return unless repository.exists?
return if default_branch == MASTER_BRANCH
# All snippets must have at least 1 file. Therefore, if
# `HEAD` is empty is because it's pointing to the wrong
# default branch
......
# frozen_string_literal: true
# Class is used while we're migrating from master to main
module Gitlab
module DefaultBranch
def self.value(project: nil)
Feature.enabled?(:main_branch_over_master, project, default_enabled: :yaml) ? 'main' : 'master'
end
end
end
# frozen_string_literal: true
require 'spec_helper'
# We disabled main_branch_over_master feature for tests
# In order to have consistent branch usages
# When we migrate the branch name to main, we can enable it
RSpec.describe Gitlab::DefaultBranch do
context 'main_branch_over_master is enabled' do
before do
stub_feature_flags(main_branch_over_master: true)
end
it 'returns main' do
expect(described_class.value).to eq('main')
end
end
context 'main_branch_over_master is disabled' do
it 'returns master' do
expect(described_class.value).to eq('master')
end
end
end
......@@ -6903,11 +6903,6 @@ RSpec.describe Project, factory_default: :keep do
describe '#default_branch_or_main' do
let(:project) { create(:project, :repository) }
before do
# Stubbing it as true since the FF disabled for tests globally
stub_feature_flags(main_branch_over_master: true)
end
it 'returns default branch' do
expect(project.default_branch_or_main).to eq(project.default_branch)
end
......@@ -6915,18 +6910,8 @@ RSpec.describe Project, factory_default: :keep do
context 'when default branch is nil' do
let(:project) { create(:project, :empty_repo) }
it 'returns main' do
expect(project.default_branch_or_main).to eq('main')
end
context 'main_branch_over_master is disabled' do
before do
stub_feature_flags(main_branch_over_master: false)
end
it 'returns master' do
expect(project.default_branch_or_main).to eq('master')
end
it 'returns Gitlab::DefaultBranch.value' do
expect(project.default_branch_or_main).to eq(Gitlab::DefaultBranch.value)
end
end
end
......
......@@ -826,18 +826,6 @@ RSpec.describe Snippet do
allow(Gitlab::CurrentSettings).to receive(:default_branch_name).and_return(default_branch)
end
context 'when default branch in settings is "master"' do
let(:default_branch) { 'master' }
it 'does nothing' do
expect(File.read(head_path).squish).to eq 'ref: refs/heads/master'
expect(snippet.repository.raw_repository).not_to receive(:write_ref)
subject
end
end
context 'when default branch in settings is different from "master"' do
let(:default_branch) { 'main' }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment