From abcb1453e210beb6de70a69d3501cf842b38636e Mon Sep 17 00:00:00 2001 From: scott Chacon Date: Sun, 11 Nov 2007 12:21:30 -0800 Subject: added branch and checkout functionality --- lib/git/base.rb | 9 +++++++++ lib/git/branch.rb | 36 +++++++++++++++++++++++++++++++++--- lib/git/branches.rb | 2 +- lib/git/lib.rb | 21 +++++++++++++++++++++ 4 files changed, 64 insertions(+), 4 deletions(-) (limited to 'lib') 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 -- cgit