Skip to content

Commit 77707c2

Browse files
Supporting git init with --bare
Closes ruby-git#45
1 parent a80e1bb commit 77707c2

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

lib/git/base.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,23 @@ def self.open(working_dir, opts={})
1616
# initializes a git repository
1717
#
1818
# options:
19+
# :bare
20+
# :index
1921
# :repository
20-
# :index_file
2122
#
2223
def self.init(working_dir, opts = {})
23-
opts = {
24-
:working_directory => working_dir,
25-
:repository => File.join(working_dir, '.git')
26-
}.merge(opts)
24+
opts[:working_directory] = working_dir if !opts[:working_directory]
25+
opts[:repository] = File.join(opts[:working_directory], '.git') if !opts[:repository]
2726

2827
FileUtils.mkdir_p(opts[:working_directory]) if opts[:working_directory] && !File.directory?(opts[:working_directory])
2928

30-
# run git_init there
31-
Git::Lib.new(opts).init
29+
init_opts = {
30+
bare: opts[:bare]
31+
}
32+
33+
opts.delete(:working_directory) if opts[:bare]
34+
35+
Git::Lib.new(opts).init(init_opts)
3236

3337
self.new(opts)
3438
end

lib/git/lib.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,18 @@ def initialize(base = nil, logger = nil)
2424
end
2525
@logger = logger
2626
end
27-
28-
def init
29-
command('init')
27+
28+
# creates or reinitializes the repository
29+
#
30+
# options:
31+
# :bare
32+
# :working_directory
33+
#
34+
def init(opts={})
35+
arr_opts = []
36+
arr_opts << '--bare' if opts[:bare]
37+
38+
command('init', arr_opts, false)
3039
end
3140

3241
# tries to clone the given repo
@@ -659,11 +668,13 @@ def command_lines(cmd, opts = [], chdir = true, redirect = '')
659668

660669
def command(cmd, opts = [], chdir = true, redirect = '', &block)
661670
ENV['GIT_DIR'] = @git_dir
662-
ENV['GIT_INDEX_FILE'] = @git_index_file
663671
ENV['GIT_WORK_TREE'] = @git_work_dir
672+
ENV['GIT_INDEX_FILE'] = @git_index_file
673+
664674
path = @git_work_dir || @git_dir || @path
665675

666676
opts = [opts].flatten.map {|s| escape(s) }.join(' ')
677+
667678
git_cmd = "git #{cmd} #{opts} #{redirect} 2>&1"
668679

669680
out = nil

tests/units/test_init.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ def test_git_bare
3232
# :index_file => '/tmp/index'} )
3333
def test_git_init
3434
in_temp_dir do |path|
35-
Git.init
35+
repo = Git.init(path)
3636
assert(File.directory?(File.join(path, '.git')))
3737
assert(File.exists?(File.join(path, '.git', 'config')))
38+
assert_equal('false', repo.config('core.bare'))
39+
end
40+
end
41+
42+
def test_git_init_bare
43+
in_temp_dir do |path|
44+
repo = Git.init(path, bare: true)
45+
assert(File.directory?(File.join(path, '.git')))
46+
assert(File.exists?(File.join(path, '.git', 'config')))
47+
assert_equal('true', repo.config('core.bare'))
3848
end
3949
end
4050

0 commit comments

Comments
 (0)