Skip to content

Commit

Permalink
Merge pull request #40254 from prateekkish/default-main
Browse files Browse the repository at this point in the history
Change default branch for new Rails projects and plugins to main
  • Loading branch information
rafaelfranca committed Feb 5, 2021
2 parents d7ff764 + eb26193 commit abff811
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 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.
Expand Down
8 changes: 8 additions & 0 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -73,6 +73,9 @@ def gitattributes
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

Expand Down Expand Up @@ -265,6 +268,11 @@ def vendor
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
Expand Down
Expand Up @@ -65,6 +65,9 @@ def gitignore
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

Expand Down Expand Up @@ -178,6 +181,11 @@ def gemfile_entry
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
Expand Down
29 changes: 29 additions & 0 deletions railties/test/generators/app_generator_test.rb
Expand Up @@ -1077,6 +1077,35 @@ def test_version_control_initializes_git_repo
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(
Expand Down
29 changes: 29 additions & 0 deletions railties/test/generators/plugin_generator_test.rb
Expand Up @@ -91,6 +91,35 @@ def test_initializes_git_repo
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|
Expand Down

0 comments on commit abff811

Please sign in to comment.