summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorscott Chacon <schacon@agadorsparticus.(none)>2007-11-11 12:21:30 -0800
committerscott Chacon <schacon@agadorsparticus.(none)>2007-11-11 12:21:30 -0800
commitabcb1453e210beb6de70a69d3501cf842b38636e (patch)
tree0612905ab3ec279694d2780b43274cb332757d1a
parent3cb57d82c301e9b8a16f30f468401e3007845bb7 (diff)
downloadthird_party-ruby-git-abcb1453e210beb6de70a69d3501cf842b38636e.zip
third_party-ruby-git-abcb1453e210beb6de70a69d3501cf842b38636e.tar.gz
third_party-ruby-git-abcb1453e210beb6de70a69d3501cf842b38636e.tar.xz
added branch and checkout functionality
-rw-r--r--EXAMPLES25
-rw-r--r--lib/git/base.rb9
-rw-r--r--lib/git/branch.rb36
-rw-r--r--lib/git/branches.rb2
-rw-r--r--lib/git/lib.rb21
-rw-r--r--tests/test_helper.rb13
-rw-r--r--tests/units/test_branch.rb42
-rw-r--r--tests/units/test_index_ops.rb12
8 files changed, 137 insertions, 23 deletions
diff --git a/EXAMPLES b/EXAMPLES
index 9d05965..4a8d185 100644
--- a/EXAMPLES
+++ b/EXAMPLES
@@ -97,22 +97,33 @@ end
g.reset # defaults to HEAD
g.reset_hard(Git::Commit)
-
-***** IMPLEMENTED *****
-
-g.branch('new_branch')
+g.branch('new_branch') # creates new or fetches existing
+g.branch('new_branch').checkout
g.branch('new_branch').delete
+g.branch('existing_branch').checkout
g.checkout('new_branch')
-g.checkout('new_branch', :create_branch => true)
-g.checkout_b('new_branch')
+g.checkout(g.branch('new_branch'))
+
+
+***** IMPLEMENTED *****
+
+g.branch(name).merge(branch2)
+g.branch(branch2).merge
g.merge('new_branch')
g.merge(Git::Branch)
g.merge(Git::Branch, Git::Branch)
+g.merge(Git::Remote)
+
+g.add_remote(uri, name) # Git::Remote
+g.remotes
+g.remote(name).fetch
+g.remote(name).merge
+g.remote(name).merge(branch)
g.fetch
-g.fetch(Git::Repo)
+g.fetch(Git::Remote)
g.pull
g.pull(Git::Repo, Git::Branch) # fetch and a merge
diff --git a/lib/git/base.rb b/lib/git/base.rb
index 46c75eb..342d535 100644
--- a/lib/git/base.rb
+++ b/lib/git/base.rb
@@ -124,6 +124,11 @@ module Git
Git::Branches.new(self)
end
+ def branch(branch_name = 'master')
+ Git::Branch.new(self, branch_name)
+ end
+
+
def lib
Git::Lib.new(self)
end
@@ -162,6 +167,10 @@ module Git
opts = {:add_all => true}.merge(opts)
self.lib.commit(message, opts)
end
+
+ def checkout(branch, opts = {})
+ self.lib.checkout(branch, opts)
+ end
# convenience methods
diff --git a/lib/git/branch.rb b/lib/git/branch.rb
index bf4d80a..3d8337b 100644
--- a/lib/git/branch.rb
+++ b/lib/git/branch.rb
@@ -1,16 +1,15 @@
module Git
class Branch < Path
- attr_accessor :full, :remote, :name, :current
+ attr_accessor :full, :remote, :name
@base = nil
@gcommit = nil
- def initialize(base, name, current = false)
+ def initialize(base, name)
@remote = nil
@full = name
@base = base
- @current = current
parts = name.split('/')
if parts[1]
@@ -26,5 +25,36 @@ module Git
@gcommit
end
+ def checkout
+ check_if_create
+ @base.lib.checkout(@name)
+ end
+
+ def create
+ check_if_create
+ end
+
+ def delete
+ @base.lib.branch_delete(@name)
+ end
+
+ def current
+ determine_current
+ end
+
+ def to_s
+ @name
+ end
+
+ private
+
+ def check_if_create
+ @base.lib.branch_new(@name) rescue nil
+ end
+
+ def determine_current
+ @base.lib.branch_current == @name
+ end
+
end
end
diff --git a/lib/git/branches.rb b/lib/git/branches.rb
index 72dff0a..47d001a 100644
--- a/lib/git/branches.rb
+++ b/lib/git/branches.rb
@@ -13,7 +13,7 @@ module Git
@base = base
@base.lib.branches_all.each do |b|
- @branches[b[0]] = Git::Branch.new(@base, b[0], b[1])
+ @branches[b[0]] = Git::Branch.new(@base, b[0])
end
end
diff --git a/lib/git/lib.rb b/lib/git/lib.rb
index bc40e0c..41b3efb 100644
--- a/lib/git/lib.rb
+++ b/lib/git/lib.rb
@@ -93,6 +93,11 @@ module Git
arr
end
+ def branch_current
+ branches_all.select { |b| b[1] }.first[0] rescue nil
+ end
+
+
# returns hash
# [tree-ish] = [[line_no, match], [line_no, match2]]
# [tree-ish] = [[line_no, match], [line_no, match2]]
@@ -235,6 +240,22 @@ module Git
command('reset', arr_opts)
end
+
+ def branch_new(branch)
+ command('branch', branch)
+ end
+
+ def branch_delete(branch)
+ command('branch', ['-d', branch])
+ end
+
+ def checkout(branch, opts = {})
+ arr_opts = []
+ arr_opts << '-f' if opts[:force]
+ arr_opts << branch.to_s
+
+ command('checkout', arr_opts)
+ end
private
diff --git a/tests/test_helper.rb b/tests/test_helper.rb
index cf67576..c55e1f4 100644
--- a/tests/test_helper.rb
+++ b/tests/test_helper.rb
@@ -29,4 +29,17 @@ class Test::Unit::TestCase
FileUtils.rm_r(tmp_path) if remove_after
end
+
+ def new_file(name, contents)
+ File.open(name, 'w') do |f|
+ f.puts contents
+ end
+ end
+
+ def append_file(name, contents)
+ File.open(name, 'a') do |f|
+ f.puts contents
+ end
+ end
+
end \ No newline at end of file
diff --git a/tests/units/test_branch.rb b/tests/units/test_branch.rb
index c1fe9d6..1560c55 100644
--- a/tests/units/test_branch.rb
+++ b/tests/units/test_branch.rb
@@ -46,4 +46,46 @@ class TestBranch < Test::Unit::TestCase
assert_equal(270, @git.branches[:test_branches].gcommit.size)
end
+ def test_branch_create_and_switch
+ in_temp_dir do |path|
+ g = Git.clone(@wbare, 'branch_test')
+ Dir.chdir('branch_test') do
+ assert(!g.branch('new_branch').current)
+ g.branch('other_branch').create
+ g.branch('new_branch').checkout
+ assert(g.branch('new_branch').current)
+
+ assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)
+
+ new_file('test-file1', 'blahblahblah1')
+ new_file('test-file2', 'blahblahblah2')
+ assert(g.status.untracked.assoc('test-file1'))
+
+ g.add(['test-file1', 'test-file2'])
+ assert(!g.status.untracked.assoc('test-file1'))
+
+ g.reset
+ assert(g.status.untracked.assoc('test-file1'))
+ assert(!g.status.added.assoc('test-file1'))
+
+ g.branch('new_branch').delete
+ assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)
+
+ g.branch('master').checkout
+ g.branch('new_branch').delete
+ assert_equal(0, g.branches.select { |b| b.name == 'new_branch' }.size)
+
+ g.checkout('other_branch')
+ assert(g.branch('other_branch').current)
+
+ g.checkout('master')
+ assert(!g.branch('other_branch').current)
+
+ g.checkout(g.branch('other_branch'))
+ assert(g.branch('other_branch').current)
+
+ end
+ end
+ end
+
end \ No newline at end of file
diff --git a/tests/units/test_index_ops.rb b/tests/units/test_index_ops.rb
index b3946b8..de3a9c3 100644
--- a/tests/units/test_index_ops.rb
+++ b/tests/units/test_index_ops.rb
@@ -91,16 +91,4 @@ class TestIndexOps < Test::Unit::TestCase
end
end
- def new_file(name, contents)
- File.open(name, 'w') do |f|
- f.puts contents
- end
- end
-
- def append_file(name, contents)
- File.open(name, 'a') do |f|
- f.puts contents
- end
- end
-
end \ No newline at end of file