Skip to content

Commit 1c66c1b

Browse files
endenisbyroot
authored andcommitted
[Fix rails#55379] Default branch in github ci templates
1 parent 3e54482 commit 1c66c1b

File tree

5 files changed

+17
-6
lines changed

5 files changed

+17
-6
lines changed

railties/lib/rails/generators/app_base.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,13 @@ def using_bun?
529529
using_js_runtime? && %w[bun].include?(options[:javascript])
530530
end
531531

532-
def capture_command(command, pattern)
533-
`#{command}`[pattern]
532+
def capture_command(command, pattern = nil)
533+
output = `#{command}`
534+
if pattern
535+
output[pattern]
536+
else
537+
output
538+
end
534539
rescue SystemCallError
535540
nil
536541
end
@@ -760,11 +765,13 @@ def keep_file(destination)
760765
end
761766

762767
def user_default_branch
763-
@user_default_branch ||= `git config init.defaultbranch`
768+
@user_default_branch ||= capture_command("git config init.defaultbranch").strip.presence || "main"
764769
end
765770

766771
def git_init_command
767-
return "git init" if user_default_branch.strip.present?
772+
if capture_command("git config init.defaultbranch").present?
773+
return "git init"
774+
end
768775

769776
git_version = `git --version`[/\d+\.\d+\.\d+/]
770777

railties/lib/rails/generators/rails/app/templates/github/ci.yml.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: CI
33
on:
44
pull_request:
55
push:
6-
branches: [ main ]
6+
branches: [ <%= user_default_branch %> ]
77

88
jobs:
99
<%- unless skip_brakeman? -%>

railties/lib/rails/generators/rails/plugin/templates/github/ci.yml.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: CI
33
on:
44
pull_request:
55
push:
6-
branches: [ main ]
6+
branches: [ <%= user_default_branch %> ]
77

88
jobs:
99
<%- unless skip_rubocop? -%>

railties/test/generators/app_generator_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,7 @@ def test_default_branch_main_without_user_default
11501150

11511151
run_generator [destination_root]
11521152
assert_file ".git/HEAD", /main/
1153+
assert_file ".github/workflows/ci.yml", /branches: \[ main \]/
11531154
ensure
11541155
if !current_default_branch.strip.empty?
11551156
`git config --global init.defaultBranch #{current_default_branch}`
@@ -1165,6 +1166,7 @@ def test_version_control_initializes_git_repo_with_user_default_branch
11651166

11661167
run_generator [destination_root]
11671168
assert_file ".git/HEAD", /master/
1169+
assert_file ".github/workflows/ci.yml", /branches: \[ master \]/
11681170
ensure
11691171
if current_default_branch && current_default_branch.strip.empty?
11701172
`git config --global --unset init.defaultBranch`

railties/test/generators/plugin_generator_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def test_initializes_git_repo_with_main_branch_without_user_default
145145

146146
run_generator
147147
assert_file ".git/HEAD", /main/
148+
assert_file ".github/workflows/ci.yml", /branches: \[ main \]/
148149
ensure
149150
if !current_default_branch.strip.empty?
150151
`git config --global init.defaultBranch #{current_default_branch}`
@@ -160,6 +161,7 @@ def test_version_control_initializes_git_repo_with_user_default_branch
160161

161162
run_generator
162163
assert_file ".git/HEAD", /master/
164+
assert_file ".github/workflows/ci.yml", /branches: \[ master \]/
163165
ensure
164166
if current_default_branch && current_default_branch.strip.empty?
165167
`git config --global --unset init.defaultBranch`

0 commit comments

Comments
 (0)