From 6cba37ef428fe17cc2a60d7de34c4426200f7a63 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 17 Dec 2021 14:01:47 -0500 Subject: [PATCH] Add support for `git init --initial-branch=main` argument (#539) Signed-off-by: Jon Dufresne --- lib/git.rb | 3 +++ lib/git/base.rb | 5 ++++- lib/git/lib.rb | 2 ++ tests/units/test_init.rb | 11 +++++++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/git.rb b/lib/git.rb index eb4c7cce..6e93957c 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -212,6 +212,9 @@ def self.global_config(name = nil, value = nil) # `"#{directory}/.git"`, create a bare repository at `"#{directory}"`. # See [what is a bare repository?](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefbarerepositoryabarerepository). # + # @option options [String] :initial_branch Use the specified name for the + # initial branch in the newly created repository. + # # @option options [Pathname] :repository the path to put the newly initialized # Git repository. The default for non-bare repository is `"#{directory}/.git"`. # diff --git a/lib/git/base.rb b/lib/git/base.rb index 6e7c7a10..30cf386f 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -34,7 +34,10 @@ def self.init(directory, options = {}) FileUtils.mkdir_p(options[:working_directory]) if options[:working_directory] && !File.directory?(options[:working_directory]) - init_options = { :bare => options[:bare] } + init_options = { + :bare => options[:bare], + :initial_branch => options[:initial_branch], + } options.delete(:working_directory) if options[:bare] diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 9cb0a3a2..66a37b60 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -71,10 +71,12 @@ def initialize(base = nil, logger = nil) # options: # :bare # :working_directory + # :initial_branch # def init(opts={}) arr_opts = [] arr_opts << '--bare' if opts[:bare] + arr_opts << "--initial-branch=#{opts[:initial_branch]}" if opts[:initial_branch] command('init', arr_opts) end diff --git a/tests/units/test_init.rb b/tests/units/test_init.rb index bbb04b94..c9cd1af5 100644 --- a/tests/units/test_init.rb +++ b/tests/units/test_init.rb @@ -38,6 +38,7 @@ def test_git_init assert(File.directory?(File.join(path, '.git'))) assert(File.exist?(File.join(path, '.git', 'config'))) assert_equal('false', repo.config('core.bare')) + assert_equal("ref: refs/heads/master\n", File.read("#{path}/.git/HEAD")) end end @@ -61,6 +62,16 @@ def test_git_init_remote_git end end + def test_git_init_initial_branch + in_temp_dir do |path| + repo = Git.init(path, initial_branch: 'main') + assert(File.directory?(File.join(path, '.git'))) + assert(File.exist?(File.join(path, '.git', 'config'))) + assert_equal('false', repo.config('core.bare')) + assert_equal("ref: refs/heads/main\n", File.read("#{path}/.git/HEAD")) + end + end + def test_git_clone in_temp_dir do |path| g = Git.clone(@wbare, 'bare-co')