From efcce7fc123b9e64fb9d93224e4e78d09144af3d Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Sat, 10 Nov 2007 15:34:12 -0800 Subject: got clone and init to work - my first writing functions --- EXAMPLES | 10 ++++------ lib/git.rb | 10 +++++----- lib/git/base.rb | 45 +++++++++++++++++++++++++++++++++------------ lib/git/lib.rb | 33 ++++++++++++++++++++++++++++++--- lib/git/path.rb | 4 ++-- tests/test_helper.rb | 6 +++--- tests/units/test_init.rb | 16 ++++++++++++---- tests/units/test_lib.rb | 8 ++++---- tests/units/test_log.rb | 2 +- tests/units/test_object.rb | 6 +++--- 10 files changed, 97 insertions(+), 43 deletions(-) diff --git a/EXAMPLES b/EXAMPLES index 6dbfa6c..864db8d 100644 --- a/EXAMPLES +++ b/EXAMPLES @@ -78,15 +78,13 @@ g = Git.init { :git_dir => '/opt/git/proj.git', :index_file => '/tmp/index'} ) - -***** IMPLEMENTED ***** - - g = Git.clone(URI, :name => 'name', :path => '/tmp/checkout' - (username, password, ssl_key, git_dir, index_file) - + (git_dir, index_file) +***** IMPLEMENTED ***** + + g.config('user.name', 'Scott Chacon') g.config('user.email', 'email@email.com') diff --git a/lib/git.rb b/lib/git.rb index ec52c54..bb68388 100644 --- a/lib/git.rb +++ b/lib/git.rb @@ -29,9 +29,9 @@ require 'git/ref' =end module Git - - def self.repo(git_dir) - Base.repo(git_dir) + + def self.bare(git_dir) + Base.bare(git_dir) end def self.open(working_dir, options = {}) @@ -42,8 +42,8 @@ module Git Base.init(working_dir, options) end - def self.clone(uri, options = {}) - Base.clone(working_dir, options) + def self.clone(repository, name, options = {}) + Base.clone(repository, name, options) end end diff --git a/lib/git/base.rb b/lib/git/base.rb index 1111887..00495c7 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -6,22 +6,26 @@ module Git @repository = nil @index = nil - # opens a Git Repository - no working directory options - def self.repo(git_dir) + # opens a bare Git Repository - no working directory options + def self.bare(git_dir) self.new :repository => git_dir end # opens a new Git Project from a working directory # you can specify non-standard git_dir and index file in the options def self.open(working_dir, opts={}) - default = {:working_directory => working_dir, - :repository => File.join(working_dir, '.git'), - :index => File.join(working_dir, '.git', 'index')} + default = {:working_directory => working_dir} git_options = default.merge(opts) self.new(git_options) end - + + # initializes a git repository + # + # options: + # :repository + # :index_file + # def self.init(working_dir, opts = {}) default = {:working_directory => working_dir, :repository => File.join(working_dir, '.git')} @@ -38,19 +42,36 @@ module Git self.new(git_options) end - def self.clone - raise NotImplementedError + # clones a git repository locally + # + # repository - http://repo.or.cz/w/sinatra.git + # name - sinatra + # + # options: + # :repository + # + # :bare + # or + # :working_directory + # :index_file + # + def self.clone(repository, name, opts = {}) + # run git-clone + self.new(Git::Lib.new.clone(repository, name, opts)) end def initialize(options = {}) + if working_dir = options[:working_directory] + options[:repository] = File.join(working_dir, '.git') if !options[:repository] + options[:index] = File.join(working_dir, '.git', 'index') if !options[:index] + end + @working_directory = Git::WorkingDirectory.new(options[:working_directory]) if options[:working_directory] @repository = Git::Repository.new(options[:repository]) if options[:repository] - @index = Git::Index.new(options[:index]) if options[:index] + @index = Git::Index.new(options[:index], false) if options[:index] end - - - + def dir @working_directory end diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 8bb518b..9fa3986 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -8,8 +8,9 @@ module Git @git_dir = nil @git_index_file = nil @git_work_dir = nil + @path = nil - def initialize(base) + def initialize(base = nil) if base.is_a?(Git::Base) @git_dir = base.repo.path @git_index_file = base.index.path @@ -25,6 +26,32 @@ module Git command('init') end + # tries to clone the given repo + # + # returns {:repository} (if bare) + # {:working_directory} otherwise + # + # accepts options: + # :remote - name of remote (rather than 'origin') + # :bare - no working directory + # + # TODO - make this work with SSH password or auth_key + # + def clone(repository, name, opts = {}) + @path = opts[:path] || '.' + clone_dir = File.join(@path, name) + + arr_opts = [] + arr_opts << "--bare" if opts[:bare] + arr_opts << "-o #{opts[:remote]}" if opts[:remote] + arr_opts << repository + arr_opts << clone_dir + + command('clone', arr_opts) + + opts[:bare] ? {:repository => clone_dir} : {:working_directory => clone_dir} + end + def log_commits(opts = {}) arr_opts = ['--pretty=oneline'] arr_opts << "-#{opts[:count]}" if opts[:count] @@ -142,10 +169,10 @@ module Git end def command(cmd, opts = {}) - ENV['GIT_DIR'] = @git_dir + ENV['GIT_DIR'] = @git_dir if @git_dir ENV['GIT_INDEX_FILE'] = @git_index_file if @git_index_file ENV['GIT_WORK_DIR'] = @git_work_dir if @git_work_dir - Dir.chdir(@git_work_dir || @git_dir) do + Dir.chdir(@git_work_dir || @git_dir || @path) do opts = opts.to_a.join(' ') #puts "git #{cmd} #{opts}" out = `git #{cmd} #{opts} 2>&1`.chomp diff --git a/lib/git/path.rb b/lib/git/path.rb index 18845fa..e429d6f 100644 --- a/lib/git/path.rb +++ b/lib/git/path.rb @@ -5,9 +5,9 @@ module Git def initialize(path, check_path = true) if !check_path || File.exists?(path) - @path = path + @path = File.expand_path(path) else - raise ArgumentError, "path does not exist", path + raise ArgumentError, "path does not exist", File.expand_path(path) end end diff --git a/tests/test_helper.rb b/tests/test_helper.rb index 03b5f8a..cf67576 100644 --- a/tests/test_helper.rb +++ b/tests/test_helper.rb @@ -14,9 +14,9 @@ class Test::Unit::TestCase @test_dir = File.join(cwd, 'tests', 'files') end - @wdir = File.join(@test_dir, 'working') - @wbare = File.join(@test_dir, 'working.git') - @index = File.join(@test_dir, 'index') + @wdir = File.expand_path(File.join(@test_dir, 'working')) + @wbare = File.expand_path(File.join(@test_dir, 'working.git')) + @index = File.expand_path(File.join(@test_dir, 'index')) end def in_temp_dir(remove_after = true) diff --git a/tests/units/test_init.rb b/tests/units/test_init.rb index f1a8ba4..4681ba0 100644 --- a/tests/units/test_init.rb +++ b/tests/units/test_init.rb @@ -21,7 +21,7 @@ class TestInit < Test::Unit::TestCase end def test_git_bare - g = Git.repo @wbare + g = Git.bare @wbare assert_equal(g.repo.path, @wbare) end @@ -51,11 +51,19 @@ class TestInit < Test::Unit::TestCase def test_git_clone in_temp_dir do |path| - Git.clone(uri, :repository => dir) - assert(File.exists?(File.join(dir, 'config'))) + g = Git.clone(@wbare, 'bare-co') + assert(File.exists?(File.join(g.repo.path, 'config'))) end end - + + def test_git_clone_bare + in_temp_dir do |path| + g = Git.clone(@wbare, 'bare.git', :bare => true) + assert(File.exists?(File.join(g.repo.path, 'config'))) + assert_nil(g.dir) + end + end + # trying to open a git project using a bare repo - rather than using Git.repo def test_git_open_error assert_raise ArgumentError do diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index 03a4411..5fbd896 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -82,10 +82,10 @@ class TestLib < Test::Unit::TestCase def test_branches_all branches = @lib.branches_all assert(branches.size > 0) - assert(branches.select { |b| b.current }.size > 0) # has a current branch - assert(branches.select { |b| b.remote }.size > 0) # has a remote branch - assert(branches.select { |b| !b.remote }.size > 0) # has a local branch - assert(branches.select { |b| b.name == 'master' }.size > 0) # has a master branch + assert(branches.select { |b| b[1] }.size > 0) # has a current branch + assert(branches.select { |b| /\//.match(b[0]) }.size > 0) # has a remote branch + assert(branches.select { |b| !/\//.match(b[0]) }.size > 0) # has a local branch + assert(branches.select { |b| /master/.match(b[0]) }.size > 0) # has a master branch end def test_config_remote diff --git a/tests/units/test_log.rb b/tests/units/test_log.rb index b62c544..7198b8a 100644 --- a/tests/units/test_log.rb +++ b/tests/units/test_log.rb @@ -36,7 +36,7 @@ class TestLog < Test::Unit::TestCase assert_equal(30, l.size) l = @git.log.between('v2.5').object('example.txt') - assert_equal(2, l.size) + assert_equal(3, l.size) l = @git.log.between('v2.5', 'test').object('example.txt') assert_equal(1, l.size) diff --git a/tests/units/test_object.rb b/tests/units/test_object.rb index df565d5..c115b75 100644 --- a/tests/units/test_object.rb +++ b/tests/units/test_object.rb @@ -32,9 +32,9 @@ class TestObject < Test::Unit::TestCase end def test_object_to_s - assert_equal('commit 1cc8667014381e2788a94777532a788307f38d26', @commit.to_s) - assert_equal('tree 94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @tree.to_s) - assert_equal('blob ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @blob.to_s) + assert_equal('1cc8667014381e2788a94777532a788307f38d26', @commit.to_s) + assert_equal('94c827875e2cadb8bc8d4cdd900f19aa9e8634c7', @tree.to_s) + assert_equal('ba492c62b6227d7f3507b4dcc6e6d5f13790eabf', @blob.to_s) end def test_object_size -- cgit