Skip to content

Commit efcce7f

Browse files
author
scott Chacon
committed
got clone and init to work - my first writing functions
1 parent fde3263 commit efcce7f

File tree

10 files changed

+97
-43
lines changed

10 files changed

+97
-43
lines changed

EXAMPLES

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,13 @@ g = Git.init
7878
{ :git_dir => '/opt/git/proj.git',
7979
:index_file => '/tmp/index'} )
8080

81-
82-
***** IMPLEMENTED *****
83-
84-
8581
g = Git.clone(URI, :name => 'name', :path => '/tmp/checkout'
86-
(username, password, ssl_key, git_dir, index_file)
87-
82+
(git_dir, index_file)
8883

8984

85+
***** IMPLEMENTED *****
86+
87+
9088
g.config('user.name', 'Scott Chacon')
9189
g.config('user.email', 'email@email.com')
9290

lib/git.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
=end
3030

3131
module Git
32-
33-
def self.repo(git_dir)
34-
Base.repo(git_dir)
32+
33+
def self.bare(git_dir)
34+
Base.bare(git_dir)
3535
end
3636

3737
def self.open(working_dir, options = {})
@@ -42,8 +42,8 @@ def self.init(working_dir = '.', options = {})
4242
Base.init(working_dir, options)
4343
end
4444

45-
def self.clone(uri, options = {})
46-
Base.clone(working_dir, options)
45+
def self.clone(repository, name, options = {})
46+
Base.clone(repository, name, options)
4747
end
4848

4949
end

lib/git/base.rb

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@ class Base
66
@repository = nil
77
@index = nil
88

9-
# opens a Git Repository - no working directory options
10-
def self.repo(git_dir)
9+
# opens a bare Git Repository - no working directory options
10+
def self.bare(git_dir)
1111
self.new :repository => git_dir
1212
end
1313

1414
# opens a new Git Project from a working directory
1515
# you can specify non-standard git_dir and index file in the options
1616
def self.open(working_dir, opts={})
17-
default = {:working_directory => working_dir,
18-
:repository => File.join(working_dir, '.git'),
19-
:index => File.join(working_dir, '.git', 'index')}
17+
default = {:working_directory => working_dir}
2018
git_options = default.merge(opts)
2119

2220
self.new(git_options)
2321
end
24-
22+
23+
# initializes a git repository
24+
#
25+
# options:
26+
# :repository
27+
# :index_file
28+
#
2529
def self.init(working_dir, opts = {})
2630
default = {:working_directory => working_dir,
2731
:repository => File.join(working_dir, '.git')}
@@ -38,19 +42,36 @@ def self.init(working_dir, opts = {})
3842
self.new(git_options)
3943
end
4044

41-
def self.clone
42-
raise NotImplementedError
45+
# clones a git repository locally
46+
#
47+
# repository - http://repo.or.cz/w/sinatra.git
48+
# name - sinatra
49+
#
50+
# options:
51+
# :repository
52+
#
53+
# :bare
54+
# or
55+
# :working_directory
56+
# :index_file
57+
#
58+
def self.clone(repository, name, opts = {})
59+
# run git-clone
60+
self.new(Git::Lib.new.clone(repository, name, opts))
4361
end
4462

4563
def initialize(options = {})
64+
if working_dir = options[:working_directory]
65+
options[:repository] = File.join(working_dir, '.git') if !options[:repository]
66+
options[:index] = File.join(working_dir, '.git', 'index') if !options[:index]
67+
end
68+
4669
@working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory]
4770
@repository = Git::Repository.new(options[:repository]) if options[:repository]
48-
@index = Git::Index.new(options[:index]) if options[:index]
71+
@index = Git::Index.new(options[:index], false) if options[:index]
4972
end
5073

51-
52-
53-
74+
5475
def dir
5576
@working_directory
5677
end

lib/git/lib.rb

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class Lib
88
@git_dir = nil
99
@git_index_file = nil
1010
@git_work_dir = nil
11+
@path = nil
1112

12-
def initialize(base)
13+
def initialize(base = nil)
1314
if base.is_a?(Git::Base)
1415
@git_dir = base.repo.path
1516
@git_index_file = base.index.path
@@ -25,6 +26,32 @@ def init
2526
command('init')
2627
end
2728

29+
# tries to clone the given repo
30+
#
31+
# returns {:repository} (if bare)
32+
# {:working_directory} otherwise
33+
#
34+
# accepts options:
35+
# :remote - name of remote (rather than 'origin')
36+
# :bare - no working directory
37+
#
38+
# TODO - make this work with SSH password or auth_key
39+
#
40+
def clone(repository, name, opts = {})
41+
@path = opts[:path] || '.'
42+
clone_dir = File.join(@path, name)
43+
44+
arr_opts = []
45+
arr_opts << "--bare" if opts[:bare]
46+
arr_opts << "-o #{opts[:remote]}" if opts[:remote]
47+
arr_opts << repository
48+
arr_opts << clone_dir
49+
50+
command('clone', arr_opts)
51+
52+
opts[:bare] ? {:repository => clone_dir} : {:working_directory => clone_dir}
53+
end
54+
2855
def log_commits(opts = {})
2956
arr_opts = ['--pretty=oneline']
3057
arr_opts << "-#{opts[:count]}" if opts[:count]
@@ -142,10 +169,10 @@ def command_lines(cmd, opts)
142169
end
143170

144171
def command(cmd, opts = {})
145-
ENV['GIT_DIR'] = @git_dir
172+
ENV['GIT_DIR'] = @git_dir if @git_dir
146173
ENV['GIT_INDEX_FILE'] = @git_index_file if @git_index_file
147174
ENV['GIT_WORK_DIR'] = @git_work_dir if @git_work_dir
148-
Dir.chdir(@git_work_dir || @git_dir) do
175+
Dir.chdir(@git_work_dir || @git_dir || @path) do
149176
opts = opts.to_a.join(' ')
150177
#puts "git #{cmd} #{opts}"
151178
out = `git #{cmd} #{opts} 2>&1`.chomp

lib/git/path.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class Path
55

66
def initialize(path, check_path = true)
77
if !check_path || File.exists?(path)
8-
@path = path
8+
@path = File.expand_path(path)
99
else
10-
raise ArgumentError, "path does not exist", path
10+
raise ArgumentError, "path does not exist", File.expand_path(path)
1111
end
1212
end
1313

tests/test_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ def set_file_paths
1414
@test_dir = File.join(cwd, 'tests', 'files')
1515
end
1616

17-
@wdir = File.join(@test_dir, 'working')
18-
@wbare = File.join(@test_dir, 'working.git')
19-
@index = File.join(@test_dir, 'index')
17+
@wdir = File.expand_path(File.join(@test_dir, 'working'))
18+
@wbare = File.expand_path(File.join(@test_dir, 'working.git'))
19+
@index = File.expand_path(File.join(@test_dir, 'index'))
2020
end
2121

2222
def in_temp_dir(remove_after = true)

tests/units/test_init.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_open_opts
2121
end
2222

2323
def test_git_bare
24-
g = Git.repo @wbare
24+
g = Git.bare @wbare
2525
assert_equal(g.repo.path, @wbare)
2626
end
2727

@@ -51,11 +51,19 @@ def test_git_init_remote_git
5151

5252
def test_git_clone
5353
in_temp_dir do |path|
54-
Git.clone(uri, :repository => dir)
55-
assert(File.exists?(File.join(dir, 'config')))
54+
g = Git.clone(@wbare, 'bare-co')
55+
assert(File.exists?(File.join(g.repo.path, 'config')))
5656
end
5757
end
58-
58+
59+
def test_git_clone_bare
60+
in_temp_dir do |path|
61+
g = Git.clone(@wbare, 'bare.git', :bare => true)
62+
assert(File.exists?(File.join(g.repo.path, 'config')))
63+
assert_nil(g.dir)
64+
end
65+
end
66+
5967
# trying to open a git project using a bare repo - rather than using Git.repo
6068
def test_git_open_error
6169
assert_raise ArgumentError do

tests/units/test_lib.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@ def test_object_contents
8282
def test_branches_all
8383
branches = @lib.branches_all
8484
assert(branches.size > 0)
85-
assert(branches.select { |b| b.current }.size > 0) # has a current branch
86-
assert(branches.select { |b| b.remote }.size > 0) # has a remote branch
87-
assert(branches.select { |b| !b.remote }.size > 0) # has a local branch
88-
assert(branches.select { |b| b.name == 'master' }.size > 0) # has a master branch
85+
assert(branches.select { |b| b[1] }.size > 0) # has a current branch
86+
assert(branches.select { |b| /\//.match(b[0]) }.size > 0) # has a remote branch
87+
assert(branches.select { |b| !/\//.match(b[0]) }.size > 0) # has a local branch
88+
assert(branches.select { |b| /master/.match(b[0]) }.size > 0) # has a master branch
8989
end
9090

9191
def test_config_remote

tests/units/test_log.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_get_log_since_file
3636
assert_equal(30, l.size)
3737

3838
l = @git.log.between('v2.5').object('example.txt')
39-
assert_equal(2, l.size)
39+
assert_equal(3, l.size)
4040

4141
l = @git.log.between('v2.5', 'test').object('example.txt')
4242
assert_equal(1, l.size)

tests/units/test_object.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def test_commit_contents
3232
end
3333

3434
def test_object_to_s
35-
assert_equal('commit 1cc8667014381e2788a94777532a788307f38d26', @commit.to_s)
36-
assert_equal('tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @tree.to_s)
37-
assert_equal('blob ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @blob.to_s)
35+
assert_equal('1cc8667014381e2788a94777532a788307f38d26', @commit.to_s)
36+
assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @tree.to_s)
37+
assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @blob.to_s)
3838
end
3939

4040
def test_object_size

0 commit comments

Comments
 (0)