Commit 4c9cae31 authored by Igor Drozdov's avatar Igor Drozdov

Rename default_branch_or_master to default_branch_or_main

Let's rename the method and return `main` when default branch
is not set.

In most cases it shouldn't change the product behavior, but
in the cases when a project is empty `main` will be created
by default instead of `master` if for some action a branch
is required.
parent 1a993332
......@@ -144,7 +144,7 @@ module Projects
end
def define_badges_variables
@ref = params[:ref] || @project.default_branch || 'master'
@ref = params[:ref] || @project.default_branch_or_main
@badges = [Gitlab::Ci::Badge::Pipeline::Status,
Gitlab::Ci::Badge::Coverage::Report]
......
......@@ -2532,8 +2532,10 @@ class Project < ApplicationRecord
.exists?
end
def default_branch_or_master
default_branch || 'master'
def default_branch_or_main
return default_branch if default_branch
Feature.enabled?(:main_branch_over_master, self, default_enabled: :yaml) ? 'main' : 'master'
end
def ci_config_path_or_default
......
......@@ -108,7 +108,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end
def add_license_ide_path
ide_edit_path(project, default_branch_or_master, 'LICENSE')
ide_edit_path(project, default_branch_or_main, 'LICENSE')
end
def add_changelog_path
......@@ -116,7 +116,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end
def add_changelog_ide_path
ide_edit_path(project, default_branch_or_master, 'CHANGELOG')
ide_edit_path(project, default_branch_or_main, 'CHANGELOG')
end
def add_contribution_guide_path
......@@ -124,7 +124,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end
def add_contribution_guide_ide_path
ide_edit_path(project, default_branch_or_master, 'CONTRIBUTING.md')
ide_edit_path(project, default_branch_or_main, 'CONTRIBUTING.md')
end
def add_readme_path
......@@ -132,7 +132,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
end
def add_readme_ide_path
ide_edit_path(project, default_branch_or_master, 'README.md')
ide_edit_path(project, default_branch_or_main, 'README.md')
end
def add_ci_yml_path
......@@ -249,10 +249,10 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
nil,
nil,
{
'target_branch' => default_branch_or_master,
'original_branch' => default_branch_or_master,
'target_branch' => default_branch_or_main,
'original_branch' => default_branch_or_main,
'can_push_code' => 'true',
'path' => project_create_blob_path(project, default_branch_or_master),
'path' => project_create_blob_path(project, default_branch_or_main),
'project_path' => project.path
}
)
......@@ -268,7 +268,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
def new_file_anchor_data
if can_current_user_push_to_default_branch?
new_file_path = empty_repo? ? ide_edit_path(project, default_branch_or_master) : project_new_blob_path(project, default_branch_or_master)
new_file_path = empty_repo? ? ide_edit_path(project, default_branch_or_main) : project_new_blob_path(project, default_branch_or_main)
AnchorData.new(false,
statistic_icon + _('New file'),
......@@ -472,7 +472,7 @@ class ProjectPresenter < Gitlab::View::Presenter::Delegated
commit_message ||= s_("CommitMessage|Add %{file_name}") % { file_name: file_name }
project_new_blob_path(
project,
default_branch_or_master,
default_branch_or_main,
file_name: file_name,
commit_message: commit_message,
branch_name: branch_name
......
......@@ -30,7 +30,7 @@ module Ci
end
def should_track_failures?
return false unless project.default_branch_or_master == pipeline.ref
return false unless project.default_branch_or_main == pipeline.ref
# We fetch for up to MAX_TRACKABLE_FAILURES + 1 builds. So if ever we get
# 201 total number of builds with the assumption that each job has at least
......
......@@ -73,7 +73,7 @@ module MergeRequests
end
def default_branch
target_project.default_branch || 'master'
target_project.default_branch_or_main
end
def merge_request
......
......@@ -149,7 +149,7 @@ module Projects
def create_readme
commit_attrs = {
branch_name: @project.default_branch || 'master',
branch_name: @project.default_branch_or_main,
commit_message: 'Initial commit',
file_path: 'README.md',
file_content: "# #{@project.name}\n\n#{@project.description}"
......
......@@ -39,7 +39,7 @@ module Repositories
project,
user,
version:,
branch: project.default_branch_or_master,
branch: project.default_branch_or_main,
from: nil,
to: branch,
date: DateTime.now,
......
......@@ -28,14 +28,14 @@
%li.dropdown-header= _('This repository')
- if can_push_code
%li.qa-new-file-option= link_to _('New file'), project_new_blob_path(@project, @project.default_branch || 'master')
%li.qa-new-file-option= link_to _('New file'), project_new_blob_path(@project, @project.default_branch_or_main)
- unless @project.empty_repo?
%li= link_to _('New branch'), new_project_branch_path(@project)
%li= link_to _('New tag'), new_project_tag_path(@project)
- elsif can_collaborate_with_project?(@project)
%li= link_to _('New file'), project_new_blob_path(@project, @project.default_branch || 'master')
%li= link_to _('New file'), project_new_blob_path(@project, @project.default_branch_or_main)
- elsif create_mr_from_new_fork
- continue_params = { to: project_new_blob_path(@project, @project.default_branch || 'master'),
- continue_params = { to: project_new_blob_path(@project, @project.default_branch_or_main),
notice: edit_in_new_fork_notice,
notice_now: edit_in_new_fork_notice_now }
- fork_path = project_forks_path(@project, namespace_key: current_user.namespace.id, continue: continue_params)
......
- @content_class = "limit-container-width" unless fluid_layout
- default_branch_name = @project.default_branch_or_master
- default_branch_name = @project.default_branch_or_main
- @skip_current_level_breadcrumb = true
= render partial: 'flash_messages', locals: { project: @project }
......
......@@ -7,4 +7,4 @@
"new-environment-path" => new_project_environment_path(@project),
"help-page-path" => help_page_path("ci/environments/index.md"),
"project-path" => @project.full_path,
"default-branch-name" => @project.default_branch_or_master } }
"default-branch-name" => @project.default_branch_or_main } }
---
title: Rename default branch for empty project actions
merge_request: 60258
author:
type: changed
---
name: main_branch_over_master
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/60258
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/329190
milestone: '13.12'
type: development
group: group::source code
default_enabled: false
......@@ -53,8 +53,8 @@ module Security
security_policy_management_project.repository
end
def default_branch_or_master
security_policy_management_project.default_branch_or_master
def default_branch_or_main
security_policy_management_project.default_branch_or_main
end
def active_policy_names_with_dast_profiles
......@@ -75,7 +75,7 @@ module Security
end
def scan_execution_policy_at(path)
policy_repo.blob_data_at(default_branch_or_master, path)
policy_repo.blob_data_at(default_branch_or_main, path)
.then { |config| Gitlab::Config::Loader::Yaml.new(config).load!.fetch(:scan_execution_policy, []) }
end
......
......@@ -53,7 +53,7 @@ module Projects
return '' if project.empty_repo?
gitlab_ci = Gitlab::FileDetector::PATTERNS[:gitlab_ci]
Gitlab::Routing.url_helpers.project_blame_path(project, File.join(project.default_branch_or_master, gitlab_ci))
Gitlab::Routing.url_helpers.project_blame_path(project, File.join(project.default_branch_or_main, gitlab_ci))
end
def features
......
......@@ -15,7 +15,7 @@ module Gitlab
@version = attributes[:version]
end
def blob_path_for(project, sha: project&.default_branch_or_master)
def blob_path_for(project, sha: project&.default_branch_or_main)
return if path.blank?
return path if sha.blank?
......
......@@ -47,7 +47,7 @@ RSpec.describe Gitlab::Ci::Reports::LicenseScanning::Dependency do
let(:project) { build_stubbed(:project) }
specify { expect(subject).to eql("/#{project.namespace.path}/#{project.name}/-/blob/#{project.default_branch_or_master}/#{lockfile}") }
specify { expect(subject).to eql("/#{project.namespace.path}/#{project.name}/-/blob/master/#{lockfile}") }
end
end
end
......@@ -8,7 +8,7 @@ RSpec.describe Security::OrchestrationPolicyConfiguration do
create( :security_orchestration_policy_configuration, security_policy_management_project: security_policy_management_project)
end
let(:default_branch) { security_policy_management_project.default_branch_or_master }
let(:default_branch) { security_policy_management_project.default_branch }
let(:repository) { instance_double(Repository, root_ref: 'master') }
describe 'associations' do
......
......@@ -68,7 +68,7 @@ RSpec.describe DastOnDemandScans::CreateService do
auth_url: dast_site_profile.auth_url,
auth_username: dast_site_profile.auth_username,
auth_username_field: dast_site_profile.auth_username_field,
branch: project.default_branch_or_master,
branch: project.default_branch,
dast_profile: nil,
excluded_urls: dast_site_profile.excluded_urls.join(','),
full_scan_enabled: false,
......
......@@ -55,7 +55,7 @@ RSpec.describe Security::SecurityOrchestrationPolicies::OnDemandScanPipelineConf
auth_username: site_profile.auth_username,
auth_username_field: site_profile.auth_username_field,
dast_profile: nil,
branch: project.default_branch_or_master,
branch: project.default_branch,
excluded_urls: site_profile.excluded_urls.join(','),
full_scan_enabled: false,
show_debug_messages: false,
......
......@@ -224,7 +224,7 @@ module API
desc: 'The commit message to use when committing the changelog'
end
post ':id/repository/changelog' do
branch = params[:branch] || user_project.default_branch_or_master
branch = params[:branch] || user_project.default_branch_or_main
access = Gitlab::UserAccess.new(current_user, container: user_project)
unless access.can_push_to_branch?(branch)
......
......@@ -13,7 +13,7 @@ module Gitlab
def load!
recent_failures_count.each do |key_hash, count|
failed_junit_tests[key_hash].set_recent_failures(count, project.default_branch_or_master)
failed_junit_tests[key_hash].set_recent_failures(count, project.default_branch_or_main)
end
end
......
......@@ -194,7 +194,7 @@ FactoryBot.define do
filename,
content,
message: "Automatically created file #{filename}",
branch_name: project.default_branch_or_master
branch_name: project.default_branch || 'master'
)
end
end
......
......@@ -85,7 +85,7 @@ RSpec.describe Gitlab::TreeSummary do
'long.txt',
'',
message: message,
branch_name: project.default_branch_or_master
branch_name: project.default_branch
)
end
......
......@@ -6893,6 +6893,37 @@ RSpec.describe Project, factory_default: :keep do
end
end
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
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
end
end
end
def finish_job(export_job)
export_job.start
export_job.finish
......
......@@ -275,6 +275,12 @@ RSpec.configure do |config|
# https://gitlab.com/groups/gitlab-org/-/epics/5531
stub_feature_flags(refactor_blob_viewer: false)
# Disable `main_branch_over_master` as we migrate
# from `master` to `main` accross our codebase.
# It's done in order to preserve the concistency in tests
# As we're ready to change `master` usages to `main`, let's enable it
stub_feature_flags(main_branch_over_master: false)
allow(Gitlab::GitalyClient).to receive(:can_use_disk?).and_return(enable_rugged)
else
unstub_all_feature_flags
......
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