Skip to content

Commit 2b2e789

Browse files
authored
Merge pull request rails#44515 from SkipKayhil/git-init-b
use `git init -b` in generator when supported
2 parents 746d115 + 67b9347 commit 2b2e789

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

railties/lib/rails/generators/app_base.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,22 @@ def empty_directory_with_keep_file(destination, config = {})
476476
def keep_file(destination)
477477
create_file("#{destination}/.keep") if keeps?
478478
end
479+
480+
def user_default_branch
481+
@user_default_branch ||= `git config init.defaultbranch`
482+
end
483+
484+
def git_init_command
485+
return "git init" if user_default_branch.strip.present?
486+
487+
git_version = `git --version`[/\d+\.\d+\.\d+/]
488+
489+
if Gem::Version.new(git_version) >= Gem::Version.new("2.28.0")
490+
"git init -b main"
491+
else
492+
"git init && git symbolic-ref HEAD refs/heads/main"
493+
end
494+
end
479495
end
480496
end
481497
end

railties/lib/rails/generators/rails/app/app_generator.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ def gitattributes
7272

7373
def version_control
7474
if !options[:skip_git] && !options[:pretend]
75-
run "git init", capture: options[:quiet], abort_on_failure: false
76-
if user_default_branch.strip.empty?
77-
`git symbolic-ref HEAD refs/heads/main`
78-
end
75+
run git_init_command, capture: options[:quiet], abort_on_failure: false
7976
end
8077
end
8178

@@ -244,11 +241,6 @@ def vendor
244241
def config_target_version
245242
defined?(@config_target_version) ? @config_target_version : Rails::VERSION::STRING.to_f
246243
end
247-
248-
private
249-
def user_default_branch
250-
@user_default_branch ||= `git config init.defaultbranch`
251-
end
252244
end
253245

254246
module Generators

railties/lib/rails/generators/rails/plugin/plugin_generator.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ def gitignore
6868

6969
def version_control
7070
if !options[:skip_git] && !options[:pretend]
71-
run "git init", capture: options[:quiet], abort_on_failure: false
72-
if user_default_branch.strip.empty?
73-
`git symbolic-ref HEAD refs/heads/main`
74-
end
71+
run git_init_command, capture: options[:quiet], abort_on_failure: false
7572
end
7673
end
7774

@@ -191,11 +188,6 @@ def gemfile_entry
191188
append_file gemfile_in_app_path, entry
192189
end
193190
end
194-
195-
private
196-
def user_default_branch
197-
@user_default_branch ||= `git config init.defaultbranch`
198-
end
199191
end
200192

201193
module Generators

0 commit comments

Comments
 (0)