Skip to content

Commit 6cba37e

Browse files
authored
Add support for git init --initial-branch=main argument (#539)
Signed-off-by: Jon Dufresne <jon.dufresne@gmail.com>
1 parent ff98c42 commit 6cba37e

File tree

4 files changed

+20
-1
lines changed

4 files changed

+20
-1
lines changed

lib/git.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ def self.global_config(name = nil, value = nil)
212212
# `"#{directory}/.git"`, create a bare repository at `"#{directory}"`.
213213
# See [what is a bare repository?](https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefbarerepositoryabarerepository).
214214
#
215+
# @option options [String] :initial_branch Use the specified name for the
216+
# initial branch in the newly created repository.
217+
#
215218
# @option options [Pathname] :repository the path to put the newly initialized
216219
# Git repository. The default for non-bare repository is `"#{directory}/.git"`.
217220
#

lib/git/base.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ def self.init(directory, options = {})
3434

3535
FileUtils.mkdir_p(options[:working_directory]) if options[:working_directory] && !File.directory?(options[:working_directory])
3636

37-
init_options = { :bare => options[:bare] }
37+
init_options = {
38+
:bare => options[:bare],
39+
:initial_branch => options[:initial_branch],
40+
}
3841

3942
options.delete(:working_directory) if options[:bare]
4043

lib/git/lib.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ def initialize(base = nil, logger = nil)
7171
# options:
7272
# :bare
7373
# :working_directory
74+
# :initial_branch
7475
#
7576
def init(opts={})
7677
arr_opts = []
7778
arr_opts << '--bare' if opts[:bare]
79+
arr_opts << "--initial-branch=#{opts[:initial_branch]}" if opts[:initial_branch]
7880

7981
command('init', arr_opts)
8082
end

tests/units/test_init.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def test_git_init
3838
assert(File.directory?(File.join(path, '.git')))
3939
assert(File.exist?(File.join(path, '.git', 'config')))
4040
assert_equal('false', repo.config('core.bare'))
41+
assert_equal("ref: refs/heads/master\n", File.read("#{path}/.git/HEAD"))
4142
end
4243
end
4344

@@ -61,6 +62,16 @@ def test_git_init_remote_git
6162
end
6263
end
6364

65+
def test_git_init_initial_branch
66+
in_temp_dir do |path|
67+
repo = Git.init(path, initial_branch: 'main')
68+
assert(File.directory?(File.join(path, '.git')))
69+
assert(File.exist?(File.join(path, '.git', 'config')))
70+
assert_equal('false', repo.config('core.bare'))
71+
assert_equal("ref: refs/heads/main\n", File.read("#{path}/.git/HEAD"))
72+
end
73+
end
74+
6475
def test_git_clone
6576
in_temp_dir do |path|
6677
g = Git.clone(@wbare, 'bare-co')

0 commit comments

Comments
 (0)