Skip to content

Commit 819d0d8

Browse files
guimalufperlun
authored andcommitted
Enable mirror option for git clone and push (#342)
c.f. https://help.github.com/articles/duplicating-a-repository Updating original PR #167 from Author: Lars Schneider <larsxschneider@gmail.com> Date: Sat Aug 9 23:24:41 2014 -0700
1 parent fe680f8 commit 819d0d8

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

lib/git/lib.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ def clone(repository, name, opts = {})
6767
arr_opts << '--config' << opts[:config] if opts[:config]
6868
arr_opts << '--origin' << opts[:remote] || opts[:origin] if opts[:remote] || opts[:origin]
6969
arr_opts << '--recursive' if opts[:recursive]
70+
arr_opts << "--mirror" if opts[:mirror]
7071

7172
arr_opts << '--'
7273

@@ -75,7 +76,7 @@ def clone(repository, name, opts = {})
7576

7677
command('clone', arr_opts)
7778

78-
opts[:bare] ? {:repository => clone_dir} : {:working_directory => clone_dir}
79+
(opts[:bare] or opts[:mirror]) ? {:repository => clone_dir} : {:working_directory => clone_dir}
7980
end
8081

8182

@@ -737,11 +738,16 @@ def push(remote, branch = 'master', opts = {})
737738
opts = {:tags => opts} if [true, false].include?(opts)
738739

739740
arr_opts = []
741+
arr_opts << '--mirror' if opts[:mirror]
740742
arr_opts << '--force' if opts[:force] || opts[:f]
741743
arr_opts << remote
742744

743-
command('push', arr_opts + [branch])
744-
command('push', ['--tags'] + arr_opts) if opts[:tags]
745+
if opts[:mirror]
746+
command('push', arr_opts)
747+
else
748+
command('push', arr_opts + [branch])
749+
command('push', ['--tags'] + arr_opts) if opts[:tags]
750+
end
745751
end
746752

747753
def pull(remote='origin', branch='master')

tests/units/test_init.rb

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ def test_open_simple
1313
assert_equal(g.repo.path, File.join(@wdir, '.git'))
1414
assert_equal(g.index.path, File.join(@wdir, '.git', 'index'))
1515
end
16-
17-
def test_open_opts
16+
17+
def test_open_opts
1818
g = Git.open @wdir, :repository => @wbare, :index => @index
1919
assert_equal(g.repo.path, @wbare)
2020
assert_equal(g.index.path, @index)
2121
end
22-
22+
2323
def test_git_bare
2424
g = Git.bare @wbare
2525
assert_equal(g.repo.path, @wbare)
2626
end
27-
27+
2828
#g = Git.init
2929
# Git.init('project')
30-
# Git.init('/home/schacon/proj',
31-
# { :git_dir => '/opt/git/proj.git',
30+
# Git.init('/home/schacon/proj',
31+
# { :git_dir => '/opt/git/proj.git',
3232
# :index_file => '/tmp/index'} )
3333
def test_git_init
3434
in_temp_dir do |path|
@@ -47,55 +47,63 @@ def test_git_init_bare
4747
assert_equal('true', repo.config('core.bare'))
4848
end
4949
end
50-
50+
5151
def test_git_init_remote_git
5252
in_temp_dir do |dir|
5353
assert(!File.exist?(File.join(dir, 'config')))
54-
55-
in_temp_dir do |path|
54+
55+
in_temp_dir do |path|
5656
Git.init(path, :repository => dir)
5757
assert(File.exist?(File.join(dir, 'config')))
5858
end
5959
end
6060
end
61-
61+
6262
def test_git_clone
63-
in_temp_dir do |path|
63+
in_temp_dir do |path|
6464
g = Git.clone(@wbare, 'bare-co')
6565
assert(File.exist?(File.join(g.repo.path, 'config')))
6666
assert(g.dir)
6767
end
6868
end
69-
69+
7070
def test_git_clone_with_branch
71-
in_temp_dir do |path|
71+
in_temp_dir do |path|
7272
g = Git.clone(@wbare, 'clone-branch', :branch => 'test')
7373
assert_equal(g.current_branch, 'test')
7474
end
7575
end
76-
76+
7777
def test_git_clone_bare
78-
in_temp_dir do |path|
78+
in_temp_dir do |path|
7979
g = Git.clone(@wbare, 'bare.git', :bare => true)
8080
assert(File.exist?(File.join(g.repo.path, 'config')))
8181
assert_nil(g.dir)
8282
end
8383
end
8484

85+
def test_git_clone_mirror
86+
in_temp_dir do |path|
87+
g = Git.clone(@wbare, 'bare.git', :mirror => true)
88+
assert(File.exist?(File.join(g.repo.path, 'config')))
89+
assert_nil(g.dir)
90+
end
91+
end
92+
8593
def test_git_clone_config
86-
in_temp_dir do |path|
94+
in_temp_dir do |path|
8795
g = Git.clone(@wbare, 'config.git', :config => "receive.denyCurrentBranch=ignore")
8896
assert_equal('ignore', g.config['receive.denycurrentbranch'])
8997
assert(File.exist?(File.join(g.repo.path, 'config')))
9098
assert(g.dir)
9199
end
92100
end
93-
101+
94102
# trying to open a git project using a bare repo - rather than using Git.repo
95103
def test_git_open_error
96104
assert_raise ArgumentError do
97105
Git.open @wbare
98106
end
99107
end
100-
108+
101109
end

0 commit comments

Comments
 (0)