diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 3e8bbccbfa..f492651edc 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -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. This method is used as a quick way to measure & log the speed of some code. diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index b212ab474b..e54eb65c88 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -73,6 +73,9 @@ module Rails def version_control if !options[:skip_git] && !options[:pretend] 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 @@ -265,6 +268,11 @@ module Rails def config_target_version defined?(@config_target_version) ? @config_target_version : Rails::VERSION::STRING.to_f end + + private + def user_default_branch + @user_default_branch ||= `git config init.defaultbranch` + end end module Generators diff --git a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb index 59bd5875cb..d9bd53e7fe 100644 --- a/railties/lib/rails/generators/rails/plugin/plugin_generator.rb +++ b/railties/lib/rails/generators/rails/plugin/plugin_generator.rb @@ -65,6 +65,9 @@ module Rails def version_control if !options[:skip_git] && !options[:pretend] 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 @@ -178,6 +181,11 @@ task default: :test append_file gemfile_in_app_path, entry end end + + private + def user_default_branch + @user_default_branch ||= `git config init.defaultbranch` + end end module Generators diff --git a/railties/test/generators/app_generator_test.rb b/railties/test/generators/app_generator_test.rb index 60f5b6b8d2..71fdbe05da 100644 --- a/railties/test/generators/app_generator_test.rb +++ b/railties/test/generators/app_generator_test.rb @@ -1072,6 +1072,35 @@ class AppGeneratorTest < Rails::Generators::TestCase assert_directory ".git" 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 run_generator folders_with_keep = %w( diff --git a/railties/test/generators/plugin_generator_test.rb b/railties/test/generators/plugin_generator_test.rb index 6f91011855..643aa1259e 100644 --- a/railties/test/generators/plugin_generator_test.rb +++ b/railties/test/generators/plugin_generator_test.rb @@ -91,6 +91,35 @@ class PluginGeneratorTest < Rails::Generators::TestCase assert_directory ".git" 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 run_generator [destination_root, "--full", "-M", "-O", "-C", "-S", "-T", "--skip-active-storage"] assert_file "bin/rails" do |content|