1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Change default branch for new Rails projects and plugins to main

This commit changes the default branch of new Rails projects
and plugins from master to main.
This commit is contained in:
Prateek Choudhary 2020-09-19 14:40:41 +05:30
parent 8462129a1a
commit eb261937ac
5 changed files with 78 additions and 0 deletions

View file

@ -1,3 +1,7 @@
* Change default branch for new Rails projects and plugins to `main`
*Prateek Choudhary*
* Add benchmark method that can be called from anywhere. * Add benchmark method that can be called from anywhere.
This method is used as a quick way to measure & log the speed of some code. This method is used as a quick way to measure & log the speed of some code.

View file

@ -73,6 +73,9 @@ module Rails
def version_control def version_control
if !options[:skip_git] && !options[:pretend] if !options[:skip_git] && !options[:pretend]
run "git init", capture: options[:quiet], abort_on_failure: false run "git init", capture: options[:quiet], abort_on_failure: false
if user_default_branch.strip.empty?
`git symbolic-ref HEAD refs/heads/main`
end
end end
end end
@ -265,6 +268,11 @@ module Rails
def config_target_version def config_target_version
defined?(@config_target_version) ? @config_target_version : Rails::VERSION::STRING.to_f defined?(@config_target_version) ? @config_target_version : Rails::VERSION::STRING.to_f
end end
private
def user_default_branch
@user_default_branch ||= `git config init.defaultbranch`
end
end end
module Generators module Generators

View file

@ -65,6 +65,9 @@ module Rails
def version_control def version_control
if !options[:skip_git] && !options[:pretend] if !options[:skip_git] && !options[:pretend]
run "git init", capture: options[:quiet], abort_on_failure: false run "git init", capture: options[:quiet], abort_on_failure: false
if user_default_branch.strip.empty?
`git symbolic-ref HEAD refs/heads/main`
end
end end
end end
@ -178,6 +181,11 @@ task default: :test
append_file gemfile_in_app_path, entry append_file gemfile_in_app_path, entry
end end
end end
private
def user_default_branch
@user_default_branch ||= `git config init.defaultbranch`
end
end end
module Generators module Generators

View file

@ -1072,6 +1072,35 @@ class AppGeneratorTest < Rails::Generators::TestCase
assert_directory ".git" assert_directory ".git"
end end
def test_default_branch_main_without_user_default
current_default_branch = `git config --global init.defaultBranch`
`git config --global --unset init.defaultBranch`
run_generator [destination_root]
assert_file ".git/HEAD", /main/
ensure
if !current_default_branch.strip.empty?
`git config --global init.defaultBranch #{current_default_branch}`
end
end
def test_version_control_initializes_git_repo_with_user_default_branch
git_version = `git --version`[/\d+.\d+.\d+/]
return if Gem::Version.new(git_version) < Gem::Version.new("2.28.0")
current_default_branch = `git config --global init.defaultBranch`
`git config --global init.defaultBranch master`
run_generator [destination_root]
assert_file ".git/HEAD", /master/
ensure
if current_default_branch && current_default_branch.strip.empty?
`git config --global --unset init.defaultBranch`
elsif current_default_branch
`git config --global init.defaultBranch #{current_default_branch}`
end
end
def test_create_keeps def test_create_keeps
run_generator run_generator
folders_with_keep = %w( folders_with_keep = %w(

View file

@ -91,6 +91,35 @@ class PluginGeneratorTest < Rails::Generators::TestCase
assert_directory ".git" assert_directory ".git"
end end
def test_initializes_git_repo_with_main_branch_without_user_default
current_default_branch = `git config --global init.defaultBranch`
`git config --global --unset init.defaultBranch`
run_generator
assert_file ".git/HEAD", /main/
ensure
if !current_default_branch.strip.empty?
`git config --global init.defaultBranch #{current_default_branch}`
end
end
def test_version_control_initializes_git_repo_with_user_default_branch
git_version = `git --version`[/\d+.\d+.\d+/]
return if Gem::Version.new(git_version) < Gem::Version.new("2.28.0")
current_default_branch = `git config --global init.defaultBranch`
`git config --global init.defaultBranch master`
run_generator
assert_file ".git/HEAD", /master/
ensure
if current_default_branch && current_default_branch.strip.empty?
`git config --global --unset init.defaultBranch`
elsif current_default_branch
`git config --global init.defaultBranch #{current_default_branch}`
end
end
def test_generating_in_full_mode_with_almost_of_all_skip_options def test_generating_in_full_mode_with_almost_of_all_skip_options
run_generator [destination_root, "--full", "-M", "-O", "-C", "-S", "-T", "--skip-active-storage"] run_generator [destination_root, "--full", "-M", "-O", "-C", "-S", "-T", "--skip-active-storage"]
assert_file "bin/rails" do |content| assert_file "bin/rails" do |content|