Skip to content

Commit

Permalink
Add support for git init --initial-branch=main argument (#539)
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Dufresne <jon.dufresne@gmail.com>
  • Loading branch information
jdufresne committed Dec 17, 2021
1 parent ff98c42 commit 6cba37e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/git.rb
Expand Up @@ -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"`.
#
Expand Down
5 changes: 4 additions & 1 deletion lib/git/base.rb
Expand Up @@ -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]

Expand Down
2 changes: 2 additions & 0 deletions lib/git/lib.rb
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions tests/units/test_init.rb
Expand Up @@ -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

Expand All @@ -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')
Expand Down

0 comments on commit 6cba37e

Please sign in to comment.