Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for git init --initial-branch=main argument #539

Merged
merged 2 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/git.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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